simply_stored 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,489 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/fixtures/couch')
3
-
4
- class HasManyTest < Test::Unit::TestCase
5
- context "has_many" do
6
- setup do
7
- CouchPotato::Config.database_name = 'simply_stored_test'
8
- recreate_db
9
- end
10
-
11
- context "with has_many" do
12
- should "create a fetch method for the associated objects" do
13
- user = User.new
14
- assert user.respond_to?(:posts)
15
- end
16
-
17
- should "raise an error if another property with the same name already exists" do
18
- assert_raise(RuntimeError) do
19
- class ::DoubleHasManyUser
20
- include SimplyStored::Couch
21
- property :other_users
22
- has_many :other_users
23
- end
24
- end
25
- end
26
-
27
- should "fetch the associated objects" do
28
- user = User.create(:title => "Mr.")
29
- 3.times {
30
- post = Post.new
31
- post.user = user
32
- post.save!
33
- }
34
- assert_equal 3, user.posts.size
35
- end
36
-
37
- should "set the parent object on the clients cache" do
38
- User.expects(:find).never
39
- user = User.create(:title => "Mr.")
40
- 3.times {
41
- post = Post.new
42
- post.user = user
43
- post.save!
44
- }
45
- post = user.posts.first
46
- assert_equal user, user.posts.first.user
47
- end
48
-
49
- context "limit" do
50
-
51
- should "be able to limit the result set" do
52
- user = User.create(:title => "Mr.")
53
- 3.times {
54
- post = Post.new
55
- post.user = user
56
- post.save!
57
- }
58
- assert_equal 2, user.posts(:limit => 2).size
59
- end
60
-
61
- should "use the given options in the cache-key" do
62
- user = User.create(:title => "Mr.")
63
- 3.times {
64
- post = Post.new
65
- post.user = user
66
- post.save!
67
- }
68
- assert_equal 2, user.posts(:limit => 2).size
69
- assert_equal 3, user.posts(:limit => 3).size
70
- end
71
-
72
- should "be able to limit the result set - also for through objects" do
73
- @user = User.create(:title => "Mr.")
74
- first_pain = Pain.create
75
- frist_hemorrhoid = Hemorrhoid.create(:user => @user, :pain => first_pain)
76
- assert_equal [first_pain], @user.pains
77
- second_pain = Pain.create
78
- second_hemorrhoid = Hemorrhoid.create(:user => @user, :pain => second_pain)
79
- @user.reload
80
- assert_equal 2, @user.pains.size
81
- assert_equal 1, @user.pains(:limit => 1).size
82
- end
83
- end
84
-
85
- context "order" do
86
- setup do
87
- @user = User.create(:title => "Mr.")
88
- 3.times {
89
- post = Post.new
90
- post.user = @user
91
- post.save!
92
- }
93
- end
94
-
95
- should "support different order" do
96
- assert_nothing_raised do
97
- @user.posts(:order => :asc)
98
- end
99
-
100
- assert_nothing_raised do
101
- @user.posts(:order => :desc)
102
- end
103
- end
104
-
105
- should "reverse the order if :desc" do
106
- assert_equal @user.posts(:order => :asc).map(&:id).reverse, @user.posts(:order => :desc).map(&:id)
107
- end
108
-
109
- should "work with the limit option" do
110
- last_post = Post.create(:user => @user)
111
- assert_not_equal @user.posts(:order => :asc, :limit => 3).map(&:id).reverse, @user.posts(:order => :desc, :limit => 3).map(&:id)
112
- end
113
- end
114
-
115
- should "verify the given options for the accessor method" do
116
- user = User.create(:title => "Mr.")
117
- assert_raise(ArgumentError) do
118
- user.posts(:foo => false)
119
- end
120
- end
121
-
122
- should "verify the given options for the association defintion" do
123
- assert_raise(ArgumentError) do
124
- User.instance_eval do
125
- has_many :foo, :bar => :do
126
- end
127
- end
128
- end
129
-
130
- should "only fetch objects of the correct type" do
131
- user = User.create(:title => "Mr.")
132
- post = Post.new
133
- post.user = user
134
- post.save!
135
-
136
- comment = Comment.new
137
- comment.user = user
138
- comment.save!
139
-
140
- assert_equal 1, user.posts.size
141
- end
142
-
143
- should "getter should user cache" do
144
- user = User.create(:title => "Mr.")
145
- post = Post.new
146
- post.user = user
147
- post.save!
148
- user.posts
149
- assert_equal [post], user.instance_variable_get("@posts")[:all]
150
- end
151
-
152
- should "add methods to handle associated objects" do
153
- user = User.new(:title => "Mr.")
154
- assert user.respond_to?(:add_post)
155
- assert user.respond_to?(:remove_post)
156
- assert user.respond_to?(:remove_all_posts)
157
- end
158
-
159
- should 'ignore the cache when requesting explicit reload' do
160
- user = User.create(:title => "Mr.")
161
- assert_equal [], user.posts
162
- post = Post.new
163
- post.user = user
164
- post.save!
165
- assert_equal [post], user.posts(:force_reload => true)
166
- end
167
-
168
- should "use the correct view when handling inheritance" do
169
- problem = Problem.create
170
- big_problem = BigProblem.create
171
- issue = Issue.create(:name => 'Thing', :problem => problem)
172
- assert_equal 1, problem.issues.size
173
- issue.update_attributes(:problem_id => nil, :big_problem_id => big_problem.id)
174
- assert_equal 1, big_problem.issues.size
175
- end
176
-
177
- context "when adding items" do
178
- should "add the item to the internal cache" do
179
- daddy = User.new(:title => "Mr.")
180
- item = Post.new
181
- assert_equal [], daddy.posts
182
- daddy.add_post(item)
183
- assert_equal [item], daddy.posts
184
- assert_equal [item], daddy.instance_variable_get("@posts")[:all]
185
- end
186
-
187
- should "raise an error when the added item is not an object of the expected class" do
188
- user = User.new
189
- assert_raise(ArgumentError, 'excepted Post got String') do
190
- user.add_post('foo')
191
- end
192
- end
193
-
194
- should "save the added item" do
195
- post = Post.new
196
- user = User.create(:title => "Mr.")
197
- user.add_post(post)
198
- assert !post.new_record?
199
- end
200
-
201
- should 'set the forein key on the added object' do
202
- post = Post.new
203
- user = User.create(:title => "Mr.")
204
- user.add_post(post)
205
- assert_equal user.id, post.user_id
206
- end
207
- end
208
-
209
- context "when removing items" do
210
- should "should unset the foreign key" do
211
- user = User.create(:title => "Mr.")
212
- post = Post.create(:user => user)
213
-
214
- user.remove_post(post)
215
- assert_nil post.user_id
216
- end
217
-
218
- should "remove the item from the cache" do
219
- user = User.create(:title => "Mr.")
220
- post = Post.create(:user => user)
221
- assert user.posts.include?(post)
222
- user.remove_post(post)
223
- assert !user.posts.any?{|p| post.id == p.id}
224
- assert_equal [], user.instance_variable_get("@posts")[:all]
225
- end
226
-
227
- should "save the removed item with the nullified foreign key" do
228
- user = User.create(:title => "Mr.")
229
- post = Post.create(:user => user)
230
-
231
- user.remove_post(post)
232
- post = Post.find(post.id)
233
- assert_nil post.user_id
234
- end
235
-
236
- should 'raise an error when another object is the owner of the object to be removed' do
237
- user = User.create(:title => "Mr.")
238
- mrs = User.create(:title => "Mrs.")
239
- post = Post.create(:user => user)
240
- assert_raise(ArgumentError) do
241
- mrs.remove_post(post)
242
- end
243
- end
244
-
245
- should 'raise an error when the object is the wrong type' do
246
- user = User.new
247
- assert_raise(ArgumentError, 'excepted Post got String') do
248
- user.remove_post('foo')
249
- end
250
- end
251
-
252
- should "delete the object when dependent:destroy" do
253
- Category.instance_eval do
254
- has_many :tags, :dependent => :destroy
255
- end
256
-
257
- category = Category.create(:name => "food")
258
- tag = Tag.create(:name => "food", :category => category)
259
- assert !tag.new?
260
- category.remove_tag(tag)
261
-
262
- assert_equal [], Tag.find(:all)
263
- end
264
-
265
- should "not nullify or delete dependents if the options is set to :ignore when removing" do
266
- master = Master.create
267
- master_id = master.id
268
- servant = Servant.create(:master => master)
269
- master.remove_servant(servant)
270
- assert_equal master_id, servant.reload.master_id
271
- end
272
-
273
- should "not nullify or delete dependents if the options is set to :ignore when deleting" do
274
- master = Master.create
275
- master_id = master.id
276
- servant = Servant.create(:master => master)
277
- master.destroy
278
- assert_equal master_id, servant.reload.master_id
279
- end
280
-
281
- end
282
-
283
- context "when removing all items" do
284
- should 'nullify the foreign keys on all referenced items' do
285
- user = User.create(:title => "Mr.")
286
- post = Post.create(:user => user)
287
- post2 = Post.create(:user => user)
288
- user.remove_all_posts
289
- post = Post.find(post.id)
290
- post2 = Post.find(post2.id)
291
- assert_nil post.user_id
292
- assert_nil post2.user_id
293
- end
294
-
295
- should 'empty the cache' do
296
- user = User.create(:title => "Mr.")
297
- post = Post.create(:user => user)
298
- post2 = Post.create(:user => user)
299
- user.remove_all_posts
300
- assert_equal [], user.posts
301
- assert_equal [], user.instance_variable_get("@posts")[:all]
302
- end
303
-
304
- context "when counting" do
305
- setup do
306
- @user = User.create(:title => "Mr.")
307
- end
308
-
309
- should "define a count method" do
310
- assert @user.respond_to?(:post_count)
311
- end
312
-
313
- should "cache the result" do
314
- assert_equal 0, @user.post_count
315
- Post.create(:user => @user)
316
- assert_equal 0, @user.post_count
317
- assert_equal 0, @user.instance_variable_get("@post_count")
318
- @user.instance_variable_set("@post_count", nil)
319
- assert_equal 1, @user.post_count
320
- end
321
-
322
- should "force reload even if cached" do
323
- assert_equal 0, @user.post_count
324
- Post.create(:user => @user)
325
- assert_equal 0, @user.post_count
326
- assert_equal 1, @user.post_count(:force_reload => true)
327
- end
328
-
329
- should "count the number of belongs_to objects" do
330
- assert_equal 0, @user.post_count(:force_reload => true)
331
- Post.create(:user => @user)
332
- assert_equal 1, @user.post_count(:force_reload => true)
333
- Post.create(:user => @user)
334
- assert_equal 2, @user.post_count(:force_reload => true)
335
- end
336
-
337
- should "not count foreign objects" do
338
- assert_equal 0, @user.post_count
339
- Post.create(:user => nil)
340
- Post.create(:user => User.create(:title => 'Doc'))
341
- assert_equal 0, @user.post_count
342
- assert_equal 2, Post.count
343
- end
344
-
345
- should "not count delete objects" do
346
- hemorrhoid = Hemorrhoid.create(:user => @user)
347
- assert_equal 1, @user.hemorrhoid_count
348
- hemorrhoid.delete
349
- assert_equal 0, @user.hemorrhoid_count(:force_reload => true)
350
- assert_equal 1, @user.hemorrhoid_count(:force_reload => true, :with_deleted => true)
351
- end
352
-
353
- should "work with has_many :through" do
354
- assert_equal 0, @user.pain_count
355
- first_pain = Pain.create
356
- frist_hemorrhoid = Hemorrhoid.create(:user => @user, :pain => first_pain)
357
- assert_equal [first_pain], @user.pains
358
- assert_equal 1, @user.pain_count(:force_reload => true)
359
-
360
- second_pain = Pain.create
361
- second_hemorrhoid = Hemorrhoid.create(:user => @user, :pain => second_pain)
362
- assert_equal 2, @user.pain_count(:force_reload => true)
363
- end
364
-
365
- end
366
- end
367
-
368
- context 'when destroying the parent objects' do
369
- should "delete relations when dependent is destroy" do
370
- Category.instance_eval do
371
- has_many :tags, :dependent => :destroy
372
- end
373
-
374
- category = Category.create(:name => "food")
375
- tag = Tag.create(:name => "food", :category => category)
376
-
377
- assert_equal [tag], Tag.find(:all)
378
- category.destroy
379
- assert_equal [], Tag.find(:all)
380
- end
381
-
382
- should "nullify relations when dependent is nullify" do
383
-
384
- user = User.create(:title => "Mr.")
385
- post = Post.create(:user => user)
386
-
387
- user.destroy
388
- post = Post.find(post.id)
389
- assert_nil post.user_id
390
- end
391
-
392
- should "nullify the foreign key even if validation forbids" do
393
- user = User.create(:title => "Mr.")
394
- post = StrictPost.create(:user => user)
395
-
396
- user.destroy
397
- post = StrictPost.find(post.id)
398
- assert_nil post.user_id
399
- end
400
- end
401
- end
402
-
403
- context "with has_many :trough" do
404
- setup do
405
- @journal_1 = Journal.create
406
- @journal_2 = Journal.create
407
- @reader_1 = Reader.create
408
- @reader_2 = Reader.create
409
- end
410
-
411
- should "raise an exception if there is no :through relation" do
412
-
413
- assert_raise(ArgumentError) do
414
- class FooHasManyThroughBar
415
- include SimplyStored::Couch
416
- has_many :foos, :through => :bars
417
- end
418
- end
419
- end
420
-
421
- should "define a getter" do
422
- assert @journal_1.respond_to?(:readers)
423
- assert @reader_1.respond_to?(:journals)
424
- end
425
-
426
- should "load the objects through" do
427
- membership = Membership.new
428
- membership.journal = @journal_1
429
- membership.reader = @reader_1
430
- assert membership.save
431
-
432
- assert_equal @journal_1, membership.journal
433
- assert_equal @reader_1, membership.reader
434
- assert_equal [membership], @journal_1.reload.memberships
435
- assert_equal [membership], @journal_1.reload.memberships
436
-
437
- assert_equal [@reader_1], @journal_1.readers
438
- assert_equal [@journal_1], @reader_1.journals
439
-
440
- membership_2 = Membership.new
441
- membership_2.journal = @journal_1
442
- membership_2.reader = @reader_2
443
- assert membership_2.save
444
-
445
- assert_equal [@reader_1.id, @reader_2.id].sort, @journal_1.reload.readers.map(&:id).sort
446
- assert_equal [@journal_1.id], @reader_1.reload.journals.map(&:id).sort
447
- assert_equal [@journal_1.id], @reader_2.reload.journals.map(&:id).sort
448
-
449
- membership_3 = Membership.new
450
- membership_3.journal = @journal_2
451
- membership_3.reader = @reader_2
452
- assert membership_3.save
453
-
454
- assert_equal [@reader_1.id, @reader_2.id].sort, @journal_1.reload.readers.map(&:id).sort
455
- assert_equal [@reader_2.id].sort, @journal_2.reload.readers.map(&:id).sort
456
- assert_equal [@journal_1.id], @reader_1.reload.journals.map(&:id).sort
457
- assert_equal [@journal_1.id, @journal_2.id].sort, @reader_2.reload.journals.map(&:id).sort
458
-
459
- membership_3.destroy
460
-
461
- assert_equal [@reader_1.id, @reader_2.id].sort, @journal_1.reload.readers.map(&:id).sort
462
- assert_equal [], @journal_2.reload.readers
463
- assert_equal [@journal_1.id], @reader_1.reload.journals.map(&:id).sort
464
- assert_equal [@journal_1.id], @reader_2.reload.journals.map(&:id).sort
465
- end
466
-
467
- should "verify the given options" do
468
- assert_raise(ArgumentError) do
469
- @journal_1.readers(:foo => true)
470
- end
471
- end
472
-
473
- should "not try to destroy/nullify through-objects on parent object delete" do
474
- membership = Membership.new
475
- membership.journal = @journal_1
476
- membership.reader = @reader_1
477
- assert membership.save
478
-
479
- @reader_1.reload
480
- @journal_1.reload
481
-
482
- Reader.any_instance.expects("journal=").never
483
- Journal.any_instance.expects(:readers).never
484
-
485
- @journal_1.delete
486
- end
487
- end
488
- end
489
- end
data/test/has_one_test.rb DELETED
@@ -1,154 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/fixtures/couch')
3
-
4
- class HasOneTest < Test::Unit::TestCase
5
- context "with has_one" do
6
- setup do
7
- CouchPotato::Config.database_name = 'simply_stored_test'
8
- recreate_db
9
- end
10
-
11
- should "add a getter method" do
12
- assert Instance.new.respond_to?(:identity)
13
- end
14
-
15
- should "raise an error if another property with the same name already exists" do
16
- assert_raise(RuntimeError) do
17
- class ::DoubleHasOneUser
18
- include SimplyStored::Couch
19
- property :user
20
- has_one :user
21
- end
22
- end
23
- end
24
-
25
- should "fetch the object when invoking the getter" do
26
- instance = Instance.create
27
- identity = Identity.create(:instance => instance)
28
- assert_equal identity, instance.identity
29
- end
30
-
31
- should "set the parent object on the clients cache" do
32
- Instance.expects(:find).never
33
- instance = Instance.create
34
- identity = Identity.create(:instance => instance)
35
-
36
- assert_equal instance, instance.identity.instance
37
- end
38
-
39
- should "use the correct view when handling inheritance" do
40
- problem = Problem.create
41
- big_problem = BigProblem.create
42
- issue = Issue.create(:name => 'Thing', :problem => problem)
43
- assert_equal issue, problem.issue
44
- issue.update_attributes(:problem_id => nil, :big_problem_id => big_problem.id)
45
- assert_equal issue, big_problem.issue
46
- end
47
-
48
- should "verify the given options for the accessor method" do
49
- instance = Instance.create
50
- assert_raise(ArgumentError) do
51
- instance.identity(:foo => :var)
52
- end
53
- end
54
-
55
- should "verify the given options for the association defintion" do
56
- assert_raise(ArgumentError) do
57
- User.instance_eval do
58
- has_one :foo, :bar => :do
59
- end
60
- end
61
- end
62
-
63
- should "store the fetched object into the cache" do
64
- instance = Instance.create
65
- identity = Identity.create(:instance => instance)
66
- instance.identity
67
- assert_equal identity, instance.instance_variable_get("@identity")
68
- end
69
-
70
- should "not fetch from the database when object is in cache" do
71
- instance = Instance.create
72
- identity = Identity.create(:instance => instance)
73
- instance.identity
74
- CouchPotato.database.expects(:view).never
75
- instance.identity
76
- end
77
-
78
- should "update the foreign object to have the owner's id in the forein key" do
79
- instance = Instance.create
80
- identity = Identity.create
81
- instance.identity = identity
82
- identity.reload
83
- assert_equal instance.id, identity.instance_id
84
- end
85
-
86
- should "update the cache when setting" do
87
- instance = Instance.create
88
- identity = Identity.create
89
- instance.identity = identity
90
- CouchPotato.expects(:database).never
91
- assert_equal identity, instance.identity
92
- end
93
-
94
- should "set the foreign key value to nil when assigning nil" do
95
- instance = Instance.create
96
- identity = Identity.create(:instance => instance)
97
- instance.identity = nil
98
- identity = Identity.find(identity.id)
99
- assert_nil identity.instance_id
100
- end
101
-
102
- should 'check the class' do
103
- instance = Instance.create
104
- assert_raise(ArgumentError, 'expected Item got String') do
105
- instance.identity = 'foo'
106
- end
107
- end
108
-
109
- should 'delete the dependent objects when dependent is set to destroy' do
110
- identity = Identity.create
111
- mag = Magazine.create
112
- mag.identity = identity
113
- mag.identity = nil
114
- assert_nil Identity.find_by_id(identity.id)
115
- end
116
-
117
- should 'unset the id on the foreign object when a new object is set' do
118
- instance = Instance.create
119
- identity = Identity.create(:instance => instance)
120
- identity2 = Identity.create
121
-
122
- instance.identity = identity2
123
- identity = Identity.find(identity.id)
124
- assert_nil identity.instance_id
125
- end
126
-
127
- should 'delete the foreign object when a new object is set and dependent is set to destroy' do
128
- identity = Identity.create
129
- identity2 = Identity.create
130
- mag = Magazine.create
131
- mag.identity = identity
132
- mag.identity = identity2
133
- assert_nil Identity.find_by_id(identity.id)
134
- end
135
-
136
- should 'delete the foreign object when parent is destroyed and dependent is set to destroy' do
137
- identity = Identity.create
138
- mag = Magazine.create
139
- mag.identity = identity
140
-
141
- mag.destroy
142
- assert_nil Identity.find_by_id(identity.id)
143
- end
144
-
145
- should 'nullify the foreign objects foreign key when parent is destroyed' do
146
- identity = Identity.create
147
- instance = Instance.create
148
- instance.identity = identity
149
- instance.destroy
150
- identity = Identity.find(identity.id)
151
- assert_nil identity.instance_id
152
- end
153
- end
154
- end