added document retreival (GET)

This commit is contained in:
Chris Anderson 2008-03-19 10:17:25 -07:00
parent 873943ed6f
commit 99f25bbbfc
2 changed files with 47 additions and 16 deletions

View file

@ -14,7 +14,12 @@ class CouchRest
def view name def view name
CouchRest.get "#{@root}/#{name}" CouchRest.get "#{@root}/#{name}"
end end
def get id
CouchRest.get "#{@root}/#{id}"
end
# PUT or POST depending on precense of _id attribute
def save doc def save doc
if doc['_id'] if doc['_id']
url = doc['_id'] url = doc['_id']

View file

@ -16,35 +16,61 @@ describe CouchRest::Database do
end end
end end
describe "inserting documents without an id: POST" do describe "GET (document by id) when the doc exists" do
before(:each) do
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
end
it "should get the document" do
doc = @db.get(@r['id'])
doc['lemons'].should == 'from texas'
end
end
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'})
# this needs to be a loop over docs on content with the post # this needs to be a loop over docs on content with the post
# or instead make it return something with a fancy <=> method # or instead make it return something with a fancy <=> method
end end
it "should create the document" do it "should create the document and return the id" do
r = @db.save({'lemons' => 'from texas', 'and' => 'spain'}) r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
@db.documents.should include(r) # @db.documents.should include(r)
lambda{@db.save({'_id' => r['id']})}.should raise_error(RestClient::Request::RequestFailed)
end end
end end
describe "with documents in it" do describe "PUT (new document with id)" do
before(:each) do it "should start without the document" do
# @db.create_doc() # 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 end
it "should list them" do it "should create the document" do
ds = @db.documents @db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
ds['rows'].should be_an_instance_of(Array) lambda{@db.save({'_id' => 'my-doc'})}.should raise_error(RestClient::Request::RequestFailed)
# ds[:total_rows].should be_greater_than 0
# should I use a View class?
ds.should be_an_instance_of(CouchRest::View)
# ds.rows = []
# ds.rows.include?(...)
# ds.total_rows
end end
end end
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
describe "deleting a database" do describe "deleting a database" do
it "should start with the test database" do it "should start with the test database" do
@cr.databases.should include('couchrest-test') @cr.databases.should include('couchrest-test')