Permalink
Browse files

fix #load!

  • Loading branch information...
1 parent 721abbd commit 6414e4e94d3debcd98484f6810a0a4d7ff8f9468 @langalex committed Feb 26, 2012
Showing with 23 additions and 16 deletions.
  1. +4 −3 lib/couch_potato/database.rb
  2. +19 −13 spec/unit/database_spec.rb
@@ -102,7 +102,7 @@ def destroy_document(document)
# loads a document by its id(s)
def load_document(id)
raise "Can't load a document without an id (got nil)" if id.nil?
-
+
if id.is_a?(Array)
bulk_load id
else
@@ -122,7 +122,8 @@ def load!(id)
if id.is_a?(Array)
missing_docs = id - doc.map(&:id)
end
- raise(CouchPotato::NotFound, missing_docs) if doc.nil? || !missing_docs.empty?
+ raise(CouchPotato::NotFound, missing_docs) if doc.nil? || missing_docs.try(:any?)
+ doc
end
def inspect #:nodoc:
@@ -135,7 +136,7 @@ def couchrest_database
end
private
-
+
def bulk_load(ids)
response = couchrest_database.bulk_load ids
existing_rows = response['rows'].select{|row| row.key? 'doc'}
View
@@ -45,10 +45,10 @@ class Child
end
describe CouchPotato::Database, 'load' do
-
+
let(:couchrest_db) { stub('couchrest db', :info => nil) }
let(:db) { CouchPotato::Database.new couchrest_db }
-
+
it "should raise an exception if nil given" do
lambda {
db.load nil
@@ -67,15 +67,15 @@ class Child
couchrest_db.stub(:get).and_return Parent::Child.json_create({JSON.create_id => 'Parent::Child'})
db.load('1').class.should == Parent::Child
end
-
+
context "when several ids given" do
-
+
let(:doc1) { DbTestUser.new }
let(:doc2) { DbTestUser.new }
let(:response) do
{"rows" => [{}, {"doc" => doc1}, {"doc" => doc2}]}
end
-
+
before(:each) do
couchrest_db.stub(:bulk_load).and_return response
end
@@ -98,35 +98,41 @@ class Child
end
describe CouchPotato::Database, 'load!' do
-
- let(:db) { CouchPotato::Database.new(stub('couchrest db', :info => nil)) }
-
+
+ let(:db) { CouchPotato::Database.new(stub('couchrest db', :info => nil).as_null_object) }
+
it "should raise an error if no document found" do
db.couchrest_database.stub(:get).and_raise(RestClient::ResourceNotFound)
lambda {
db.load! '1'
}.should raise_error(CouchPotato::NotFound)
end
-
+
+ it 'returns the found document' do
+ doc = stub(:doc).as_null_object
+ db.couchrest_database.stub(:get) {doc}
+ db.load!('1').should == doc
+ end
+
context "when several ids given" do
-
+
let(:docs) do
[
DbTestUser.new(:id => '1'),
DbTestUser.new(:id => '2')
]
end
-
+
before(:each) do
db.stub(:load).and_return(docs)
end
-
+
it "raises an exception when not all documents could be found" do
lambda {
db.load! ['1', '2', '3']
}.should raise_error(CouchPotato::NotFound, ['3'])
end
-
+
it "raises no exception when all documents are found" do
docs << DbTestUser.new(:id => '3')
lambda {

0 comments on commit 6414e4e

Please sign in to comment.