Sha256: 3d970187c2a64193bb00cb3d383274229a80119234ba5a6364bac3d8a33f0ace

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

require 'db/h2'
require 'jdbc_common'

class H2ChangeColumnTest < Test::Unit::TestCase

  class Person < ActiveRecord::Base; end

  class CreatePeopleTable < ActiveRecord::Migration
    def self.up
      create_table :people do |t|
        t.integer :phone
      end
    end

    def self.down
      drop_table :people
    end
  end

  def setup
    CreatePeopleTable.up
  end

  def teardown
    CreatePeopleTable.down
  end

  def test_should_change_column_type
    ActiveRecord::Migration.change_column :people, :phone, :string
    Person.reset_column_information

    p = Person.create!(:phone => 'ABC')
    assert_equal 'ABC', p.phone
  end

  def test_sets_defaults_on_column
    ActiveRecord::Migration.change_column :people, :phone, :string, :default => '123456'
    Person.reset_column_information

    p = Person.create!
    assert_equal '123456', p.phone
  end

  def test_should_change_column_default_value
    ActiveRecord::Migration.add_column    :people, :email, :string, :default => 'foo@example.com'
    ActiveRecord::Migration.change_column :people, :email, :string, :default => 'bar@example.com'
    Person.reset_column_information

    p = Person.create!
    assert_equal 'bar@example.com', p.email
  end

  def test_should_set_non_null_restriction
    ActiveRecord::Migration.change_column :people, :phone, :string, :null => false
    Person.reset_column_information
    assert_raise(ActiveRecord::StatementInvalid) { Person.create! }
  end

  def test_should_set_null_restriction_with_default
    p = Person.create!
    ActiveRecord::Migration.change_column :people, :phone, :string, :null => true, :default => '123456'
    Person.reset_column_information

    assert_nil p.reload.phone
    assert_equal '123456', Person.create!.phone
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-jdbc-adapter-1.3.0.beta2 test/db/h2/change_column_test.rb
activerecord-jdbc-adapter-1.3.0.beta1 test/db/h2/change_column_test.rb