bulk save

This commit is contained in:
Chris Anderson 2008-03-19 16:38:07 -07:00
parent 43f51c5490
commit 7f14458694
2 changed files with 51 additions and 0 deletions

View file

@ -32,6 +32,10 @@ class CouchRest
end end
end end
def bulk_save docs
CouchRest.post "#{@root}/_bulk_docs", docs
end
def delete doc def delete doc
slug = CGI.escape(doc['_id']) slug = CGI.escape(doc['_id'])
CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}" CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}"

View file

@ -31,6 +31,53 @@ describe CouchRest::Database do
end end
end end
describe "POST (adding bulk documents)" do
it "should add them without ids" do
rs = @db.bulk_save([
{"wild" => "and random"},
{"mild" => "yet local"},
{"another" => ["set","of","keys"]}
])
rs['results'].each do |r|
@db.get(r['id'])
end
end
it "should add them with uniq ids" do
rs = @db.bulk_save([
{"_id" => "oneB", "wild" => "and random"},
{"_id" => "twoB", "mild" => "yet local"},
{"another" => ["set","of","keys"]}
])
rs['results'].each do |r|
@db.get(r['id'])
end
end
it "should with one bad id should save the ones it can" do
@r = @db.save({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"})
rs = @db.bulk_save([
{"_id" => "oneB", "wild" => "and random"},
{"_id" => "twoB", "mild" => "yet local"},
{"another" => ["set","of","keys"]}
])
# should save the new document
newid = @db.documents['rows'].reject do |d|
['oneB','twoB'].include?(d['id'])
end.first['id']
newdoc = @db.get(newid)
newdoc["another"][2].should == 'keys'
# should save the ok id
@db.get('twoB')['mild'].should == "yet local"
# should confict on the duplicate id
oneB = @db.get('oneB')
oneB['wild'].should be_nil
oneB['lemons'].should == 'from texas'
end
end
describe "POST (new document without an id)" do describe "POST (new document without an id)" do
it "should start without the document" do it "should start without the document" do
@db.documents.should_not include({'_id' => 'my-doc'}) @db.documents.should_not include({'_id' => 'my-doc'})