2008-03-19 07:11:44 +01:00
|
|
|
require File.dirname(__FILE__) + '/../lib/couch_rest'
|
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-05-17 23:40:45 +02:00
|
|
|
@cr = CouchRest.new("http://localhost:5984")
|
2008-03-19 07:11:44 +01:00
|
|
|
begin
|
2008-03-19 16:57:20 +01:00
|
|
|
@db = @cr.create_db('couchrest-test')
|
2008-03-19 07:11:44 +01:00
|
|
|
rescue RestClient::Request::RequestFailed
|
|
|
|
end
|
2008-03-18 19:37:10 +01:00
|
|
|
end
|
2008-03-19 16:57:20 +01:00
|
|
|
|
|
|
|
after(:each) do
|
|
|
|
begin
|
|
|
|
@db.delete!
|
|
|
|
rescue RestClient::Request::RequestFailed
|
|
|
|
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"]}
|
|
|
|
])
|
|
|
|
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){for(var w in doc){ if(!w.match(/^_/))emit(w,doc[w])}}")
|
2008-03-20 02:10:16 +01:00
|
|
|
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
|
|
|
|
end
|
|
|
|
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([
|
|
|
|
{"beverage" => "beer", :count => 4},
|
|
|
|
{"beverage" => "beer", :count => 2},
|
|
|
|
{"beverage" => "tea", :count => 3}
|
|
|
|
])
|
|
|
|
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-05-17 22:41:16 +02:00
|
|
|
rs['result'].should == 9
|
|
|
|
end
|
|
|
|
end
|
2008-05-21 23:58:37 +02:00
|
|
|
|
2008-03-20 06:38:01 +01:00
|
|
|
describe "select from an existing view" do
|
|
|
|
before(:each) do
|
2008-05-21 20:08:42 +02:00
|
|
|
r = @db.save({
|
|
|
|
"_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
|
|
|
|
end
|
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
describe "GET (document by id) when the doc exists" do
|
|
|
|
before(:each) do
|
|
|
|
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
2008-03-19 22:33:41 +01:00
|
|
|
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
|
|
|
@db.save({'_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"]}
|
|
|
|
])
|
2008-05-15 02:27:21 +02:00
|
|
|
rs['new_revs'].each do |r|
|
2008-03-20 00:38:07 +01:00
|
|
|
@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"]}
|
|
|
|
])
|
2008-05-15 02:27:21 +02:00
|
|
|
rs['new_revs'].each do |r|
|
2008-03-20 00:38:07 +01:00
|
|
|
@db.get(r['id'])
|
|
|
|
end
|
|
|
|
end
|
2008-05-15 02:27:21 +02:00
|
|
|
it "in the case of an id conflict should not insert anything" do
|
2008-03-20 00:38:07 +01:00
|
|
|
@r = @db.save({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"})
|
|
|
|
|
2008-05-15 02:27:21 +02:00
|
|
|
lambda do
|
2008-03-20 00:38:07 +01:00
|
|
|
rs = @db.bulk_save([
|
|
|
|
{"_id" => "oneB", "wild" => "and random"},
|
|
|
|
{"_id" => "twoB", "mild" => "yet local"},
|
|
|
|
{"another" => ["set","of","keys"]}
|
|
|
|
])
|
2008-05-15 02:27:21 +02:00
|
|
|
end.should raise_error(RestClient::Request::RequestFailed)
|
|
|
|
|
|
|
|
lambda do
|
|
|
|
@db.get('twoB')
|
|
|
|
end.should raise_error(RestClient::Request::RequestFailed)
|
2008-03-20 00:38:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
describe "POST (new document without an id)" do
|
2008-03-19 16:57:20 +01:00
|
|
|
it "should start without the document" do
|
|
|
|
@db.documents.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
|
2008-03-19 18:17:25 +01:00
|
|
|
it "should create the document and return the id" do
|
2008-03-19 16:57:20 +01:00
|
|
|
r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
2008-03-19 18:17:25 +01:00
|
|
|
# @db.documents.should include(r)
|
|
|
|
lambda{@db.save({'_id' => r['id']})}.should raise_error(RestClient::Request::RequestFailed)
|
2008-03-19 16:57:20 +01:00
|
|
|
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"
|
|
|
|
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
|
|
|
lambda{@db.save({'_id' => @docid})}.should raise_error(RestClient::Request::RequestFailed)
|
|
|
|
@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
|
|
|
|
# r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
|
|
|
@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
|
|
|
|
@db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
|
|
|
|
lambda{@db.save({'_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
|
|
|
|
@db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
|
|
|
|
@doc = @db.get('my-doc')
|
2008-03-20 02:10:16 +01:00
|
|
|
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
|
|
|
@db.save({'_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.']
|
|
|
|
@db.save doc
|
|
|
|
@db.get(@docid)['yaml'].should == ['json', 'word.']
|
2008-03-20 00:01:04 +01:00
|
|
|
end
|
|
|
|
it "should update the document" do
|
|
|
|
@doc['them-keys'] = 'huge'
|
|
|
|
@db.save(@doc)
|
|
|
|
now = @db.get('my-doc')
|
|
|
|
now['them-keys'].should == 'huge'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-19 23:21:27 +01:00
|
|
|
describe "DELETE existing document" do
|
|
|
|
before(:each) do
|
|
|
|
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
|
|
|
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
|
|
|
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
|
|
|
end
|
|
|
|
it "should work" do
|
|
|
|
doc = @db.get(@r['id'])
|
|
|
|
doc['and'].should == 'spain'
|
|
|
|
@db.delete doc
|
|
|
|
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)
|
|
|
|
@db.delete doc
|
|
|
|
lambda{@db.get @docid}.should raise_error
|
|
|
|
end
|
2008-03-19 23:21:27 +01:00
|
|
|
end
|
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
it "should list documents" do
|
|
|
|
5.times do
|
|
|
|
@db.save({'another' => 'doc', 'will-exist' => 'anywhere'})
|
|
|
|
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
|
|
|
|
# should I use a View class?
|
|
|
|
# ds.should be_an_instance_of(CouchRest::View)
|
|
|
|
|
|
|
|
# ds.rows = []
|
|
|
|
# ds.rows.include?(...)
|
|
|
|
# ds.total_rows
|
|
|
|
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
|
|
|
|
|
2008-03-19 23:21:27 +01:00
|
|
|
|
2008-03-18 19:37:10 +01:00
|
|
|
end
|