jchris-couchrest 0.9.12 → 0.12.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/{README.rdoc → README.md} +10 -8
  2. data/Rakefile +60 -39
  3. data/THANKS.md +18 -0
  4. data/examples/word_count/markov +1 -1
  5. data/examples/word_count/views/word_count/count-reduce.js +2 -2
  6. data/examples/word_count/word_count.rb +2 -2
  7. data/examples/word_count/word_count_query.rb +2 -2
  8. data/lib/couchrest/commands/push.rb +8 -4
  9. data/lib/couchrest/core/database.rb +95 -14
  10. data/lib/couchrest/core/design.rb +89 -0
  11. data/lib/couchrest/core/document.rb +63 -0
  12. data/lib/couchrest/core/model.rb +203 -120
  13. data/lib/couchrest/core/server.rb +1 -1
  14. data/lib/couchrest/core/view.rb +4 -0
  15. data/lib/couchrest/helper/pager.rb +10 -10
  16. data/lib/couchrest/monkeypatches.rb +1 -1
  17. data/lib/couchrest.rb +20 -2
  18. data/spec/couchrest/core/couchrest_spec.rb +33 -23
  19. data/spec/couchrest/core/database_spec.rb +163 -8
  20. data/spec/couchrest/core/design_spec.rb +131 -0
  21. data/spec/couchrest/core/document_spec.rb +130 -0
  22. data/spec/couchrest/core/model_spec.rb +319 -35
  23. data/spec/couchrest/helpers/pager_spec.rb +2 -2
  24. data/spec/fixtures/attachments/README +3 -0
  25. data/spec/spec_helper.rb +17 -3
  26. data/utils/remap.rb +2 -2
  27. data/utils/subset.rb +2 -2
  28. metadata +24 -33
  29. data/THANKS +0 -15
  30. data/bin/couchapp +0 -55
  31. data/bin/couchview +0 -48
  32. data/lib/couchrest/helper/file_manager.rb +0 -285
  33. data/lib/couchrest/helper/templates/example-map.js +0 -8
  34. data/lib/couchrest/helper/templates/example-reduce.js +0 -10
  35. data/lib/couchrest/helper/templates/index.html +0 -26
  36. data/spec/couchapp_spec.rb +0 -82
  37. data/spec/couchrest/helpers/file_manager_spec.rb +0 -170
@@ -46,48 +46,48 @@ describe CouchRest do
46
46
  it "should parse just a dbname" do
47
47
  db = CouchRest.parse "my-db"
48
48
  db[:database].should == "my-db"
49
- db[:host].should == "localhost:5984"
49
+ db[:host].should == "127.0.0.1:5984"
50
50
  end
51
51
  it "should parse a host and db" do
52
- db = CouchRest.parse "localhost/my-db"
52
+ db = CouchRest.parse "127.0.0.1/my-db"
53
53
  db[:database].should == "my-db"
54
- db[:host].should == "localhost"
54
+ db[:host].should == "127.0.0.1"
55
55
  end
56
56
  it "should parse a host and db with http" do
57
- db = CouchRest.parse "http://localhost/my-db"
57
+ db = CouchRest.parse "http://127.0.0.1/my-db"
58
58
  db[:database].should == "my-db"
59
- db[:host].should == "localhost"
59
+ db[:host].should == "127.0.0.1"
60
60
  end
61
61
  it "should parse a host with a port and db" do
62
- db = CouchRest.parse "localhost:5555/my-db"
62
+ db = CouchRest.parse "127.0.0.1:5555/my-db"
63
63
  db[:database].should == "my-db"
64
- db[:host].should == "localhost:5555"
64
+ db[:host].should == "127.0.0.1:5555"
65
65
  end
66
66
  it "should parse a host with a port and db with http" do
67
- db = CouchRest.parse "http://localhost:5555/my-db"
67
+ db = CouchRest.parse "http://127.0.0.1:5555/my-db"
68
68
  db[:database].should == "my-db"
69
- db[:host].should == "localhost:5555"
69
+ db[:host].should == "127.0.0.1:5555"
70
70
  end
71
71
  it "should parse just a host" do
72
- db = CouchRest.parse "http://localhost:5555/"
72
+ db = CouchRest.parse "http://127.0.0.1:5555/"
73
73
  db[:database].should be_nil
74
- db[:host].should == "localhost:5555"
74
+ db[:host].should == "127.0.0.1:5555"
75
75
  end
76
76
  it "should parse just a host no slash" do
77
- db = CouchRest.parse "http://localhost:5555"
78
- db[:host].should == "localhost:5555"
77
+ db = CouchRest.parse "http://127.0.0.1:5555"
78
+ db[:host].should == "127.0.0.1:5555"
79
79
  db[:database].should be_nil
80
80
  end
81
81
  it "should get docid" do
82
- db = CouchRest.parse "localhost:5555/my-db/my-doc"
82
+ db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc"
83
83
  db[:database].should == "my-db"
84
- db[:host].should == "localhost:5555"
84
+ db[:host].should == "127.0.0.1:5555"
85
85
  db[:doc].should == "my-doc"
86
86
  end
87
87
  it "should get docid with http" do
88
- db = CouchRest.parse "http://localhost:5555/my-db/my-doc"
88
+ db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc"
89
89
  db[:database].should == "my-db"
90
- db[:host].should == "localhost:5555"
90
+ db[:host].should == "127.0.0.1:5555"
91
91
  db[:doc].should == "my-doc"
92
92
  end
93
93
 
@@ -137,24 +137,24 @@ describe CouchRest do
137
137
 
138
138
  describe "easy initializing a database adapter" do
139
139
  it "should be possible without an explicit CouchRest instantiation" do
140
- db = CouchRest.database "http://localhost:5984/couchrest-test"
140
+ db = CouchRest.database "http://127.0.0.1:5984/couchrest-test"
141
141
  db.should be_an_instance_of(CouchRest::Database)
142
- db.host.should == "localhost:5984"
142
+ db.host.should == "127.0.0.1:5984"
143
143
  end
144
144
  # TODO add https support (need test environment...)
145
145
  # it "should work with https" # do
146
- # db = CouchRest.database "https://localhost:5984/couchrest-test"
147
- # db.host.should == "https://localhost:5984"
146
+ # db = CouchRest.database "https://127.0.0.1:5984/couchrest-test"
147
+ # db.host.should == "https://127.0.0.1:5984"
148
148
  # end
149
149
  it "should not create the database automatically" do
150
- db = CouchRest.database "http://localhost:5984/couchrest-test"
150
+ db = CouchRest.database "http://127.0.0.1:5984/couchrest-test"
151
151
  lambda{db.info}.should raise_error(RestClient::ResourceNotFound)
152
152
  end
153
153
  end
154
154
 
155
155
  describe "ensuring the db exists" do
156
156
  it "should be super easy" do
157
- db = CouchRest.database! "http://localhost:5984/couchrest-test-2"
157
+ db = CouchRest.database! "http://127.0.0.1:5984/couchrest-test-2"
158
158
  db.name.should == 'couchrest-test-2'
159
159
  db.info["db_name"].should == 'couchrest-test-2'
160
160
  end
@@ -188,4 +188,14 @@ describe CouchRest do
188
188
  end
189
189
  end
190
190
 
191
+ describe "using a proxy for RestClient connections" do
192
+ it "should set proxy url for RestClient" do
193
+ CouchRest.proxy 'http://localhost:8888/'
194
+ proxy_uri = URI.parse(RestClient.proxy)
195
+ proxy_uri.host.should eql( 'localhost' )
196
+ proxy_uri.port.should eql( 8888 )
197
+ CouchRest.proxy nil
198
+ end
199
+ end
200
+
191
201
  end
@@ -29,8 +29,8 @@ describe CouchRest::Database do
29
29
  rs = @db.temp_view(@temp_view, :key => "wild")
30
30
  rs['rows'].length.should == 1
31
31
  end
32
- it "should work with a count" do
33
- rs = @db.temp_view(@temp_view, :count => 1)
32
+ it "should work with a limit" do
33
+ rs = @db.temp_view(@temp_view, :limit => 1)
34
34
  rs['rows'].length.should == 1
35
35
  end
36
36
  it "should work with multi-keys" do
@@ -109,8 +109,8 @@ describe CouchRest::Database do
109
109
  rs = @db.view('first/test', :key => "wild")
110
110
  rs['rows'].length.should == 1
111
111
  end
112
- it "should work with a count" do
113
- rs = @db.view('first/test', :count => 1)
112
+ it "should work with a limit" do
113
+ rs = @db.view('first/test', :limit => 1)
114
114
  rs['rows'].length.should == 1
115
115
  end
116
116
  it "should work with multi-keys" do
@@ -159,7 +159,7 @@ describe CouchRest::Database do
159
159
 
160
160
  docs = [{'key' => 'value'}, {'_id' => 'totally-uniq'}]
161
161
  id_docs = [{'key' => 'value', '_id' => 'asdf6sgadkfhgsdfusdf'}, {'_id' => 'totally-uniq'}]
162
- CouchRest.should_receive(:post).with("http://localhost:5984/couchrest-test/_bulk_docs", {:docs => id_docs})
162
+ CouchRest.should_receive(:post).with("http://127.0.0.1:5984/couchrest-test/_bulk_docs", {:docs => id_docs})
163
163
 
164
164
  @db.bulk_save(docs)
165
165
  end
@@ -190,6 +190,16 @@ describe CouchRest::Database do
190
190
  @db.get('twoB')
191
191
  end.should raise_error(RestClient::ResourceNotFound)
192
192
  end
193
+
194
+ it "should empty the bulk save cache if no documents are given" do
195
+ @db.save({"_id" => "bulk_cache_1", "val" => "test"}, true)
196
+ lambda do
197
+ @db.get('bulk_cache_1')
198
+ end.should raise_error(RestClient::ResourceNotFound)
199
+ @db.bulk_save
200
+ @db.get("bulk_cache_1")["val"].should == "test"
201
+ end
202
+
193
203
  it "should raise an error that is useful for recovery" do
194
204
  @r = @db.save({"_id" => "taken", "field" => "stuff"})
195
205
  begin
@@ -215,7 +225,7 @@ describe CouchRest::Database do
215
225
  r2["lemons"].should == "from texas"
216
226
  end
217
227
  it "should use PUT with UUIDs" do
218
- CouchRest.should_receive(:put)
228
+ CouchRest.should_receive(:put).and_return({"ok" => true, "id" => "100", "rev" => "55"})
219
229
  r = @db.save({'just' => ['another document']})
220
230
  end
221
231
 
@@ -400,7 +410,48 @@ describe CouchRest::Database do
400
410
  now['them-keys'].should == 'huge'
401
411
  end
402
412
  end
403
-
413
+
414
+ describe "cached bulk save" do
415
+ it "stores documents in a database-specific cache" do
416
+ td = {"_id" => "btd1", "val" => "test"}
417
+ @db.save(td, true)
418
+ @db.instance_variable_get("@bulk_save_cache").should == [td]
419
+
420
+ end
421
+
422
+ it "doesn't save to the database until the configured cache size is exceded" do
423
+ @db.bulk_save_cache_limit = 3
424
+ td1 = {"_id" => "td1", "val" => true}
425
+ td2 = {"_id" => "td2", "val" => 4}
426
+ @db.save(td1, true)
427
+ @db.save(td2, true)
428
+ lambda do
429
+ @db.get(td1["_id"])
430
+ end.should raise_error(RestClient::ResourceNotFound)
431
+ lambda do
432
+ @db.get(td2["_id"])
433
+ end.should raise_error(RestClient::ResourceNotFound)
434
+ td3 = {"_id" => "td3", "val" => "foo"}
435
+ @db.save(td3, true)
436
+ @db.get(td1["_id"])["val"].should == td1["val"]
437
+ @db.get(td2["_id"])["val"].should == td2["val"]
438
+ @db.get(td3["_id"])["val"].should == td3["val"]
439
+ end
440
+
441
+ it "clears the bulk save cache the first time a non bulk save is requested" do
442
+ td1 = {"_id" => "blah", "val" => true}
443
+ td2 = {"_id" => "steve", "val" => 3}
444
+ @db.bulk_save_cache_limit = 50
445
+ @db.save(td1, true)
446
+ lambda do
447
+ @db.get(td1["_id"])
448
+ end.should raise_error(RestClient::ResourceNotFound)
449
+ @db.save(td2)
450
+ @db.get(td1["_id"])["val"].should == td1["val"]
451
+ @db.get(td2["_id"])["val"].should == td2["val"]
452
+ end
453
+ end
454
+
404
455
  describe "DELETE existing document" do
405
456
  before(:each) do
406
457
  @r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
@@ -418,8 +469,102 @@ describe CouchRest::Database do
418
469
  @db.delete doc
419
470
  lambda{@db.get @docid}.should raise_error
420
471
  end
472
+ it "should fail without an _id" do
473
+ lambda{@db.delete({"not"=>"a real doc"})}.should raise_error(ArgumentError)
474
+ end
475
+ it "should defer actual deletion when using bulk save" do
476
+ doc = @db.get(@docid)
477
+ @db.delete doc, true
478
+ lambda{@db.get @docid}.should_not raise_error
479
+ @db.bulk_save
480
+ lambda{@db.get @docid}.should raise_error
481
+ end
482
+
483
+ end
484
+
485
+ describe "COPY existing document" do
486
+ before :each do
487
+ @r = @db.save({'artist' => 'Zappa', 'title' => 'Muffin Man'})
488
+ @docid = 'tracks/zappa/muffin-man'
489
+ @doc = @db.get(@r['id'])
490
+ end
491
+ describe "to a new location" do
492
+ it "should work" do
493
+ @db.copy @doc, @docid
494
+ newdoc = @db.get(@docid)
495
+ debugger
496
+ newdoc['artist'].should == 'Zappa'
497
+ end
498
+ it "should fail without an _id" do
499
+ lambda{@db.copy({"not"=>"a real doc"})}.should raise_error(ArgumentError)
500
+ end
501
+ end
502
+ describe "to an existing location" do
503
+ before :each do
504
+ @db.save({'_id' => @docid, 'will-exist' => 'here'})
505
+ end
506
+ it "should fail without a rev" do
507
+ lambda{@db.copy @doc, @docid}.should raise_error(RestClient::RequestFailed)
508
+ end
509
+ it "should succeed with a rev" do
510
+ @to_be_overwritten = @db.get(@docid)
511
+ @db.copy @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
512
+ newdoc = @db.get(@docid)
513
+ newdoc['artist'].should == 'Zappa'
514
+ end
515
+ it "should succeed given the doc to overwrite" do
516
+ @to_be_overwritten = @db.get(@docid)
517
+ @db.copy @doc, @to_be_overwritten
518
+ newdoc = @db.get(@docid)
519
+ newdoc['artist'].should == 'Zappa'
520
+ end
521
+ end
421
522
  end
422
523
 
524
+ describe "MOVE existing document" do
525
+ before :each do
526
+ @r = @db.save({'artist' => 'Zappa', 'title' => 'Muffin Man'})
527
+ @docid = 'tracks/zappa/muffin-man'
528
+ @doc = @db.get(@r['id'])
529
+ end
530
+ describe "to a new location" do
531
+ it "should work" do
532
+ @db.move @doc, @docid
533
+ newdoc = @db.get(@docid)
534
+ newdoc['artist'].should == 'Zappa'
535
+ lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
536
+ end
537
+ it "should fail without an _id or _rev" do
538
+ lambda{@db.move({"not"=>"a real doc"})}.should raise_error(ArgumentError)
539
+ lambda{@db.move({"_id"=>"not a real doc"})}.should raise_error(ArgumentError)
540
+ end
541
+ end
542
+ describe "to an existing location" do
543
+ before :each do
544
+ @db.save({'_id' => @docid, 'will-exist' => 'here'})
545
+ end
546
+ it "should fail without a rev" do
547
+ lambda{@db.move @doc, @docid}.should raise_error(RestClient::RequestFailed)
548
+ lambda{@db.get(@r['id'])}.should_not raise_error
549
+ end
550
+ it "should succeed with a rev" do
551
+ @to_be_overwritten = @db.get(@docid)
552
+ @db.move @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
553
+ newdoc = @db.get(@docid)
554
+ newdoc['artist'].should == 'Zappa'
555
+ lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
556
+ end
557
+ it "should succeed given the doc to overwrite" do
558
+ @to_be_overwritten = @db.get(@docid)
559
+ @db.move @doc, @to_be_overwritten
560
+ newdoc = @db.get(@docid)
561
+ newdoc['artist'].should == 'Zappa'
562
+ lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
563
+ end
564
+ end
565
+ end
566
+
567
+
423
568
  it "should list documents" do
424
569
  5.times do
425
570
  @db.save({'another' => 'doc', 'will-exist' => 'anywhere'})
@@ -458,6 +603,16 @@ describe CouchRest::Database do
458
603
  end
459
604
  end
460
605
 
606
+
607
+ describe "compacting a database" do
608
+ it "should compact the database" do
609
+ db = @cr.database('couchrest-test')
610
+ # r =
611
+ db.compact!
612
+ # r['ok'].should == true
613
+ end
614
+ end
615
+
461
616
  describe "deleting a database" do
462
617
  it "should start with the test database" do
463
618
  @cr.databases.should include('couchrest-test')
@@ -472,4 +627,4 @@ describe CouchRest::Database do
472
627
  end
473
628
 
474
629
 
475
- end
630
+ end
@@ -0,0 +1,131 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe CouchRest::Design do
4
+
5
+ describe "defining a view" do
6
+ it "should add a view to the design doc" do
7
+ @des = CouchRest::Design.new
8
+ method = @des.view_by :name
9
+ method.should == "by_name"
10
+ @des["views"]["by_name"].should_not be_nil
11
+ end
12
+ end
13
+
14
+ describe "with an unsaved view" do
15
+ before(:each) do
16
+ @des = CouchRest::Design.new
17
+ method = @des.view_by :name
18
+ end
19
+ it "should accept a name" do
20
+ @des.name = "mytest"
21
+ @des.name.should == "mytest"
22
+ end
23
+ it "should not save on view definition" do
24
+ @des.rev.should be_nil
25
+ end
26
+ it "should freak out on view access" do
27
+ lambda{@des.view :by_name}.should raise_error
28
+ end
29
+ end
30
+
31
+ describe "saving" do
32
+ before(:each) do
33
+ @des = CouchRest::Design.new
34
+ method = @des.view_by :name
35
+ @des.database = reset_test_db!
36
+ end
37
+ it "should fail without a name" do
38
+ lambda{@des.save}.should raise_error(ArgumentError)
39
+ end
40
+ it "should work with a name" do
41
+ @des.name = "myview"
42
+ @des.save
43
+ end
44
+ end
45
+
46
+ describe "when it's saved" do
47
+ before(:each) do
48
+ @db = reset_test_db!
49
+ @db.bulk_save([{"name" => "x"},{"name" => "y"}])
50
+ @des = CouchRest::Design.new
51
+ @des.database = @db
52
+ method = @des.view_by :name
53
+ end
54
+ it "should by queryable when it's saved" do
55
+ @des.name = "mydesign"
56
+ @des.save
57
+ res = @des.view :by_name
58
+ res["rows"][0]["key"].should == "x"
59
+ end
60
+ end
61
+
62
+ describe "from a saved document" do
63
+ before(:each) do
64
+ @db = reset_test_db!
65
+ @db.save({
66
+ "_id" => "_design/test",
67
+ "views" => {
68
+ "by_name" => {
69
+ "map" => "function(doc){if (doc.name) emit(doc.name, null)}"
70
+ }
71
+ }
72
+ })
73
+ @db.bulk_save([{"name" => "a"},{"name" => "b"}])
74
+ @des = @db.get "_design/test"
75
+ end
76
+ it "should be a Design" do
77
+ @des.should be_an_instance_of(CouchRest::Design)
78
+ end
79
+ it "should have a modifiable name" do
80
+ @des.name.should == "test"
81
+ @des.name = "supertest"
82
+ @des.id.should == "_design/supertest"
83
+ end
84
+ it "should by queryable" do
85
+ res = @des.view :by_name
86
+ res["rows"][0]["key"].should == "a"
87
+ end
88
+ end
89
+
90
+ describe "a view with default options" do
91
+ before(:all) do
92
+ @db = reset_test_db!
93
+ @des = CouchRest::Design.new
94
+ @des.name = "test"
95
+ method = @des.view_by :name, :descending => true
96
+ @des.database = @db
97
+ @des.save
98
+ @db.bulk_save([{"name" => "a"},{"name" => "z"}])
99
+ end
100
+ it "should save them" do
101
+ @d2 = @db.get(@des.id)
102
+ @d2["views"]["by_name"]["couchrest-defaults"].should == {"descending"=>true}
103
+ end
104
+ it "should use them" do
105
+ res = @des.view :by_name
106
+ res["rows"].first["key"].should == "z"
107
+ end
108
+ it "should override them" do
109
+ res = @des.view :by_name, :descending => false
110
+ res["rows"].first["key"].should == "a"
111
+ end
112
+ end
113
+
114
+ describe "a view with multiple keys" do
115
+ before(:all) do
116
+ @db = reset_test_db!
117
+ @des = CouchRest::Design.new
118
+ @des.name = "test"
119
+ method = @des.view_by :name, :age
120
+ @des.database = @db
121
+ @des.save
122
+ @db.bulk_save([{"name" => "a", "age" => 2},
123
+ {"name" => "a", "age" => 4},{"name" => "z", "age" => 9}])
124
+ end
125
+ it "should work" do
126
+ res = @des.view :by_name_and_age
127
+ res["rows"].first["key"].should == ["a",2]
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,130 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe CouchRest::Document, "[]=" do
4
+ before(:each) do
5
+ @doc = CouchRest::Document.new
6
+ end
7
+ it "should work" do
8
+ @doc["enamel"].should == nil
9
+ @doc["enamel"] = "Strong"
10
+ @doc["enamel"].should == "Strong"
11
+ end
12
+ it "[]= should convert to string" do
13
+ @doc["enamel"].should == nil
14
+ @doc[:enamel] = "Strong"
15
+ @doc["enamel"].should == "Strong"
16
+ end
17
+ it "should read as a string" do
18
+ @doc[:enamel] = "Strong"
19
+ @doc[:enamel].should == "Strong"
20
+ end
21
+ end
22
+
23
+ describe CouchRest::Document, "new" do
24
+ before(:each) do
25
+ @doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
26
+ end
27
+ it "should create itself from a Hash" do
28
+ @doc["key"].should == [1,2,3]
29
+ @doc["more"].should == "values"
30
+ end
31
+ it "should not have rev and id" do
32
+ @doc.rev.should be_nil
33
+ @doc.id.should be_nil
34
+ end
35
+ it "should freak out when saving without a database" do
36
+ lambda{@doc.save}.should raise_error(ArgumentError)
37
+ end
38
+ end
39
+
40
+ # move to database spec
41
+ describe CouchRest::Document, "saving using a database" do
42
+ before(:all) do
43
+ @doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
44
+ @db = reset_test_db!
45
+ @resp = @db.save(@doc)
46
+ end
47
+ it "should apply the database" do
48
+ @doc.database.should == @db
49
+ end
50
+ it "should get id and rev" do
51
+ @doc.id.should == @resp["id"]
52
+ @doc.rev.should == @resp["rev"]
53
+ end
54
+ end
55
+
56
+ describe CouchRest::Document, "bulk saving" do
57
+ before :all do
58
+ @db = reset_test_db!
59
+ end
60
+
61
+ it "should use the document bulk save cache" do
62
+ doc = CouchRest::Document.new({"_id" => "bulkdoc", "val" => 3})
63
+ doc.database = @db
64
+ doc.save(true)
65
+ lambda { doc.database.get(doc["_id"]) }.should raise_error(RestClient::ResourceNotFound)
66
+ doc.database.bulk_save
67
+ doc.database.get(doc["_id"])["val"].should == doc["val"]
68
+ end
69
+ end
70
+
71
+ describe "getting from a database" do
72
+ before(:all) do
73
+ @db = reset_test_db!
74
+ @resp = @db.save({
75
+ "key" => "value"
76
+ })
77
+ @doc = @db.get @resp['id']
78
+ end
79
+ it "should return a document" do
80
+ @doc.should be_an_instance_of(CouchRest::Document)
81
+ end
82
+ it "should have a database" do
83
+ @doc.database.should == @db
84
+ end
85
+ it "should be saveable and resavable" do
86
+ @doc["more"] = "keys"
87
+ @doc.save
88
+ @db.get(@resp['id'])["more"].should == "keys"
89
+ @doc["more"] = "these keys"
90
+ @doc.save
91
+ @db.get(@resp['id'])["more"].should == "these keys"
92
+ end
93
+ end
94
+
95
+ describe "destroying a document from a db" do
96
+ before(:all) do
97
+ @db = reset_test_db!
98
+ @resp = @db.save({
99
+ "key" => "value"
100
+ })
101
+ @doc = @db.get @resp['id']
102
+ end
103
+ it "should make it disappear" do
104
+ @doc.destroy
105
+ lambda{@db.get @resp['id']}.should raise_error
106
+ end
107
+ it "should error when there's no db" do
108
+ @doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
109
+ lambda{@doc.destroy}.should raise_error(ArgumentError)
110
+ end
111
+ end
112
+
113
+
114
+ describe "destroying a document from a db using bulk save" do
115
+ before(:all) do
116
+ @db = reset_test_db!
117
+ @resp = @db.save({
118
+ "key" => "value"
119
+ })
120
+ @doc = @db.get @resp['id']
121
+ end
122
+ it "should defer actual deletion" do
123
+ @doc.destroy(true)
124
+ @doc['_id'].should == nil
125
+ @doc['_rev'].should == nil
126
+ lambda{@db.get @resp['id']}.should_not raise_error
127
+ @db.bulk_save
128
+ lambda{@db.get @resp['id']}.should raise_error
129
+ end
130
+ end