Repository: rails/rails
File: activerecord/test/cases/adapters/postgresql/virtual_column_test.rb (Ruby)

Code before:
    def teardown
      @connection.drop_table :virtual_columns, if_exists: true
      VirtualColumn.reset_column_information
    end

    def test_virtual_column
      column = VirtualColumn.columns_hash["upper_name"]
      assert_predicate column, :virtual?
      assert_equal "RAILS", VirtualColumn.take.upper_name
    end

    def test_stored_column
      column = VirtualColumn.columns_hash["name_length"]
      assert_predicate column, :virtual?
      assert_predicate column, :virtual_stored? if @connection.database_version >= 18_000
      assert_equal 5, VirtualColumn.take.name_length
    end

    def test_change_table
      @connection.change_table :virtual_columns do |t|
        t.virtual :lower_name, type: :string, as: "LOWER(name)", stored: true
      end
      VirtualColumn.reset_column_information
      column = VirtualColumn.columns_hash["lower_name"]
      assert_predicate column, :virtual?
      assert_predicate column, :virtual_stored? if @connection.database_version >= 18_000
      assert_equal "rails", VirtualColumn.take.lower_name
    end

    if ActiveRecord::Base.lease_connection.database_version >= 180_000
      def test_change_table_as_stored_false
        @connection.change_table :virtual_columns do |t|
          t.virtual :reversed_name, type: :string, as: "REVERSE(name)", stored: false
        end
        VirtualColumn.reset_column_information
        column = VirtualColumn.columns_hash["reversed_name"]
        assert_predicate column, :virtual?
        assert_not_predicate column, :virtual_stored?
        assert_equal "sliaR", VirtualColumn.take.reversed_name
      end

      def test_change_table_without_stored_option
        @connection.change_table :virtual_columns do |t|
          t.virtual :ascii_name, type: :string, as: "ASCII(name)"
        end
        VirtualColumn.reset_column_information
        column = VirtualColumn.columns_hash["ascii_name"]
        assert_predicate column, :virtual?
        assert_not_predicate column, :virtual_stored?
        assert_equal "82", VirtualColumn.take.ascii_name
      end

Review comment (suggestion):
```suggestion
      assert_predicate column, :virtual_stored? if @connection.database_version >= 18_00_00
```

Code after:
    end

    def test_stored_column
      column = VirtualColumn.columns_hash["name_length"]
      assert_predicate column, :virtual?
      assert_predicate column, :virtual_stored? if @connection.database_version >= 18_00_00
      assert_equal 5, VirtualColumn.take.name_length
    end

    def test_change_table
      @connection.change_table :virtual_columns do |t|
        t.virtual :lower_name, type: :string, as: "LOWER(name)", stored: true
      end
      VirtualColumn.reset_column_information
      column = VirtualColumn.columns_hash["lower_name"]
      assert_predicate column, :virtual?
      assert_predicate column, :virtual_stored? if @connection.database_version >= 18_00_00
      assert_equal "rails", VirtualColumn.take.lower_name
    end

    if ActiveRecord::Base.lease_connection.database_version >= 18_00_00
      def test_change_table_as_stored_false
        @connection.change_table :virtual_columns do |t|
          t.virtual :reversed_name, type: :string, as: "REVERSE(name)", stored: false
        end
        VirtualColumn.reset_column_information
        column = VirtualColumn.columns_hash["reversed_name"]
        assert_predicate column, :virtual?
        assert_not_predicate column, :virtual_stored?
        assert_equal "sliaR", VirtualColumn.take.reversed_name
      end

      def test_change_table_without_stored_option
        @connection.change_table :virtual_columns do |t|
          t.virtual :ascii_name, type: :string, as: "ASCII(name)"
        end
        VirtualColumn.reset_column_information
        column = VirtualColumn.columns_hash["ascii_name"]
        assert_predicate column, :virtual?
        assert_not_predicate column, :virtual_stored?
        assert_equal "82", VirtualColumn.take.ascii_name
      end

    else
      def test_non_persisted_column
        message = <<~MSG
          PostgreSQL versions before 18 do not support VIRTUAL (not persisted) generated columns.
          Specify 'stored: true' option for 'invalid_definition'
        MSG

        assert_raise ArgumentError, match: message do