ibm_db 0.9.5 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CHANGES +5 -0
  2. data/README +63 -97
  3. data/ext/ibm_db.c +20 -4
  4. data/ext/ruby_ibm_db.h +10 -0
  5. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +2 -4
  6. data/test/{adapter_test.rb → cases/adapter_test.rb} +39 -14
  7. data/test/cases/associations/cascaded_eager_loading_test.rb +113 -0
  8. data/test/{associations → cases/associations}/eager_test.rb +231 -65
  9. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +686 -0
  10. data/test/cases/associations/join_model_test.rb +797 -0
  11. data/test/{base_test.rb → cases/base_test.rb} +605 -385
  12. data/test/cases/calculations_test.rb +275 -0
  13. data/test/cases/finder_test.rb +885 -0
  14. data/test/cases/fixtures_test.rb +630 -0
  15. data/test/{migration_test.rb → cases/migration_test.rb} +530 -146
  16. data/test/cases/query_cache_test.rb +127 -0
  17. data/test/cases/validations_test.rb +1509 -0
  18. data/test/connections/native_ibm_db/connection.rb +1 -1
  19. data/test/models/warehouse_thing.rb +5 -0
  20. data/test/schema/i5/ibm_db_specific_schema.rb +133 -0
  21. data/test/schema/ids/ibm_db_specific_schema.rb +136 -0
  22. data/test/schema/luw/ibm_db_specific_schema.rb +133 -0
  23. data/test/schema/schema.rb +432 -0
  24. data/test/schema/zOS/ibm_db_specific_schema.rb +204 -0
  25. metadata +29 -33
  26. data/test/associations_test.rb +0 -2151
  27. data/test/fixtures/db_definitions/i5/ibm_db.drop.sql +0 -33
  28. data/test/fixtures/db_definitions/i5/ibm_db.sql +0 -236
  29. data/test/fixtures/db_definitions/i5/ibm_db2.drop.sql +0 -2
  30. data/test/fixtures/db_definitions/i5/ibm_db2.sql +0 -5
  31. data/test/fixtures/db_definitions/ids/ibm_db.drop.sql +0 -33
  32. data/test/fixtures/db_definitions/ids/ibm_db.sql +0 -237
  33. data/test/fixtures/db_definitions/ids/ibm_db2.drop.sql +0 -2
  34. data/test/fixtures/db_definitions/ids/ibm_db2.sql +0 -5
  35. data/test/fixtures/db_definitions/luw/ibm_db.drop.sql +0 -33
  36. data/test/fixtures/db_definitions/luw/ibm_db.sql +0 -236
  37. data/test/fixtures/db_definitions/luw/ibm_db2.drop.sql +0 -2
  38. data/test/fixtures/db_definitions/luw/ibm_db2.sql +0 -5
  39. data/test/fixtures/db_definitions/schema.rb +0 -361
  40. data/test/fixtures/db_definitions/zOS/ibm_db.drop.sql +0 -33
  41. data/test/fixtures/db_definitions/zOS/ibm_db.sql +0 -288
  42. data/test/fixtures/db_definitions/zOS/ibm_db2.drop.sql +0 -2
  43. data/test/fixtures/db_definitions/zOS/ibm_db2.sql +0 -7
  44. data/test/locking_test.rb +0 -282
@@ -0,0 +1,204 @@
1
+ ActiveRecord::Schema.define do
2
+
3
+ execute "DROP TABLE COMMENTS" rescue nil
4
+ execute "DROP TABLE POSTS" rescue nil
5
+ execute "DROP TABLE ITEMS" rescue nil
6
+ execute "DROP TABLE TOPICS" rescue nil
7
+ execute "DROP TABLE fk_test_has_fk" rescue nil
8
+ execute "DROP TABLE fk_test_has_pk" rescue nil
9
+ execute "DROP TABLE CIRCLES" rescue nil
10
+ execute "DROP TABLE SQUARES" rescue nil
11
+ execute "DROP TABLE TRIANGLES" rescue nil
12
+ execute "DROP TABLE NON_POLY_ONES" rescue nil
13
+ execute "DROP TABLE NON_POLY_TWOS" rescue nil
14
+ execute "DROP TABLE PAINT_COLORS" rescue nil
15
+ execute "DROP TABLE PAINT_TEXTURES" rescue nil
16
+
17
+ execute <<_SQL
18
+ CREATE TABLE comments (
19
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 100),
20
+ post_id INT NOT NULL,
21
+ type VARCHAR(255) DEFAULT NULL,
22
+ body VARCHAR(3000)NOT NULL,
23
+ PRIMARY KEY (id)
24
+ );
25
+ _SQL
26
+
27
+ execute <<_SQL
28
+ CREATE UNIQUE INDEX comments_id_idx ON comments(id);
29
+ _SQL
30
+
31
+ execute <<_SQL
32
+ CREATE TABLE posts (
33
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 100),
34
+ author_id INT DEFAULT NULL,
35
+ title VARCHAR(255) NOT NULL,
36
+ type VARCHAR(255) DEFAULT NULL,
37
+ body VARCHAR(3000) NOT NULL,
38
+ comments_count integer DEFAULT 0,
39
+ taggings_count integer DEFAULT 0,
40
+ PRIMARY KEY (id)
41
+ );
42
+
43
+ _SQL
44
+
45
+ execute <<_SQL
46
+ CREATE UNIQUE INDEX posts_id_idx ON posts(id);
47
+ _SQL
48
+
49
+ execute <<_SQL
50
+ CREATE TABLE fk_test_has_pk (
51
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 100),
52
+ PRIMARY KEY (id)
53
+ );
54
+ _SQL
55
+
56
+ execute <<_SQL
57
+ CREATE UNIQUE INDEX fk_has_pk_id_idx ON fk_test_has_pk(id);
58
+ _SQL
59
+
60
+ execute <<_SQL
61
+ CREATE TABLE fk_test_has_fk (
62
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 100),
63
+ fk_id integer NOT NULL,
64
+ PRIMARY KEY (id),
65
+ FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
66
+ );
67
+ _SQL
68
+
69
+ execute <<_SQL
70
+ CREATE UNIQUE INDEX fk_has_fk_id_idx ON fk_test_has_fk(id);
71
+ _SQL
72
+
73
+ execute <<_SQL
74
+ CREATE TABLE items (
75
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 100),
76
+ name VARCHAR(255) DEFAULT NULL,
77
+ PRIMARY KEY (id)
78
+ );
79
+
80
+ _SQL
81
+
82
+ execute <<_SQL
83
+ CREATE UNIQUE INDEX items_id_idx ON items(id);
84
+ _SQL
85
+
86
+ execute <<_SQL
87
+ CREATE TABLE circles (
88
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
89
+ PRIMARY KEY (id)
90
+ );
91
+ _SQL
92
+
93
+ execute <<_SQL
94
+ CREATE UNIQUE INDEX circles_id_idx ON circles(id);
95
+ _SQL
96
+
97
+ execute <<_SQL
98
+ CREATE TABLE squares(
99
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
100
+ PRIMARY KEY (id)
101
+ );
102
+ _SQL
103
+
104
+ execute <<_SQL
105
+ CREATE UNIQUE INDEX squares_id_idx ON squares(id);
106
+ _SQL
107
+
108
+ execute <<_SQL
109
+ CREATE TABLE triangles(
110
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
111
+ PRIMARY KEY (id)
112
+ );
113
+ _SQL
114
+
115
+ execute <<_SQL
116
+ CREATE UNIQUE INDEX triangles_id_idx ON triangles(id);
117
+ _SQL
118
+
119
+ execute <<_SQL
120
+ CREATE TABLE non_poly_ones(
121
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
122
+ PRIMARY KEY (id)
123
+ );
124
+ _SQL
125
+
126
+ execute <<_SQL
127
+ CREATE UNIQUE INDEX non_poly_ones_id_idx ON non_poly_ones(id);
128
+ _SQL
129
+
130
+ execute <<_SQL
131
+ CREATE TABLE non_poly_twos(
132
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
133
+ PRIMARY KEY (id)
134
+ );
135
+ _SQL
136
+
137
+ execute <<_SQL
138
+ CREATE UNIQUE INDEX non_poly_twos_id_idx ON non_poly_twos(id);
139
+ _SQL
140
+
141
+ execute <<_SQL
142
+ CREATE TABLE paint_colors(
143
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
144
+ non_poly_one_id INT,
145
+ PRIMARY KEY (id)
146
+ );
147
+ _SQL
148
+
149
+ execute <<_SQL
150
+ CREATE UNIQUE INDEX paint_colors_id_idx ON paint_colors(id);
151
+ _SQL
152
+
153
+ execute <<_SQL
154
+ CREATE TABLE paint_textures(
155
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
156
+ non_poly_two_id INT,
157
+ PRIMARY KEY (id)
158
+ );
159
+ _SQL
160
+
161
+ execute <<_SQL
162
+ CREATE UNIQUE INDEX paint_textures_id_idx ON paint_textures(id);
163
+ _SQL
164
+
165
+ execute <<_SQL
166
+ CREATE TABLE topics (
167
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 100),
168
+ title VARCHAR(255) DEFAULT NULL,
169
+ author_name VARCHAR(255) DEFAULT NULL,
170
+ author_email_address VARCHAR(255) DEFAULT NULL,
171
+ written_on TIMESTAMP DEFAULT NULL,
172
+ bonus_time TIME DEFAULT NULL,
173
+ last_read DATE DEFAULT NULL,
174
+ content VARCHAR(3000),
175
+ approved SMALLINT DEFAULT 1,
176
+ replies_count INT DEFAULT 0,
177
+ parent_id INT DEFAULT NULL,
178
+ type VARCHAR(50) DEFAULT NULL,
179
+ PRIMARY KEY (id)
180
+ );
181
+
182
+ _SQL
183
+
184
+ execute <<_SQL
185
+ CREATE UNIQUE INDEX topics_id_idx ON topics(id);
186
+ _SQL
187
+
188
+ execute <<_SQL
189
+ CREATE UNIQUE INDEX movies_id_idx ON movies(movieid);
190
+ _SQL
191
+
192
+ execute <<_SQL
193
+ CREATE UNIQUE INDEX auto_id_tests_id_idx ON auto_id_tests(auto_id);
194
+ _SQL
195
+
196
+ execute <<_SQL
197
+ CREATE UNIQUE INDEX mixed_case_monkeys_id_idx ON mixed_case_monkeys(monkeyID);
198
+ _SQL
199
+
200
+ execute <<_SQL
201
+ CREATE UNIQUE INDEX keyboards_id_idx ON keyboards(key_number);
202
+ _SQL
203
+
204
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibm_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - IBM
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-25 00:00:00 -07:00
12
+ date: 2008-09-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -33,37 +33,33 @@ extra_rdoc_files:
33
33
  - MANIFEST
34
34
  files:
35
35
  - test
36
- - test/associations_test.rb
37
- - test/adapter_test.rb
38
- - test/locking_test.rb
39
36
  - test/ibm_db_test.rb
40
- - test/fixtures
41
- - test/fixtures/db_definitions
42
- - test/fixtures/db_definitions/ids
43
- - test/fixtures/db_definitions/ids/ibm_db.drop.sql
44
- - test/fixtures/db_definitions/ids/ibm_db2.drop.sql
45
- - test/fixtures/db_definitions/ids/ibm_db2.sql
46
- - test/fixtures/db_definitions/ids/ibm_db.sql
47
- - test/fixtures/db_definitions/i5
48
- - test/fixtures/db_definitions/i5/ibm_db.drop.sql
49
- - test/fixtures/db_definitions/i5/ibm_db2.drop.sql
50
- - test/fixtures/db_definitions/i5/ibm_db2.sql
51
- - test/fixtures/db_definitions/i5/ibm_db.sql
52
- - test/fixtures/db_definitions/zOS
53
- - test/fixtures/db_definitions/zOS/ibm_db.drop.sql
54
- - test/fixtures/db_definitions/zOS/ibm_db2.drop.sql
55
- - test/fixtures/db_definitions/zOS/ibm_db2.sql
56
- - test/fixtures/db_definitions/zOS/ibm_db.sql
57
- - test/fixtures/db_definitions/schema.rb
58
- - test/fixtures/db_definitions/luw
59
- - test/fixtures/db_definitions/luw/ibm_db.drop.sql
60
- - test/fixtures/db_definitions/luw/ibm_db2.drop.sql
61
- - test/fixtures/db_definitions/luw/ibm_db2.sql
62
- - test/fixtures/db_definitions/luw/ibm_db.sql
63
- - test/associations
64
- - test/associations/eager_test.rb
65
- - test/base_test.rb
66
- - test/migration_test.rb
37
+ - test/cases
38
+ - test/cases/adapter_test.rb
39
+ - test/cases/calculations_test.rb
40
+ - test/cases/query_cache_test.rb
41
+ - test/cases/finder_test.rb
42
+ - test/cases/validations_test.rb
43
+ - test/cases/associations
44
+ - test/cases/associations/join_model_test.rb
45
+ - test/cases/associations/cascaded_eager_loading_test.rb
46
+ - test/cases/associations/eager_test.rb
47
+ - test/cases/associations/has_and_belongs_to_many_associations_test.rb
48
+ - test/cases/base_test.rb
49
+ - test/cases/fixtures_test.rb
50
+ - test/cases/migration_test.rb
51
+ - test/models
52
+ - test/models/warehouse_thing.rb
53
+ - test/schema
54
+ - test/schema/ids
55
+ - test/schema/ids/ibm_db_specific_schema.rb
56
+ - test/schema/i5
57
+ - test/schema/i5/ibm_db_specific_schema.rb
58
+ - test/schema/zOS
59
+ - test/schema/zOS/ibm_db_specific_schema.rb
60
+ - test/schema/schema.rb
61
+ - test/schema/luw
62
+ - test/schema/luw/ibm_db_specific_schema.rb
67
63
  - test/connections
68
64
  - test/connections/native_ibm_db
69
65
  - test/connections/native_ibm_db/connection.rb
@@ -109,6 +105,6 @@ rubyforge_project: rubyibm
109
105
  rubygems_version: 1.0.0
110
106
  signing_key:
111
107
  specification_version: 2
112
- summary: "Rails Driver and Adapter for IBM Data Servers: {LUW, zOS, i5, IDS}"
108
+ summary: "Rails Driver and Adapter for IBM Data Servers: {DB2 on Linux/Unix/Windows, DB2 on zOS, DB2 on i5/OS, Informix (IDS)}"
113
109
  test_files:
114
110
  - test/ibm_db_test.rb
@@ -1,2151 +0,0 @@
1
- require 'abstract_unit'
2
- require 'fixtures/developer'
3
- require 'fixtures/project'
4
- require 'fixtures/company'
5
- require 'fixtures/topic'
6
- require 'fixtures/reply'
7
- require 'fixtures/computer'
8
- require 'fixtures/customer'
9
- require 'fixtures/order'
10
- require 'fixtures/categorization'
11
- require 'fixtures/category'
12
- require 'fixtures/post'
13
- require 'fixtures/author'
14
- require 'fixtures/comment'
15
- require 'fixtures/tag'
16
- require 'fixtures/tagging'
17
- require 'fixtures/person'
18
- require 'fixtures/reader'
19
-
20
- class IBMDBAssociationsTest < Test::Unit::TestCase
21
- fixtures :accounts, :companies, :developers, :projects, :developers_projects,
22
- :computers
23
-
24
- def test_bad_collection_keys
25
- assert_raise(ArgumentError, 'ActiveRecord should have barked on bad collection keys') do
26
- Class.new(ActiveRecord::Base).has_many(:wheels, :name => 'wheels')
27
- end
28
- end
29
-
30
- def test_should_construct_new_finder_sql_after_create
31
- person = Person.new
32
- assert_equal [], person.readers.find(:all)
33
- person.save!
34
- reader = Reader.create! :person => person, :post => Post.new(:title => "foo", :body => "bar")
35
- assert_equal [reader], person.readers.find(:all)
36
- end
37
-
38
- def test_force_reload
39
- firm = Firm.new("name" => "A New Firm, Inc")
40
- firm.save
41
- firm.clients.each {|c|} # forcing to load all clients
42
- assert firm.clients.empty?, "New firm shouldn't have client objects"
43
- assert_equal 0, firm.clients.size, "New firm should have 0 clients"
44
-
45
- client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
46
- client.save
47
-
48
- assert firm.clients.empty?, "New firm should have cached no client objects"
49
- assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count"
50
-
51
- assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
52
- assert_equal 1, firm.clients(true).size, "New firm should have reloaded clients count"
53
- end
54
-
55
- def test_storing_in_pstore
56
- require "tmpdir"
57
- store_filename = File.join(Dir.tmpdir, "ar-pstore-association-test")
58
- File.delete(store_filename) if File.exist?(store_filename)
59
- require "pstore"
60
- apple = Firm.create("name" => "Apple")
61
- natural = Client.new("name" => "Natural Company")
62
- apple.clients << natural
63
-
64
- db = PStore.new(store_filename)
65
- db.transaction do
66
- db["apple"] = apple
67
- end
68
-
69
- db = PStore.new(store_filename)
70
- db.transaction do
71
- assert_equal "Natural Company", db["apple"].clients.first.name
72
- end
73
- end
74
- end
75
-
76
- class IBMDBAssociationProxyTest < Test::Unit::TestCase
77
- fixtures :authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects
78
-
79
- def test_proxy_accessors
80
- welcome = posts(:welcome)
81
- assert_equal welcome, welcome.author.proxy_owner
82
- assert_equal welcome.class.reflect_on_association(:author), welcome.author.proxy_reflection
83
- welcome.author.class # force load target
84
- assert_equal welcome.author, welcome.author.proxy_target
85
-
86
- david = authors(:david)
87
- assert_equal david, david.posts.proxy_owner
88
- assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection
89
- david.posts.first # force load target
90
- assert_equal david.posts, david.posts.proxy_target
91
-
92
- assert_equal david, david.posts_with_extension.testing_proxy_owner
93
- assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection
94
- david.posts_with_extension.first # force load target
95
- assert_equal david.posts_with_extension, david.posts_with_extension.testing_proxy_target
96
- end
97
-
98
- def test_push_does_not_load_target
99
- david = authors(:david)
100
-
101
- david.categories << categories(:technology)
102
- assert !david.categories.loaded?
103
- assert david.categories.include?(categories(:technology))
104
- end
105
-
106
- def test_push_does_not_lose_additions_to_new_record
107
- josh = Author.new(:name => "Josh")
108
- josh.posts << Post.new(:title => "New on Edge", :body => "More cool stuff!")
109
- assert josh.posts.loaded?
110
- assert_equal 1, josh.posts.size
111
- end
112
-
113
- def test_save_on_parent_does_not_load_target
114
- david = developers(:david)
115
-
116
- assert !david.projects.loaded?
117
- david.update_attribute(:created_at, Time.now)
118
- assert !david.projects.loaded?
119
- end
120
-
121
- def test_save_on_parent_saves_children
122
- developer = Developer.create :name => "Bryan", :salary => 50_000
123
- assert_equal 1, developer.reload.audit_logs.size
124
- end
125
-
126
- def test_failed_reload_returns_nil
127
- p = setup_dangling_association
128
- assert_nil p.author.reload
129
- end
130
-
131
- def test_failed_reset_returns_nil
132
- p = setup_dangling_association
133
- assert_nil p.author.reset
134
- end
135
-
136
- def test_reload_returns_assocition
137
- david = developers(:david)
138
- assert_nothing_raised do
139
- assert_equal david.projects, david.projects.reload.reload
140
- end
141
- end
142
-
143
- def setup_dangling_association
144
- josh = Author.create(:name => "Josh")
145
- p = Post.create(:title => "New on Edge", :body => "More cool stuff!", :author => josh)
146
- josh.destroy
147
- p
148
- end
149
- end
150
-
151
- class IBMDBHasOneAssociationsTest < Test::Unit::TestCase
152
- fixtures :accounts, :companies, :developers, :projects, :developers_projects
153
-
154
- def setup
155
- Account.destroyed_account_ids.clear
156
- end
157
-
158
- def test_has_one
159
- assert_equal companies(:first_firm).account, Account.find(1)
160
- assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
161
- end
162
-
163
- def test_has_one_cache_nils
164
- firm = companies(:another_firm)
165
- assert_queries(1) { assert_nil firm.account }
166
- assert_queries(0) { assert_nil firm.account }
167
-
168
- firms = Firm.find(:all, :include => :account)
169
- assert_queries(0) { firms.each(&:account) }
170
- end
171
-
172
- def test_can_marshal_has_one_association_with_nil_target
173
- firm = Firm.new
174
- assert_nothing_raised do
175
- assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes
176
- end
177
-
178
- firm.account
179
- assert_nothing_raised do
180
- assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes
181
- end
182
- end
183
-
184
- def test_proxy_assignment
185
- company = companies(:first_firm)
186
- assert_nothing_raised { company.account = company.account }
187
- end
188
-
189
- def test_triple_equality
190
- assert Account === companies(:first_firm).account
191
- assert companies(:first_firm).account === Account
192
- end
193
-
194
- def test_type_mismatch
195
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = 1 }
196
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = Project.find(1) }
197
- end
198
-
199
- def test_natural_assignment
200
- apple = Firm.create("name" => "Apple")
201
- citibank = Account.create("credit_limit" => 10)
202
- apple.account = citibank
203
- assert_equal apple.id, citibank.firm_id
204
- end
205
-
206
- def test_natural_assignment_to_nil
207
- old_account_id = companies(:first_firm).account.id
208
- companies(:first_firm).account = nil
209
- companies(:first_firm).save
210
- assert_nil companies(:first_firm).account
211
- # account is dependent, therefore is destroyed when reference to owner is lost
212
- assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
213
- end
214
-
215
- def test_assignment_without_replacement
216
- apple = Firm.create("name" => "Apple")
217
- citibank = Account.create("credit_limit" => 10)
218
- apple.account = citibank
219
- assert_equal apple.id, citibank.firm_id
220
-
221
- hsbc = apple.build_account({ :credit_limit => 20}, false)
222
- assert_equal apple.id, hsbc.firm_id
223
- hsbc.save
224
- assert_equal apple.id, citibank.firm_id
225
-
226
- nykredit = apple.create_account({ :credit_limit => 30}, false)
227
- assert_equal apple.id, nykredit.firm_id
228
- assert_equal apple.id, citibank.firm_id
229
- assert_equal apple.id, hsbc.firm_id
230
- end
231
-
232
- def test_assignment_without_replacement_on_create
233
- apple = Firm.create("name" => "Apple")
234
- citibank = Account.create("credit_limit" => 10)
235
- apple.account = citibank
236
- assert_equal apple.id, citibank.firm_id
237
-
238
- hsbc = apple.create_account({:credit_limit => 10}, false)
239
- assert_equal apple.id, hsbc.firm_id
240
- hsbc.save
241
- assert_equal apple.id, citibank.firm_id
242
- end
243
-
244
- def test_dependence
245
- num_accounts = Account.count
246
-
247
- firm = Firm.find(1)
248
- assert !firm.account.nil?
249
- account_id = firm.account.id
250
- assert_equal [], Account.destroyed_account_ids[firm.id]
251
-
252
- firm.destroy
253
- assert_equal num_accounts - 1, Account.count
254
- assert_equal [account_id], Account.destroyed_account_ids[firm.id]
255
- end
256
-
257
- def test_exclusive_dependence
258
- num_accounts = Account.count
259
-
260
- firm = ExclusivelyDependentFirm.find(9)
261
- assert !firm.account.nil?
262
- account_id = firm.account.id
263
- assert_equal [], Account.destroyed_account_ids[firm.id]
264
-
265
- firm.destroy
266
- assert_equal num_accounts - 1, Account.count
267
- assert_equal [], Account.destroyed_account_ids[firm.id]
268
- end
269
-
270
- def test_dependence_with_nil_associate
271
- firm = DependentFirm.new(:name => 'nullify')
272
- firm.save!
273
- assert_nothing_raised { firm.destroy }
274
- end
275
-
276
- def test_succesful_build_association
277
- firm = Firm.new("name" => "GlobalMegaCorp")
278
- firm.save
279
-
280
- account = firm.build_account("credit_limit" => 1000)
281
- assert account.save
282
- assert_equal account, firm.account
283
- end
284
-
285
- def test_failing_build_association
286
- firm = Firm.new("name" => "GlobalMegaCorp")
287
- firm.save
288
-
289
- account = firm.build_account
290
- assert !account.save
291
- assert_equal "can't be empty", account.errors.on("credit_limit")
292
- end
293
-
294
- def test_build_association_twice_without_saving_affects_nothing
295
- count_of_account = Account.count
296
- firm = Firm.find(:first)
297
- account1 = firm.build_account("credit_limit" => 1000)
298
- account2 = firm.build_account("credit_limit" => 2000)
299
-
300
- assert_equal count_of_account, Account.count
301
- end
302
-
303
- def test_create_association
304
- firm = Firm.create(:name => "GlobalMegaCorp")
305
- account = firm.create_account(:credit_limit => 1000)
306
- assert_equal account, firm.reload.account
307
- end
308
-
309
- def test_build
310
- firm = Firm.new("name" => "GlobalMegaCorp")
311
- firm.save
312
-
313
- firm.account = account = Account.new("credit_limit" => 1000)
314
- assert_equal account, firm.account
315
- assert account.save
316
- assert_equal account, firm.account
317
- end
318
-
319
- def test_build_before_child_saved
320
- firm = Firm.find(1)
321
-
322
- account = firm.account.build("credit_limit" => 1000)
323
- assert_equal account, firm.account
324
- assert account.new_record?
325
- assert firm.save
326
- assert_equal account, firm.account
327
- assert !account.new_record?
328
- end
329
-
330
- def test_build_before_either_saved
331
- firm = Firm.new("name" => "GlobalMegaCorp")
332
-
333
- firm.account = account = Account.new("credit_limit" => 1000)
334
- assert_equal account, firm.account
335
- assert account.new_record?
336
- assert firm.save
337
- assert_equal account, firm.account
338
- assert !account.new_record?
339
- end
340
-
341
- def test_failing_build_association
342
- firm = Firm.new("name" => "GlobalMegaCorp")
343
- firm.save
344
-
345
- firm.account = account = Account.new
346
- assert_equal account, firm.account
347
- assert !account.save
348
- assert_equal account, firm.account
349
- assert_equal "can't be empty", account.errors.on("credit_limit")
350
- end
351
-
352
- def test_create
353
- firm = Firm.new("name" => "GlobalMegaCorp")
354
- firm.save
355
- firm.account = account = Account.create("credit_limit" => 1000)
356
- assert_equal account, firm.account
357
- end
358
-
359
- def test_create_before_save
360
- firm = Firm.new("name" => "GlobalMegaCorp")
361
- firm.account = account = Account.create("credit_limit" => 1000)
362
- assert_equal account, firm.account
363
- end
364
-
365
- def test_dependence_with_missing_association
366
- Account.destroy_all
367
- firm = Firm.find(1)
368
- assert firm.account.nil?
369
- firm.destroy
370
- end
371
-
372
- def test_dependence_with_missing_association_and_nullify
373
- Account.destroy_all
374
- firm = DependentFirm.find(:first)
375
- assert firm.account.nil?
376
- firm.destroy
377
- end
378
-
379
- def test_assignment_before_parent_saved
380
- firm = Firm.new("name" => "GlobalMegaCorp")
381
- firm.account = a = Account.find(1)
382
- assert firm.new_record?
383
- assert_equal a, firm.account
384
- assert firm.save
385
- assert_equal a, firm.account
386
- assert_equal a, firm.account(true)
387
- end
388
-
389
- def test_finding_with_interpolated_condition
390
- firm = Firm.find(:first)
391
- superior = firm.clients.create(:name => 'SuperiorCo')
392
- superior.rating = 10
393
- superior.save
394
- assert_equal 10, firm.clients_with_interpolated_conditions.first.rating
395
- end
396
-
397
- def test_assignment_before_child_saved
398
- firm = Firm.find(1)
399
- firm.account = a = Account.new("credit_limit" => 1000)
400
- assert !a.new_record?
401
- assert_equal a, firm.account
402
- assert_equal a, firm.account
403
- assert_equal a, firm.account(true)
404
- end
405
-
406
- def test_assignment_before_either_saved
407
- firm = Firm.new("name" => "GlobalMegaCorp")
408
- firm.account = a = Account.new("credit_limit" => 1000)
409
- assert firm.new_record?
410
- assert a.new_record?
411
- assert_equal a, firm.account
412
- assert firm.save
413
- assert !firm.new_record?
414
- assert !a.new_record?
415
- assert_equal a, firm.account
416
- assert_equal a, firm.account(true)
417
- end
418
-
419
- def test_not_resaved_when_unchanged
420
- firm = Firm.find(:first, :include => :account)
421
- assert_queries(1) { firm.save! }
422
-
423
- firm = Firm.find(:first)
424
- firm.account = Account.find(:first)
425
- assert_queries(1) { firm.save! }
426
-
427
- firm = Firm.find(:first).clone
428
- firm.account = Account.find(:first)
429
- assert_queries(2) { firm.save! }
430
-
431
- firm = Firm.find(:first).clone
432
- firm.account = Account.find(:first).clone
433
- assert_queries(2) { firm.save! }
434
- end
435
-
436
- def test_save_still_works_after_accessing_nil_has_one
437
- jp = Company.new :name => 'Jaded Pixel'
438
- jp.dummy_account.nil?
439
-
440
- assert_nothing_raised do
441
- jp.save!
442
- end
443
- end
444
-
445
- end
446
-
447
-
448
- class IBMDBHasManyAssociationsTest < Test::Unit::TestCase
449
- fixtures :accounts, :companies, :developers, :projects,
450
- :developers_projects, :topics, :authors, :comments
451
-
452
- def setup
453
- Client.destroyed_client_ids.clear
454
- end
455
-
456
- def force_signal37_to_load_all_clients_of_firm
457
- companies(:first_firm).clients_of_firm.each {|f| }
458
- end
459
-
460
- def test_counting_with_counter_sql
461
- assert_equal 2, Firm.find(:first).clients.count
462
- end
463
-
464
- def test_counting
465
- assert_equal 2, Firm.find(:first).plain_clients.count
466
- end
467
-
468
- def test_counting_with_single_conditions
469
- assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => '1=1')
470
- end
471
-
472
- def test_counting_with_single_hash
473
- assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => '1=1')
474
- end
475
-
476
- def test_counting_with_column_name_and_hash
477
- assert_equal 2, Firm.find(:first).plain_clients.count(:all, :conditions => '1=1')
478
- end
479
-
480
- def test_finding
481
- assert_equal 2, Firm.find(:first).clients.length
482
- end
483
-
484
- def test_find_many_with_merged_options
485
- assert_equal 1, companies(:first_firm).limited_clients.size
486
- assert_equal 1, companies(:first_firm).limited_clients.find(:all).size
487
- assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size
488
- end
489
-
490
- def test_dynamic_find_should_respect_association_order
491
- assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'")
492
- assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client')
493
- end
494
-
495
- def test_dynamic_find_order_should_override_association_order
496
- assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'", :order => 'id')
497
- assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client', :order => 'id')
498
- end
499
-
500
- def test_dynamic_find_all_should_respect_association_order
501
- assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'")
502
- assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client')
503
- end
504
-
505
- def test_dynamic_find_all_order_should_override_association_order
506
- assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'", :order => 'id')
507
- assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client', :order => 'id')
508
- end
509
-
510
- def test_dynamic_find_all_should_respect_association_limit
511
- assert_equal 1, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'").length
512
- assert_equal 1, companies(:first_firm).limited_clients.find_all_by_type('Client').length
513
- end
514
-
515
- def test_dynamic_find_all_limit_should_override_association_limit
516
- assert_equal 2, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'", :limit => 9_000).length
517
- assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length
518
- end
519
-
520
- def test_triple_equality
521
- assert !(Array === Firm.find(:first).clients)
522
- assert Firm.find(:first).clients === Array
523
- end
524
-
525
- def test_finding_default_orders
526
- assert_equal "Summit", Firm.find(:first).clients.first.name
527
- end
528
-
529
- def test_finding_with_different_class_name_and_order
530
- assert_equal "Microsoft", Firm.find(:first).clients_sorted_desc.first.name
531
- end
532
-
533
- def test_finding_with_foreign_key
534
- assert_equal "Microsoft", Firm.find(:first).clients_of_firm.first.name
535
- end
536
-
537
- def test_finding_with_condition
538
- assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name
539
- end
540
-
541
- def test_finding_with_condition_hash
542
- assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name
543
- end
544
-
545
- def test_finding_using_sql
546
- firm = Firm.find(:first)
547
- first_client = firm.clients_using_sql.first
548
- assert_not_nil first_client
549
- assert_equal "Microsoft", first_client.name
550
- assert_equal 1, firm.clients_using_sql.size
551
- assert_equal 1, Firm.find(:first).clients_using_sql.size
552
- end
553
-
554
- def test_counting_using_sql
555
- assert_equal 1, Firm.find(:first).clients_using_counter_sql.size
556
- assert Firm.find(:first).clients_using_counter_sql.any?
557
- assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size
558
- assert !Firm.find(:first).clients_using_zero_counter_sql.any?
559
- end
560
-
561
- def test_counting_non_existant_items_using_sql
562
- assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size
563
- end
564
-
565
- def test_belongs_to_sanity
566
- c = Client.new
567
- assert_nil c.firm
568
-
569
- if c.firm
570
- assert false, "belongs_to failed if check"
571
- end
572
-
573
- unless c.firm
574
- else
575
- assert false, "belongs_to failed unless check"
576
- end
577
- end
578
-
579
- def test_find_ids
580
- firm = Firm.find(:first)
581
-
582
- assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find }
583
-
584
- client = firm.clients.find(2)
585
- assert_kind_of Client, client
586
-
587
- client_ary = firm.clients.find([2])
588
- assert_kind_of Array, client_ary
589
- assert_equal client, client_ary.first
590
-
591
- client_ary = firm.clients.find(2, 3)
592
- assert_kind_of Array, client_ary
593
- assert_equal 2, client_ary.size
594
- assert_equal client, client_ary.first
595
-
596
- assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
597
- end
598
-
599
- def test_find_string_ids_when_using_finder_sql
600
- firm = Firm.find(:first)
601
-
602
- client = firm.clients_using_finder_sql.find("2")
603
- assert_kind_of Client, client
604
-
605
- client_ary = firm.clients_using_finder_sql.find(["2"])
606
- assert_kind_of Array, client_ary
607
- assert_equal client, client_ary.first
608
-
609
- client_ary = firm.clients_using_finder_sql.find("2", "3")
610
- assert_kind_of Array, client_ary
611
- assert_equal 2, client_ary.size
612
- assert client_ary.include?(client)
613
- end
614
-
615
- def test_find_all
616
- firm = Firm.find(:first)
617
- assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
618
- assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
619
- end
620
-
621
- def test_find_all_sanitized
622
- firm = Firm.find(:first)
623
- summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
624
- assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
625
- assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
626
- end
627
-
628
- def test_find_first
629
- firm = Firm.find(:first)
630
- client2 = Client.find(2)
631
- assert_equal firm.clients.first, firm.clients.find(:first)
632
- assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
633
- end
634
-
635
- def test_find_first_sanitized
636
- firm = Firm.find(:first)
637
- client2 = Client.find(2)
638
- assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
639
- assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
640
- end
641
-
642
- def test_find_in_collection
643
- assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
644
- assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
645
- end
646
-
647
- def test_find_grouped
648
- all_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1")
649
- grouped_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count')
650
- assert_equal 2, all_clients_of_firm1.size
651
- assert_equal 1, grouped_clients_of_firm1.size
652
- end
653
-
654
- def test_adding
655
- force_signal37_to_load_all_clients_of_firm
656
- natural = Client.new("name" => "Natural Company")
657
- companies(:first_firm).clients_of_firm << natural
658
- assert_equal 2, companies(:first_firm).clients_of_firm.size # checking via the collection
659
- assert_equal 2, companies(:first_firm).clients_of_firm(true).size # checking using the db
660
- assert_equal natural, companies(:first_firm).clients_of_firm.last
661
- end
662
-
663
- def test_adding_using_create
664
- first_firm = companies(:first_firm)
665
- assert_equal 2, first_firm.plain_clients.size
666
- natural = first_firm.plain_clients.create(:name => "Natural Company")
667
- assert_equal 3, first_firm.plain_clients.length
668
- assert_equal 3, first_firm.plain_clients.size
669
- end
670
-
671
- def test_create_with_bang_on_has_many_when_parent_is_new_raises
672
- assert_raises(ActiveRecord::RecordNotSaved) do
673
- firm = Firm.new
674
- firm.plain_clients.create! :name=>"Whoever"
675
- end
676
- end
677
-
678
- def test_regular_create_on_has_many_when_parent_is_new_raises
679
- assert_raises(ActiveRecord::RecordNotSaved) do
680
- firm = Firm.new
681
- firm.plain_clients.create :name=>"Whoever"
682
- end
683
- end
684
-
685
- def test_create_with_bang_on_has_many_raises_when_record_not_saved
686
- assert_raises(ActiveRecord::RecordInvalid) do
687
- firm = Firm.find(:first)
688
- firm.plain_clients.create!
689
- end
690
- end
691
-
692
- def test_create_with_bang_on_habtm_when_parent_is_new_raises
693
- assert_raises(ActiveRecord::RecordNotSaved) do
694
- Developer.new("name" => "Aredridel").projects.create!
695
- end
696
- end
697
-
698
- def test_adding_a_mismatch_class
699
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
700
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
701
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
702
- end
703
-
704
- def test_adding_a_collection
705
- force_signal37_to_load_all_clients_of_firm
706
- companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
707
- assert_equal 3, companies(:first_firm).clients_of_firm.size
708
- assert_equal 3, companies(:first_firm).clients_of_firm(true).size
709
- end
710
-
711
- def test_adding_before_save
712
- no_of_firms = Firm.count
713
- no_of_clients = Client.count
714
-
715
- new_firm = Firm.new("name" => "A New Firm, Inc")
716
- c = Client.new("name" => "Apple")
717
-
718
- new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
719
- assert_equal 1, new_firm.clients_of_firm.size
720
- new_firm.clients_of_firm << c
721
- assert_equal 2, new_firm.clients_of_firm.size
722
-
723
- assert_equal no_of_firms, Firm.count # Firm was not saved to database.
724
- assert_equal no_of_clients, Client.count # Clients were not saved to database.
725
- assert new_firm.save
726
- assert !new_firm.new_record?
727
- assert !c.new_record?
728
- assert_equal new_firm, c.firm
729
- assert_equal no_of_firms+1, Firm.count # Firm was saved to database.
730
- assert_equal no_of_clients+2, Client.count # Clients were saved to database.
731
-
732
- assert_equal 2, new_firm.clients_of_firm.size
733
- assert_equal 2, new_firm.clients_of_firm(true).size
734
- end
735
-
736
- def test_invalid_adding
737
- firm = Firm.find(1)
738
- assert !(firm.clients_of_firm << c = Client.new)
739
- assert c.new_record?
740
- assert !firm.valid?
741
- assert !firm.save
742
- assert c.new_record?
743
- end
744
-
745
- def test_invalid_adding_before_save
746
- no_of_firms = Firm.count
747
- no_of_clients = Client.count
748
- new_firm = Firm.new("name" => "A New Firm, Inc")
749
- new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
750
- assert c.new_record?
751
- assert !c.valid?
752
- assert !new_firm.valid?
753
- assert !new_firm.save
754
- assert c.new_record?
755
- assert new_firm.new_record?
756
- end
757
-
758
- def test_build
759
- new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client")
760
- assert_equal "Another Client", new_client.name
761
- assert new_client.new_record?
762
- assert_equal new_client, companies(:first_firm).clients_of_firm.last
763
- assert companies(:first_firm).save
764
- assert !new_client.new_record?
765
- assert_equal 2, companies(:first_firm).clients_of_firm(true).size
766
- end
767
-
768
- def test_build_many
769
- new_clients = companies(:first_firm).clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}])
770
- assert_equal 2, new_clients.size
771
-
772
- assert companies(:first_firm).save
773
- assert_equal 3, companies(:first_firm).clients_of_firm(true).size
774
- end
775
-
776
- def test_build_without_loading_association
777
- first_topic = topics(:first)
778
- Reply.column_names
779
-
780
- assert_equal 1, first_topic.replies.length
781
-
782
- assert_no_queries do
783
- first_topic.replies.build(:title => "Not saved", :content => "Superstars")
784
- assert_equal 2, first_topic.replies.size
785
- end
786
-
787
- assert_equal 2, first_topic.replies.to_ary.size
788
- end
789
-
790
- def test_create_without_loading_association
791
- first_firm = companies(:first_firm)
792
- Firm.column_names
793
- Client.column_names
794
-
795
- assert_equal 1, first_firm.clients_of_firm.size
796
- first_firm.clients_of_firm.reset
797
-
798
- assert_queries(1) do
799
- first_firm.clients_of_firm.create(:name => "Superstars")
800
- end
801
-
802
- assert_equal 2, first_firm.clients_of_firm.size
803
- end
804
-
805
- def test_invalid_build
806
- new_client = companies(:first_firm).clients_of_firm.build
807
- assert new_client.new_record?
808
- assert !new_client.valid?
809
- assert_equal new_client, companies(:first_firm).clients_of_firm.last
810
- assert !companies(:first_firm).save
811
- assert new_client.new_record?
812
- assert_equal 1, companies(:first_firm).clients_of_firm(true).size
813
- end
814
-
815
- def test_create
816
- force_signal37_to_load_all_clients_of_firm
817
- new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
818
- assert !new_client.new_record?
819
- assert_equal new_client, companies(:first_firm).clients_of_firm.last
820
- assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
821
- end
822
-
823
- def test_create_many
824
- companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
825
- assert_equal 3, companies(:first_firm).clients_of_firm(true).size
826
- end
827
-
828
- def test_find_or_initialize
829
- the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client")
830
- assert_equal companies(:first_firm).id, the_client.firm_id
831
- assert_equal "Yet another client", the_client.name
832
- assert the_client.new_record?
833
- end
834
-
835
- def test_find_or_create
836
- number_of_clients = companies(:first_firm).clients.size
837
- the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client")
838
- assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
839
- assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client")
840
- assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
841
- end
842
-
843
- def test_deleting
844
- force_signal37_to_load_all_clients_of_firm
845
- companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
846
- assert_equal 0, companies(:first_firm).clients_of_firm.size
847
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
848
- end
849
-
850
- def test_deleting_before_save
851
- new_firm = Firm.new("name" => "A New Firm, Inc.")
852
- new_client = new_firm.clients_of_firm.build("name" => "Another Client")
853
- assert_equal 1, new_firm.clients_of_firm.size
854
- new_firm.clients_of_firm.delete(new_client)
855
- assert_equal 0, new_firm.clients_of_firm.size
856
- end
857
-
858
- def test_deleting_a_collection
859
- force_signal37_to_load_all_clients_of_firm
860
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
861
- assert_equal 2, companies(:first_firm).clients_of_firm.size
862
- companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
863
- assert_equal 0, companies(:first_firm).clients_of_firm.size
864
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
865
- end
866
-
867
- def test_delete_all
868
- force_signal37_to_load_all_clients_of_firm
869
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
870
- assert_equal 2, companies(:first_firm).clients_of_firm.size
871
- companies(:first_firm).clients_of_firm.delete_all
872
- assert_equal 0, companies(:first_firm).clients_of_firm.size
873
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
874
- end
875
-
876
- def test_delete_all_with_not_yet_loaded_association_collection
877
- force_signal37_to_load_all_clients_of_firm
878
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
879
- assert_equal 2, companies(:first_firm).clients_of_firm.size
880
- companies(:first_firm).clients_of_firm.reset
881
- companies(:first_firm).clients_of_firm.delete_all
882
- assert_equal 0, companies(:first_firm).clients_of_firm.size
883
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
884
- end
885
-
886
- def test_clearing_an_association_collection
887
- firm = companies(:first_firm)
888
- client_id = firm.clients_of_firm.first.id
889
- assert_equal 1, firm.clients_of_firm.size
890
-
891
- firm.clients_of_firm.clear
892
-
893
- assert_equal 0, firm.clients_of_firm.size
894
- assert_equal 0, firm.clients_of_firm(true).size
895
- assert_equal [], Client.destroyed_client_ids[firm.id]
896
-
897
- # Should not be destroyed since the association is not dependent.
898
- assert_nothing_raised do
899
- assert Client.find(client_id).firm.nil?
900
- end
901
- end
902
-
903
- def test_clearing_a_dependent_association_collection
904
- firm = companies(:first_firm)
905
- client_id = firm.dependent_clients_of_firm.first.id
906
- assert_equal 1, firm.dependent_clients_of_firm.size
907
-
908
- # :dependent means destroy is called on each client
909
- firm.dependent_clients_of_firm.clear
910
-
911
- assert_equal 0, firm.dependent_clients_of_firm.size
912
- assert_equal 0, firm.dependent_clients_of_firm(true).size
913
- assert_equal [client_id], Client.destroyed_client_ids[firm.id]
914
-
915
- # Should be destroyed since the association is dependent.
916
- assert Client.find_by_id(client_id).nil?
917
- end
918
-
919
- def test_clearing_an_exclusively_dependent_association_collection
920
- firm = companies(:first_firm)
921
- client_id = firm.exclusively_dependent_clients_of_firm.first.id
922
- assert_equal 1, firm.exclusively_dependent_clients_of_firm.size
923
-
924
- assert_equal [], Client.destroyed_client_ids[firm.id]
925
-
926
- # :exclusively_dependent means each client is deleted directly from
927
- # the database without looping through them calling destroy.
928
- firm.exclusively_dependent_clients_of_firm.clear
929
-
930
- assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
931
- assert_equal 0, firm.exclusively_dependent_clients_of_firm(true).size
932
- # no destroy-filters should have been called
933
- assert_equal [], Client.destroyed_client_ids[firm.id]
934
-
935
- # Should be destroyed since the association is exclusively dependent.
936
- assert Client.find_by_id(client_id).nil?
937
- end
938
-
939
- def test_dependent_association_respects_optional_conditions_on_delete
940
- firm = companies(:odegy)
941
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
942
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
943
- # only one of two clients is included in the association due to the :conditions key
944
- assert_equal 2, Client.find_all_by_client_of(firm.id).size
945
- assert_equal 1, firm.dependent_conditional_clients_of_firm.size
946
- firm.destroy
947
- # only the correctly associated client should have been deleted
948
- assert_equal 1, Client.find_all_by_client_of(firm.id).size
949
- end
950
-
951
- def test_dependent_association_respects_optional_sanitized_conditions_on_delete
952
- firm = companies(:odegy)
953
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
954
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
955
- # only one of two clients is included in the association due to the :conditions key
956
- assert_equal 2, Client.find_all_by_client_of(firm.id).size
957
- assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
958
- firm.destroy
959
- # only the correctly associated client should have been deleted
960
- assert_equal 1, Client.find_all_by_client_of(firm.id).size
961
- end
962
-
963
- def test_clearing_without_initial_access
964
- firm = companies(:first_firm)
965
-
966
- firm.clients_of_firm.clear
967
-
968
- assert_equal 0, firm.clients_of_firm.size
969
- assert_equal 0, firm.clients_of_firm(true).size
970
- end
971
-
972
- def test_deleting_a_item_which_is_not_in_the_collection
973
- force_signal37_to_load_all_clients_of_firm
974
- summit = Client.find_by_name('Summit')
975
- companies(:first_firm).clients_of_firm.delete(summit)
976
- assert_equal 1, companies(:first_firm).clients_of_firm.size
977
- assert_equal 1, companies(:first_firm).clients_of_firm(true).size
978
- assert_equal 2, summit.client_of
979
- end
980
-
981
- def test_deleting_type_mismatch
982
- david = Developer.find(1)
983
- david.projects.reload
984
- assert_raises(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(1) }
985
- end
986
-
987
- def test_deleting_self_type_mismatch
988
- david = Developer.find(1)
989
- david.projects.reload
990
- assert_raises(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
991
- end
992
-
993
- def test_destroy_all
994
- force_signal37_to_load_all_clients_of_firm
995
- assert !companies(:first_firm).clients_of_firm.empty?, "37signals has clients after load"
996
- companies(:first_firm).clients_of_firm.destroy_all
997
- assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
998
- assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh"
999
- end
1000
-
1001
- def test_dependence
1002
- firm = companies(:first_firm)
1003
- assert_equal 2, firm.clients.size
1004
- firm.destroy
1005
- assert Client.find(:all, :conditions => "firm_id=#{firm.id}").empty?
1006
- end
1007
-
1008
- def test_destroy_dependent_when_deleted_from_association
1009
- firm = Firm.find(:first)
1010
- assert_equal 2, firm.clients.size
1011
-
1012
- client = firm.clients.first
1013
- firm.clients.delete(client)
1014
-
1015
- assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
1016
- assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
1017
- assert_equal 1, firm.clients.size
1018
- end
1019
-
1020
- def test_three_levels_of_dependence
1021
- topic = Topic.create "title" => "neat and simple"
1022
- reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
1023
- silly_reply = reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
1024
-
1025
- assert_nothing_raised { topic.destroy }
1026
- end
1027
-
1028
- uses_transaction :test_dependence_with_transaction_support_on_failure
1029
- def test_dependence_with_transaction_support_on_failure
1030
- firm = companies(:first_firm)
1031
- clients = firm.clients
1032
- assert_equal 2, clients.length
1033
- clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
1034
-
1035
- firm.destroy rescue "do nothing"
1036
-
1037
- assert_equal 2, Client.find(:all, :conditions => "firm_id=#{firm.id}").size
1038
- end
1039
-
1040
- def test_dependence_on_account
1041
- num_accounts = Account.count
1042
- companies(:first_firm).destroy
1043
- assert_equal num_accounts - 1, Account.count
1044
- end
1045
-
1046
- def test_depends_and_nullify
1047
- num_accounts = Account.count
1048
- num_companies = Company.count
1049
-
1050
- core = companies(:rails_core)
1051
- assert_equal accounts(:rails_core_account), core.account
1052
- assert_equal companies(:leetsoft, :jadedpixel), core.companies
1053
- core.destroy
1054
- assert_nil accounts(:rails_core_account).reload.firm_id
1055
- assert_nil companies(:leetsoft).reload.client_of
1056
- assert_nil companies(:jadedpixel).reload.client_of
1057
-
1058
-
1059
- assert_equal num_accounts, Account.count
1060
- end
1061
-
1062
- def test_included_in_collection
1063
- assert companies(:first_firm).clients.include?(Client.find(2))
1064
- end
1065
-
1066
- def test_adding_array_and_collection
1067
- assert_nothing_raised { Firm.find(:first).clients + Firm.find(:all).last.clients }
1068
- end
1069
-
1070
- def test_find_all_without_conditions
1071
- firm = companies(:first_firm)
1072
- assert_equal 2, firm.clients.find(:all).length
1073
- end
1074
-
1075
- def test_replace_with_less
1076
- firm = Firm.find(:first)
1077
- firm.clients = [companies(:first_client)]
1078
- assert firm.save, "Could not save firm"
1079
- firm.reload
1080
- assert_equal 1, firm.clients.length
1081
- end
1082
-
1083
- def test_replace_with_less_and_dependent_nullify
1084
- num_companies = Company.count
1085
- companies(:rails_core).companies = []
1086
- assert_equal num_companies, Company.count
1087
- end
1088
-
1089
- def test_replace_with_new
1090
- firm = Firm.find(:first)
1091
- firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
1092
- firm.save
1093
- firm.reload
1094
- assert_equal 2, firm.clients.length
1095
- assert !firm.clients.include?(:first_client)
1096
- end
1097
-
1098
- def test_replace_on_new_object
1099
- firm = Firm.new("name" => "New Firm")
1100
- firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
1101
- assert firm.save
1102
- firm.reload
1103
- assert_equal 2, firm.clients.length
1104
- assert firm.clients.include?(Client.find_by_name("New Client"))
1105
- end
1106
-
1107
- def test_get_ids
1108
- assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
1109
- end
1110
-
1111
- def test_assign_ids
1112
- firm = Firm.new("name" => "Apple")
1113
- firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
1114
- firm.save
1115
- firm.reload
1116
- assert_equal 2, firm.clients.length
1117
- assert firm.clients.include?(companies(:second_client))
1118
- end
1119
-
1120
- def test_assign_ids_ignoring_blanks
1121
- firm = Firm.create!(:name => 'Apple')
1122
- firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
1123
- firm.save!
1124
-
1125
- assert_equal 2, firm.clients(true).size
1126
- assert firm.clients.include?(companies(:second_client))
1127
- end
1128
-
1129
- def test_get_ids_for_through
1130
- assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
1131
- end
1132
-
1133
- def test_assign_ids_for_through
1134
- assert_raise(NoMethodError) { authors(:mary).comment_ids = [123] }
1135
- end
1136
-
1137
- def test_dynamic_find_should_respect_association_order_for_through
1138
- assert_equal Comment.find(10), authors(:david).comments_desc.find(:first, :conditions => "comments.type = 'SpecialComment'")
1139
- assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment')
1140
- end
1141
-
1142
- def test_dynamic_find_order_should_override_association_order_for_through
1143
- assert_equal Comment.find(3), authors(:david).comments_desc.find(:first, :conditions => "comments.type = 'SpecialComment'", :order => 'comments.id')
1144
- assert_equal Comment.find(3), authors(:david).comments_desc.find_by_type('SpecialComment', :order => 'comments.id')
1145
- end
1146
-
1147
- def test_dynamic_find_all_should_respect_association_order_for_through
1148
- assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.find(:all, :conditions => "comments.type = 'SpecialComment'")
1149
- assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.find_all_by_type('SpecialComment')
1150
- end
1151
-
1152
- def test_dynamic_find_all_order_should_override_association_order_for_through
1153
- assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find(:all, :conditions => "comments.type = 'SpecialComment'", :order => 'comments.id')
1154
- assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find_all_by_type('SpecialComment', :order => 'comments.id')
1155
- end
1156
-
1157
- def test_dynamic_find_all_should_respect_association_limit_for_through
1158
- assert_equal 1, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'").length
1159
- assert_equal 1, authors(:david).limited_comments.find_all_by_type('SpecialComment').length
1160
- end
1161
-
1162
- def test_dynamic_find_all_order_should_override_association_limit_for_through
1163
- assert_equal 4, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'", :limit => 9_000).length
1164
- assert_equal 4, authors(:david).limited_comments.find_all_by_type('SpecialComment', :limit => 9_000).length
1165
- end
1166
-
1167
- end
1168
-
1169
- class IBMDBBelongsToAssociationsTest < Test::Unit::TestCase
1170
- fixtures :accounts, :companies, :developers, :projects, :topics,
1171
- :developers_projects, :computers, :authors, :posts, :tags, :taggings
1172
-
1173
- def test_belongs_to
1174
- Client.find(3).firm.name
1175
- assert_equal companies(:first_firm).name, Client.find(3).firm.name
1176
- assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
1177
- end
1178
-
1179
- def test_proxy_assignment
1180
- account = Account.find(1)
1181
- assert_nothing_raised { account.firm = account.firm }
1182
- end
1183
-
1184
- def test_triple_equality
1185
- assert Client.find(3).firm === Firm
1186
- assert Firm === Client.find(3).firm
1187
- end
1188
-
1189
- def test_type_mismatch
1190
- assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
1191
- assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
1192
- end
1193
-
1194
- def test_natural_assignment
1195
- apple = Firm.create("name" => "Apple")
1196
- citibank = Account.create("credit_limit" => 10)
1197
- citibank.firm = apple
1198
- assert_equal apple.id, citibank.firm_id
1199
- end
1200
-
1201
- def test_no_unexpected_aliasing
1202
- first_firm = companies(:first_firm)
1203
- another_firm = companies(:another_firm)
1204
-
1205
- citibank = Account.create("credit_limit" => 10)
1206
- citibank.firm = first_firm
1207
- original_proxy = citibank.firm
1208
- citibank.firm = another_firm
1209
-
1210
- assert_equal first_firm.object_id, original_proxy.object_id
1211
- assert_equal another_firm.object_id, citibank.firm.object_id
1212
- end
1213
-
1214
- def test_creating_the_belonging_object
1215
- citibank = Account.create("credit_limit" => 10)
1216
- apple = citibank.create_firm("name" => "Apple")
1217
- assert_equal apple, citibank.firm
1218
- citibank.save
1219
- citibank.reload
1220
- assert_equal apple, citibank.firm
1221
- end
1222
-
1223
- def test_building_the_belonging_object
1224
- citibank = Account.create("credit_limit" => 10)
1225
- apple = citibank.build_firm("name" => "Apple")
1226
- citibank.save
1227
- assert_equal apple.id, citibank.firm_id
1228
- end
1229
-
1230
- def test_natural_assignment_to_nil
1231
- client = Client.find(3)
1232
- client.firm = nil
1233
- client.save
1234
- assert_nil client.firm(true)
1235
- assert_nil client.client_of
1236
- end
1237
-
1238
- def test_with_different_class_name
1239
- assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
1240
- assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
1241
- end
1242
-
1243
- def test_with_condition
1244
- assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
1245
- assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
1246
- end
1247
-
1248
- def test_belongs_to_counter
1249
- debate = Topic.create("title" => "debate")
1250
- assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
1251
-
1252
- trash = debate.replies.create("title" => "blah!", "content" => "world around!")
1253
- assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
1254
-
1255
- trash.destroy
1256
- assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
1257
- end
1258
-
1259
- def test_belongs_to_counter_with_reassigning
1260
- t1 = Topic.create("title" => "t1")
1261
- t2 = Topic.create("title" => "t2")
1262
- r1 = Reply.new("title" => "r1", "content" => "r1")
1263
- r1.topic = t1
1264
-
1265
- assert r1.save
1266
- assert_equal 1, Topic.find(t1.id).replies.size
1267
- assert_equal 0, Topic.find(t2.id).replies.size
1268
-
1269
- r1.topic = Topic.find(t2.id)
1270
-
1271
- assert r1.save
1272
- assert_equal 0, Topic.find(t1.id).replies.size
1273
- assert_equal 1, Topic.find(t2.id).replies.size
1274
-
1275
- r1.topic = nil
1276
-
1277
- assert_equal 0, Topic.find(t1.id).replies.size
1278
- assert_equal 0, Topic.find(t2.id).replies.size
1279
-
1280
- r1.topic = t1
1281
-
1282
- assert_equal 1, Topic.find(t1.id).replies.size
1283
- assert_equal 0, Topic.find(t2.id).replies.size
1284
-
1285
- r1.destroy
1286
-
1287
- assert_equal 0, Topic.find(t1.id).replies.size
1288
- assert_equal 0, Topic.find(t2.id).replies.size
1289
- end
1290
-
1291
- def test_belongs_to_counter_after_save
1292
- topic = Topic.create!(:title => "monday night")
1293
- topic.replies.create!(:title => "re: monday night", :content => "football")
1294
- assert_equal 1, Topic.find(topic.id)[:replies_count]
1295
-
1296
- topic.save!
1297
- assert_equal 1, Topic.find(topic.id)[:replies_count]
1298
- end
1299
-
1300
- def test_belongs_to_counter_after_update_attributes
1301
- topic = Topic.create!(:title => "37s")
1302
- topic.replies.create!(:title => "re: 37s", :content => "rails")
1303
- assert_equal 1, Topic.find(topic.id)[:replies_count]
1304
-
1305
- topic.update_attributes(:title => "37signals")
1306
- assert_equal 1, Topic.find(topic.id)[:replies_count]
1307
- end
1308
-
1309
- def test_belongs_to_counter_after_save
1310
- topic = Topic.create("title" => "monday night")
1311
- topic.replies.create("title" => "re: monday night", "content" => "football")
1312
- assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
1313
-
1314
- topic.save
1315
- assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
1316
- end
1317
-
1318
- def test_belongs_to_counter_after_update_attributes
1319
- topic = Topic.create("title" => "37s")
1320
- topic.replies.create("title" => "re: 37s", "content" => "rails")
1321
- assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
1322
-
1323
- topic.update_attributes("title" => "37signals")
1324
- assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
1325
- end
1326
-
1327
- def test_assignment_before_parent_saved
1328
- client = Client.find(:first)
1329
- apple = Firm.new("name" => "Apple")
1330
- client.firm = apple
1331
- assert_equal apple, client.firm
1332
- assert apple.new_record?
1333
- assert client.save
1334
- assert apple.save
1335
- assert !apple.new_record?
1336
- assert_equal apple, client.firm
1337
- assert_equal apple, client.firm(true)
1338
- end
1339
-
1340
- def test_assignment_before_child_saved
1341
- final_cut = Client.new("name" => "Final Cut")
1342
- firm = Firm.find(1)
1343
- final_cut.firm = firm
1344
- assert final_cut.new_record?
1345
- assert final_cut.save
1346
- assert !final_cut.new_record?
1347
- assert !firm.new_record?
1348
- assert_equal firm, final_cut.firm
1349
- assert_equal firm, final_cut.firm(true)
1350
- end
1351
-
1352
- def test_assignment_before_either_saved
1353
- final_cut = Client.new("name" => "Final Cut")
1354
- apple = Firm.new("name" => "Apple")
1355
- final_cut.firm = apple
1356
- assert final_cut.new_record?
1357
- assert apple.new_record?
1358
- assert final_cut.save
1359
- assert !final_cut.new_record?
1360
- assert !apple.new_record?
1361
- assert_equal apple, final_cut.firm
1362
- assert_equal apple, final_cut.firm(true)
1363
- end
1364
-
1365
- def test_new_record_with_foreign_key_but_no_object
1366
- c = Client.new("firm_id" => 1)
1367
- assert_equal Firm.find(:first), c.firm_with_basic_id
1368
- end
1369
-
1370
- def test_forgetting_the_load_when_foreign_key_enters_late
1371
- c = Client.new
1372
- assert_nil c.firm_with_basic_id
1373
-
1374
- c.firm_id = 1
1375
- assert_equal Firm.find(:first), c.firm_with_basic_id
1376
- end
1377
-
1378
- def test_field_name_same_as_foreign_key
1379
- computer = Computer.find(1)
1380
- assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
1381
- end
1382
-
1383
- def test_counter_cache
1384
- topic = Topic.create :title => "Zoom-zoom-zoom"
1385
- assert_equal 0, topic[:replies_count]
1386
-
1387
- reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
1388
- reply.topic = topic
1389
-
1390
- assert_equal 1, topic.reload[:replies_count]
1391
- assert_equal 1, topic.replies.size
1392
-
1393
- topic[:replies_count] = 15
1394
- assert_equal 15, topic.replies.size
1395
- end
1396
-
1397
- def test_custom_counter_cache
1398
- reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
1399
- assert_equal 0, reply[:replies_count]
1400
-
1401
- silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
1402
- silly.reply = reply
1403
-
1404
- assert_equal 1, reply.reload[:replies_count]
1405
- assert_equal 1, reply.replies.size
1406
-
1407
- reply[:replies_count] = 17
1408
- assert_equal 17, reply.replies.size
1409
- end
1410
-
1411
- def test_store_two_association_with_one_save
1412
- num_orders = Order.count
1413
- num_customers = Customer.count
1414
- order = Order.new
1415
-
1416
- customer1 = order.billing = Customer.new
1417
- customer2 = order.shipping = Customer.new
1418
- assert order.save
1419
- assert_equal customer1, order.billing
1420
- assert_equal customer2, order.shipping
1421
-
1422
- order.reload
1423
-
1424
- assert_equal customer1, order.billing
1425
- assert_equal customer2, order.shipping
1426
-
1427
- assert_equal num_orders +1, Order.count
1428
- assert_equal num_customers +2, Customer.count
1429
- end
1430
-
1431
-
1432
- def test_store_association_in_two_relations_with_one_save
1433
- num_orders = Order.count
1434
- num_customers = Customer.count
1435
- order = Order.new
1436
-
1437
- customer = order.billing = order.shipping = Customer.new
1438
- assert order.save
1439
- assert_equal customer, order.billing
1440
- assert_equal customer, order.shipping
1441
-
1442
- order.reload
1443
-
1444
- assert_equal customer, order.billing
1445
- assert_equal customer, order.shipping
1446
-
1447
- assert_equal num_orders +1, Order.count
1448
- assert_equal num_customers +1, Customer.count
1449
- end
1450
-
1451
- def test_store_association_in_two_relations_with_one_save_in_existing_object
1452
- num_orders = Order.count
1453
- num_customers = Customer.count
1454
- order = Order.create
1455
-
1456
- customer = order.billing = order.shipping = Customer.new
1457
- assert order.save
1458
- assert_equal customer, order.billing
1459
- assert_equal customer, order.shipping
1460
-
1461
- order.reload
1462
-
1463
- assert_equal customer, order.billing
1464
- assert_equal customer, order.shipping
1465
-
1466
- assert_equal num_orders +1, Order.count
1467
- assert_equal num_customers +1, Customer.count
1468
- end
1469
-
1470
- def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
1471
- num_orders = Order.count
1472
- num_customers = Customer.count
1473
- order = Order.create
1474
-
1475
- customer = order.billing = order.shipping = Customer.new
1476
- assert order.save
1477
- assert_equal customer, order.billing
1478
- assert_equal customer, order.shipping
1479
-
1480
- order.reload
1481
-
1482
- customer = order.billing = order.shipping = Customer.new
1483
-
1484
- assert order.save
1485
- order.reload
1486
-
1487
- assert_equal customer, order.billing
1488
- assert_equal customer, order.shipping
1489
-
1490
- assert_equal num_orders +1, Order.count
1491
- assert_equal num_customers +2, Customer.count
1492
- end
1493
-
1494
-
1495
- def test_association_assignment_sticks
1496
- post = Post.find(:first)
1497
-
1498
- author1, author2 = Author.find(:all, :limit => 2)
1499
- assert_not_nil author1
1500
- assert_not_nil author2
1501
-
1502
- # make sure the association is loaded
1503
- post.author
1504
-
1505
- # set the association by id, directly
1506
- post.author_id = author2.id
1507
-
1508
- # save and reload
1509
- post.save!
1510
- post.reload
1511
-
1512
- # the author id of the post should be the id we set
1513
- assert_equal post.author_id, author2.id
1514
- end
1515
-
1516
- end
1517
-
1518
-
1519
- class ProjectWithAfterCreateHook < ActiveRecord::Base
1520
- set_table_name 'projects'
1521
- has_and_belongs_to_many :developers,
1522
- :class_name => "DeveloperForProjectWithAfterCreateHook",
1523
- :join_table => "developers_projects",
1524
- :foreign_key => "project_id",
1525
- :association_foreign_key => "developer_id"
1526
-
1527
- after_create :add_david
1528
-
1529
- def add_david
1530
- david = DeveloperForProjectWithAfterCreateHook.find_by_name('David')
1531
- david.projects << self
1532
- end
1533
- end
1534
-
1535
- class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
1536
- set_table_name 'developers'
1537
- has_and_belongs_to_many :projects,
1538
- :class_name => "ProjectWithAfterCreateHook",
1539
- :join_table => "developers_projects",
1540
- :association_foreign_key => "project_id",
1541
- :foreign_key => "developer_id"
1542
- end
1543
-
1544
-
1545
- class IBMDBHasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
1546
- fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects
1547
-
1548
- def test_has_and_belongs_to_many
1549
- david = Developer.find(1)
1550
-
1551
- assert !david.projects.empty?
1552
- assert_equal 2, david.projects.size
1553
-
1554
- active_record = Project.find(1)
1555
- assert !active_record.developers.empty?
1556
- assert_equal 3, active_record.developers.size
1557
- assert active_record.developers.include?(david)
1558
- end
1559
-
1560
- def test_triple_equality
1561
- assert !(Array === Developer.find(1).projects)
1562
- assert Developer.find(1).projects === Array
1563
- end
1564
-
1565
- def test_adding_single
1566
- jamis = Developer.find(2)
1567
- jamis.projects.reload # causing the collection to load
1568
- action_controller = Project.find(2)
1569
- assert_equal 1, jamis.projects.size
1570
- assert_equal 1, action_controller.developers.size
1571
-
1572
- jamis.projects << action_controller
1573
-
1574
- assert_equal 2, jamis.projects.size
1575
- assert_equal 2, jamis.projects(true).size
1576
- assert_equal 2, action_controller.developers(true).size
1577
- end
1578
-
1579
- def test_adding_type_mismatch
1580
- jamis = Developer.find(2)
1581
- assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << nil }
1582
- assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << 1 }
1583
- end
1584
-
1585
- def test_adding_from_the_project
1586
- jamis = Developer.find(2)
1587
- action_controller = Project.find(2)
1588
- action_controller.developers.reload
1589
- assert_equal 1, jamis.projects.size
1590
- assert_equal 1, action_controller.developers.size
1591
-
1592
- action_controller.developers << jamis
1593
-
1594
- assert_equal 2, jamis.projects(true).size
1595
- assert_equal 2, action_controller.developers.size
1596
- assert_equal 2, action_controller.developers(true).size
1597
- end
1598
-
1599
- def test_adding_from_the_project_fixed_timestamp
1600
- jamis = Developer.find(2)
1601
- action_controller = Project.find(2)
1602
- action_controller.developers.reload
1603
- assert_equal 1, jamis.projects.size
1604
- assert_equal 1, action_controller.developers.size
1605
- updated_at = jamis.updated_at
1606
-
1607
- action_controller.developers << jamis
1608
-
1609
- assert_equal updated_at, jamis.updated_at
1610
- assert_equal 2, jamis.projects(true).size
1611
- assert_equal 2, action_controller.developers.size
1612
- assert_equal 2, action_controller.developers(true).size
1613
- end
1614
-
1615
- def test_adding_multiple
1616
- aredridel = Developer.new("name" => "Aredridel")
1617
- aredridel.save
1618
- aredridel.projects.reload
1619
- aredridel.projects.push(Project.find(1), Project.find(2))
1620
- assert_equal 2, aredridel.projects.size
1621
- assert_equal 2, aredridel.projects(true).size
1622
- end
1623
-
1624
- def test_adding_a_collection
1625
- aredridel = Developer.new("name" => "Aredridel")
1626
- aredridel.save
1627
- aredridel.projects.reload
1628
- aredridel.projects.concat([Project.find(1), Project.find(2)])
1629
- assert_equal 2, aredridel.projects.size
1630
- assert_equal 2, aredridel.projects(true).size
1631
- end
1632
-
1633
- def test_adding_uses_default_values_on_join_table
1634
- ac = projects(:action_controller)
1635
- assert !developers(:jamis).projects.include?(ac)
1636
- developers(:jamis).projects << ac
1637
-
1638
- assert developers(:jamis, :reload).projects.include?(ac)
1639
- project = developers(:jamis).projects.detect { |p| p == ac }
1640
- assert_equal 1, project.access_level.to_i
1641
- end
1642
-
1643
- def test_habtm_attribute_access_and_respond_to
1644
- project = developers(:jamis).projects[0]
1645
- assert project.has_attribute?("name")
1646
- assert project.has_attribute?("joined_on")
1647
- assert project.has_attribute?("access_level")
1648
- assert project.respond_to?("name")
1649
- assert project.respond_to?("name=")
1650
- assert project.respond_to?("name?")
1651
- assert project.respond_to?("joined_on")
1652
- # given that the 'join attribute' won't be persisted, I don't
1653
- # think we should define the mutators
1654
- #assert project.respond_to?("joined_on=")
1655
- assert project.respond_to?("joined_on?")
1656
- assert project.respond_to?("access_level")
1657
- #assert project.respond_to?("access_level=")
1658
- assert project.respond_to?("access_level?")
1659
- end
1660
-
1661
- def test_habtm_adding_before_save
1662
- no_of_devels = Developer.count
1663
- no_of_projects = Project.count
1664
- aredridel = Developer.new("name" => "Aredridel")
1665
- aredridel.projects.concat([Project.find(1), p = Project.new("name" => "Projekt")])
1666
- assert aredridel.new_record?
1667
- assert p.new_record?
1668
- assert aredridel.save
1669
- assert !aredridel.new_record?
1670
- assert_equal no_of_devels+1, Developer.count
1671
- assert_equal no_of_projects+1, Project.count
1672
- assert_equal 2, aredridel.projects.size
1673
- assert_equal 2, aredridel.projects(true).size
1674
- end
1675
-
1676
- def test_habtm_saving_multiple_relationships
1677
- new_project = Project.new("name" => "Grimetime")
1678
- amount_of_developers = 4
1679
- developers = (0...amount_of_developers).collect {|i| Developer.create(:name => "JME #{i}") }.reverse
1680
-
1681
- new_project.developer_ids = [developers[0].id, developers[1].id]
1682
- new_project.developers_with_callback_ids = [developers[2].id, developers[3].id]
1683
- assert new_project.save
1684
-
1685
- new_project.reload
1686
- assert_equal amount_of_developers, new_project.developers.size
1687
- assert_equal developers, new_project.developers
1688
- end
1689
-
1690
- def test_habtm_unique_order_preserved
1691
- assert_equal developers(:poor_jamis, :jamis, :david), projects(:active_record).non_unique_developers
1692
- assert_equal developers(:poor_jamis, :jamis, :david), projects(:active_record).developers
1693
- end
1694
-
1695
- def test_build
1696
- devel = Developer.find(1)
1697
- proj = devel.projects.build("name" => "Projekt")
1698
- assert_equal devel.projects.last, proj
1699
- assert proj.new_record?
1700
- devel.save
1701
- assert !proj.new_record?
1702
- assert_equal devel.projects.last, proj
1703
- assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
1704
- end
1705
-
1706
- def test_build_by_new_record
1707
- devel = Developer.new(:name => "Marcel", :salary => 75000)
1708
- proj1 = devel.projects.build(:name => "Make bed")
1709
- proj2 = devel.projects.build(:name => "Lie in it")
1710
- assert_equal devel.projects.last, proj2
1711
- assert proj2.new_record?
1712
- devel.save
1713
- assert !devel.new_record?
1714
- assert !proj2.new_record?
1715
- assert_equal devel.projects.last, proj2
1716
- assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
1717
- end
1718
-
1719
- def test_create
1720
- devel = Developer.find(1)
1721
- proj = devel.projects.create("name" => "Projekt")
1722
- assert_equal devel.projects.last, proj
1723
- assert !proj.new_record?
1724
- assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
1725
- end
1726
-
1727
- unless current_adapter?(:IBM_DBAdapter)#same as test case test_build_by_new_record
1728
- def test_create_by_new_record
1729
- devel = Developer.new(:name => "Marcel", :salary => 75000)
1730
- proj1 = devel.projects.build(:name => "Make bed")
1731
- proj2 = devel.projects.build(:name => "Lie in it")
1732
- assert_equal devel.projects.last, proj2
1733
- assert proj2.new_record?
1734
- devel.save
1735
- assert !devel.new_record?
1736
- assert !proj2.new_record?
1737
- assert_equal devel.projects.last, proj2
1738
- assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
1739
- end
1740
- end
1741
-
1742
- def test_uniq_after_the_fact
1743
- developers(:jamis).projects << projects(:active_record)
1744
- developers(:jamis).projects << projects(:active_record)
1745
- assert_equal 3, developers(:jamis).projects.size
1746
- assert_equal 1, developers(:jamis).projects.uniq.size
1747
- end
1748
-
1749
- def test_uniq_before_the_fact
1750
- projects(:active_record).developers << developers(:jamis)
1751
- projects(:active_record).developers << developers(:david)
1752
- assert_equal 3, projects(:active_record, :reload).developers.size
1753
- end
1754
-
1755
- def test_deleting
1756
- david = Developer.find(1)
1757
- active_record = Project.find(1)
1758
- david.projects.reload
1759
- assert_equal 2, david.projects.size
1760
- assert_equal 3, active_record.developers.size
1761
-
1762
- david.projects.delete(active_record)
1763
-
1764
- assert_equal 1, david.projects.size
1765
- assert_equal 1, david.projects(true).size
1766
- assert_equal 2, active_record.developers(true).size
1767
- end
1768
-
1769
- def test_deleting_array
1770
- david = Developer.find(1)
1771
- david.projects.reload
1772
- david.projects.delete(Project.find(:all))
1773
- assert_equal 0, david.projects.size
1774
- assert_equal 0, david.projects(true).size
1775
- end
1776
-
1777
- def test_deleting_with_sql
1778
- david = Developer.find(1)
1779
- active_record = Project.find(1)
1780
- active_record.developers.reload
1781
- assert_equal 3, active_record.developers_by_sql.size
1782
-
1783
- active_record.developers_by_sql.delete(david)
1784
- assert_equal 2, active_record.developers_by_sql(true).size
1785
- end
1786
-
1787
- def test_deleting_array_with_sql
1788
- active_record = Project.find(1)
1789
- active_record.developers.reload
1790
- assert_equal 3, active_record.developers_by_sql.size
1791
-
1792
- active_record.developers_by_sql.delete(Developer.find(:all))
1793
- assert_equal 0, active_record.developers_by_sql(true).size
1794
- end
1795
-
1796
- def test_deleting_all
1797
- david = Developer.find(1)
1798
- david.projects.reload
1799
- david.projects.clear
1800
- assert_equal 0, david.projects.size
1801
- assert_equal 0, david.projects(true).size
1802
- end
1803
-
1804
- def test_removing_associations_on_destroy
1805
- david = DeveloperWithBeforeDestroyRaise.find(1)
1806
- assert !david.projects.empty?
1807
- assert_nothing_raised { david.destroy }
1808
- assert david.projects.empty?
1809
- assert DeveloperWithBeforeDestroyRaise.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = 1").empty?
1810
- end
1811
-
1812
- def test_additional_columns_from_join_table
1813
- assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.first.joined_on.to_date
1814
- end
1815
-
1816
- def test_destroy_all
1817
- david = Developer.find(1)
1818
- david.projects.reload
1819
- assert !david.projects.empty?
1820
- david.projects.destroy_all
1821
- assert david.projects.empty?
1822
- assert david.projects(true).empty?
1823
- end
1824
-
1825
- def test_deprecated_push_with_attributes_was_removed
1826
- jamis = developers(:jamis)
1827
- assert_raise(NoMethodError) do
1828
- jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today)
1829
- end
1830
- end
1831
-
1832
- def test_associations_with_conditions
1833
- assert_equal 3, projects(:active_record).developers.size
1834
- assert_equal 1, projects(:active_record).developers_named_david.size
1835
- assert_equal 1, projects(:active_record).developers_named_david_with_hash_conditions.size
1836
-
1837
- assert_equal developers(:david), projects(:active_record).developers_named_david.find(developers(:david).id)
1838
- assert_equal developers(:david), projects(:active_record).developers_named_david_with_hash_conditions.find(developers(:david).id)
1839
- assert_equal developers(:david), projects(:active_record).salaried_developers.find(developers(:david).id)
1840
-
1841
- projects(:active_record).developers_named_david.clear
1842
- assert_equal 2, projects(:active_record, :reload).developers.size
1843
- end
1844
-
1845
- def test_find_in_association
1846
- # Using sql
1847
- assert_equal developers(:david), projects(:active_record).developers.find(developers(:david).id), "SQL find"
1848
-
1849
- # Using ruby
1850
- active_record = projects(:active_record)
1851
- active_record.developers.reload
1852
- assert_equal developers(:david), active_record.developers.find(developers(:david).id), "Ruby find"
1853
- end
1854
-
1855
- def test_find_in_association_with_custom_finder_sql
1856
- assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id), "SQL find"
1857
-
1858
- active_record = projects(:active_record)
1859
- active_record.developers_with_finder_sql.reload
1860
- assert_equal developers(:david), active_record.developers_with_finder_sql.find(developers(:david).id), "Ruby find"
1861
- end
1862
-
1863
- def test_find_in_association_with_custom_finder_sql_and_string_id
1864
- assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id.to_s), "SQL find"
1865
- end
1866
-
1867
- def test_find_with_merged_options
1868
- assert_equal 1, projects(:active_record).limited_developers.size
1869
- assert_equal 1, projects(:active_record).limited_developers.find(:all).size
1870
- assert_equal 3, projects(:active_record).limited_developers.find(:all, :limit => nil).size
1871
- end
1872
-
1873
- def test_dynamic_find_should_respect_association_order
1874
- # Developers are ordered 'name DESC, id DESC'
1875
- low_id_jamis = developers(:jamis)
1876
- middle_id_jamis = developers(:poor_jamis)
1877
- high_id_jamis = projects(:active_record).developers.create(:name => 'Jamis')
1878
-
1879
- assert_equal high_id_jamis, projects(:active_record).developers.find(:first, :conditions => "name = 'Jamis'")
1880
- assert_equal high_id_jamis, projects(:active_record).developers.find_by_name('Jamis')
1881
- end
1882
-
1883
- def test_dynamic_find_order_should_override_association_order
1884
- # Developers are ordered 'name DESC, id DESC'
1885
- low_id_jamis = developers(:jamis)
1886
- middle_id_jamis = developers(:poor_jamis)
1887
- high_id_jamis = projects(:active_record).developers.create(:name => 'Jamis')
1888
-
1889
- assert_equal low_id_jamis, projects(:active_record).developers.find(:first, :conditions => "name = 'Jamis'", :order => 'id')
1890
- assert_equal low_id_jamis, projects(:active_record).developers.find_by_name('Jamis', :order => 'id')
1891
- end
1892
-
1893
- def test_dynamic_find_all_should_respect_association_order
1894
- # Developers are ordered 'name DESC, id DESC'
1895
- low_id_jamis = developers(:jamis)
1896
- middle_id_jamis = developers(:poor_jamis)
1897
- high_id_jamis = projects(:active_record).developers.create(:name => 'Jamis')
1898
-
1899
- assert_equal [high_id_jamis, middle_id_jamis, low_id_jamis], projects(:active_record).developers.find(:all, :conditions => "name = 'Jamis'")
1900
- assert_equal [high_id_jamis, middle_id_jamis, low_id_jamis], projects(:active_record).developers.find_all_by_name('Jamis')
1901
- end
1902
-
1903
- def test_dynamic_find_all_order_should_override_association_order
1904
- # Developers are ordered 'name DESC, id DESC'
1905
- low_id_jamis = developers(:jamis)
1906
- middle_id_jamis = developers(:poor_jamis)
1907
- high_id_jamis = projects(:active_record).developers.create(:name => 'Jamis')
1908
-
1909
- assert_equal [low_id_jamis, middle_id_jamis, high_id_jamis], projects(:active_record).developers.find(:all, :conditions => "name = 'Jamis'", :order => 'id')
1910
- assert_equal [low_id_jamis, middle_id_jamis, high_id_jamis], projects(:active_record).developers.find_all_by_name('Jamis', :order => 'id')
1911
- end
1912
-
1913
- def test_dynamic_find_all_should_respect_association_limit
1914
- assert_equal 1, projects(:active_record).limited_developers.find(:all, :conditions => "name = 'Jamis'").length
1915
- assert_equal 1, projects(:active_record).limited_developers.find_all_by_name('Jamis').length
1916
- end
1917
-
1918
- def test_dynamic_find_all_order_should_override_association_limit
1919
- assert_equal 2, projects(:active_record).limited_developers.find(:all, :conditions => "name = 'Jamis'", :limit => 9_000).length
1920
- assert_equal 2, projects(:active_record).limited_developers.find_all_by_name('Jamis', :limit => 9_000).length
1921
- end
1922
-
1923
- def test_new_with_values_in_collection
1924
- jamis = DeveloperForProjectWithAfterCreateHook.find_by_name('Jamis')
1925
- david = DeveloperForProjectWithAfterCreateHook.find_by_name('David')
1926
- project = ProjectWithAfterCreateHook.new(:name => "Cooking with Bertie")
1927
- project.developers << jamis
1928
- project.save!
1929
- project.reload
1930
-
1931
- assert project.developers.include?(jamis)
1932
- assert project.developers.include?(david)
1933
- end
1934
-
1935
- def test_find_in_association_with_options
1936
- developers = projects(:active_record).developers.find(:all)
1937
- assert_equal 3, developers.size
1938
-
1939
- assert_equal developers(:poor_jamis), projects(:active_record).developers.find(:first, :conditions => "salary < 10000")
1940
- assert_equal developers(:jamis), projects(:active_record).developers.find(:first, :order => "salary DESC")
1941
- end
1942
-
1943
- def test_replace_with_less
1944
- david = developers(:david)
1945
- david.projects = [projects(:action_controller)]
1946
- assert david.save
1947
- assert_equal 1, david.projects.length
1948
- end
1949
-
1950
- def test_replace_with_new
1951
- david = developers(:david)
1952
- david.projects = [projects(:action_controller), Project.new("name" => "ActionWebSearch")]
1953
- david.save
1954
- assert_equal 2, david.projects.length
1955
- assert !david.projects.include?(projects(:active_record))
1956
- end
1957
-
1958
- def test_replace_on_new_object
1959
- new_developer = Developer.new("name" => "Matz")
1960
- new_developer.projects = [projects(:action_controller), Project.new("name" => "ActionWebSearch")]
1961
- new_developer.save
1962
- assert_equal 2, new_developer.projects.length
1963
- end
1964
-
1965
- def test_consider_type
1966
- developer = Developer.find(:first)
1967
- special_project = SpecialProject.create("name" => "Special Project")
1968
-
1969
- other_project = developer.projects.first
1970
- developer.special_projects << special_project
1971
- developer.reload
1972
-
1973
- assert developer.projects.include?(special_project)
1974
- assert developer.special_projects.include?(special_project)
1975
- assert !developer.special_projects.include?(other_project)
1976
- end
1977
-
1978
- def test_update_attributes_after_push_without_duplicate_join_table_rows
1979
- developer = Developer.new("name" => "Kano")
1980
- project = SpecialProject.create("name" => "Special Project")
1981
- assert developer.save
1982
- developer.projects << project
1983
- developer.update_attribute("name", "Bruza")
1984
- assert_equal 1, Developer.connection.select_value(<<-end_sql).to_i
1985
- SELECT count(*) FROM developers_projects
1986
- WHERE project_id = #{project.id}
1987
- AND developer_id = #{developer.id}
1988
- end_sql
1989
- end
1990
-
1991
- def test_updating_attributes_on_non_rich_associations
1992
- welcome = categories(:technology).posts.first
1993
- welcome.title = "Something else"
1994
- assert welcome.save!
1995
- end
1996
-
1997
- def test_habtm_respects_select
1998
- categories(:technology).select_testing_posts(true).each do |o|
1999
- assert_respond_to o, :correctness_marker
2000
- end
2001
- assert_respond_to categories(:technology).select_testing_posts.find(:first), :correctness_marker
2002
- end
2003
-
2004
- def test_updating_attributes_on_rich_associations
2005
- david = projects(:action_controller).developers.first
2006
- david.name = "DHH"
2007
- assert_raises(ActiveRecord::ReadOnlyRecord) { david.save! }
2008
- end
2009
-
2010
- def test_updating_attributes_on_rich_associations_with_limited_find_from_reflection
2011
- david = projects(:action_controller).selected_developers.first
2012
- david.name = "DHH"
2013
- assert_nothing_raised { david.save! }
2014
- end
2015
-
2016
-
2017
- def test_updating_attributes_on_rich_associations_with_limited_find
2018
- david = projects(:action_controller).developers.find(:all, :select => "developers.*").first
2019
- david.name = "DHH"
2020
- assert david.save!
2021
- end
2022
-
2023
- def test_join_table_alias
2024
- assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL').size
2025
- end
2026
-
2027
- def test_join_with_group
2028
- group = Developer.columns.inject([]) do |g, c|
2029
- g << "developers.#{c.name}"
2030
- g << "developers_projects_2.#{c.name}"
2031
- end
2032
- Project.columns.each { |c| group << "projects.#{c.name}" }
2033
-
2034
- assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL', :group => group.join(",")).size
2035
- end
2036
-
2037
- def test_get_ids
2038
- assert_equal projects(:active_record, :action_controller).map(&:id), developers(:david).project_ids
2039
- assert_equal [projects(:active_record).id], developers(:jamis).project_ids
2040
- end
2041
-
2042
- def test_assign_ids
2043
- developer = Developer.new("name" => "Joe")
2044
- developer.project_ids = projects(:active_record, :action_controller).map(&:id)
2045
- developer.save
2046
- developer.reload
2047
- assert_equal 2, developer.projects.length
2048
- assert_equal projects(:active_record), developer.projects[0]
2049
- assert_equal projects(:action_controller), developer.projects[1]
2050
- end
2051
-
2052
- def test_assign_ids_ignoring_blanks
2053
- developer = Developer.new("name" => "Joe")
2054
- developer.project_ids = [projects(:active_record).id, nil, projects(:action_controller).id, '']
2055
- developer.save
2056
- developer.reload
2057
- assert_equal 2, developer.projects.length
2058
- assert_equal projects(:active_record), developer.projects[0]
2059
- assert_equal projects(:action_controller), developer.projects[1]
2060
- end
2061
-
2062
- def test_select_limited_ids_list
2063
- # Set timestamps
2064
- Developer.transaction do
2065
- Developer.find(:all, :order => 'id').each_with_index do |record, i|
2066
- record.update_attributes(:created_at => 5.years.ago + (i * 5.minutes))
2067
- end
2068
- end
2069
-
2070
- join_base = ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase.new(Project)
2071
- join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(join_base, :developers, nil)
2072
- unless current_adapter?(:IBM_DBAdapter)
2073
- projects = Project.send(:select_limited_ids_list, {:order => 'developers.created_at'}, join_dep)
2074
- assert !projects.include?("'"), projects
2075
- assert_equal %w(1 2), projects.scan(/\d/).sort
2076
- end
2077
- end
2078
-
2079
- def test_scoped_find_on_through_association_doesnt_return_read_only_records
2080
- tag = Post.find(1).tags.find_by_name("General")
2081
-
2082
- assert_nothing_raised do
2083
- tag.save!
2084
- end
2085
- end
2086
- end
2087
-
2088
-
2089
- class IBMDBOverridingAssociationsTest < Test::Unit::TestCase
2090
- class Person < ActiveRecord::Base; end
2091
- class DifferentPerson < ActiveRecord::Base; end
2092
-
2093
- class PeopleList < ActiveRecord::Base
2094
- has_and_belongs_to_many :has_and_belongs_to_many, :before_add => :enlist
2095
- has_many :has_many, :before_add => :enlist
2096
- belongs_to :belongs_to
2097
- has_one :has_one
2098
- end
2099
-
2100
- class DifferentPeopleList < PeopleList
2101
- # Different association with the same name, callbacks should be omitted here.
2102
- has_and_belongs_to_many :has_and_belongs_to_many, :class_name => 'DifferentPerson'
2103
- has_many :has_many, :class_name => 'DifferentPerson'
2104
- belongs_to :belongs_to, :class_name => 'DifferentPerson'
2105
- has_one :has_one, :class_name => 'DifferentPerson'
2106
- end
2107
-
2108
- def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherited
2109
- # redeclared association on AR descendant should not inherit callbacks from superclass
2110
- callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
2111
- assert_equal([:enlist], callbacks)
2112
- callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
2113
- assert_equal([], callbacks)
2114
- end
2115
-
2116
- def test_has_many_association_redefinition_callbacks_should_differ_and_not_inherited
2117
- # redeclared association on AR descendant should not inherit callbacks from superclass
2118
- callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_many)
2119
- assert_equal([:enlist], callbacks)
2120
- callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_many)
2121
- assert_equal([], callbacks)
2122
- end
2123
-
2124
- def test_habtm_association_redefinition_reflections_should_differ_and_not_inherited
2125
- assert_not_equal(
2126
- PeopleList.reflect_on_association(:has_and_belongs_to_many),
2127
- DifferentPeopleList.reflect_on_association(:has_and_belongs_to_many)
2128
- )
2129
- end
2130
-
2131
- def test_has_many_association_redefinition_reflections_should_differ_and_not_inherited
2132
- assert_not_equal(
2133
- PeopleList.reflect_on_association(:has_many),
2134
- DifferentPeopleList.reflect_on_association(:has_many)
2135
- )
2136
- end
2137
-
2138
- def test_belongs_to_association_redefinition_reflections_should_differ_and_not_inherited
2139
- assert_not_equal(
2140
- PeopleList.reflect_on_association(:belongs_to),
2141
- DifferentPeopleList.reflect_on_association(:belongs_to)
2142
- )
2143
- end
2144
-
2145
- def test_has_one_association_redefinition_reflections_should_differ_and_not_inherited
2146
- assert_not_equal(
2147
- PeopleList.reflect_on_association(:has_one),
2148
- DifferentPeopleList.reflect_on_association(:has_one)
2149
- )
2150
- end
2151
- end