/
test_modifiers.rb
483 lines (372 loc) · 14.3 KB
/
test_modifiers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# require 'test_helper'
require File.expand_path("../../test_helper", __FILE__)
class ModifierTest < Test::Unit::TestCase
def setup
@page_class = Doc do
key :title, String
key :day_count, Integer, :default => 0
key :week_count, Integer, :default => 0
key :month_count, Integer, :default => 0
key :tags, Array
end
end
def assert_page_counts(page, day_count, week_count, month_count)
page.reload
page.day_count.should == day_count
page.week_count.should == week_count
page.month_count.should == month_count
end
def assert_keys_removed(page, *keys)
keys.each do |key|
doc = @page_class.collection.find_one({:_id => page.id})
doc.keys.should_not include(key)
end
end
context "ClassMethods" do
context "unset" do
setup do
@page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
@page2 = @page_class.create(:title => 'Home')
end
should "work with criteria and keys" do
@page_class.unset({:title => 'Home'}, :title, :tags)
assert_keys_removed @page, :title, :tags
assert_keys_removed @page2, :title, :tags
end
should "work with ids and keys" do
@page_class.unset(@page.id, @page2.id, :title, :tags)
assert_keys_removed @page, :title, :tags
assert_keys_removed @page2, :title, :tags
end
end
context "increment" do
setup do
@page = @page_class.create(:title => 'Home')
@page2 = @page_class.create(:title => 'Home')
end
should "work with criteria and modifier hashes" do
@page_class.increment({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
assert_page_counts @page, 1, 2, 3
assert_page_counts @page2, 1, 2, 3
end
should "work with ids and modifier hash" do
@page_class.increment(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
assert_page_counts @page, 1, 2, 3
assert_page_counts @page2, 1, 2, 3
end
end
context "decrement" do
setup do
@page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
@page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
end
should "work with criteria and modifier hashes" do
@page_class.decrement({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
assert_page_counts @page, 0, 0, 0
assert_page_counts @page2, 0, 0, 0
end
should "work with ids and modifier hash" do
@page_class.decrement(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
assert_page_counts @page, 0, 0, 0
assert_page_counts @page2, 0, 0, 0
end
should "decrement with positive or negative numbers" do
@page_class.decrement(@page.id, @page2.id, :day_count => -1, :week_count => 2, :month_count => -3)
assert_page_counts @page, 0, 0, 0
assert_page_counts @page2, 0, 0, 0
end
end
context "set" do
setup do
@page = @page_class.create(:title => 'Home')
@page2 = @page_class.create(:title => 'Home')
end
should "work with criteria and modifier hashes" do
@page_class.set({:title => 'Home'}, :title => 'Home Revised')
@page.reload
@page.title.should == 'Home Revised'
@page2.reload
@page2.title.should == 'Home Revised'
end
should "work with ids and modifier hash" do
@page_class.set(@page.id, @page2.id, :title => 'Home Revised')
@page.reload
@page.title.should == 'Home Revised'
@page2.reload
@page2.title.should == 'Home Revised'
end
should "typecast values before querying" do
@page_class.key :tags, Set
assert_nothing_raised do
@page_class.set(@page.id, :tags => ['foo', 'bar'].to_set)
@page.reload
@page.tags.should == Set.new(['foo', 'bar'])
end
end
should "not typecast keys that are not defined in document" do
assert_raises(BSON::InvalidDocument) do
@page_class.set(@page.id, :colors => ['red', 'green'].to_set)
end
end
should "set keys that are not defined in document" do
@page_class.set(@page.id, :colors => %w[red green])
@page.reload
@page[:colors].should == %w[red green]
end
end
context "push" do
setup do
@page = @page_class.create(:title => 'Home')
@page2 = @page_class.create(:title => 'Home')
end
should "work with criteria and modifier hashes" do
@page_class.push({:title => 'Home'}, :tags => 'foo')
@page.reload
@page.tags.should == %w(foo)
@page2.reload
@page2.tags.should == %w(foo)
end
should "work with ids and modifier hash" do
@page_class.push(@page.id, @page2.id, :tags => 'foo')
@page.reload
@page.tags.should == %w(foo)
@page2.reload
@page2.tags.should == %w(foo)
end
end
context "push_all" do
setup do
@page = @page_class.create(:title => 'Home')
@page2 = @page_class.create(:title => 'Home')
@tags = %w(foo bar)
end
should "work with criteria and modifier hashes" do
@page_class.push_all({:title => 'Home'}, :tags => @tags)
@page.reload
@page.tags.should == @tags
@page2.reload
@page2.tags.should == @tags
end
should "work with ids and modifier hash" do
@page_class.push_all(@page.id, @page2.id, :tags => @tags)
@page.reload
@page.tags.should == @tags
@page2.reload
@page2.tags.should == @tags
end
end
context "pull" do
setup do
@page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
@page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
end
should "work with criteria and modifier hashes" do
@page_class.pull({:title => 'Home'}, :tags => 'foo')
@page.reload
@page.tags.should == %w(bar)
@page2.reload
@page2.tags.should == %w(bar)
end
should "be able to pull with ids and modifier hash" do
@page_class.pull(@page.id, @page2.id, :tags => 'foo')
@page.reload
@page.tags.should == %w(bar)
@page2.reload
@page2.tags.should == %w(bar)
end
end
context "pull_all" do
setup do
@page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
@page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
end
should "work with criteria and modifier hashes" do
@page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
@page.reload
@page.tags.should == %w(baz)
@page2.reload
@page2.tags.should == %w(baz)
end
should "work with ids and modifier hash" do
@page_class.pull_all(@page.id, @page2.id, :tags => %w(foo bar))
@page.reload
@page.tags.should == %w(baz)
@page2.reload
@page2.tags.should == %w(baz)
end
end
context "add_to_set" do
setup do
@page = @page_class.create(:title => 'Home', :tags => 'foo')
@page2 = @page_class.create(:title => 'Home')
end
should "be able to add to set with criteria and modifier hash" do
@page_class.add_to_set({:title => 'Home'}, :tags => 'foo')
@page.reload
@page.tags.should == %w(foo)
@page2.reload
@page2.tags.should == %w(foo)
end
should "be able to add to set with ids and modifier hash" do
@page_class.add_to_set(@page.id, @page2.id, :tags => 'foo')
@page.reload
@page.tags.should == %w(foo)
@page2.reload
@page2.tags.should == %w(foo)
end
end
context "push_uniq" do
setup do
@page = @page_class.create(:title => 'Home', :tags => 'foo')
@page2 = @page_class.create(:title => 'Home')
end
should "be able to push uniq with criteria and modifier hash" do
@page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
@page.reload
@page.tags.should == %w(foo)
@page2.reload
@page2.tags.should == %w(foo)
end
should "be able to push uniq with ids and modifier hash" do
@page_class.push_uniq(@page.id, @page2.id, :tags => 'foo')
@page.reload
@page.tags.should == %w(foo)
@page2.reload
@page2.tags.should == %w(foo)
end
end
context "pop" do
setup do
@page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
end
should "be able to remove the last element the array" do
@page_class.pop(@page.id, :tags => 1)
@page.reload
@page.tags.should == %w(foo)
end
should "be able to remove the first element of the array" do
@page_class.pop(@page.id, :tags => -1)
@page.reload
@page.tags.should == %w(bar)
end
end
context "additional options (upsert & safe)" do
should "be able to pass upsert option" do
new_key_value = DateTime.now.to_s
@page_class.increment({:title => new_key_value}, {:day_count => 1}, {:upsert => true})
@page_class.count(:title => new_key_value).should == 1
# @page_class.first(:title => new_key_value).day_count.should == 1
end
should "be able to pass safe option" do
@page_class.create(:title => "Better Be Safe than Sorry")
# We are trying to increment a key of type string here which should fail
assert_raises(Mongo::OperationFailure) do
@page_class.increment({:title => "Better Be Safe than Sorry"}, {:title => 1}, {:safe => true})
end
end
should "be able to pass both safe and upsert options" do
new_key_value = DateTime.now.to_s
@page_class.increment({:title => new_key_value}, {:day_count => 1}, {:upsert => true, :safe => true})
@page_class.count(:title => new_key_value).should == 1
@page_class.first(:title => new_key_value).day_count.should == 1
end
end
end
context "instance methods" do
should "be able to unset with keys" do
page = @page_class.create(:title => 'Foo', :tags => %w(foo))
page.unset(:title, :tags)
assert_keys_removed page, :title, :tags
end
should "be able to increment with modifier hashes" do
page = @page_class.create
page.increment(:day_count => 1, :week_count => 2, :month_count => 3)
assert_page_counts page, 1, 2, 3
end
should "be able to decrement with modifier hashes" do
page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
page.decrement(:day_count => 1, :week_count => 2, :month_count => 3)
assert_page_counts page, 0, 0, 0
end
should "always decrement when decrement is called whether number is positive or negative" do
page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
page.decrement(:day_count => -1, :week_count => 2, :month_count => -3)
assert_page_counts page, 0, 0, 0
end
should "be able to set with modifier hashes" do
page = @page_class.create(:title => 'Home')
page.set(:title => 'Home Revised')
page.reload
page.title.should == 'Home Revised'
end
should "be able to push with modifier hashes" do
page = @page_class.create
page.push(:tags => 'foo')
page.reload
page.tags.should == %w(foo)
end
should "be able to push_all with modifier hashes" do
page = @page_class.create
page.push_all(:tags => %w(foo bar))
page.reload
page.tags.should == %w(foo bar)
end
should "be able to pull with criteria and modifier hashes" do
page = @page_class.create(:tags => %w(foo bar))
page.pull(:tags => 'foo')
page.reload
page.tags.should == %w(bar)
end
should "be able to pull_all with criteria and modifier hashes" do
page = @page_class.create(:tags => %w(foo bar baz))
page.pull_all(:tags => %w(foo bar))
page.reload
page.tags.should == %w(baz)
end
should "be able to add_to_set with criteria and modifier hash" do
page = @page_class.create(:tags => 'foo')
page2 = @page_class.create
page.add_to_set(:tags => 'foo')
page2.add_to_set(:tags => 'foo')
page.reload
page.tags.should == %w(foo)
page2.reload
page2.tags.should == %w(foo)
end
should "be able to push uniq with criteria and modifier hash" do
page = @page_class.create(:tags => 'foo')
page2 = @page_class.create
page.push_uniq(:tags => 'foo')
page2.push_uniq(:tags => 'foo')
page.reload
page.tags.should == %w(foo)
page2.reload
page2.tags.should == %w(foo)
end
should "be able to pop with modifier hashes" do
page = @page_class.create(:tags => %w(foo bar))
page.pop(:tags => 1)
page.reload
page.tags.should == %w(foo)
end
should "be able to pass upsert option" do
page = @page_class.create(:title => "Upsert Page")
page.increment({:new_count => 1}, {:upsert => true})
page.reload
page.new_count.should == 1
end
should "be able to pass safe option" do
page = @page_class.create(:title => "Safe Page")
# We are trying to increment a key of type string here which should fail
assert_raises(Mongo::OperationFailure) do
page.increment({:title => 1}, {:safe => true})
end
end
should "be able to pass upsert and safe options" do
page = @page_class.create(:title => "Upsert and Safe Page")
page.increment({:another_count => 1}, {:upsert => true, :safe => true})
page.reload
page.another_count.should == 1
end
end
end