theaboutbox-foreigner 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,152 @@
1
+ require File.expand_path('../spec_helper.rb', File.dirname(__FILE__))
2
+
3
+ describe Foreigner::ConnectionAdapters::PostgreSQLAdapter do
4
+ include MigrationFactory
5
+
6
+ before(:each) do
7
+ @adapter = AdapterHelper::PostgreSQLTestAdapter.new
8
+ @adapter.recreate_test_environment
9
+ end
10
+
11
+ describe 'when creating tables with t.foreign_key' do
12
+
13
+ it 'should understand t.foreign_key' do
14
+ create_table :items do |t|
15
+ t.string :name
16
+ t.references :collection, :null => false
17
+ t.foreign_key :collection
18
+ end
19
+
20
+ foreign_key = @adapter.foreign_keys(:items)[0]
21
+ foreign_key.to_table.should eql('collections')
22
+ foreign_key.options[:primary_key].should eql('id')
23
+ foreign_key.options[:column].should eql('collection_id')
24
+ foreign_key.options[:dependent].should be_nil
25
+ end
26
+
27
+ it 'should use a default foreign key name' do
28
+ create_table :items do |t|
29
+ t.string :name
30
+ t.references :collection, :null => false
31
+ t.foreign_key :collection
32
+ end
33
+
34
+ foreign_key = @adapter.foreign_keys(:items)[0]
35
+ foreign_key.options[:name].should_not be_nil
36
+ end
37
+
38
+ it 'should use a conventional primary key' do
39
+ create_table :items do |t|
40
+ t.string :name
41
+ t.references :collection, :null => false
42
+ t.foreign_key :collection
43
+ end
44
+
45
+ foreign_key = @adapter.foreign_keys(:items)[0]
46
+ foreign_key.options[:primary_key].should eql('id')
47
+ end
48
+
49
+ it 'should use a conventional column id' do
50
+ create_table :items do |t|
51
+ t.string :name
52
+ t.references :collection, :null => false
53
+ t.foreign_key :collection
54
+ end
55
+
56
+ foreign_key = @adapter.foreign_keys(:items)[0]
57
+ foreign_key.options[:column].should eql('collection_id')
58
+ end
59
+
60
+ it 'should accept a :column parameter' do
61
+ @column = :item_collection_id
62
+
63
+ create_table :items do |t|
64
+ t.string :name
65
+ t.integer @column
66
+ t.foreign_key :collection, :column => @column
67
+ end
68
+
69
+ foreign_key = @adapter.foreign_keys(:items)[0]
70
+ foreign_key.to_table.should eql('collections')
71
+ foreign_key.options[:primary_key].should eql('id')
72
+ foreign_key.options[:column].should eql(@column.to_s)
73
+ foreign_key.options[:dependent].should be_nil
74
+ end
75
+
76
+ it 'should accept :dependent => :nullify' do
77
+ @dependent = :nullify
78
+ create_table :items do |t|
79
+ t.string :name
80
+ t.references :collection
81
+ t.foreign_key :collection, :dependent => @dependent
82
+ end
83
+
84
+ foreign_key = @adapter.foreign_keys(:items)[0]
85
+ foreign_key.to_table.should eql('collections')
86
+ foreign_key.options[:primary_key].should eql('id')
87
+ foreign_key.options[:column].should eql('collection_id')
88
+ foreign_key.options[:dependent].should eql(@dependent)
89
+ end
90
+
91
+ it 'should accept :dependent => :delete' do
92
+ @dependent = :delete
93
+ create_table :items do |t|
94
+ t.string :name
95
+ t.references :collection
96
+ t.foreign_key :collection, :dependent => @dependent
97
+ end
98
+
99
+ foreign_key = @adapter.foreign_keys(:items)[0]
100
+ foreign_key.to_table.should eql('collections')
101
+ foreign_key.options[:primary_key].should eql('id')
102
+ foreign_key.options[:column].should eql('collection_id')
103
+ foreign_key.options[:dependent].should eql(@dependent)
104
+ end
105
+ end
106
+
107
+ describe 'when creating tables with t.reference' do
108
+
109
+ it 'should accept a t.references constraint' do
110
+ create_table :items do |t|
111
+ t.string :name
112
+ t.references :collection, :foreign_key => true
113
+ end
114
+
115
+ foreign_key = @adapter.foreign_keys(:items)[0]
116
+ foreign_key.to_table.should eql('collections')
117
+ foreign_key.options[:primary_key].should eql('id')
118
+ foreign_key.options[:column].should eql('collection_id')
119
+ foreign_key.options[:dependent].should be_nil
120
+ end
121
+
122
+ it 'should accept :foreign_key => { :dependent => :nullify }' do
123
+ @dependent = :nullify
124
+ create_table :items do |t|
125
+ t.string :name
126
+ t.references :collection, :foreign_key => {:dependent => @dependent}
127
+ end
128
+
129
+ foreign_key = @adapter.foreign_keys(:items)[0]
130
+ foreign_key.to_table.should eql('collections')
131
+ foreign_key.options[:primary_key].should eql('id')
132
+ foreign_key.options[:column].should eql('collection_id')
133
+ foreign_key.options[:dependent].should eql(@dependent)
134
+ end
135
+
136
+ it 'should accept :foreign_key => { :dependent => :delete }' do
137
+ @dependent = :delete
138
+ create_table :items do |t|
139
+ t.string :name
140
+ t.references :collection, :foreign_key => {:dependent => @dependent}
141
+ end
142
+
143
+ foreign_key = @adapter.foreign_keys(:items)[0]
144
+ foreign_key.to_table.should eql('collections')
145
+ foreign_key.options[:primary_key].should eql('id')
146
+ foreign_key.options[:column].should eql('collection_id')
147
+ foreign_key.options[:dependent].should eql(@dependent)
148
+ end
149
+ end
150
+
151
+ end
152
+
@@ -0,0 +1,75 @@
1
+ require File.expand_path('../spec_helper.rb', File.dirname(__FILE__))
2
+
3
+ describe Foreigner::ConnectionAdapters::PostgreSQLAdapter do
4
+ include MigrationFactory
5
+
6
+ before(:each) do
7
+ @adapter = AdapterHelper::PostgreSQLTestAdapter.new
8
+ end
9
+
10
+ describe 'when adding foreign keys' do
11
+ it 'should add foreign key without options' do
12
+ @adapter.add_foreign_key(:employees, :companies).should eql(
13
+ "ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_company_id` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)"
14
+ )
15
+ end
16
+
17
+ it 'should add foreign key with a name' do
18
+ @name = 'favorite_company_fk'
19
+ @adapter.add_foreign_key(:employees, :companies, :name => @name).should eql(
20
+ "ALTER TABLE `employees` ADD CONSTRAINT `#{@name}` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)"
21
+ )
22
+ end
23
+
24
+ it 'should add foreign key with a column' do
25
+ @column = 'last_employer_id'
26
+ @adapter.add_foreign_key(:employees, :companies, :column => @column).should eql(
27
+ "ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_last_employer_id` FOREIGN KEY (`#{@column}`) REFERENCES `companies`(id)"
28
+ )
29
+
30
+ end
31
+
32
+ it 'should add foreign key with column and a name' do
33
+ @name = 'favorite_company_fk'
34
+ @column = 'last_employer_id'
35
+ @adapter.add_foreign_key(:employees, :companies, :column => @column, :name => @name).should eql(
36
+ "ALTER TABLE `employees` ADD CONSTRAINT `#{@name}` FOREIGN KEY (`#{@column}`) REFERENCES `companies`(id)"
37
+ )
38
+ end
39
+
40
+ it 'should add foreign key with :dependent => :delete' do
41
+ @adapter.add_foreign_key(:employees, :companies, :dependent => :delete).should eql(
42
+ "ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_company_id` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
43
+ "ON DELETE CASCADE")
44
+ end
45
+
46
+ it 'should add foreign key with :dependent => :nullify' do
47
+ @adapter.add_foreign_key(:employees, :companies, :dependent => :nullify).should eql(
48
+ "ALTER TABLE `employees` ADD CONSTRAINT `fk_employees_company_id` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
49
+ "ON DELETE SET NULL"
50
+ )
51
+ end
52
+ end
53
+
54
+ describe 'when dropping foreign keys' do
55
+ it 'should drop foreign key' do
56
+ @adapter.remove_foreign_key(:suppliers, :companies).should eql(
57
+ "ALTER TABLE `suppliers` DROP CONSTRAINT `fk_suppliers_company_id`"
58
+ )
59
+ end
60
+
61
+ it 'should drop foreign key by name' do
62
+ @adapter.remove_foreign_key(:suppliers, :name => "belongs_to_supplier").should eql(
63
+ "ALTER TABLE `suppliers` DROP CONSTRAINT `belongs_to_supplier`"
64
+ )
65
+ end
66
+
67
+ it 'should drop foreign key by column' do
68
+ @adapter.remove_foreign_key(:suppliers, :column => "ship_to_id").should eql(
69
+ "ALTER TABLE `suppliers` DROP CONSTRAINT `fk_suppliers_ship_to_id`"
70
+ )
71
+ end
72
+ end
73
+
74
+ end
75
+
@@ -0,0 +1,96 @@
1
+ require File.expand_path('spec_helper.rb', File.dirname(__FILE__))
2
+
3
+ class SchemaDumperTester
4
+
5
+ def tables(stream)
6
+ stream
7
+ end
8
+
9
+ include Foreigner::SchemaDumper
10
+ include ForeignKeyDefinitionFactory
11
+
12
+ def valid_foreign_key_definition(opt = {})
13
+ options = {
14
+ :from_table => 'items',
15
+ :to_table => 'collections'
16
+ }.merge(opt)
17
+ end
18
+
19
+ # Helper when we want to test a single generated statement
20
+ def generate_schema_statement(foreign_key_definition)
21
+ fkd = new_foreign_key(foreign_key_definition)
22
+ generate_foreign_keys_statements([fkd]).first
23
+ end
24
+ end
25
+
26
+ describe Foreigner::SchemaDumper do
27
+
28
+ before(:each) do
29
+ @dumper = SchemaDumperTester.new
30
+ @fk_definition = {
31
+ :from_table => 'items',
32
+ :to_table => 'collections'
33
+ }
34
+
35
+ # Sanity Check
36
+ fkd = @dumper.new_foreign_key(@fk_definition)
37
+ fkd.from_table.should eql('items')
38
+ fkd.to_table.should eql('collections')
39
+ end
40
+
41
+ it 'should generate an add_foreign_key' do
42
+ @dumper.generate_schema_statement(@fk_definition).should match(/\s*add_foreign_key\s+:items,\s:collections/)
43
+ end
44
+
45
+ it 'should generate with a custom foreign key name' do
46
+ @foreign_key_name = 'fk_foreign_key_name'
47
+ @dumper.generate_schema_statement(:name => @foreign_key_name).should match(
48
+ /\s*add_foreign_key\s+:items,\s+:collections,\s+:name\s+=>\s+\"#{@foreign_key_name}\"/
49
+ )
50
+ end
51
+
52
+ it 'should generate with a custom column id' do
53
+ @column = 'acctno'
54
+ @dumper.generate_schema_statement(:column => @column).should match(
55
+ /\s*add_foreign_key\s+:items,\s+:collections,\s+:column\s+=>\s+\"#{@column}\"/
56
+ )
57
+ end
58
+
59
+ it 'should ignore custom column id conforming to Rails convention' do
60
+ @to_table = 'collections'
61
+ @column = @to_table.to_s.singularize + '_id'
62
+ @dumper.generate_schema_statement(:to_table => @to_table, :column => @column).should match(
63
+ /\s*add_foreign_key\s+:items,\s+#{@to_table.to_sym.inspect}/
64
+ )
65
+ end
66
+
67
+ it 'should generate with a custom primary key' do
68
+ @primary_key = 'upc'
69
+ @dumper.generate_schema_statement(:primary_key => @primary_key).should match(
70
+ /\s*add_foreign_key\s+:items,\s+:collections,\s+:primary_key\s+=>\s+\"#{@primary_key}\"/
71
+ )
72
+ end
73
+
74
+ it 'should ignore custom primary key conforming to Rails convention' do
75
+ @primary_key = 'id'
76
+ @dumper.generate_schema_statement(:primary_key => @primary_key).should match(
77
+ /\s*add_foreign_key\s+:items,\s+:collections/
78
+ )
79
+ end
80
+
81
+ it 'should generate with a :dependent => :nullify' do
82
+ @dependent = :nullify
83
+ @dumper.generate_schema_statement(:dependent => @dependent).should match(
84
+ /\s*add_foreign_key\s+:items,\s+:collections,\s+:dependent\s+=>\s+#{@dependent.inspect}/
85
+ )
86
+ end
87
+
88
+ it 'should generate with a :dependent => :delete' do
89
+ @dependent = :delete
90
+ @dumper.generate_schema_statement(:dependent => @dependent).should match(
91
+ /\s*add_foreign_key\s+:items,\s+:collections,\s+:dependent\s+=>\s+#{@dependent.inspect}/
92
+ )
93
+ end
94
+
95
+ end
96
+
@@ -0,0 +1,51 @@
1
+
2
+ require 'rubygems'
3
+
4
+ #require 'rspec/rails'
5
+ require 'rspec'
6
+
7
+ require 'active_support'
8
+ require 'active_record'
9
+ require 'active_record/connection_adapters/postgresql_adapter'
10
+ require 'active_record/connection_adapters/sqlite3_adapter'
11
+ require 'active_record/connection_adapters/mysql_adapter'
12
+ require 'foreigner'
13
+ require "foreigner/connection_adapters/postgresql_adapter"
14
+ require "foreigner/connection_adapters/mysql_adapter"
15
+ require "foreigner/connection_adapters/sqlite3_adapter"
16
+
17
+ require File.expand_path('factory_helper.rb', File.dirname(__FILE__))
18
+ require File.expand_path('adapter_helper.rb', File.dirname(__FILE__))
19
+
20
+ CONFIGURATIONS = {
21
+ :postgresql => {
22
+ :adapter => "postgresql",
23
+ :username => "cpope",
24
+ :password => "",
25
+ :database => "test_foreigner_gem",
26
+ :min_messages => "ERROR"
27
+ },
28
+ :postgresql_admin => {
29
+ :adapter => "postgresql",
30
+ :username => "cpope",
31
+ :password => "",
32
+ :database => "test",
33
+ :min_messages => "ERROR"
34
+ }, # :postgresql_admin is used to connect in; :postgresql is used to actually test the migrations
35
+ :mysql => {
36
+ :adapter => 'mysql',
37
+ :host => 'localhost',
38
+ :username => 'root',
39
+ :database => 'foreigner_test'
40
+
41
+ },
42
+ :sqlite3 => {
43
+ :adapter => "sqlite3",
44
+ :database => ":memory:"
45
+ }
46
+ }
47
+
48
+ # Turn this on for debugging
49
+ ActiveRecord::Migration.verbose = false
50
+
51
+
@@ -0,0 +1,86 @@
1
+ require File.expand_path('../spec_helper.rb', File.dirname(__FILE__))
2
+
3
+ describe Foreigner::ConnectionAdapters::SQLite3Adapter do
4
+ include MigrationFactory
5
+
6
+ before(:each) do
7
+ @adapter = AdapterHelper::SQLite3TestAdapter.new
8
+ @adapter.recreate_test_environment
9
+ @adapter.schema(:items).should be_nil
10
+ end
11
+
12
+ describe 'when creating tables with t.foreign_key' do
13
+ it 'should understand t.foreign_key' do
14
+ create_table :items do |t|
15
+ t.string :name
16
+ t.references :collection, :null => false
17
+ t.foreign_key :collection
18
+ end
19
+
20
+ @adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\)/)
21
+ end
22
+
23
+ it 'should accept a :column parameter' do
24
+ @column = :item_collection_id
25
+
26
+ create_table :items do |t|
27
+ t.string :name
28
+ t.integer @column
29
+ t.foreign_key :collection, :column => @column
30
+ end
31
+
32
+ @adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"#{@column}\"\) REFERENCES \"collections\"\s*\(id\)/)
33
+ end
34
+
35
+ it 'should accept :dependent => :nullify' do
36
+ create_table :items do |t|
37
+ t.string :name
38
+ t.references :collection
39
+ t.foreign_key :collection, :dependent => :nullify
40
+ end
41
+
42
+ @adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE SET NULL/)
43
+ end
44
+
45
+ it 'should accept :dependent => :delete' do
46
+ create_table :items do |t|
47
+ t.string :name
48
+ t.references :collection
49
+ t.foreign_key :collection, :dependent => :delete
50
+ end
51
+
52
+ @adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE CASCADE/)
53
+ end
54
+ end
55
+
56
+ describe 'when creating tables with t.reference' do
57
+ it 'should accept a t.references constraint' do
58
+ create_table :items do |t|
59
+ t.string :name
60
+ t.references :collection, :foreign_key => true
61
+ end
62
+
63
+ @adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\)/)
64
+ end
65
+
66
+ it 'should accept :foreign_key => { :dependent => :nullify }' do
67
+ create_table :items do |t|
68
+ t.string :name
69
+ t.references :collection, :foreign_key => {:dependent => :nullify}
70
+ end
71
+
72
+ @adapter.schema(:items).match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE SET NULL/)
73
+ end
74
+
75
+ it 'should accept :foreign_key => { :dependent => :delete }' do
76
+ create_table :items do |t|
77
+ t.string :name
78
+ t.references :collection, :foreign_key => {:dependent => :delete}
79
+ end
80
+
81
+ @adapter.schema(:items).should match(/FOREIGN KEY\s*\(\"collection_id\"\) REFERENCES \"collections\"\s*\(id\) ON DELETE CASCADE/)
82
+ end
83
+ end
84
+
85
+ end
86
+