2008-10-01 01:21:28 +02:00
|
|
|
require File.dirname(__FILE__) + '/../../spec_helper'
|
2008-03-18 19:37:10 +01:00
|
|
|
|
2008-03-19 07:11:44 +01:00
|
|
|
describe CouchRest::Database do
|
2008-03-18 19:37:10 +01:00
|
|
|
before(:each) do
|
2008-07-05 01:56:09 +02:00
|
|
|
@cr = CouchRest.new(COUCHHOST)
|
2008-07-19 21:25:02 +02:00
|
|
|
@db = @cr.database(TESTDB)
|
|
|
|
@db.delete! rescue nil
|
|
|
|
@db = @cr.create_db(TESTDB) rescue nil
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
2009-03-26 10:40:45 +01:00
|
|
|
|
|
|
|
describe "database name including slash" do
|
|
|
|
it "should escape the name in the URI" do
|
|
|
|
db = @cr.database("foo/bar")
|
|
|
|
db.name.should == "foo/bar"
|
|
|
|
db.uri.should == "#{COUCHHOST}/foo%2Fbar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-17 22:41:16 +02:00
|
|
|
describe "map query with _temp_view in Javascript" do
|
2008-03-20 02:10:16 +01:00
|
|
|
before(:each) do
|
|
|
|
@db.bulk_save([
|
|
|
|
{"wild" => "and random"},
|
|
|
|
{"mild" => "yet local"},
|
|
|
|
{"another" => ["set","of","keys"]}
|
|
|
|
])
|
2008-05-23 06:57:21 +02:00
|
|
|
@temp_view = {:map => "function(doc){for(var w in doc){ if(!w.match(/^_/))emit(w,doc[w])}}"}
|
2008-03-20 02:10:16 +01:00
|
|
|
end
|
|
|
|
it "should return the result of the temporary function" do
|
2008-05-23 06:57:21 +02:00
|
|
|
rs = @db.temp_view(@temp_view)
|
2008-03-20 02:10:16 +01:00
|
|
|
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
|
2008-05-23 06:57:21 +02:00
|
|
|
end
|
|
|
|
it "should work with a range" do
|
2008-10-08 21:19:28 +02:00
|
|
|
rs = @db.temp_view(@temp_view, :startkey => "b", :endkey => "z")
|
2008-05-23 06:57:21 +02:00
|
|
|
rs['rows'].length.should == 2
|
|
|
|
end
|
|
|
|
it "should work with a key" do
|
2008-10-08 21:19:28 +02:00
|
|
|
rs = @db.temp_view(@temp_view, :key => "wild")
|
2008-05-23 06:57:21 +02:00
|
|
|
rs['rows'].length.should == 1
|
|
|
|
end
|
2009-01-05 06:00:36 +01:00
|
|
|
it "should work with a limit" do
|
|
|
|
rs = @db.temp_view(@temp_view, :limit => 1)
|
2008-05-23 06:57:21 +02:00
|
|
|
rs['rows'].length.should == 1
|
2008-03-20 02:10:16 +01:00
|
|
|
end
|
2008-10-08 21:19:28 +02:00
|
|
|
it "should work with multi-keys" do
|
|
|
|
rs = @db.temp_view(@temp_view, :keys => ["another", "wild"])
|
|
|
|
rs['rows'].length.should == 2
|
|
|
|
end
|
2008-03-20 02:10:16 +01:00
|
|
|
end
|
|
|
|
|
2008-05-17 22:41:16 +02:00
|
|
|
describe "map/reduce query with _temp_view in Javascript" do
|
|
|
|
before(:each) do
|
|
|
|
@db.bulk_save([
|
2009-01-05 08:00:59 +01:00
|
|
|
{"beverage" => "beer", :count => 4},
|
|
|
|
{"beverage" => "beer", :count => 2},
|
|
|
|
{"beverage" => "tea", :count => 3}
|
2008-05-17 22:41:16 +02:00
|
|
|
])
|
|
|
|
end
|
|
|
|
it "should return the result of the temporary function" do
|
2008-05-21 23:58:37 +02:00
|
|
|
rs = @db.temp_view(:map => "function(doc){emit(doc.beverage, doc.count)}", :reduce => "function(beverage,counts){return sum(counts)}")
|
2008-06-01 19:23:24 +02:00
|
|
|
# rs.should == 'x'
|
|
|
|
rs['rows'][0]['value'].should == 9
|
2008-05-17 22:41:16 +02:00
|
|
|
end
|
|
|
|
end
|
2008-05-21 23:58:37 +02:00
|
|
|
|
2008-05-25 20:49:26 +02:00
|
|
|
describe "saving a view" do
|
|
|
|
before(:each) do
|
|
|
|
@view = {'test' => {'map' => 'function(doc) {
|
|
|
|
if (doc.word && !/\W/.test(doc.word)) {
|
|
|
|
emit(doc.word,null);
|
|
|
|
}
|
|
|
|
}'}}
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({
|
2008-05-25 20:49:26 +02:00
|
|
|
"_id" => "_design/test",
|
|
|
|
:views => @view
|
|
|
|
})
|
|
|
|
end
|
|
|
|
it "should work properly" do
|
|
|
|
@db.bulk_save([
|
|
|
|
{"word" => "once"},
|
|
|
|
{"word" => "and again"}
|
|
|
|
])
|
|
|
|
@db.view('test/test')['total_rows'].should == 1
|
|
|
|
end
|
|
|
|
it "should round trip" do
|
|
|
|
@db.get("_design/test")['views'].should == @view
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-20 06:38:01 +01:00
|
|
|
describe "select from an existing view" do
|
|
|
|
before(:each) do
|
2009-01-29 02:36:36 +01:00
|
|
|
r = @db.save_doc({
|
2008-05-21 20:08:42 +02:00
|
|
|
"_id" => "_design/first",
|
|
|
|
:views => {
|
|
|
|
:test => {
|
|
|
|
:map => "function(doc){for(var w in doc){ if(!w.match(/^_/))emit(w,doc[w])}}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2008-03-20 06:38:01 +01:00
|
|
|
@db.bulk_save([
|
|
|
|
{"wild" => "and random"},
|
|
|
|
{"mild" => "yet local"},
|
|
|
|
{"another" => ["set","of","keys"]}
|
|
|
|
])
|
|
|
|
end
|
|
|
|
it "should have the view" do
|
2008-05-21 20:08:42 +02:00
|
|
|
@db.get('_design/first')['views']['test']['map'].should include("for(var w in doc)")
|
2008-03-20 06:38:01 +01:00
|
|
|
end
|
|
|
|
it "should list from the view" do
|
|
|
|
rs = @db.view('first/test')
|
|
|
|
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
|
|
|
|
end
|
2008-05-23 06:41:52 +02:00
|
|
|
it "should work with a range" do
|
2008-10-08 21:19:28 +02:00
|
|
|
rs = @db.view('first/test', :startkey => "b", :endkey => "z")
|
2008-05-23 06:41:52 +02:00
|
|
|
rs['rows'].length.should == 2
|
|
|
|
end
|
|
|
|
it "should work with a key" do
|
2008-10-08 21:19:28 +02:00
|
|
|
rs = @db.view('first/test', :key => "wild")
|
2008-05-23 06:41:52 +02:00
|
|
|
rs['rows'].length.should == 1
|
|
|
|
end
|
2009-01-05 06:00:36 +01:00
|
|
|
it "should work with a limit" do
|
|
|
|
rs = @db.view('first/test', :limit => 1)
|
2008-05-23 06:41:52 +02:00
|
|
|
rs['rows'].length.should == 1
|
|
|
|
end
|
2008-10-08 21:19:28 +02:00
|
|
|
it "should work with multi-keys" do
|
|
|
|
rs = @db.view('first/test', :keys => ["another", "wild"])
|
|
|
|
rs['rows'].length.should == 2
|
|
|
|
end
|
2008-10-14 01:46:48 +02:00
|
|
|
it "should accept a block" do
|
|
|
|
rows = []
|
|
|
|
rs = @db.view('first/test', :include_docs => true) do |row|
|
|
|
|
rows << row
|
|
|
|
end
|
2008-10-14 10:07:48 +02:00
|
|
|
rows.length.should == 4
|
2008-10-15 00:08:17 +02:00
|
|
|
rs["total_rows"].should == 3
|
2008-10-14 01:46:48 +02:00
|
|
|
end
|
2008-03-20 06:38:01 +01:00
|
|
|
end
|
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
describe "GET (document by id) when the doc exists" do
|
|
|
|
before(:each) do
|
2009-01-29 02:36:36 +01:00
|
|
|
@r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
2008-03-19 22:33:41 +01:00
|
|
|
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
2008-03-19 18:17:25 +01:00
|
|
|
end
|
|
|
|
it "should get the document" do
|
|
|
|
doc = @db.get(@r['id'])
|
|
|
|
doc['lemons'].should == 'from texas'
|
|
|
|
end
|
2008-03-19 22:33:41 +01:00
|
|
|
it "should work with a funky id" do
|
|
|
|
@db.get(@docid)['will-exist'].should == 'here'
|
|
|
|
end
|
2008-03-19 18:17:25 +01:00
|
|
|
end
|
|
|
|
|
2008-03-20 00:38:07 +01:00
|
|
|
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"]}
|
|
|
|
])
|
2009-03-15 21:00:47 +01:00
|
|
|
rs.each do |r|
|
|
|
|
@db.get(r['id']).rev.should == r["rev"]
|
2008-03-20 00:38:07 +01:00
|
|
|
end
|
|
|
|
end
|
2008-09-05 23:09:17 +02:00
|
|
|
|
2008-09-07 22:51:26 +02:00
|
|
|
it "should use uuids when ids aren't provided" do
|
|
|
|
@db.server.stub!(:next_uuid).and_return('asdf6sgadkfhgsdfusdf')
|
|
|
|
|
|
|
|
docs = [{'key' => 'value'}, {'_id' => 'totally-uniq'}]
|
|
|
|
id_docs = [{'key' => 'value', '_id' => 'asdf6sgadkfhgsdfusdf'}, {'_id' => 'totally-uniq'}]
|
2008-12-14 12:05:02 +01:00
|
|
|
CouchRest.should_receive(:post).with("http://127.0.0.1:5984/couchrest-test/_bulk_docs", {:docs => id_docs})
|
2008-09-07 22:51:26 +02:00
|
|
|
|
|
|
|
@db.bulk_save(docs)
|
|
|
|
end
|
|
|
|
|
2008-03-20 00:38:07 +01:00
|
|
|
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"]}
|
|
|
|
])
|
2009-03-15 21:00:47 +01:00
|
|
|
rs.each do |r|
|
|
|
|
@db.get(r['id']).rev.should == r["rev"]
|
2008-03-20 00:38:07 +01:00
|
|
|
end
|
|
|
|
end
|
2008-12-15 06:17:35 +01:00
|
|
|
|
|
|
|
it "should empty the bulk save cache if no documents are given" do
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({"_id" => "bulk_cache_1", "val" => "test"}, true)
|
2008-12-15 06:17:35 +01:00
|
|
|
lambda do
|
|
|
|
@db.get('bulk_cache_1')
|
|
|
|
end.should raise_error(RestClient::ResourceNotFound)
|
|
|
|
@db.bulk_save
|
|
|
|
@db.get("bulk_cache_1")["val"].should == "test"
|
|
|
|
end
|
|
|
|
|
2008-09-05 23:09:17 +02:00
|
|
|
it "should raise an error that is useful for recovery" do
|
2009-01-29 02:36:36 +01:00
|
|
|
@r = @db.save_doc({"_id" => "taken", "field" => "stuff"})
|
2008-09-05 23:09:17 +02:00
|
|
|
begin
|
|
|
|
rs = @db.bulk_save([
|
|
|
|
{"_id" => "taken", "wild" => "and random"},
|
|
|
|
{"_id" => "free", "mild" => "yet local"},
|
|
|
|
{"another" => ["set","of","keys"]}
|
|
|
|
])
|
|
|
|
rescue RestClient::RequestFailed => e
|
|
|
|
# soon CouchDB will provide _which_ docs conflicted
|
|
|
|
JSON.parse(e.response.body)['error'].should == 'conflict'
|
|
|
|
end
|
|
|
|
end
|
2008-03-20 00:38:07 +01:00
|
|
|
end
|
|
|
|
|
2008-09-07 22:51:26 +02:00
|
|
|
describe "new document without an id" do
|
2008-05-25 20:49:26 +02:00
|
|
|
it "should start empty" do
|
|
|
|
@db.documents["total_rows"].should == 0
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
2008-03-19 18:17:25 +01:00
|
|
|
it "should create the document and return the id" do
|
2009-01-29 02:36:36 +01:00
|
|
|
r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
2008-05-25 20:49:26 +02:00
|
|
|
r2 = @db.get(r['id'])
|
|
|
|
r2["lemons"].should == "from texas"
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
2008-09-07 22:51:26 +02:00
|
|
|
it "should use PUT with UUIDs" do
|
2008-11-09 01:28:58 +01:00
|
|
|
CouchRest.should_receive(:put).and_return({"ok" => true, "id" => "100", "rev" => "55"})
|
2009-01-29 02:36:36 +01:00
|
|
|
r = @db.save_doc({'just' => ['another document']})
|
2008-09-07 22:51:26 +02:00
|
|
|
end
|
|
|
|
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
2009-02-02 09:11:38 +01:00
|
|
|
|
|
|
|
describe "fetch_attachment" do
|
|
|
|
before do
|
|
|
|
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
|
|
|
@doc = {
|
|
|
|
"_id" => "mydocwithattachment",
|
|
|
|
"field" => ["some value"],
|
|
|
|
"_attachments" => {
|
|
|
|
"test.html" => {
|
|
|
|
"type" => "text/html",
|
|
|
|
"data" => @attach
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-03 01:10:07 +01:00
|
|
|
@db.save_doc(@doc)
|
2009-02-02 09:11:38 +01:00
|
|
|
end
|
|
|
|
|
2009-02-25 09:22:11 +01:00
|
|
|
# Depreacated
|
|
|
|
# it "should get the attachment with the doc's _id" do
|
|
|
|
# @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
|
|
|
|
# end
|
|
|
|
|
2009-02-02 09:11:38 +01:00
|
|
|
it "should get the attachment with the doc itself" do
|
|
|
|
@db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-01 02:22:54 +02:00
|
|
|
describe "PUT attachment from file" do
|
|
|
|
before(:each) do
|
2008-10-15 00:08:17 +02:00
|
|
|
filename = FIXTURE_PATH + '/attachments/couchdb.png'
|
2008-10-01 02:22:54 +02:00
|
|
|
@file = File.open(filename)
|
|
|
|
end
|
|
|
|
after(:each) do
|
|
|
|
@file.close
|
|
|
|
end
|
|
|
|
it "should save the attachment to a new doc" do
|
|
|
|
r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
|
|
|
|
r['ok'].should == true
|
2009-02-25 09:22:11 +01:00
|
|
|
doc = @db.get("attach-this")
|
|
|
|
attachment = @db.fetch_attachment(doc,"couchdb.png")
|
2008-10-01 02:22:54 +02:00
|
|
|
attachment.should == image
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-07 17:32:51 +02:00
|
|
|
describe "PUT document with attachment" do
|
|
|
|
before(:each) do
|
|
|
|
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
2009-02-25 09:22:11 +01:00
|
|
|
doc = {
|
2008-06-07 17:32:51 +02:00
|
|
|
"_id" => "mydocwithattachment",
|
|
|
|
"field" => ["some value"],
|
|
|
|
"_attachments" => {
|
2008-06-07 18:00:41 +02:00
|
|
|
"test.html" => {
|
|
|
|
"type" => "text/html",
|
|
|
|
"data" => @attach
|
|
|
|
}
|
2008-06-07 17:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2009-02-25 09:22:11 +01:00
|
|
|
@db.save_doc(doc)
|
|
|
|
@doc = @db.get("mydocwithattachment")
|
2008-06-07 17:32:51 +02:00
|
|
|
end
|
|
|
|
it "should save and be indicated" do
|
2009-02-25 09:22:11 +01:00
|
|
|
@doc['_attachments']['test.html']['length'].should == @attach.length
|
2008-06-07 17:32:51 +02:00
|
|
|
end
|
|
|
|
it "should be there" do
|
2009-02-25 09:22:11 +01:00
|
|
|
attachment = @db.fetch_attachment(@doc,"test.html")
|
2008-06-07 17:32:51 +02:00
|
|
|
attachment.should == @attach
|
|
|
|
end
|
|
|
|
end
|
2008-06-12 17:40:52 +02:00
|
|
|
|
|
|
|
describe "PUT document with attachment stub" do
|
|
|
|
before(:each) do
|
|
|
|
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
|
|
|
doc = {
|
|
|
|
'_id' => 'mydocwithattachment',
|
|
|
|
'field' => ['some_value'],
|
|
|
|
'_attachments' => {
|
|
|
|
'test.html' => {
|
|
|
|
'type' => 'text/html', 'data' => @attach
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(doc)
|
2009-02-27 19:58:48 +01:00
|
|
|
doc['_rev'].should_not be_nil
|
2008-06-12 17:40:52 +02:00
|
|
|
doc['field'] << 'another value'
|
2009-02-27 19:58:48 +01:00
|
|
|
@db.save_doc(doc)["ok"].should be_true
|
2008-06-12 17:40:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be there' do
|
2009-02-25 09:22:11 +01:00
|
|
|
doc = @db.get('mydocwithattachment')
|
|
|
|
attachment = @db.fetch_attachment(doc, 'test.html')
|
|
|
|
Base64.decode64(attachment).should == @attach
|
2008-06-12 17:40:52 +02:00
|
|
|
end
|
|
|
|
end
|
2008-06-07 17:32:51 +02:00
|
|
|
|
2008-06-09 22:18:52 +02:00
|
|
|
describe "PUT document with multiple attachments" do
|
|
|
|
before(:each) do
|
|
|
|
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
|
|
|
@attach2 = "<html><head><title>Other Doc</title></head><body><p>Has more words.</p></body></html>"
|
|
|
|
@doc = {
|
|
|
|
"_id" => "mydocwithattachment",
|
|
|
|
"field" => ["some value"],
|
|
|
|
"_attachments" => {
|
|
|
|
"test.html" => {
|
|
|
|
"type" => "text/html",
|
|
|
|
"data" => @attach
|
|
|
|
},
|
|
|
|
"other.html" => {
|
|
|
|
"type" => "text/html",
|
|
|
|
"data" => @attach2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(@doc)
|
2009-02-25 09:22:11 +01:00
|
|
|
@doc = @db.get("mydocwithattachment")
|
2008-06-09 22:18:52 +02:00
|
|
|
end
|
|
|
|
it "should save and be indicated" do
|
2009-02-25 09:22:11 +01:00
|
|
|
@doc['_attachments']['test.html']['length'].should == @attach.length
|
|
|
|
@doc['_attachments']['other.html']['length'].should == @attach2.length
|
2008-06-09 22:18:52 +02:00
|
|
|
end
|
|
|
|
it "should be there" do
|
2009-02-25 09:22:11 +01:00
|
|
|
attachment = @db.fetch_attachment(@doc,"test.html")
|
2008-06-09 22:18:52 +02:00
|
|
|
attachment.should == @attach
|
|
|
|
end
|
|
|
|
it "should be there" do
|
2009-02-25 09:22:11 +01:00
|
|
|
attachment = @db.fetch_attachment(@doc,"other.html")
|
2008-06-09 22:18:52 +02:00
|
|
|
attachment.should == @attach2
|
|
|
|
end
|
|
|
|
end
|
2009-02-02 09:11:38 +01:00
|
|
|
|
|
|
|
describe "DELETE an attachment directly from the database" do
|
|
|
|
before(:each) do
|
|
|
|
doc = {
|
|
|
|
'_id' => 'mydocwithattachment',
|
|
|
|
'_attachments' => {
|
|
|
|
'test.html' => {
|
|
|
|
'type' => 'text/html',
|
|
|
|
'data' => "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-03 01:10:07 +01:00
|
|
|
@db.save_doc(doc)
|
2009-02-02 09:11:38 +01:00
|
|
|
@doc = @db.get('mydocwithattachment')
|
|
|
|
end
|
|
|
|
it "should delete the attachment" do
|
2009-02-25 09:22:11 +01:00
|
|
|
lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error
|
2009-02-02 09:11:38 +01:00
|
|
|
@db.delete_attachment(@doc, "test.html")
|
2009-02-25 09:22:11 +01:00
|
|
|
lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound)
|
2009-02-02 09:11:38 +01:00
|
|
|
end
|
|
|
|
end
|
2008-06-09 22:18:52 +02:00
|
|
|
|
2008-06-07 17:32:51 +02:00
|
|
|
describe "POST document with attachment (with funky name)" do
|
|
|
|
before(:each) do
|
|
|
|
@attach = "<html><head><title>My Funky Doc</title></head><body><p>Has words.</p></body></html>"
|
|
|
|
@doc = {
|
|
|
|
"field" => ["some other value"],
|
|
|
|
"_attachments" => {
|
2008-06-07 18:00:41 +02:00
|
|
|
"http://example.com/stuff.cgi?things=and%20stuff" => {
|
|
|
|
"type" => "text/html",
|
|
|
|
"data" => @attach
|
|
|
|
}
|
2008-06-07 17:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-29 02:36:36 +01:00
|
|
|
@docid = @db.save_doc(@doc)['id']
|
2008-06-07 17:32:51 +02:00
|
|
|
end
|
|
|
|
it "should save and be indicated" do
|
|
|
|
doc = @db.get(@docid)
|
|
|
|
doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length
|
|
|
|
end
|
|
|
|
it "should be there" do
|
2009-02-25 09:22:11 +01:00
|
|
|
doc = @db.get(@docid)
|
|
|
|
attachment = @db.fetch_attachment(doc,"http://example.com/stuff.cgi?things=and%20stuff")
|
2008-06-07 17:32:51 +02:00
|
|
|
attachment.should == @attach
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-19 22:33:41 +01:00
|
|
|
describe "PUT (new document with url id)" do
|
|
|
|
it "should create the document" do
|
|
|
|
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
|
|
|
lambda{@db.save_doc({'_id' => @docid})}.should raise_error(RestClient::Request::RequestFailed)
|
2008-03-19 22:33:41 +01:00
|
|
|
@db.get(@docid)['will-exist'].should == 'here'
|
|
|
|
end
|
|
|
|
end
|
2008-03-19 16:57:20 +01:00
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
describe "PUT (new document with id)" do
|
|
|
|
it "should start without the document" do
|
2009-01-29 02:36:36 +01:00
|
|
|
# r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
2008-03-19 18:17:25 +01:00
|
|
|
@db.documents['rows'].each do |doc|
|
|
|
|
doc['id'].should_not == 'my-doc'
|
|
|
|
end
|
|
|
|
# should_not include({'_id' => 'my-doc'})
|
|
|
|
# this needs to be a loop over docs on content with the post
|
|
|
|
# or instead make it return something with a fancy <=> method
|
|
|
|
end
|
|
|
|
it "should create the document" do
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => 'my-doc', 'will-exist' => 'here'})
|
|
|
|
lambda{@db.save_doc({'_id' => 'my-doc'})}.should raise_error(RestClient::Request::RequestFailed)
|
2008-03-19 07:11:44 +01:00
|
|
|
end
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
|
|
|
|
2008-03-20 00:01:04 +01:00
|
|
|
describe "PUT (existing document with rev)" do
|
|
|
|
before(:each) do
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => 'my-doc', 'will-exist' => 'here'})
|
2008-03-20 00:01:04 +01:00
|
|
|
@doc = @db.get('my-doc')
|
2008-03-20 02:10:16 +01:00
|
|
|
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => @docid, 'now' => 'save'})
|
2008-03-20 00:01:04 +01:00
|
|
|
end
|
|
|
|
it "should start with the document" do
|
|
|
|
@doc['will-exist'].should == 'here'
|
2008-03-20 02:10:16 +01:00
|
|
|
@db.get(@docid)['now'].should == 'save'
|
|
|
|
end
|
|
|
|
it "should save with url id" do
|
|
|
|
doc = @db.get(@docid)
|
|
|
|
doc['yaml'] = ['json', 'word.']
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc doc
|
2008-03-20 02:10:16 +01:00
|
|
|
@db.get(@docid)['yaml'].should == ['json', 'word.']
|
2008-03-20 00:01:04 +01:00
|
|
|
end
|
2008-06-09 22:18:52 +02:00
|
|
|
it "should fail to resave without the rev" do
|
|
|
|
@doc['them-keys'] = 'huge'
|
|
|
|
@doc['_rev'] = 'wrong'
|
2009-01-29 02:36:36 +01:00
|
|
|
# @db.save_doc(@doc)
|
|
|
|
lambda {@db.save_doc(@doc)}.should raise_error
|
2008-06-09 22:18:52 +02:00
|
|
|
end
|
2008-03-20 00:01:04 +01:00
|
|
|
it "should update the document" do
|
|
|
|
@doc['them-keys'] = 'huge'
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(@doc)
|
2008-03-20 00:01:04 +01:00
|
|
|
now = @db.get('my-doc')
|
|
|
|
now['them-keys'].should == 'huge'
|
|
|
|
end
|
|
|
|
end
|
2008-12-15 06:17:35 +01:00
|
|
|
|
|
|
|
describe "cached bulk save" do
|
|
|
|
it "stores documents in a database-specific cache" do
|
|
|
|
td = {"_id" => "btd1", "val" => "test"}
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(td, true)
|
2008-12-15 06:17:35 +01:00
|
|
|
@db.instance_variable_get("@bulk_save_cache").should == [td]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't save to the database until the configured cache size is exceded" do
|
|
|
|
@db.bulk_save_cache_limit = 3
|
|
|
|
td1 = {"_id" => "td1", "val" => true}
|
|
|
|
td2 = {"_id" => "td2", "val" => 4}
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(td1, true)
|
|
|
|
@db.save_doc(td2, true)
|
2008-12-15 06:17:35 +01:00
|
|
|
lambda do
|
|
|
|
@db.get(td1["_id"])
|
|
|
|
end.should raise_error(RestClient::ResourceNotFound)
|
|
|
|
lambda do
|
|
|
|
@db.get(td2["_id"])
|
|
|
|
end.should raise_error(RestClient::ResourceNotFound)
|
|
|
|
td3 = {"_id" => "td3", "val" => "foo"}
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(td3, true)
|
2008-12-15 06:17:35 +01:00
|
|
|
@db.get(td1["_id"])["val"].should == td1["val"]
|
|
|
|
@db.get(td2["_id"])["val"].should == td2["val"]
|
|
|
|
@db.get(td3["_id"])["val"].should == td3["val"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "clears the bulk save cache the first time a non bulk save is requested" do
|
|
|
|
td1 = {"_id" => "blah", "val" => true}
|
|
|
|
td2 = {"_id" => "steve", "val" => 3}
|
|
|
|
@db.bulk_save_cache_limit = 50
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(td1, true)
|
2008-12-15 06:17:35 +01:00
|
|
|
lambda do
|
|
|
|
@db.get(td1["_id"])
|
|
|
|
end.should raise_error(RestClient::ResourceNotFound)
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc(td2)
|
2008-12-15 06:17:35 +01:00
|
|
|
@db.get(td1["_id"])["val"].should == td1["val"]
|
|
|
|
@db.get(td2["_id"])["val"].should == td2["val"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-19 23:21:27 +01:00
|
|
|
describe "DELETE existing document" do
|
|
|
|
before(:each) do
|
2009-01-29 02:36:36 +01:00
|
|
|
@r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
2008-03-19 23:21:27 +01:00
|
|
|
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
2008-03-19 23:21:27 +01:00
|
|
|
end
|
|
|
|
it "should work" do
|
|
|
|
doc = @db.get(@r['id'])
|
|
|
|
doc['and'].should == 'spain'
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.delete_doc doc
|
2008-03-19 23:21:27 +01:00
|
|
|
lambda{@db.get @r['id']}.should raise_error
|
|
|
|
end
|
2008-03-20 00:01:04 +01:00
|
|
|
it "should work with uri id" do
|
|
|
|
doc = @db.get(@docid)
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.delete_doc doc
|
2008-03-20 00:01:04 +01:00
|
|
|
lambda{@db.get @docid}.should raise_error
|
|
|
|
end
|
2008-11-22 01:21:20 +01:00
|
|
|
it "should fail without an _id" do
|
2009-01-29 02:36:36 +01:00
|
|
|
lambda{@db.delete_doc({"not"=>"a real doc"})}.should raise_error(ArgumentError)
|
2008-11-22 01:21:20 +01:00
|
|
|
end
|
2009-01-09 10:59:08 +01:00
|
|
|
it "should defer actual deletion when using bulk save" do
|
|
|
|
doc = @db.get(@docid)
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.delete_doc doc, true
|
2009-01-09 10:59:08 +01:00
|
|
|
lambda{@db.get @docid}.should_not raise_error
|
|
|
|
@db.bulk_save
|
|
|
|
lambda{@db.get @docid}.should raise_error
|
|
|
|
end
|
|
|
|
|
2008-03-19 23:21:27 +01:00
|
|
|
end
|
|
|
|
|
2009-01-05 09:44:12 +01:00
|
|
|
describe "COPY existing document" do
|
|
|
|
before :each do
|
2009-01-29 02:36:36 +01:00
|
|
|
@r = @db.save_doc({'artist' => 'Zappa', 'title' => 'Muffin Man'})
|
2009-01-05 09:44:12 +01:00
|
|
|
@docid = 'tracks/zappa/muffin-man'
|
|
|
|
@doc = @db.get(@r['id'])
|
|
|
|
end
|
|
|
|
describe "to a new location" do
|
|
|
|
it "should work" do
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.copy_doc @doc, @docid
|
2009-01-05 09:44:12 +01:00
|
|
|
newdoc = @db.get(@docid)
|
|
|
|
newdoc['artist'].should == 'Zappa'
|
|
|
|
end
|
|
|
|
it "should fail without an _id" do
|
|
|
|
lambda{@db.copy({"not"=>"a real doc"})}.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
describe "to an existing location" do
|
|
|
|
before :each do
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
2009-01-05 09:44:12 +01:00
|
|
|
end
|
|
|
|
it "should fail without a rev" do
|
2009-01-29 02:36:36 +01:00
|
|
|
lambda{@db.copy_doc @doc, @docid}.should raise_error(RestClient::RequestFailed)
|
2009-01-05 09:44:12 +01:00
|
|
|
end
|
|
|
|
it "should succeed with a rev" do
|
|
|
|
@to_be_overwritten = @db.get(@docid)
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.copy_doc @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
2009-01-05 09:44:12 +01:00
|
|
|
newdoc = @db.get(@docid)
|
|
|
|
newdoc['artist'].should == 'Zappa'
|
|
|
|
end
|
|
|
|
it "should succeed given the doc to overwrite" do
|
|
|
|
@to_be_overwritten = @db.get(@docid)
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.copy_doc @doc, @to_be_overwritten
|
2009-01-05 09:44:12 +01:00
|
|
|
newdoc = @db.get(@docid)
|
|
|
|
newdoc['artist'].should == 'Zappa'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
it "should list documents" do
|
|
|
|
5.times do
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'another' => 'doc', 'will-exist' => 'anywhere'})
|
2008-03-19 18:17:25 +01:00
|
|
|
end
|
|
|
|
ds = @db.documents
|
|
|
|
ds['rows'].should be_an_instance_of(Array)
|
|
|
|
ds['rows'][0]['id'].should_not be_nil
|
|
|
|
ds['total_rows'].should == 5
|
2008-05-25 02:01:28 +02:00
|
|
|
end
|
|
|
|
|
2008-10-08 21:32:22 +02:00
|
|
|
describe "documents / _all_docs" do
|
|
|
|
before(:each) do
|
|
|
|
9.times do |i|
|
2009-01-29 02:36:36 +01:00
|
|
|
@db.save_doc({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
|
2008-10-08 21:32:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
it "should list documents with keys and such" do
|
|
|
|
ds = @db.documents
|
|
|
|
ds['rows'].should be_an_instance_of(Array)
|
|
|
|
ds['rows'][0]['id'].should == "doc0"
|
|
|
|
ds['total_rows'].should == 9
|
|
|
|
end
|
|
|
|
it "should take query params" do
|
|
|
|
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3')
|
|
|
|
ds['rows'].length.should == 4
|
|
|
|
ds = @db.documents(:key => 'doc0')
|
|
|
|
ds['rows'].length.should == 1
|
|
|
|
end
|
|
|
|
it "should work with multi-key" do
|
|
|
|
rs = @db.documents :keys => ["doc0", "doc7"]
|
|
|
|
rs['rows'].length.should == 2
|
|
|
|
end
|
|
|
|
it "should work with include_docs" do
|
|
|
|
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3', :include_docs => true)
|
|
|
|
ds['rows'][0]['doc']['another'].should == "doc"
|
2008-05-25 02:01:28 +02:00
|
|
|
end
|
2008-03-19 18:17:25 +01:00
|
|
|
end
|
|
|
|
|
2008-12-15 00:29:15 +01:00
|
|
|
|
|
|
|
describe "compacting a database" do
|
|
|
|
it "should compact the database" do
|
|
|
|
db = @cr.database('couchrest-test')
|
|
|
|
# r =
|
|
|
|
db.compact!
|
|
|
|
# r['ok'].should == true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-19 16:57:20 +01:00
|
|
|
describe "deleting a database" do
|
2008-03-18 19:37:10 +01:00
|
|
|
it "should start with the test database" do
|
|
|
|
@cr.databases.should include('couchrest-test')
|
|
|
|
end
|
|
|
|
it "should delete the database" do
|
|
|
|
db = @cr.database('couchrest-test')
|
2008-03-19 16:57:20 +01:00
|
|
|
# r =
|
|
|
|
db.delete!
|
|
|
|
# r['ok'].should == true
|
2008-03-18 19:37:10 +01:00
|
|
|
@cr.databases.should_not include('couchrest-test')
|
|
|
|
end
|
|
|
|
end
|
2009-01-31 19:38:44 +01:00
|
|
|
|
|
|
|
describe "replicating a database" do
|
|
|
|
before do
|
2009-02-03 01:10:07 +01:00
|
|
|
@db.save_doc({'_id' => 'test_doc', 'some-value' => 'foo'})
|
2009-01-31 19:38:44 +01:00
|
|
|
@other_db = @cr.database 'couchrest-test-replication'
|
|
|
|
@other_db.delete! rescue nil
|
|
|
|
@other_db = @cr.create_db 'couchrest-test-replication'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "via pulling" do
|
|
|
|
before do
|
|
|
|
@other_db.replicate_from @db
|
|
|
|
end
|
|
|
|
|
|
|
|
it "contains the document from the original database" do
|
|
|
|
doc = @other_db.get('test_doc')
|
|
|
|
doc['some-value'].should == 'foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "via pushing" do
|
|
|
|
before do
|
|
|
|
@db.replicate_to @other_db
|
|
|
|
end
|
|
|
|
|
|
|
|
it "copies the document to the other database" do
|
|
|
|
doc = @other_db.get('test_doc')
|
|
|
|
doc['some-value'].should == 'foo'
|
|
|
|
end
|
|
|
|
end
|
2009-02-02 23:56:37 +01:00
|
|
|
end
|
2009-01-29 02:36:36 +01:00
|
|
|
|
|
|
|
describe "creating a database" do
|
|
|
|
before(:each) do
|
|
|
|
@db = @cr.database('couchrest-test-db_to_create')
|
2009-02-25 07:51:13 +01:00
|
|
|
@db.delete! if @cr.databases.include?('couchrest-test-db_to_create')
|
2009-01-29 02:36:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should just work fine" do
|
|
|
|
@cr.databases.should_not include('couchrest-test-db_to_create')
|
|
|
|
@db.create!
|
|
|
|
@cr.databases.should include('couchrest-test-db_to_create')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "recreating a database" do
|
|
|
|
before(:each) do
|
|
|
|
@db = @cr.database('couchrest-test-db_to_create')
|
|
|
|
@db2 = @cr.database('couchrest-test-db_to_recreate')
|
|
|
|
@cr.databases.include?(@db.name) ? nil : @db.create!
|
|
|
|
@cr.databases.include?(@db2.name) ? @db2.delete! : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should drop and recreate a database" do
|
|
|
|
@cr.databases.should include(@db.name)
|
|
|
|
@db.recreate!
|
|
|
|
@cr.databases.should include(@db.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should recreate a db even tho it doesn't exist" do
|
|
|
|
@cr.databases.should_not include(@db2.name)
|
|
|
|
@db2.recreate!
|
|
|
|
@cr.databases.should include(@db2.name)
|
|
|
|
end
|
|
|
|
|
2009-01-31 19:38:44 +01:00
|
|
|
end
|
2008-03-18 19:37:10 +01:00
|
|
|
|
2008-03-19 23:21:27 +01:00
|
|
|
|
2009-02-25 09:22:11 +01:00
|
|
|
end
|