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

Code before:

    def test_virtual_column_with_full_inserts
      partial_inserts_was = VirtualColumn.partial_inserts
      VirtualColumn.partial_inserts = false
      assert_nothing_raised do
        VirtualColumn.create!(name: "Rails")
      end
    ensure
      VirtualColumn.partial_inserts = partial_inserts_was
    end

    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

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

Code after:
    end

    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_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?