jchris-couchrest 0.12.6 → 0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. data/README.md +33 -8
  2. data/Rakefile +1 -1
  3. data/examples/model/example.rb +19 -13
  4. data/lib/couchrest.rb +27 -2
  5. data/lib/couchrest/core/database.rb +113 -41
  6. data/lib/couchrest/core/document.rb +48 -27
  7. data/lib/couchrest/core/response.rb +15 -0
  8. data/lib/couchrest/core/server.rb +47 -10
  9. data/lib/couchrest/mixins.rb +4 -0
  10. data/lib/couchrest/mixins/attachments.rb +31 -0
  11. data/lib/couchrest/mixins/callbacks.rb +442 -0
  12. data/lib/couchrest/mixins/design_doc.rb +63 -0
  13. data/lib/couchrest/mixins/document_queries.rb +48 -0
  14. data/lib/couchrest/mixins/extended_attachments.rb +68 -0
  15. data/lib/couchrest/mixins/extended_document_mixins.rb +6 -0
  16. data/lib/couchrest/mixins/properties.rb +120 -0
  17. data/lib/couchrest/mixins/validation.rb +234 -0
  18. data/lib/couchrest/mixins/views.rb +168 -0
  19. data/lib/couchrest/monkeypatches.rb +75 -0
  20. data/lib/couchrest/more/casted_model.rb +28 -0
  21. data/lib/couchrest/more/extended_document.rb +215 -0
  22. data/lib/couchrest/more/property.rb +40 -0
  23. data/lib/couchrest/support/blank.rb +42 -0
  24. data/lib/couchrest/support/class.rb +175 -0
  25. data/lib/couchrest/validation/auto_validate.rb +163 -0
  26. data/lib/couchrest/validation/contextual_validators.rb +78 -0
  27. data/lib/couchrest/validation/validation_errors.rb +118 -0
  28. data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
  29. data/lib/couchrest/validation/validators/confirmation_validator.rb +99 -0
  30. data/lib/couchrest/validation/validators/format_validator.rb +117 -0
  31. data/lib/couchrest/validation/validators/formats/email.rb +66 -0
  32. data/lib/couchrest/validation/validators/formats/url.rb +43 -0
  33. data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
  34. data/lib/couchrest/validation/validators/length_validator.rb +134 -0
  35. data/lib/couchrest/validation/validators/method_validator.rb +89 -0
  36. data/lib/couchrest/validation/validators/numeric_validator.rb +104 -0
  37. data/lib/couchrest/validation/validators/required_field_validator.rb +109 -0
  38. data/spec/couchrest/core/database_spec.rb +183 -67
  39. data/spec/couchrest/core/design_spec.rb +1 -1
  40. data/spec/couchrest/core/document_spec.rb +271 -173
  41. data/spec/couchrest/core/server_spec.rb +35 -0
  42. data/spec/couchrest/helpers/pager_spec.rb +1 -1
  43. data/spec/couchrest/more/casted_model_spec.rb +97 -0
  44. data/spec/couchrest/more/extended_doc_attachment_spec.rb +129 -0
  45. data/spec/couchrest/more/extended_doc_spec.rb +509 -0
  46. data/spec/couchrest/more/extended_doc_view_spec.rb +204 -0
  47. data/spec/couchrest/more/property_spec.rb +129 -0
  48. data/spec/fixtures/more/article.rb +34 -0
  49. data/spec/fixtures/more/card.rb +20 -0
  50. data/spec/fixtures/more/course.rb +14 -0
  51. data/spec/fixtures/more/event.rb +6 -0
  52. data/spec/fixtures/more/invoice.rb +17 -0
  53. data/spec/fixtures/more/person.rb +8 -0
  54. data/spec/fixtures/more/question.rb +6 -0
  55. data/spec/fixtures/more/service.rb +12 -0
  56. data/spec/spec_helper.rb +13 -7
  57. metadata +76 -3
  58. data/lib/couchrest/core/model.rb +0 -613
  59. data/spec/couchrest/core/model_spec.rb +0 -855
@@ -0,0 +1,109 @@
1
+ # Extracted from dm-validations 0.9.10
2
+ #
3
+ # Copyright (c) 2007 Guy van den Berg
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module CouchRest
25
+ module Validation
26
+
27
+ ##
28
+ #
29
+ # @author Guy van den Berg
30
+ # @since 0.9
31
+ class RequiredFieldValidator < GenericValidator
32
+
33
+ def initialize(field_name, options={})
34
+ super
35
+ @field_name, @options = field_name, options
36
+ end
37
+
38
+ def call(target)
39
+ value = target.validation_property_value(field_name)
40
+ property = target.validation_property(field_name)
41
+ return true if present?(value, property)
42
+
43
+ error_message = @options[:message] || default_error(property)
44
+ add_error(target, error_message, field_name)
45
+
46
+ false
47
+ end
48
+
49
+ protected
50
+
51
+ # Boolean property types are considered present if non-nil.
52
+ # Other property types are considered present if non-blank.
53
+ # Non-properties are considered present if non-blank.
54
+ def present?(value, property)
55
+ boolean_type?(property) ? !value.nil? : !value.blank?
56
+ end
57
+
58
+ def default_error(property)
59
+ actual = boolean_type?(property) ? :nil : :blank
60
+ ValidationErrors.default_error_message(actual, field_name)
61
+ end
62
+
63
+ # Is +property+ a boolean property?
64
+ #
65
+ # Returns true for Boolean, ParanoidBoolean, TrueClass, etc. properties.
66
+ # Returns false for other property types.
67
+ # Returns false for non-properties.
68
+ def boolean_type?(property)
69
+ property ? property.type == TrueClass : false
70
+ end
71
+
72
+ end # class RequiredFieldValidator
73
+
74
+ module ValidatesPresent
75
+
76
+ ##
77
+ # Validates that the specified attribute is present.
78
+ #
79
+ # For most property types "being present" is the same as being "not
80
+ # blank" as determined by the attribute's #blank? method. However, in
81
+ # the case of Boolean, "being present" means not nil; i.e. true or
82
+ # false.
83
+ #
84
+ # @note
85
+ # dm-core's support lib adds the blank? method to many classes,
86
+ # @see lib/dm-core/support/blank.rb (dm-core) for more information.
87
+ #
88
+ # @example [Usage]
89
+ #
90
+ # class Page
91
+ #
92
+ # property :required_attribute, String
93
+ # property :another_required, String
94
+ # property :yet_again, String
95
+ #
96
+ # validates_present :required_attribute
97
+ # validates_present :another_required, :yet_again
98
+ #
99
+ # # a call to valid? will return false unless
100
+ # # all three attributes are !blank?
101
+ # end
102
+ def validates_present(*fields)
103
+ opts = opts_from_validator_args(fields)
104
+ add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator)
105
+ end
106
+
107
+ end # module ValidatesPresent
108
+ end # module Validation
109
+ end # module CouchRest
@@ -61,7 +61,7 @@ describe CouchRest::Database do
61
61
  emit(doc.word,null);
62
62
  }
63
63
  }'}}
64
- @db.save({
64
+ @db.save_doc({
65
65
  "_id" => "_design/test",
66
66
  :views => @view
67
67
  })
@@ -80,7 +80,7 @@ describe CouchRest::Database do
80
80
 
81
81
  describe "select from an existing view" do
82
82
  before(:each) do
83
- r = @db.save({
83
+ r = @db.save_doc({
84
84
  "_id" => "_design/first",
85
85
  :views => {
86
86
  :test => {
@@ -129,9 +129,9 @@ describe CouchRest::Database do
129
129
 
130
130
  describe "GET (document by id) when the doc exists" do
131
131
  before(:each) do
132
- @r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
132
+ @r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
133
133
  @docid = "http://example.com/stuff.cgi?things=and%20stuff"
134
- @db.save({'_id' => @docid, 'will-exist' => 'here'})
134
+ @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
135
135
  end
136
136
  it "should get the document" do
137
137
  doc = @db.get(@r['id'])
@@ -176,7 +176,7 @@ describe CouchRest::Database do
176
176
  end
177
177
 
178
178
  it "in the case of an id conflict should not insert anything" do
179
- @r = @db.save({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"})
179
+ @r = @db.save_doc({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"})
180
180
 
181
181
  lambda do
182
182
  rs = @db.bulk_save([
@@ -192,7 +192,7 @@ describe CouchRest::Database do
192
192
  end
193
193
 
194
194
  it "should empty the bulk save cache if no documents are given" do
195
- @db.save({"_id" => "bulk_cache_1", "val" => "test"}, true)
195
+ @db.save_doc({"_id" => "bulk_cache_1", "val" => "test"}, true)
196
196
  lambda do
197
197
  @db.get('bulk_cache_1')
198
198
  end.should raise_error(RestClient::ResourceNotFound)
@@ -201,7 +201,7 @@ describe CouchRest::Database do
201
201
  end
202
202
 
203
203
  it "should raise an error that is useful for recovery" do
204
- @r = @db.save({"_id" => "taken", "field" => "stuff"})
204
+ @r = @db.save_doc({"_id" => "taken", "field" => "stuff"})
205
205
  begin
206
206
  rs = @db.bulk_save([
207
207
  {"_id" => "taken", "wild" => "and random"},
@@ -220,17 +220,43 @@ describe CouchRest::Database do
220
220
  @db.documents["total_rows"].should == 0
221
221
  end
222
222
  it "should create the document and return the id" do
223
- r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
223
+ r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
224
224
  r2 = @db.get(r['id'])
225
225
  r2["lemons"].should == "from texas"
226
226
  end
227
227
  it "should use PUT with UUIDs" do
228
228
  CouchRest.should_receive(:put).and_return({"ok" => true, "id" => "100", "rev" => "55"})
229
- r = @db.save({'just' => ['another document']})
229
+ r = @db.save_doc({'just' => ['another document']})
230
230
  end
231
231
 
232
232
  end
233
-
233
+
234
+ describe "fetch_attachment" do
235
+ before do
236
+ @attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
237
+ @doc = {
238
+ "_id" => "mydocwithattachment",
239
+ "field" => ["some value"],
240
+ "_attachments" => {
241
+ "test.html" => {
242
+ "type" => "text/html",
243
+ "data" => @attach
244
+ }
245
+ }
246
+ }
247
+ @db.save_doc(@doc)
248
+ end
249
+
250
+ # Depreacated
251
+ # it "should get the attachment with the doc's _id" do
252
+ # @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
253
+ # end
254
+
255
+ it "should get the attachment with the doc itself" do
256
+ @db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach
257
+ end
258
+ end
259
+
234
260
  describe "PUT attachment from file" do
235
261
  before(:each) do
236
262
  filename = FIXTURE_PATH + '/attachments/couchdb.png'
@@ -242,7 +268,8 @@ describe CouchRest::Database do
242
268
  it "should save the attachment to a new doc" do
243
269
  r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
244
270
  r['ok'].should == true
245
- attachment = @db.fetch_attachment("attach-this","couchdb.png")
271
+ doc = @db.get("attach-this")
272
+ attachment = @db.fetch_attachment(doc,"couchdb.png")
246
273
  attachment.should == image
247
274
  end
248
275
  end
@@ -250,7 +277,7 @@ describe CouchRest::Database do
250
277
  describe "PUT document with attachment" do
251
278
  before(:each) do
252
279
  @attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
253
- @doc = {
280
+ doc = {
254
281
  "_id" => "mydocwithattachment",
255
282
  "field" => ["some value"],
256
283
  "_attachments" => {
@@ -260,14 +287,14 @@ describe CouchRest::Database do
260
287
  }
261
288
  }
262
289
  }
263
- @db.save(@doc)
290
+ @db.save_doc(doc)
291
+ @doc = @db.get("mydocwithattachment")
264
292
  end
265
293
  it "should save and be indicated" do
266
- doc = @db.get("mydocwithattachment")
267
- doc['_attachments']['test.html']['length'].should == @attach.length
294
+ @doc['_attachments']['test.html']['length'].should == @attach.length
268
295
  end
269
296
  it "should be there" do
270
- attachment = @db.fetch_attachment("mydocwithattachment","test.html")
297
+ attachment = @db.fetch_attachment(@doc,"test.html")
271
298
  attachment.should == @attach
272
299
  end
273
300
  end
@@ -284,15 +311,16 @@ describe CouchRest::Database do
284
311
  }
285
312
  }
286
313
  }
287
- @db.save(doc)
288
- doc = @db.get('mydocwithattachment')
314
+ @db.save_doc(doc)
315
+ doc['_rev'].should_not be_nil
289
316
  doc['field'] << 'another value'
290
- @db.save(doc)
317
+ @db.save_doc(doc)["ok"].should be_true
291
318
  end
292
319
 
293
320
  it 'should be there' do
294
- attachment = @db.fetch_attachment('mydocwithattachment', 'test.html')
295
- attachment.should == @attach
321
+ doc = @db.get('mydocwithattachment')
322
+ attachment = @db.fetch_attachment(doc, 'test.html')
323
+ Base64.decode64(attachment).should == @attach
296
324
  end
297
325
  end
298
326
 
@@ -314,22 +342,43 @@ describe CouchRest::Database do
314
342
  }
315
343
  }
316
344
  }
317
- @db.save(@doc)
345
+ @db.save_doc(@doc)
346
+ @doc = @db.get("mydocwithattachment")
318
347
  end
319
348
  it "should save and be indicated" do
320
- doc = @db.get("mydocwithattachment")
321
- doc['_attachments']['test.html']['length'].should == @attach.length
322
- doc['_attachments']['other.html']['length'].should == @attach2.length
349
+ @doc['_attachments']['test.html']['length'].should == @attach.length
350
+ @doc['_attachments']['other.html']['length'].should == @attach2.length
323
351
  end
324
352
  it "should be there" do
325
- attachment = @db.fetch_attachment("mydocwithattachment","test.html")
353
+ attachment = @db.fetch_attachment(@doc,"test.html")
326
354
  attachment.should == @attach
327
355
  end
328
356
  it "should be there" do
329
- attachment = @db.fetch_attachment("mydocwithattachment","other.html")
357
+ attachment = @db.fetch_attachment(@doc,"other.html")
330
358
  attachment.should == @attach2
331
359
  end
332
360
  end
361
+
362
+ describe "DELETE an attachment directly from the database" do
363
+ before(:each) do
364
+ doc = {
365
+ '_id' => 'mydocwithattachment',
366
+ '_attachments' => {
367
+ 'test.html' => {
368
+ 'type' => 'text/html',
369
+ 'data' => "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
370
+ }
371
+ }
372
+ }
373
+ @db.save_doc(doc)
374
+ @doc = @db.get('mydocwithattachment')
375
+ end
376
+ it "should delete the attachment" do
377
+ lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error
378
+ @db.delete_attachment(@doc, "test.html")
379
+ lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound)
380
+ end
381
+ end
333
382
 
334
383
  describe "POST document with attachment (with funky name)" do
335
384
  before(:each) do
@@ -343,14 +392,15 @@ describe CouchRest::Database do
343
392
  }
344
393
  }
345
394
  }
346
- @docid = @db.save(@doc)['id']
395
+ @docid = @db.save_doc(@doc)['id']
347
396
  end
348
397
  it "should save and be indicated" do
349
398
  doc = @db.get(@docid)
350
399
  doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length
351
400
  end
352
401
  it "should be there" do
353
- attachment = @db.fetch_attachment(@docid,"http://example.com/stuff.cgi?things=and%20stuff")
402
+ doc = @db.get(@docid)
403
+ attachment = @db.fetch_attachment(doc,"http://example.com/stuff.cgi?things=and%20stuff")
354
404
  attachment.should == @attach
355
405
  end
356
406
  end
@@ -358,15 +408,15 @@ describe CouchRest::Database do
358
408
  describe "PUT (new document with url id)" do
359
409
  it "should create the document" do
360
410
  @docid = "http://example.com/stuff.cgi?things=and%20stuff"
361
- @db.save({'_id' => @docid, 'will-exist' => 'here'})
362
- lambda{@db.save({'_id' => @docid})}.should raise_error(RestClient::Request::RequestFailed)
411
+ @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
412
+ lambda{@db.save_doc({'_id' => @docid})}.should raise_error(RestClient::Request::RequestFailed)
363
413
  @db.get(@docid)['will-exist'].should == 'here'
364
414
  end
365
415
  end
366
416
 
367
417
  describe "PUT (new document with id)" do
368
418
  it "should start without the document" do
369
- # r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
419
+ # r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
370
420
  @db.documents['rows'].each do |doc|
371
421
  doc['id'].should_not == 'my-doc'
372
422
  end
@@ -375,17 +425,17 @@ describe CouchRest::Database do
375
425
  # or instead make it return something with a fancy <=> method
376
426
  end
377
427
  it "should create the document" do
378
- @db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
379
- lambda{@db.save({'_id' => 'my-doc'})}.should raise_error(RestClient::Request::RequestFailed)
428
+ @db.save_doc({'_id' => 'my-doc', 'will-exist' => 'here'})
429
+ lambda{@db.save_doc({'_id' => 'my-doc'})}.should raise_error(RestClient::Request::RequestFailed)
380
430
  end
381
431
  end
382
432
 
383
433
  describe "PUT (existing document with rev)" do
384
434
  before(:each) do
385
- @db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
435
+ @db.save_doc({'_id' => 'my-doc', 'will-exist' => 'here'})
386
436
  @doc = @db.get('my-doc')
387
437
  @docid = "http://example.com/stuff.cgi?things=and%20stuff"
388
- @db.save({'_id' => @docid, 'now' => 'save'})
438
+ @db.save_doc({'_id' => @docid, 'now' => 'save'})
389
439
  end
390
440
  it "should start with the document" do
391
441
  @doc['will-exist'].should == 'here'
@@ -394,18 +444,18 @@ describe CouchRest::Database do
394
444
  it "should save with url id" do
395
445
  doc = @db.get(@docid)
396
446
  doc['yaml'] = ['json', 'word.']
397
- @db.save doc
447
+ @db.save_doc doc
398
448
  @db.get(@docid)['yaml'].should == ['json', 'word.']
399
449
  end
400
450
  it "should fail to resave without the rev" do
401
451
  @doc['them-keys'] = 'huge'
402
452
  @doc['_rev'] = 'wrong'
403
- # @db.save(@doc)
404
- lambda {@db.save(@doc)}.should raise_error
453
+ # @db.save_doc(@doc)
454
+ lambda {@db.save_doc(@doc)}.should raise_error
405
455
  end
406
456
  it "should update the document" do
407
457
  @doc['them-keys'] = 'huge'
408
- @db.save(@doc)
458
+ @db.save_doc(@doc)
409
459
  now = @db.get('my-doc')
410
460
  now['them-keys'].should == 'huge'
411
461
  end
@@ -414,7 +464,7 @@ describe CouchRest::Database do
414
464
  describe "cached bulk save" do
415
465
  it "stores documents in a database-specific cache" do
416
466
  td = {"_id" => "btd1", "val" => "test"}
417
- @db.save(td, true)
467
+ @db.save_doc(td, true)
418
468
  @db.instance_variable_get("@bulk_save_cache").should == [td]
419
469
 
420
470
  end
@@ -423,8 +473,8 @@ describe CouchRest::Database do
423
473
  @db.bulk_save_cache_limit = 3
424
474
  td1 = {"_id" => "td1", "val" => true}
425
475
  td2 = {"_id" => "td2", "val" => 4}
426
- @db.save(td1, true)
427
- @db.save(td2, true)
476
+ @db.save_doc(td1, true)
477
+ @db.save_doc(td2, true)
428
478
  lambda do
429
479
  @db.get(td1["_id"])
430
480
  end.should raise_error(RestClient::ResourceNotFound)
@@ -432,7 +482,7 @@ describe CouchRest::Database do
432
482
  @db.get(td2["_id"])
433
483
  end.should raise_error(RestClient::ResourceNotFound)
434
484
  td3 = {"_id" => "td3", "val" => "foo"}
435
- @db.save(td3, true)
485
+ @db.save_doc(td3, true)
436
486
  @db.get(td1["_id"])["val"].should == td1["val"]
437
487
  @db.get(td2["_id"])["val"].should == td2["val"]
438
488
  @db.get(td3["_id"])["val"].should == td3["val"]
@@ -442,11 +492,11 @@ describe CouchRest::Database do
442
492
  td1 = {"_id" => "blah", "val" => true}
443
493
  td2 = {"_id" => "steve", "val" => 3}
444
494
  @db.bulk_save_cache_limit = 50
445
- @db.save(td1, true)
495
+ @db.save_doc(td1, true)
446
496
  lambda do
447
497
  @db.get(td1["_id"])
448
498
  end.should raise_error(RestClient::ResourceNotFound)
449
- @db.save(td2)
499
+ @db.save_doc(td2)
450
500
  @db.get(td1["_id"])["val"].should == td1["val"]
451
501
  @db.get(td2["_id"])["val"].should == td2["val"]
452
502
  end
@@ -454,27 +504,27 @@ describe CouchRest::Database do
454
504
 
455
505
  describe "DELETE existing document" do
456
506
  before(:each) do
457
- @r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
507
+ @r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
458
508
  @docid = "http://example.com/stuff.cgi?things=and%20stuff"
459
- @db.save({'_id' => @docid, 'will-exist' => 'here'})
509
+ @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
460
510
  end
461
511
  it "should work" do
462
512
  doc = @db.get(@r['id'])
463
513
  doc['and'].should == 'spain'
464
- @db.delete doc
514
+ @db.delete_doc doc
465
515
  lambda{@db.get @r['id']}.should raise_error
466
516
  end
467
517
  it "should work with uri id" do
468
518
  doc = @db.get(@docid)
469
- @db.delete doc
519
+ @db.delete_doc doc
470
520
  lambda{@db.get @docid}.should raise_error
471
521
  end
472
522
  it "should fail without an _id" do
473
- lambda{@db.delete({"not"=>"a real doc"})}.should raise_error(ArgumentError)
523
+ lambda{@db.delete_doc({"not"=>"a real doc"})}.should raise_error(ArgumentError)
474
524
  end
475
525
  it "should defer actual deletion when using bulk save" do
476
526
  doc = @db.get(@docid)
477
- @db.delete doc, true
527
+ @db.delete_doc doc, true
478
528
  lambda{@db.get @docid}.should_not raise_error
479
529
  @db.bulk_save
480
530
  lambda{@db.get @docid}.should raise_error
@@ -484,13 +534,13 @@ describe CouchRest::Database do
484
534
 
485
535
  describe "COPY existing document" do
486
536
  before :each do
487
- @r = @db.save({'artist' => 'Zappa', 'title' => 'Muffin Man'})
537
+ @r = @db.save_doc({'artist' => 'Zappa', 'title' => 'Muffin Man'})
488
538
  @docid = 'tracks/zappa/muffin-man'
489
539
  @doc = @db.get(@r['id'])
490
540
  end
491
541
  describe "to a new location" do
492
542
  it "should work" do
493
- @db.copy @doc, @docid
543
+ @db.copy_doc @doc, @docid
494
544
  newdoc = @db.get(@docid)
495
545
  newdoc['artist'].should == 'Zappa'
496
546
  end
@@ -500,20 +550,20 @@ describe CouchRest::Database do
500
550
  end
501
551
  describe "to an existing location" do
502
552
  before :each do
503
- @db.save({'_id' => @docid, 'will-exist' => 'here'})
553
+ @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
504
554
  end
505
555
  it "should fail without a rev" do
506
- lambda{@db.copy @doc, @docid}.should raise_error(RestClient::RequestFailed)
556
+ lambda{@db.copy_doc @doc, @docid}.should raise_error(RestClient::RequestFailed)
507
557
  end
508
558
  it "should succeed with a rev" do
509
559
  @to_be_overwritten = @db.get(@docid)
510
- @db.copy @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
560
+ @db.copy_doc @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
511
561
  newdoc = @db.get(@docid)
512
562
  newdoc['artist'].should == 'Zappa'
513
563
  end
514
564
  it "should succeed given the doc to overwrite" do
515
565
  @to_be_overwritten = @db.get(@docid)
516
- @db.copy @doc, @to_be_overwritten
566
+ @db.copy_doc @doc, @to_be_overwritten
517
567
  newdoc = @db.get(@docid)
518
568
  newdoc['artist'].should == 'Zappa'
519
569
  end
@@ -522,13 +572,13 @@ describe CouchRest::Database do
522
572
 
523
573
  describe "MOVE existing document" do
524
574
  before :each do
525
- @r = @db.save({'artist' => 'Zappa', 'title' => 'Muffin Man'})
575
+ @r = @db.save_doc({'artist' => 'Zappa', 'title' => 'Muffin Man'})
526
576
  @docid = 'tracks/zappa/muffin-man'
527
577
  @doc = @db.get(@r['id'])
528
578
  end
529
579
  describe "to a new location" do
530
580
  it "should work" do
531
- @db.move @doc, @docid
581
+ @db.move_doc @doc, @docid
532
582
  newdoc = @db.get(@docid)
533
583
  newdoc['artist'].should == 'Zappa'
534
584
  lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
@@ -540,22 +590,22 @@ describe CouchRest::Database do
540
590
  end
541
591
  describe "to an existing location" do
542
592
  before :each do
543
- @db.save({'_id' => @docid, 'will-exist' => 'here'})
593
+ @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
544
594
  end
545
595
  it "should fail without a rev" do
546
- lambda{@db.move @doc, @docid}.should raise_error(RestClient::RequestFailed)
596
+ lambda{@db.move_doc @doc, @docid}.should raise_error(RestClient::RequestFailed)
547
597
  lambda{@db.get(@r['id'])}.should_not raise_error
548
598
  end
549
599
  it "should succeed with a rev" do
550
600
  @to_be_overwritten = @db.get(@docid)
551
- @db.move @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
601
+ @db.move_doc @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
552
602
  newdoc = @db.get(@docid)
553
603
  newdoc['artist'].should == 'Zappa'
554
604
  lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
555
605
  end
556
606
  it "should succeed given the doc to overwrite" do
557
607
  @to_be_overwritten = @db.get(@docid)
558
- @db.move @doc, @to_be_overwritten
608
+ @db.move_doc @doc, @to_be_overwritten
559
609
  newdoc = @db.get(@docid)
560
610
  newdoc['artist'].should == 'Zappa'
561
611
  lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
@@ -566,7 +616,7 @@ describe CouchRest::Database do
566
616
 
567
617
  it "should list documents" do
568
618
  5.times do
569
- @db.save({'another' => 'doc', 'will-exist' => 'anywhere'})
619
+ @db.save_doc({'another' => 'doc', 'will-exist' => 'anywhere'})
570
620
  end
571
621
  ds = @db.documents
572
622
  ds['rows'].should be_an_instance_of(Array)
@@ -577,7 +627,7 @@ describe CouchRest::Database do
577
627
  describe "documents / _all_docs" do
578
628
  before(:each) do
579
629
  9.times do |i|
580
- @db.save({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
630
+ @db.save_doc({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
581
631
  end
582
632
  end
583
633
  it "should list documents with keys and such" do
@@ -624,6 +674,72 @@ describe CouchRest::Database do
624
674
  @cr.databases.should_not include('couchrest-test')
625
675
  end
626
676
  end
677
+
678
+ describe "replicating a database" do
679
+ before do
680
+ @db.save_doc({'_id' => 'test_doc', 'some-value' => 'foo'})
681
+ @other_db = @cr.database 'couchrest-test-replication'
682
+ @other_db.delete! rescue nil
683
+ @other_db = @cr.create_db 'couchrest-test-replication'
684
+ end
685
+
686
+ describe "via pulling" do
687
+ before do
688
+ @other_db.replicate_from @db
689
+ end
690
+
691
+ it "contains the document from the original database" do
692
+ doc = @other_db.get('test_doc')
693
+ doc['some-value'].should == 'foo'
694
+ end
695
+ end
696
+
697
+ describe "via pushing" do
698
+ before do
699
+ @db.replicate_to @other_db
700
+ end
701
+
702
+ it "copies the document to the other database" do
703
+ doc = @other_db.get('test_doc')
704
+ doc['some-value'].should == 'foo'
705
+ end
706
+ end
707
+ end
708
+
709
+ describe "creating a database" do
710
+ before(:each) do
711
+ @db = @cr.database('couchrest-test-db_to_create')
712
+ @db.delete! if @cr.databases.include?('couchrest-test-db_to_create')
713
+ end
714
+
715
+ it "should just work fine" do
716
+ @cr.databases.should_not include('couchrest-test-db_to_create')
717
+ @db.create!
718
+ @cr.databases.should include('couchrest-test-db_to_create')
719
+ end
720
+ end
721
+
722
+ describe "recreating a database" do
723
+ before(:each) do
724
+ @db = @cr.database('couchrest-test-db_to_create')
725
+ @db2 = @cr.database('couchrest-test-db_to_recreate')
726
+ @cr.databases.include?(@db.name) ? nil : @db.create!
727
+ @cr.databases.include?(@db2.name) ? @db2.delete! : nil
728
+ end
729
+
730
+ it "should drop and recreate a database" do
731
+ @cr.databases.should include(@db.name)
732
+ @db.recreate!
733
+ @cr.databases.should include(@db.name)
734
+ end
735
+
736
+ it "should recreate a db even tho it doesn't exist" do
737
+ @cr.databases.should_not include(@db2.name)
738
+ @db2.recreate!
739
+ @cr.databases.should include(@db2.name)
740
+ end
741
+
742
+ end
627
743
 
628
744
 
629
- end
745
+ end