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.
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +13 -5
- data/README.md +41 -0
- data/lib/simply_stored/couch/belongs_to.rb +6 -8
- data/lib/simply_stored/couch/ext/couch_potato.rb +26 -0
- data/lib/simply_stored/couch/has_many.rb +1 -1
- data/lib/simply_stored/couch.rb +2 -30
- data/lib/simply_stored/instance_methods.rb +14 -8
- data/lib/simply_stored.rb +12 -10
- metadata +175 -56
- data/test/active_model_compatibility_test.rb +0 -23
- data/test/belongs_to_test.rb +0 -173
- data/test/conflict_handling_test.rb +0 -96
- data/test/finder_test.rb +0 -188
- data/test/fixtures/couch.rb +0 -286
- data/test/has_and_belongs_to_many_test.rb +0 -639
- data/test/has_many_test.rb +0 -489
- data/test/has_one_test.rb +0 -154
- data/test/instance_lifecycle_test.rb +0 -249
- data/test/mass_assignment_protection_test.rb +0 -77
- data/test/s3_test.rb +0 -256
- data/test/soft_deletable_test.rb +0 -468
- data/test/test_helper.rb +0 -19
- data/test/validations_test.rb +0 -142
- data/test/views_test.rb +0 -33
data/test/soft_deletable_test.rb
DELETED
@@ -1,468 +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 SoftDeletableTest < Test::Unit::TestCase
|
5
|
-
context "when using soft deletable" do
|
6
|
-
setup do
|
7
|
-
CouchPotato::Config.database_name = 'simply_stored_test'
|
8
|
-
recreate_db
|
9
|
-
end
|
10
|
-
should "know when it is enabled" do
|
11
|
-
assert Hemorrhoid.soft_deleting_enabled?
|
12
|
-
assert !User.soft_deleting_enabled?
|
13
|
-
end
|
14
|
-
|
15
|
-
should "define a :deleted_at attribute" do
|
16
|
-
h = Hemorrhoid.new
|
17
|
-
assert h.respond_to?(:deleted_at)
|
18
|
-
assert h.respond_to?(:deleted_at=)
|
19
|
-
assert_equal :deleted_at, Hemorrhoid.soft_delete_attribute
|
20
|
-
end
|
21
|
-
|
22
|
-
should "define a hard delete methods" do
|
23
|
-
h = Hemorrhoid.new
|
24
|
-
assert h.respond_to?(:destroy!)
|
25
|
-
assert h.respond_to?(:delete!)
|
26
|
-
end
|
27
|
-
|
28
|
-
context "when deleting" do
|
29
|
-
setup do
|
30
|
-
@user = User.new(:name => 'BigT', :title => 'Dr.')
|
31
|
-
@user.save!
|
32
|
-
@hemorrhoid = Hemorrhoid.new
|
33
|
-
@hemorrhoid.user = @user
|
34
|
-
@hemorrhoid.save!
|
35
|
-
end
|
36
|
-
|
37
|
-
should "not delete the object but populate the soft_delete_attribute" do
|
38
|
-
now = Time.now
|
39
|
-
Time.stubs(:now).returns(now)
|
40
|
-
assert_nil @hemorrhoid.deleted_at
|
41
|
-
assert @hemorrhoid.delete
|
42
|
-
assert_equal now, @hemorrhoid.deleted_at
|
43
|
-
end
|
44
|
-
|
45
|
-
should "survive reloads with the new attribute" do
|
46
|
-
assert_nil @hemorrhoid.deleted_at
|
47
|
-
assert @hemorrhoid.delete
|
48
|
-
@hemorrhoid.reload
|
49
|
-
assert_not_nil @hemorrhoid.deleted_at
|
50
|
-
end
|
51
|
-
|
52
|
-
should "know when it is deleted" do
|
53
|
-
assert !@hemorrhoid.deleted?
|
54
|
-
@hemorrhoid.delete
|
55
|
-
assert @hemorrhoid.deleted?
|
56
|
-
end
|
57
|
-
|
58
|
-
should "not consider objects without soft-deleted as deleted" do
|
59
|
-
assert !@user.deleted?
|
60
|
-
@user.delete
|
61
|
-
assert !@user.deleted?
|
62
|
-
end
|
63
|
-
|
64
|
-
should "not delete in DB" do
|
65
|
-
CouchPotato.database.expects(:destroy_document).never
|
66
|
-
@hemorrhoid.destroy
|
67
|
-
end
|
68
|
-
|
69
|
-
should "really delete if asked to" do
|
70
|
-
CouchPotato.database.expects(:destroy_document).with(@hemorrhoid)
|
71
|
-
@hemorrhoid.destroy!
|
72
|
-
end
|
73
|
-
|
74
|
-
context "callbacks" do
|
75
|
-
|
76
|
-
should "still fire the callbacks" do
|
77
|
-
@hemorrhoid = Hemorrhoid.create
|
78
|
-
$before = nil
|
79
|
-
$after = nil
|
80
|
-
def @hemorrhoid.before_destroy_callback
|
81
|
-
$before = "now"
|
82
|
-
end
|
83
|
-
|
84
|
-
def @hemorrhoid.after_destroy_callback
|
85
|
-
$after = "now"
|
86
|
-
end
|
87
|
-
|
88
|
-
@hemorrhoid.destroy
|
89
|
-
|
90
|
-
assert_not_nil $before
|
91
|
-
assert_not_nil $after
|
92
|
-
end
|
93
|
-
|
94
|
-
should "not fire the callbacks on the real destroy if the object is already deleted" do
|
95
|
-
@hemorrhoid = Hemorrhoid.create
|
96
|
-
def @hemorrhoid.before_destroy_callback
|
97
|
-
raise "Callback called even though #{skip_callbacks.inspect}"
|
98
|
-
end
|
99
|
-
|
100
|
-
def @hemorrhoid.after_destroy_callback
|
101
|
-
raise "Callback called even though #{skip_callbacks.inspect}"
|
102
|
-
end
|
103
|
-
|
104
|
-
def @hemorrhoid.deleted?
|
105
|
-
true
|
106
|
-
end
|
107
|
-
|
108
|
-
assert_nothing_raised do
|
109
|
-
@hemorrhoid.destroy!
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
should "not fire the callbacks on the real destroy if the object is not deleted" do
|
114
|
-
@hemorrhoid = Hemorrhoid.create
|
115
|
-
$before = nil
|
116
|
-
$after = nil
|
117
|
-
def @hemorrhoid.before_destroy_callback
|
118
|
-
$before = "now"
|
119
|
-
end
|
120
|
-
|
121
|
-
def @hemorrhoid.after_destroy_callback
|
122
|
-
$after = "now"
|
123
|
-
end
|
124
|
-
|
125
|
-
@hemorrhoid.destroy!
|
126
|
-
|
127
|
-
assert_not_nil $before
|
128
|
-
assert_not_nil $after
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context "when handling the dependent objects" do
|
133
|
-
setup do
|
134
|
-
@sub = SubHemorrhoid.new
|
135
|
-
@sub.hemorrhoid = @hemorrhoid
|
136
|
-
@sub.save!
|
137
|
-
|
138
|
-
@easy_sub = EasySubHemorrhoid.new
|
139
|
-
@easy_sub.hemorrhoid = @hemorrhoid
|
140
|
-
@easy_sub.save!
|
141
|
-
|
142
|
-
@rash = Rash.new
|
143
|
-
@rash.hemorrhoid = @hemorrhoid
|
144
|
-
@rash.save!
|
145
|
-
|
146
|
-
@hemorrhoid.reload
|
147
|
-
end
|
148
|
-
|
149
|
-
should "delete them" do
|
150
|
-
@hemorrhoid.delete
|
151
|
-
@sub.reload
|
152
|
-
assert @sub.deleted?
|
153
|
-
assert_raise(SimplyStored::RecordNotFound) do
|
154
|
-
EasySubHemorrhoid.find(@easy_sub.id, :with_deleted => true)
|
155
|
-
end
|
156
|
-
@rash = Rash.find(@rash.id)
|
157
|
-
assert_nil @rash.hemorrhoid_id
|
158
|
-
end
|
159
|
-
|
160
|
-
should "really delete them if the parent is really deleted" do
|
161
|
-
@hemorrhoid.delete!
|
162
|
-
assert_raise(SimplyStored::RecordNotFound) do
|
163
|
-
EasySubHemorrhoid.find(@sub.id, :with_deleted => true)
|
164
|
-
end
|
165
|
-
|
166
|
-
assert_raise(SimplyStored::RecordNotFound) do
|
167
|
-
EasySubHemorrhoid.find(@easy_sub.id, :with_deleted => true)
|
168
|
-
end
|
169
|
-
|
170
|
-
@rash = Rash.find(@rash.id)
|
171
|
-
assert_nil @rash.hemorrhoid_id
|
172
|
-
end
|
173
|
-
|
174
|
-
should "nullify dependents if they are soft-deletable and deleted" do
|
175
|
-
small_rash = SmallRash.create(:hemorrhoid => @hemorrhoid)
|
176
|
-
@hemorrhoid.reload
|
177
|
-
@hemorrhoid.destroy
|
178
|
-
assert @hemorrhoid.deleted?
|
179
|
-
small_rash = SmallRash.find(small_rash.id)
|
180
|
-
assert_nil small_rash.hemorrhoid_id
|
181
|
-
end
|
182
|
-
|
183
|
-
should "not nullify dependents if they are soft-deletable and not deleted" do
|
184
|
-
small_rash = SmallRash.create(:hemorrhoid => @hemorrhoid)
|
185
|
-
@hemorrhoid.reload
|
186
|
-
assert !@hemorrhoid.deleted?
|
187
|
-
small_rash = SmallRash.find(small_rash.id)
|
188
|
-
assert_not_nil small_rash.hemorrhoid_id
|
189
|
-
assert_equal @hemorrhoid.id, small_rash.hemorrhoid_id
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|
194
|
-
|
195
|
-
context "when loading" do
|
196
|
-
setup do
|
197
|
-
@user = User.new(:name => 'BigT', :title => 'Dr.')
|
198
|
-
@user.save!
|
199
|
-
@hemorrhoid = Hemorrhoid.new
|
200
|
-
@hemorrhoid.user = @user
|
201
|
-
@hemorrhoid.save!
|
202
|
-
end
|
203
|
-
|
204
|
-
context "by id" do
|
205
|
-
should "not be found by default" do
|
206
|
-
@hemorrhoid.destroy
|
207
|
-
assert_raise(SimplyStored::RecordNotFound) do
|
208
|
-
Hemorrhoid.find(@hemorrhoid.id)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
should "be found if supplied with :with_deleted" do
|
213
|
-
@hemorrhoid.destroy
|
214
|
-
|
215
|
-
assert_not_nil Hemorrhoid.find(@hemorrhoid.id, :with_deleted => true)
|
216
|
-
end
|
217
|
-
|
218
|
-
should "not be found if it is really gone" do
|
219
|
-
old_id = @hemorrhoid.id
|
220
|
-
@hemorrhoid.destroy!
|
221
|
-
|
222
|
-
assert_raise(SimplyStored::RecordNotFound) do
|
223
|
-
Hemorrhoid.find(old_id)
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
should "always reload" do
|
228
|
-
@hemorrhoid.destroy
|
229
|
-
assert_nothing_raised do
|
230
|
-
@hemorrhoid.reload
|
231
|
-
end
|
232
|
-
assert_not_nil @hemorrhoid.deleted_at
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
context "all" do
|
237
|
-
setup do
|
238
|
-
recreate_db
|
239
|
-
@hemorrhoid = Hemorrhoid.create
|
240
|
-
assert @hemorrhoid.destroy
|
241
|
-
assert @hemorrhoid.reload.deleted?
|
242
|
-
end
|
243
|
-
|
244
|
-
should "not load deleted" do
|
245
|
-
assert_equal [], Hemorrhoid.find(:all)
|
246
|
-
assert_equal [], Hemorrhoid.find(:all, :with_deleted => false)
|
247
|
-
end
|
248
|
-
|
249
|
-
should "load non-deleted" do
|
250
|
-
hemorrhoid = Hemorrhoid.create
|
251
|
-
assert_not_equal [], Hemorrhoid.find(:all)
|
252
|
-
assert_not_equal [], Hemorrhoid.find(:all, :with_deleted => false)
|
253
|
-
end
|
254
|
-
|
255
|
-
should "load deleted if asked to" do
|
256
|
-
assert_equal [@hemorrhoid.id], Hemorrhoid.find(:all, :with_deleted => true).map(&:id)
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
context "first" do
|
261
|
-
setup do
|
262
|
-
recreate_db
|
263
|
-
@hemorrhoid = Hemorrhoid.create
|
264
|
-
assert @hemorrhoid.destroy
|
265
|
-
assert @hemorrhoid.reload.deleted?
|
266
|
-
end
|
267
|
-
|
268
|
-
should "not load deleted" do
|
269
|
-
assert_nil Hemorrhoid.find(:first)
|
270
|
-
assert_nil Hemorrhoid.find(:first, :with_deleted => false)
|
271
|
-
end
|
272
|
-
|
273
|
-
should "load non-deleted" do
|
274
|
-
hemorrhoid = Hemorrhoid.create
|
275
|
-
assert_not_nil Hemorrhoid.find(:first)
|
276
|
-
assert Hemorrhoid.find(:first).is_a?(Hemorrhoid), Hemorrhoid.find(:first).inspect
|
277
|
-
assert_not_nil Hemorrhoid.find(:first, :with_deleted => false)
|
278
|
-
assert Hemorrhoid.find(:first, :with_deleted => false).is_a?(Hemorrhoid), Hemorrhoid.find(:first, :with_deleted => false).inspect
|
279
|
-
end
|
280
|
-
|
281
|
-
should "load deleted if asked to" do
|
282
|
-
assert_equal @hemorrhoid, Hemorrhoid.find(:first, :with_deleted => true)
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
context "find_by and find_all_by" do
|
287
|
-
setup do
|
288
|
-
recreate_db
|
289
|
-
@hemorrhoid = Hemorrhoid.create(:nickname => 'Claas', :size => 3)
|
290
|
-
@hemorrhoid.destroy
|
291
|
-
end
|
292
|
-
|
293
|
-
context "find_by" do
|
294
|
-
should "not load deleted" do
|
295
|
-
assert_nil Hemorrhoid.find_by_nickname('Claas')
|
296
|
-
assert_nil Hemorrhoid.find_by_nickname('Claas', :with_deleted => false)
|
297
|
-
|
298
|
-
assert_nil Hemorrhoid.find_by_nickname_and_size('Claas', 3)
|
299
|
-
assert_nil Hemorrhoid.find_by_nickname_and_size('Claas', 3, :with_deleted => false)
|
300
|
-
end
|
301
|
-
|
302
|
-
should "load non-deleted" do
|
303
|
-
hemorrhoid = Hemorrhoid.create(:nickname => 'OtherNick', :size => 3)
|
304
|
-
assert_equal hemorrhoid.id, Hemorrhoid.find_by_nickname('OtherNick', :with_deleted => true).id
|
305
|
-
assert_equal hemorrhoid.id, Hemorrhoid.find_by_nickname('OtherNick').id
|
306
|
-
end
|
307
|
-
|
308
|
-
should "load deleted if asked to" do
|
309
|
-
assert_not_nil Hemorrhoid.find_by_nickname('Claas', :with_deleted => true)
|
310
|
-
assert_equal @hemorrhoid.id, Hemorrhoid.find_by_nickname('Claas', :with_deleted => true).id
|
311
|
-
|
312
|
-
assert_not_nil Hemorrhoid.find_by_nickname_and_size('Claas', 3, :with_deleted => true)
|
313
|
-
assert_equal @hemorrhoid.id, Hemorrhoid.find_by_nickname_and_size('Claas', 3, :with_deleted => true).id
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
|
-
context "find_all_by" do
|
318
|
-
should "not load deleted" do
|
319
|
-
assert_equal [], Hemorrhoid.find_all_by_nickname('Claas')
|
320
|
-
assert_equal [], Hemorrhoid.find_all_by_nickname('Claas', :with_deleted => false)
|
321
|
-
|
322
|
-
assert_equal [], Hemorrhoid.find_all_by_nickname_and_size('Claas', 3)
|
323
|
-
assert_equal [], Hemorrhoid.find_all_by_nickname_and_size('Claas', 3, :with_deleted => false)
|
324
|
-
end
|
325
|
-
|
326
|
-
should "load non-deleted" do
|
327
|
-
hemorrhoid = Hemorrhoid.create(:nickname => 'Lampe', :size => 4)
|
328
|
-
assert_equal [hemorrhoid.id], Hemorrhoid.find_all_by_nickname('Lampe').map(&:id)
|
329
|
-
end
|
330
|
-
|
331
|
-
should "load deleted if asked to" do
|
332
|
-
assert_equal [@hemorrhoid.id], Hemorrhoid.find_all_by_nickname('Claas', :with_deleted => true).map(&:id)
|
333
|
-
assert_equal [@hemorrhoid.id], Hemorrhoid.find_all_by_nickname_and_size('Claas', 3, :with_deleted => true).map(&:id)
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
should "reuse the same view - when find_all_by is called first" do
|
338
|
-
assert_equal [], Hemorrhoid.find_all_by_nickname('Claas')
|
339
|
-
assert_nil Hemorrhoid.find_by_nickname('Claas')
|
340
|
-
end
|
341
|
-
|
342
|
-
should "reuse the same view - when find_by is called first" do
|
343
|
-
assert_nil Hemorrhoid.find_by_nickname('Claas')
|
344
|
-
assert_equal [], Hemorrhoid.find_all_by_nickname('Claas')
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
context "by relation" do
|
349
|
-
setup do
|
350
|
-
@hemorrhoid.destroy
|
351
|
-
end
|
352
|
-
|
353
|
-
context "has_many" do
|
354
|
-
should "not load deleted by default" do
|
355
|
-
assert_equal [], @user.hemorrhoids
|
356
|
-
end
|
357
|
-
|
358
|
-
should "load deleted if asked to" do
|
359
|
-
assert_equal [@hemorrhoid.id], @user.hemorrhoids(:with_deleted => true).map(&:id)
|
360
|
-
end
|
361
|
-
end
|
362
|
-
|
363
|
-
context "has_many :through" do
|
364
|
-
setup do
|
365
|
-
@user = User.create(:name => 'BigT', :title => 'Dr.')
|
366
|
-
@pain = Pain.create
|
367
|
-
|
368
|
-
@hemorrhoid = Hemorrhoid.new
|
369
|
-
@hemorrhoid.user = @user
|
370
|
-
@hemorrhoid.pain = @pain
|
371
|
-
@hemorrhoid.save!
|
372
|
-
|
373
|
-
@hemorrhoid.destroy
|
374
|
-
end
|
375
|
-
|
376
|
-
should "not load deleted by default" do
|
377
|
-
assert_equal [], @user.pains
|
378
|
-
end
|
379
|
-
|
380
|
-
should "load deleted if asked to" do
|
381
|
-
assert_equal [@pain.id], @user.pains(:with_deleted => true).map(&:id)
|
382
|
-
end
|
383
|
-
end
|
384
|
-
|
385
|
-
context "has_one" do
|
386
|
-
setup do
|
387
|
-
@spot = Spot.create
|
388
|
-
|
389
|
-
@hemorrhoid = Hemorrhoid.new
|
390
|
-
@hemorrhoid.spot = @spot
|
391
|
-
@hemorrhoid.save!
|
392
|
-
|
393
|
-
@hemorrhoid.destroy
|
394
|
-
end
|
395
|
-
|
396
|
-
should "not load deleted by default" do
|
397
|
-
assert_nil @spot.hemorrhoid
|
398
|
-
end
|
399
|
-
|
400
|
-
should "load deleted if asked to" do
|
401
|
-
assert_equal @hemorrhoid.id, @spot.hemorrhoid(:with_deleted => true).id
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
context "belongs_to" do
|
406
|
-
setup do
|
407
|
-
@hemorrhoid = Hemorrhoid.new
|
408
|
-
@hemorrhoid.save!
|
409
|
-
|
410
|
-
@sub = SubHemorrhoid.new
|
411
|
-
@sub.hemorrhoid = @hemorrhoid
|
412
|
-
@sub.save!
|
413
|
-
|
414
|
-
@hemorrhoid.destroy
|
415
|
-
end
|
416
|
-
|
417
|
-
should "not load deleted by default" do
|
418
|
-
@sub.reload
|
419
|
-
assert_raise(SimplyStored::RecordNotFound) do
|
420
|
-
assert_nil @sub.hemorrhoid
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
should "load deleted if asked to" do
|
425
|
-
@sub.reload
|
426
|
-
assert_equal @hemorrhoid.id, @sub.hemorrhoid(:with_deleted => true).id
|
427
|
-
end
|
428
|
-
end
|
429
|
-
|
430
|
-
end
|
431
|
-
|
432
|
-
end
|
433
|
-
|
434
|
-
context "when counting" do
|
435
|
-
setup do
|
436
|
-
@hemorrhoid = Hemorrhoid.create(:nickname => 'Claas')
|
437
|
-
assert @hemorrhoid.destroy
|
438
|
-
assert @hemorrhoid.reload.deleted?
|
439
|
-
end
|
440
|
-
|
441
|
-
should "not count deleted" do
|
442
|
-
assert_equal 0, Hemorrhoid.count
|
443
|
-
assert_equal 0, Hemorrhoid.count(:with_deleted => false)
|
444
|
-
end
|
445
|
-
|
446
|
-
should "count non-deleted" do
|
447
|
-
hemorrhoid = Hemorrhoid.create(:nickname => 'Claas')
|
448
|
-
assert_equal 1, Hemorrhoid.count
|
449
|
-
assert_equal 1, Hemorrhoid.count(:with_deleted => false)
|
450
|
-
end
|
451
|
-
|
452
|
-
should "count deleted if asked to" do
|
453
|
-
assert_equal 1, Hemorrhoid.count(:with_deleted => true)
|
454
|
-
end
|
455
|
-
|
456
|
-
context "count_by" do
|
457
|
-
should "not count deleted" do
|
458
|
-
assert_equal 0, Hemorrhoid.count_by_nickname('Claas')
|
459
|
-
assert_equal 0, Hemorrhoid.count_by_nickname('Claas', :with_deleted => false)
|
460
|
-
end
|
461
|
-
|
462
|
-
should "count deleted if asked to" do
|
463
|
-
assert_equal 1, Hemorrhoid.count_by_nickname('Claas', :with_deleted => true)
|
464
|
-
end
|
465
|
-
end
|
466
|
-
end
|
467
|
-
end
|
468
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'bundler/setup'
|
4
|
-
require 'active_support/testing/assertions'
|
5
|
-
require 'shoulda'
|
6
|
-
require 'mocha'
|
7
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
|
8
|
-
puts File.expand_path(File.dirname(__FILE__) + "/lib")
|
9
|
-
require 'simply_stored'
|
10
|
-
|
11
|
-
class Test::Unit::TestCase
|
12
|
-
include ActiveSupport::Testing::Assertions
|
13
|
-
|
14
|
-
def recreate_db
|
15
|
-
CouchPotato.couchrest_database.delete! rescue nil
|
16
|
-
CouchPotato.couchrest_database.server.create_db CouchPotato::Config.database_name
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
data/test/validations_test.rb
DELETED
@@ -1,142 +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 ValidationsTest < Test::Unit::TestCase
|
5
|
-
context "with additional validations" do
|
6
|
-
setup do
|
7
|
-
CouchPotato::Config.database_name = 'simply_stored_test'
|
8
|
-
recreate_db
|
9
|
-
end
|
10
|
-
|
11
|
-
context "with validates_inclusion_of" do
|
12
|
-
should "validate inclusion of an attribute in an array" do
|
13
|
-
category = Category.new(:name => "other")
|
14
|
-
assert !category.save
|
15
|
-
end
|
16
|
-
|
17
|
-
should "validate when the attribute is an array" do
|
18
|
-
category = Category.new(:name => ['drinks', 'food'])
|
19
|
-
assert_nothing_raised do
|
20
|
-
category.save!
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
should "add an error message" do
|
25
|
-
category = Category.new(:name => "other")
|
26
|
-
category.valid?
|
27
|
-
assert_match(/must be one or more of food, drinks, party/, category.errors.full_messages.first)
|
28
|
-
end
|
29
|
-
|
30
|
-
should "allow blank" do
|
31
|
-
category = Category.new(:name => nil)
|
32
|
-
assert category.valid?
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context "with validates_format_of" do
|
37
|
-
class ValidatedUser
|
38
|
-
include SimplyStored::Couch
|
39
|
-
property :name
|
40
|
-
validates_format_of :name, :with => /Paul/
|
41
|
-
end
|
42
|
-
|
43
|
-
should 'validate the format and fail when not matched' do
|
44
|
-
user = ValidatedUser.new(:name => "John")
|
45
|
-
assert !user.valid?
|
46
|
-
end
|
47
|
-
|
48
|
-
should 'succeed when matched' do
|
49
|
-
user = ValidatedUser.new(:name => "Paul")
|
50
|
-
assert user.valid?
|
51
|
-
end
|
52
|
-
|
53
|
-
should 'fail when empty' do
|
54
|
-
user = ValidatedUser.new(:name => nil)
|
55
|
-
assert !user.valid?
|
56
|
-
end
|
57
|
-
|
58
|
-
context "with allow_blank" do
|
59
|
-
class ValidatedBlankUser
|
60
|
-
include SimplyStored::Couch
|
61
|
-
property :name
|
62
|
-
validates_format_of :name, :with => /Paul/, :allow_blank => true
|
63
|
-
end
|
64
|
-
|
65
|
-
should 'not fail when nil' do
|
66
|
-
user = ValidatedBlankUser.new(:name => nil)
|
67
|
-
assert user.valid?
|
68
|
-
end
|
69
|
-
|
70
|
-
should 'not fail when empty string' do
|
71
|
-
user = ValidatedBlankUser.new(:name => '')
|
72
|
-
assert user.valid?
|
73
|
-
end
|
74
|
-
|
75
|
-
should 'fail when not matching' do
|
76
|
-
user = ValidatedBlankUser.new(:name => 'John')
|
77
|
-
assert !user.valid?
|
78
|
-
end
|
79
|
-
|
80
|
-
should 'not fail when matching' do
|
81
|
-
user = ValidatedBlankUser.new(:name => 'Paul')
|
82
|
-
assert user.valid?
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context "with validates_uniqueness_of" do
|
89
|
-
should "add a view on the unique attribute" do
|
90
|
-
assert UniqueUser.by_name
|
91
|
-
end
|
92
|
-
|
93
|
-
should "set an error when a different with the same instance exists" do
|
94
|
-
assert UniqueUser.create(:name => "Host Master")
|
95
|
-
user = UniqueUser.create(:name => "Host Master")
|
96
|
-
assert !user.valid?
|
97
|
-
end
|
98
|
-
|
99
|
-
should "not have an error when we're the only one around" do
|
100
|
-
user = UniqueUser.create(:name => "Host Master")
|
101
|
-
assert !user.new_record?
|
102
|
-
end
|
103
|
-
|
104
|
-
should "not have an error when it's the same instance" do
|
105
|
-
user = UniqueUser.create(:name => "Host Master")
|
106
|
-
user = UniqueUser.find(user.id)
|
107
|
-
assert user.valid?
|
108
|
-
end
|
109
|
-
|
110
|
-
should 'have a nice error message' do
|
111
|
-
assert UniqueUser.create(:name => "Host Master")
|
112
|
-
user = UniqueUser.create(:name => "Host Master")
|
113
|
-
assert_equal "Name is already taken", user.errors.on(:name)
|
114
|
-
end
|
115
|
-
|
116
|
-
should 'create a view to check with' do
|
117
|
-
assert UniqueUser.respond_to?(:by_name)
|
118
|
-
assert_equal :name, UniqueUser.by_name.send(:options)[:key]
|
119
|
-
end
|
120
|
-
|
121
|
-
should 'not overwrite the view when a custom one already exists' do
|
122
|
-
assert_equal :email, UniqueUserWithAView.by_name.send(:options)[:key]
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
context "equality" do
|
127
|
-
should "know when two objects are equal" do
|
128
|
-
user = UniqueUser.create(:name => "Host Master")
|
129
|
-
other_user = UniqueUser.create(:name => "The other one")
|
130
|
-
assert user == user
|
131
|
-
assert user != other_user
|
132
|
-
end
|
133
|
-
|
134
|
-
should "not bail when comparing with non-SimplyStored objects" do
|
135
|
-
user = UniqueUser.create(:name => "Host Master")
|
136
|
-
assert 5 != user
|
137
|
-
assert user != 5
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
end
|
data/test/views_test.rb
DELETED
@@ -1,33 +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 CustomViewUser
|
5
|
-
include SimplyStored::Couch
|
6
|
-
|
7
|
-
property :tags
|
8
|
-
view :by_tags, :type => SimplyStored::Couch::Views::ArrayPropertyViewSpec, :key => :tags
|
9
|
-
end
|
10
|
-
|
11
|
-
class ViewsTest < Test::Unit::TestCase
|
12
|
-
|
13
|
-
context "Custom couch views" do
|
14
|
-
setup do
|
15
|
-
CouchPotato::Config.database_name = 'simply_stored_test'
|
16
|
-
recreate_db
|
17
|
-
end
|
18
|
-
|
19
|
-
context "With array views" do
|
20
|
-
should "find objects with one match of the array" do
|
21
|
-
CustomViewUser.create(:tags => ["agile", "cool", "extreme"])
|
22
|
-
CustomViewUser.create(:tags => ["agile"])
|
23
|
-
assert_equal 2, CustomViewUser.find_all_by_tags("agile").size
|
24
|
-
end
|
25
|
-
|
26
|
-
should "find the object when the property is not an array" do
|
27
|
-
CustomViewUser.create(:tags => "agile")
|
28
|
-
assert_equal 1, CustomViewUser.find_all_by_tags("agile").size
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|