Skip to content

Commit 20ca56b

Browse files
committed
share (change column) test methods between H2/MySQL
1 parent 6fd19fd commit 20ca56b

File tree

3 files changed

+72
-131
lines changed

3 files changed

+72
-131
lines changed

test/change_column_test_methods.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'test_helper'
2+
3+
module ChangeColumnTestMethods
4+
5+
class Person < ActiveRecord::Base; end
6+
7+
class CreatePeopleTable < ActiveRecord::Migration
8+
def self.up
9+
create_table(:people) { |t| t.string :name; t.integer :phone }
10+
end
11+
12+
def self.down
13+
drop_table :people
14+
end
15+
end
16+
17+
def setup
18+
CreatePeopleTable.up
19+
end
20+
21+
def teardown
22+
CreatePeopleTable.down
23+
end
24+
25+
def test_should_change_column_type
26+
ActiveRecord::Migration.change_column :people, :phone, :string
27+
Person.reset_column_information
28+
29+
p = Person.create! :phone => 'ABC'
30+
assert_equal 'ABC', p.phone
31+
end
32+
33+
def test_sets_defaults_on_column
34+
ActiveRecord::Migration.change_column :people, :phone, :string, :default => '123456'
35+
Person.reset_column_information
36+
37+
p = Person.create! :name => 'Petra'
38+
assert_equal '123456', p.phone
39+
end
40+
41+
def test_should_change_column_default_value
42+
ActiveRecord::Migration.add_column :people, :email, :string, :default => '[email protected]'
43+
ActiveRecord::Migration.change_column :people, :email, :string, :default => '[email protected]'
44+
Person.reset_column_information
45+
46+
p = Person.create! :name => 'Katka'
47+
assert_equal '[email protected]', p.email
48+
end
49+
50+
def test_should_set_non_null_restriction
51+
ActiveRecord::Migration.change_column :people, :phone, :string, :null => false
52+
Person.reset_column_information
53+
assert_raise(ActiveRecord::StatementInvalid) { Person.create! }
54+
end
55+
56+
def test_should_set_null_restriction_with_default
57+
p = Person.create! :name => 'Silvia'
58+
ActiveRecord::Migration.change_column :people, :phone, :string, :null => true, :default => '123456'
59+
Person.reset_column_information
60+
61+
assert_nil p.reload.phone
62+
assert_equal '123456', Person.create!(:name => 'Sisa').phone
63+
end
64+
65+
end

test/db/h2/change_column_test.rb

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,6 @@
11
require 'db/h2'
2-
require 'jdbc_common'
2+
require 'change_column_test_methods'
33

44
class H2ChangeColumnTest < Test::Unit::TestCase
5-
6-
class Person < ActiveRecord::Base; end
7-
8-
class CreatePeopleTable < ActiveRecord::Migration
9-
def self.up
10-
create_table :people do |t|
11-
t.integer :phone
12-
end
13-
end
14-
15-
def self.down
16-
drop_table :people
17-
end
18-
end
19-
20-
def setup
21-
CreatePeopleTable.up
22-
end
23-
24-
def teardown
25-
CreatePeopleTable.down
26-
end
27-
28-
def test_should_change_column_type
29-
ActiveRecord::Migration.change_column :people, :phone, :string
30-
Person.reset_column_information
31-
32-
p = Person.create!(:phone => 'ABC')
33-
assert_equal 'ABC', p.phone
34-
end
35-
36-
def test_sets_defaults_on_column
37-
ActiveRecord::Migration.change_column :people, :phone, :string, :default => '123456'
38-
Person.reset_column_information
39-
40-
p = Person.create!
41-
assert_equal '123456', p.phone
42-
end
43-
44-
def test_should_change_column_default_value
45-
ActiveRecord::Migration.add_column :people, :email, :string, :default => '[email protected]'
46-
ActiveRecord::Migration.change_column :people, :email, :string, :default => '[email protected]'
47-
Person.reset_column_information
48-
49-
p = Person.create!
50-
assert_equal '[email protected]', p.email
51-
end
52-
53-
def test_should_set_non_null_restriction
54-
ActiveRecord::Migration.change_column :people, :phone, :string, :null => false
55-
Person.reset_column_information
56-
assert_raise(ActiveRecord::StatementInvalid) { Person.create! }
57-
end
58-
59-
def test_should_set_null_restriction_with_default
60-
p = Person.create!
61-
ActiveRecord::Migration.change_column :people, :phone, :string, :null => true, :default => '123456'
62-
Person.reset_column_information
63-
64-
assert_nil p.reload.phone
65-
assert_equal '123456', Person.create!.phone
66-
end
67-
end
68-
5+
include ChangeColumnTestMethods
6+
end
Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,6 @@
1-
require 'jdbc_common'
21
require 'db/mysql'
2+
require 'change_column_test_methods'
33

4-
class MysqlChangeColumnTest < Test::Unit::TestCase
5-
6-
class Person < ActiveRecord::Base; end
7-
8-
class CreatePeopleTable < ActiveRecord::Migration
9-
def self.up
10-
create_table :people do |t|
11-
t.integer :phone
12-
end
13-
end
14-
15-
def self.down
16-
drop_table :people
17-
end
18-
end
19-
20-
def setup
21-
CreatePeopleTable.up
22-
end
23-
24-
def teardown
25-
CreatePeopleTable.down
26-
end
27-
28-
def test_should_change_column_type
29-
ActiveRecord::Migration.change_column :people, :phone, :string
30-
Person.reset_column_information
31-
32-
p = Person.create!(:phone => 'ABC')
33-
assert_equal 'ABC', p.phone
34-
end
35-
36-
def test_sets_defaults_on_column
37-
ActiveRecord::Migration.change_column :people, :phone, :string, :default => '123456'
38-
Person.reset_column_information
39-
40-
p = Person.create!
41-
assert_equal '123456', p.phone
42-
end
43-
44-
def test_should_change_column_default_value
45-
ActiveRecord::Migration.add_column :people, :email, :string, :default => '[email protected]'
46-
ActiveRecord::Migration.change_column :people, :email, :string, :default => '[email protected]'
47-
Person.reset_column_information
48-
49-
p = Person.create!
50-
assert_equal '[email protected]', p.email
51-
end
52-
53-
def test_should_set_non_null_restriction
54-
ActiveRecord::Migration.change_column :people, :phone, :string, :null => false
55-
Person.reset_column_information
56-
assert_raise(ActiveRecord::StatementInvalid) { Person.create! }
57-
end
58-
59-
def test_should_set_null_restriction_with_default
60-
p = Person.create!
61-
ActiveRecord::Migration.change_column :people, :phone, :string, :null => true, :default => '123456'
62-
Person.reset_column_information
63-
64-
assert_nil p.reload.phone
65-
assert_equal '123456', Person.create!.phone
66-
end
67-
end
68-
4+
class MySQLChangeColumnTest < Test::Unit::TestCase
5+
include ChangeColumnTestMethods
6+
end

0 commit comments

Comments
 (0)