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

Code before:

    class VirtualColumn < ActiveRecord::Base
    end

    def setup
      @connection = ActiveRecord::Base.lease_connection
      if @connection.database_version >= 18_00_00
        @connection.create_table :virtual_columns, force: true do |t|
          t.string  :name
          t.virtual :upper_name,  type: :string,  as: "UPPER(name)", stored: true
          t.virtual :name_length, type: :integer, as: "LENGTH(name)", stored: true
          t.virtual :name_octet_length, type: :integer, as: "OCTET_LENGTH(name)", stored: true
          t.integer :column1
          t.virtual :column2, type: :integer, as: "column1 + 1", stored: true
          t.virtual :column3, type: :integer, as: "column1 + 2", stored: false
          t.virtual :column4, type: :integer, as: "column1 + 3"
        end
      else
        @connection.create_table :virtual_columns, force: true do |t|
          t.string  :name
          t.virtual :upper_name,  type: :string,  as: "UPPER(name)", stored: true
          t.virtual :name_length, type: :integer, as: "LENGTH(name)", stored: true
          t.virtual :name_octet_length, type: :integer, as: "OCTET_LENGTH(name)", stored: true
          t.integer :column1
          t.virtual :column2, type: :integer, as: "column1 + 1", stored: true
        end
      end

      VirtualColumn.create(name: "Rails")
    end

    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

Review comment (suggestion):
```suggestion      
      @connection.create_table :virtual_columns, force: true do |t|
        t.string  :name
        t.virtual :upper_name,  type: :string,  as: "UPPER(name)", stored: true
        t.virtual :name_length, type: :integer, as: "LENGTH(name)", stored: true
        t.virtual :name_octet_length, type: :integer, as: "OCTET_LENGTH(name)", stored: true
        t.integer :column1
        t.virtual :column2, type: :integer, as: "column1 + 1", stored: true
        if @connection.database_version >= 18_00_00
          t.virtual :column3, type: :integer, as: "column1 + 2", stored: false
          t.virtual :column4, type: :integer, as: "column1 + 3"
        end
      end
```

Code after:

    class VirtualColumn < ActiveRecord::Base
    end

    def setup
      @connection = ActiveRecord::Base.lease_connection
      @connection.create_table :virtual_columns, force: true do |t|
        t.string  :name
        t.virtual :upper_name,  type: :string,  as: "UPPER(name)", stored: true
        t.virtual :name_length, type: :integer, as: "LENGTH(name)", stored: true
        t.virtual :name_octet_length, type: :integer, as: "OCTET_LENGTH(name)", stored: true
        t.integer :column1
        t.virtual :column2, type: :integer, as: "column1 + 1", stored: true
        if @connection.database_version >= 18_00_00
          t.virtual :column3, type: :integer, as: "column1 + 2", stored: false
          t.virtual :column4, type: :integer, as: "column1 + 3"
        end
      end

      VirtualColumn.create(name: "Rails")
    end

    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_00_00
      assert_equal 5, VirtualColumn.take.name_length
    end

    def test_change_table