couchrest_model/spec/database_spec.rb

116 lines
3.6 KiB
Ruby
Raw Normal View History

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-03-19 07:11:44 +01:00
@cr = CouchRest.new("http://local.grabb.it:5984")
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-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'})
@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
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
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
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
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
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-18 19:37:10 +01:00
end