couchrest 0.12.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +176 -0
- data/README.md +69 -0
- data/Rakefile +103 -0
- data/THANKS.md +18 -0
- data/bin/couchdir +20 -0
- data/examples/model/example.rb +138 -0
- data/examples/word_count/markov +38 -0
- data/examples/word_count/views/books/chunked-map.js +3 -0
- data/examples/word_count/views/books/united-map.js +1 -0
- data/examples/word_count/views/markov/chain-map.js +6 -0
- data/examples/word_count/views/markov/chain-reduce.js +7 -0
- data/examples/word_count/views/word_count/count-map.js +6 -0
- data/examples/word_count/views/word_count/count-reduce.js +3 -0
- data/examples/word_count/word_count.rb +67 -0
- data/examples/word_count/word_count_query.rb +39 -0
- data/lib/couchrest/commands/generate.rb +71 -0
- data/lib/couchrest/commands/push.rb +103 -0
- data/lib/couchrest/core/database.rb +239 -0
- data/lib/couchrest/core/design.rb +89 -0
- data/lib/couchrest/core/document.rb +63 -0
- data/lib/couchrest/core/model.rb +607 -0
- data/lib/couchrest/core/server.rb +51 -0
- data/lib/couchrest/core/view.rb +4 -0
- data/lib/couchrest/helper/pager.rb +103 -0
- data/lib/couchrest/helper/streamer.rb +44 -0
- data/lib/couchrest/monkeypatches.rb +24 -0
- data/lib/couchrest.rb +140 -0
- data/spec/couchrest/core/couchrest_spec.rb +201 -0
- data/spec/couchrest/core/database_spec.rb +630 -0
- data/spec/couchrest/core/design_spec.rb +131 -0
- data/spec/couchrest/core/document_spec.rb +130 -0
- data/spec/couchrest/core/model_spec.rb +855 -0
- data/spec/couchrest/helpers/pager_spec.rb +122 -0
- data/spec/couchrest/helpers/streamer_spec.rb +23 -0
- data/spec/fixtures/attachments/README +3 -0
- data/spec/fixtures/attachments/couchdb.png +0 -0
- data/spec/fixtures/attachments/test.html +11 -0
- data/spec/fixtures/views/lib.js +3 -0
- data/spec/fixtures/views/test_view/lib.js +3 -0
- data/spec/fixtures/views/test_view/only-map.js +4 -0
- data/spec/fixtures/views/test_view/test-map.js +3 -0
- data/spec/fixtures/views/test_view/test-reduce.js +3 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +20 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +156 -0
@@ -0,0 +1,630 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
|
3
|
+
describe CouchRest::Database do
|
4
|
+
before(:each) do
|
5
|
+
@cr = CouchRest.new(COUCHHOST)
|
6
|
+
@db = @cr.database(TESTDB)
|
7
|
+
@db.delete! rescue nil
|
8
|
+
@db = @cr.create_db(TESTDB) rescue nil
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "map query with _temp_view in Javascript" do
|
12
|
+
before(:each) do
|
13
|
+
@db.bulk_save([
|
14
|
+
{"wild" => "and random"},
|
15
|
+
{"mild" => "yet local"},
|
16
|
+
{"another" => ["set","of","keys"]}
|
17
|
+
])
|
18
|
+
@temp_view = {:map => "function(doc){for(var w in doc){ if(!w.match(/^_/))emit(w,doc[w])}}"}
|
19
|
+
end
|
20
|
+
it "should return the result of the temporary function" do
|
21
|
+
rs = @db.temp_view(@temp_view)
|
22
|
+
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
|
23
|
+
end
|
24
|
+
it "should work with a range" do
|
25
|
+
rs = @db.temp_view(@temp_view, :startkey => "b", :endkey => "z")
|
26
|
+
rs['rows'].length.should == 2
|
27
|
+
end
|
28
|
+
it "should work with a key" do
|
29
|
+
rs = @db.temp_view(@temp_view, :key => "wild")
|
30
|
+
rs['rows'].length.should == 1
|
31
|
+
end
|
32
|
+
it "should work with a limit" do
|
33
|
+
rs = @db.temp_view(@temp_view, :limit => 1)
|
34
|
+
rs['rows'].length.should == 1
|
35
|
+
end
|
36
|
+
it "should work with multi-keys" do
|
37
|
+
rs = @db.temp_view(@temp_view, :keys => ["another", "wild"])
|
38
|
+
rs['rows'].length.should == 2
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "map/reduce query with _temp_view in Javascript" do
|
43
|
+
before(:each) do
|
44
|
+
@db.bulk_save([
|
45
|
+
{"beverage" => "beer", :count => 4},
|
46
|
+
{"beverage" => "beer", :count => 2},
|
47
|
+
{"beverage" => "tea", :count => 3}
|
48
|
+
])
|
49
|
+
end
|
50
|
+
it "should return the result of the temporary function" do
|
51
|
+
rs = @db.temp_view(:map => "function(doc){emit(doc.beverage, doc.count)}", :reduce => "function(beverage,counts){return sum(counts)}")
|
52
|
+
# rs.should == 'x'
|
53
|
+
rs['rows'][0]['value'].should == 9
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "saving a view" do
|
58
|
+
before(:each) do
|
59
|
+
@view = {'test' => {'map' => 'function(doc) {
|
60
|
+
if (doc.word && !/\W/.test(doc.word)) {
|
61
|
+
emit(doc.word,null);
|
62
|
+
}
|
63
|
+
}'}}
|
64
|
+
@db.save({
|
65
|
+
"_id" => "_design/test",
|
66
|
+
:views => @view
|
67
|
+
})
|
68
|
+
end
|
69
|
+
it "should work properly" do
|
70
|
+
@db.bulk_save([
|
71
|
+
{"word" => "once"},
|
72
|
+
{"word" => "and again"}
|
73
|
+
])
|
74
|
+
@db.view('test/test')['total_rows'].should == 1
|
75
|
+
end
|
76
|
+
it "should round trip" do
|
77
|
+
@db.get("_design/test")['views'].should == @view
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "select from an existing view" do
|
82
|
+
before(:each) do
|
83
|
+
r = @db.save({
|
84
|
+
"_id" => "_design/first",
|
85
|
+
:views => {
|
86
|
+
:test => {
|
87
|
+
:map => "function(doc){for(var w in doc){ if(!w.match(/^_/))emit(w,doc[w])}}"
|
88
|
+
}
|
89
|
+
}
|
90
|
+
})
|
91
|
+
@db.bulk_save([
|
92
|
+
{"wild" => "and random"},
|
93
|
+
{"mild" => "yet local"},
|
94
|
+
{"another" => ["set","of","keys"]}
|
95
|
+
])
|
96
|
+
end
|
97
|
+
it "should have the view" do
|
98
|
+
@db.get('_design/first')['views']['test']['map'].should include("for(var w in doc)")
|
99
|
+
end
|
100
|
+
it "should list from the view" do
|
101
|
+
rs = @db.view('first/test')
|
102
|
+
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
|
103
|
+
end
|
104
|
+
it "should work with a range" do
|
105
|
+
rs = @db.view('first/test', :startkey => "b", :endkey => "z")
|
106
|
+
rs['rows'].length.should == 2
|
107
|
+
end
|
108
|
+
it "should work with a key" do
|
109
|
+
rs = @db.view('first/test', :key => "wild")
|
110
|
+
rs['rows'].length.should == 1
|
111
|
+
end
|
112
|
+
it "should work with a limit" do
|
113
|
+
rs = @db.view('first/test', :limit => 1)
|
114
|
+
rs['rows'].length.should == 1
|
115
|
+
end
|
116
|
+
it "should work with multi-keys" do
|
117
|
+
rs = @db.view('first/test', :keys => ["another", "wild"])
|
118
|
+
rs['rows'].length.should == 2
|
119
|
+
end
|
120
|
+
it "should accept a block" do
|
121
|
+
rows = []
|
122
|
+
rs = @db.view('first/test', :include_docs => true) do |row|
|
123
|
+
rows << row
|
124
|
+
end
|
125
|
+
rows.length.should == 4
|
126
|
+
rs["total_rows"].should == 3
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "GET (document by id) when the doc exists" do
|
131
|
+
before(:each) do
|
132
|
+
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
133
|
+
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
134
|
+
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
135
|
+
end
|
136
|
+
it "should get the document" do
|
137
|
+
doc = @db.get(@r['id'])
|
138
|
+
doc['lemons'].should == 'from texas'
|
139
|
+
end
|
140
|
+
it "should work with a funky id" do
|
141
|
+
@db.get(@docid)['will-exist'].should == 'here'
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "POST (adding bulk documents)" do
|
146
|
+
it "should add them without ids" do
|
147
|
+
rs = @db.bulk_save([
|
148
|
+
{"wild" => "and random"},
|
149
|
+
{"mild" => "yet local"},
|
150
|
+
{"another" => ["set","of","keys"]}
|
151
|
+
])
|
152
|
+
rs['new_revs'].each do |r|
|
153
|
+
@db.get(r['id'])
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should use uuids when ids aren't provided" do
|
158
|
+
@db.server.stub!(:next_uuid).and_return('asdf6sgadkfhgsdfusdf')
|
159
|
+
|
160
|
+
docs = [{'key' => 'value'}, {'_id' => 'totally-uniq'}]
|
161
|
+
id_docs = [{'key' => 'value', '_id' => 'asdf6sgadkfhgsdfusdf'}, {'_id' => 'totally-uniq'}]
|
162
|
+
CouchRest.should_receive(:post).with("http://127.0.0.1:5984/couchrest-test/_bulk_docs", {:docs => id_docs})
|
163
|
+
|
164
|
+
@db.bulk_save(docs)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should add them with uniq ids" do
|
168
|
+
rs = @db.bulk_save([
|
169
|
+
{"_id" => "oneB", "wild" => "and random"},
|
170
|
+
{"_id" => "twoB", "mild" => "yet local"},
|
171
|
+
{"another" => ["set","of","keys"]}
|
172
|
+
])
|
173
|
+
rs['new_revs'].each do |r|
|
174
|
+
@db.get(r['id'])
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
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"})
|
180
|
+
|
181
|
+
lambda do
|
182
|
+
rs = @db.bulk_save([
|
183
|
+
{"_id" => "oneB", "wild" => "and random"},
|
184
|
+
{"_id" => "twoB", "mild" => "yet local"},
|
185
|
+
{"another" => ["set","of","keys"]}
|
186
|
+
])
|
187
|
+
end.should raise_error(RestClient::RequestFailed)
|
188
|
+
|
189
|
+
lambda do
|
190
|
+
@db.get('twoB')
|
191
|
+
end.should raise_error(RestClient::ResourceNotFound)
|
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
|
+
|
203
|
+
it "should raise an error that is useful for recovery" do
|
204
|
+
@r = @db.save({"_id" => "taken", "field" => "stuff"})
|
205
|
+
begin
|
206
|
+
rs = @db.bulk_save([
|
207
|
+
{"_id" => "taken", "wild" => "and random"},
|
208
|
+
{"_id" => "free", "mild" => "yet local"},
|
209
|
+
{"another" => ["set","of","keys"]}
|
210
|
+
])
|
211
|
+
rescue RestClient::RequestFailed => e
|
212
|
+
# soon CouchDB will provide _which_ docs conflicted
|
213
|
+
JSON.parse(e.response.body)['error'].should == 'conflict'
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
describe "new document without an id" do
|
219
|
+
it "should start empty" do
|
220
|
+
@db.documents["total_rows"].should == 0
|
221
|
+
end
|
222
|
+
it "should create the document and return the id" do
|
223
|
+
r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
224
|
+
r2 = @db.get(r['id'])
|
225
|
+
r2["lemons"].should == "from texas"
|
226
|
+
end
|
227
|
+
it "should use PUT with UUIDs" do
|
228
|
+
CouchRest.should_receive(:put).and_return({"ok" => true, "id" => "100", "rev" => "55"})
|
229
|
+
r = @db.save({'just' => ['another document']})
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "PUT attachment from file" do
|
235
|
+
before(:each) do
|
236
|
+
filename = FIXTURE_PATH + '/attachments/couchdb.png'
|
237
|
+
@file = File.open(filename)
|
238
|
+
end
|
239
|
+
after(:each) do
|
240
|
+
@file.close
|
241
|
+
end
|
242
|
+
it "should save the attachment to a new doc" do
|
243
|
+
r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
|
244
|
+
r['ok'].should == true
|
245
|
+
attachment = @db.fetch_attachment("attach-this","couchdb.png")
|
246
|
+
attachment.should == image
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
describe "PUT document with attachment" do
|
251
|
+
before(:each) do
|
252
|
+
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
253
|
+
@doc = {
|
254
|
+
"_id" => "mydocwithattachment",
|
255
|
+
"field" => ["some value"],
|
256
|
+
"_attachments" => {
|
257
|
+
"test.html" => {
|
258
|
+
"type" => "text/html",
|
259
|
+
"data" => @attach
|
260
|
+
}
|
261
|
+
}
|
262
|
+
}
|
263
|
+
@db.save(@doc)
|
264
|
+
end
|
265
|
+
it "should save and be indicated" do
|
266
|
+
doc = @db.get("mydocwithattachment")
|
267
|
+
doc['_attachments']['test.html']['length'].should == @attach.length
|
268
|
+
end
|
269
|
+
it "should be there" do
|
270
|
+
attachment = @db.fetch_attachment("mydocwithattachment","test.html")
|
271
|
+
attachment.should == @attach
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
describe "PUT document with attachment stub" do
|
276
|
+
before(:each) do
|
277
|
+
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
278
|
+
doc = {
|
279
|
+
'_id' => 'mydocwithattachment',
|
280
|
+
'field' => ['some_value'],
|
281
|
+
'_attachments' => {
|
282
|
+
'test.html' => {
|
283
|
+
'type' => 'text/html', 'data' => @attach
|
284
|
+
}
|
285
|
+
}
|
286
|
+
}
|
287
|
+
@db.save(doc)
|
288
|
+
doc = @db.get('mydocwithattachment')
|
289
|
+
doc['field'] << 'another value'
|
290
|
+
@db.save(doc)
|
291
|
+
end
|
292
|
+
|
293
|
+
it 'should be there' do
|
294
|
+
attachment = @db.fetch_attachment('mydocwithattachment', 'test.html')
|
295
|
+
attachment.should == @attach
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
describe "PUT document with multiple attachments" do
|
300
|
+
before(:each) do
|
301
|
+
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
302
|
+
@attach2 = "<html><head><title>Other Doc</title></head><body><p>Has more words.</p></body></html>"
|
303
|
+
@doc = {
|
304
|
+
"_id" => "mydocwithattachment",
|
305
|
+
"field" => ["some value"],
|
306
|
+
"_attachments" => {
|
307
|
+
"test.html" => {
|
308
|
+
"type" => "text/html",
|
309
|
+
"data" => @attach
|
310
|
+
},
|
311
|
+
"other.html" => {
|
312
|
+
"type" => "text/html",
|
313
|
+
"data" => @attach2
|
314
|
+
}
|
315
|
+
}
|
316
|
+
}
|
317
|
+
@db.save(@doc)
|
318
|
+
end
|
319
|
+
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
|
323
|
+
end
|
324
|
+
it "should be there" do
|
325
|
+
attachment = @db.fetch_attachment("mydocwithattachment","test.html")
|
326
|
+
attachment.should == @attach
|
327
|
+
end
|
328
|
+
it "should be there" do
|
329
|
+
attachment = @db.fetch_attachment("mydocwithattachment","other.html")
|
330
|
+
attachment.should == @attach2
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
describe "POST document with attachment (with funky name)" do
|
335
|
+
before(:each) do
|
336
|
+
@attach = "<html><head><title>My Funky Doc</title></head><body><p>Has words.</p></body></html>"
|
337
|
+
@doc = {
|
338
|
+
"field" => ["some other value"],
|
339
|
+
"_attachments" => {
|
340
|
+
"http://example.com/stuff.cgi?things=and%20stuff" => {
|
341
|
+
"type" => "text/html",
|
342
|
+
"data" => @attach
|
343
|
+
}
|
344
|
+
}
|
345
|
+
}
|
346
|
+
@docid = @db.save(@doc)['id']
|
347
|
+
end
|
348
|
+
it "should save and be indicated" do
|
349
|
+
doc = @db.get(@docid)
|
350
|
+
doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length
|
351
|
+
end
|
352
|
+
it "should be there" do
|
353
|
+
attachment = @db.fetch_attachment(@docid,"http://example.com/stuff.cgi?things=and%20stuff")
|
354
|
+
attachment.should == @attach
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
describe "PUT (new document with url id)" do
|
359
|
+
it "should create the document" do
|
360
|
+
@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)
|
363
|
+
@db.get(@docid)['will-exist'].should == 'here'
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
describe "PUT (new document with id)" do
|
368
|
+
it "should start without the document" do
|
369
|
+
# r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
370
|
+
@db.documents['rows'].each do |doc|
|
371
|
+
doc['id'].should_not == 'my-doc'
|
372
|
+
end
|
373
|
+
# should_not include({'_id' => 'my-doc'})
|
374
|
+
# this needs to be a loop over docs on content with the post
|
375
|
+
# or instead make it return something with a fancy <=> method
|
376
|
+
end
|
377
|
+
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)
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
describe "PUT (existing document with rev)" do
|
384
|
+
before(:each) do
|
385
|
+
@db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
|
386
|
+
@doc = @db.get('my-doc')
|
387
|
+
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
388
|
+
@db.save({'_id' => @docid, 'now' => 'save'})
|
389
|
+
end
|
390
|
+
it "should start with the document" do
|
391
|
+
@doc['will-exist'].should == 'here'
|
392
|
+
@db.get(@docid)['now'].should == 'save'
|
393
|
+
end
|
394
|
+
it "should save with url id" do
|
395
|
+
doc = @db.get(@docid)
|
396
|
+
doc['yaml'] = ['json', 'word.']
|
397
|
+
@db.save doc
|
398
|
+
@db.get(@docid)['yaml'].should == ['json', 'word.']
|
399
|
+
end
|
400
|
+
it "should fail to resave without the rev" do
|
401
|
+
@doc['them-keys'] = 'huge'
|
402
|
+
@doc['_rev'] = 'wrong'
|
403
|
+
# @db.save(@doc)
|
404
|
+
lambda {@db.save(@doc)}.should raise_error
|
405
|
+
end
|
406
|
+
it "should update the document" do
|
407
|
+
@doc['them-keys'] = 'huge'
|
408
|
+
@db.save(@doc)
|
409
|
+
now = @db.get('my-doc')
|
410
|
+
now['them-keys'].should == 'huge'
|
411
|
+
end
|
412
|
+
end
|
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
|
+
|
455
|
+
describe "DELETE existing document" do
|
456
|
+
before(:each) do
|
457
|
+
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
458
|
+
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
459
|
+
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
460
|
+
end
|
461
|
+
it "should work" do
|
462
|
+
doc = @db.get(@r['id'])
|
463
|
+
doc['and'].should == 'spain'
|
464
|
+
@db.delete doc
|
465
|
+
lambda{@db.get @r['id']}.should raise_error
|
466
|
+
end
|
467
|
+
it "should work with uri id" do
|
468
|
+
doc = @db.get(@docid)
|
469
|
+
@db.delete doc
|
470
|
+
lambda{@db.get @docid}.should raise_error
|
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
|
522
|
+
end
|
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
|
+
|
568
|
+
it "should list documents" do
|
569
|
+
5.times do
|
570
|
+
@db.save({'another' => 'doc', 'will-exist' => 'anywhere'})
|
571
|
+
end
|
572
|
+
ds = @db.documents
|
573
|
+
ds['rows'].should be_an_instance_of(Array)
|
574
|
+
ds['rows'][0]['id'].should_not be_nil
|
575
|
+
ds['total_rows'].should == 5
|
576
|
+
end
|
577
|
+
|
578
|
+
describe "documents / _all_docs" do
|
579
|
+
before(:each) do
|
580
|
+
9.times do |i|
|
581
|
+
@db.save({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
|
582
|
+
end
|
583
|
+
end
|
584
|
+
it "should list documents with keys and such" do
|
585
|
+
ds = @db.documents
|
586
|
+
ds['rows'].should be_an_instance_of(Array)
|
587
|
+
ds['rows'][0]['id'].should == "doc0"
|
588
|
+
ds['total_rows'].should == 9
|
589
|
+
end
|
590
|
+
it "should take query params" do
|
591
|
+
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3')
|
592
|
+
ds['rows'].length.should == 4
|
593
|
+
ds = @db.documents(:key => 'doc0')
|
594
|
+
ds['rows'].length.should == 1
|
595
|
+
end
|
596
|
+
it "should work with multi-key" do
|
597
|
+
rs = @db.documents :keys => ["doc0", "doc7"]
|
598
|
+
rs['rows'].length.should == 2
|
599
|
+
end
|
600
|
+
it "should work with include_docs" do
|
601
|
+
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3', :include_docs => true)
|
602
|
+
ds['rows'][0]['doc']['another'].should == "doc"
|
603
|
+
end
|
604
|
+
end
|
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
|
+
|
616
|
+
describe "deleting a database" do
|
617
|
+
it "should start with the test database" do
|
618
|
+
@cr.databases.should include('couchrest-test')
|
619
|
+
end
|
620
|
+
it "should delete the database" do
|
621
|
+
db = @cr.database('couchrest-test')
|
622
|
+
# r =
|
623
|
+
db.delete!
|
624
|
+
# r['ok'].should == true
|
625
|
+
@cr.databases.should_not include('couchrest-test')
|
626
|
+
end
|
627
|
+
end
|
628
|
+
|
629
|
+
|
630
|
+
end
|