Skip to content

Commit

Permalink
fix #load!
Browse files Browse the repository at this point in the history
  • Loading branch information
langalex committed Feb 26, 2012
1 parent 721abbd commit 6414e4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
7 changes: 4 additions & 3 deletions lib/couch_potato/database.rb
Expand Up @@ -102,7 +102,7 @@ def destroy_document(document)
# loads a document by its id(s) # loads a document by its id(s)
def load_document(id) def load_document(id)
raise "Can't load a document without an id (got nil)" if id.nil? raise "Can't load a document without an id (got nil)" if id.nil?

if id.is_a?(Array) if id.is_a?(Array)
bulk_load id bulk_load id
else else
Expand All @@ -122,7 +122,8 @@ def load!(id)
if id.is_a?(Array) if id.is_a?(Array)
missing_docs = id - doc.map(&:id) missing_docs = id - doc.map(&:id)
end 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 end


def inspect #:nodoc: def inspect #:nodoc:
Expand All @@ -135,7 +136,7 @@ def couchrest_database
end end


private private

def bulk_load(ids) def bulk_load(ids)
response = couchrest_database.bulk_load ids response = couchrest_database.bulk_load ids
existing_rows = response['rows'].select{|row| row.key? 'doc'} existing_rows = response['rows'].select{|row| row.key? 'doc'}
Expand Down
32 changes: 19 additions & 13 deletions spec/unit/database_spec.rb
Expand Up @@ -45,10 +45,10 @@ class Child
end end


describe CouchPotato::Database, 'load' do describe CouchPotato::Database, 'load' do

let(:couchrest_db) { stub('couchrest db', :info => nil) } let(:couchrest_db) { stub('couchrest db', :info => nil) }
let(:db) { CouchPotato::Database.new couchrest_db } let(:db) { CouchPotato::Database.new couchrest_db }

it "should raise an exception if nil given" do it "should raise an exception if nil given" do
lambda { lambda {
db.load nil db.load nil
Expand All @@ -67,15 +67,15 @@ class Child
couchrest_db.stub(:get).and_return Parent::Child.json_create({JSON.create_id => 'Parent::Child'}) couchrest_db.stub(:get).and_return Parent::Child.json_create({JSON.create_id => 'Parent::Child'})
db.load('1').class.should == Parent::Child db.load('1').class.should == Parent::Child
end end

context "when several ids given" do context "when several ids given" do

let(:doc1) { DbTestUser.new } let(:doc1) { DbTestUser.new }
let(:doc2) { DbTestUser.new } let(:doc2) { DbTestUser.new }
let(:response) do let(:response) do
{"rows" => [{}, {"doc" => doc1}, {"doc" => doc2}]} {"rows" => [{}, {"doc" => doc1}, {"doc" => doc2}]}
end end

before(:each) do before(:each) do
couchrest_db.stub(:bulk_load).and_return response couchrest_db.stub(:bulk_load).and_return response
end end
Expand All @@ -98,35 +98,41 @@ class Child
end end


describe CouchPotato::Database, 'load!' do 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 it "should raise an error if no document found" do
db.couchrest_database.stub(:get).and_raise(RestClient::ResourceNotFound) db.couchrest_database.stub(:get).and_raise(RestClient::ResourceNotFound)
lambda { lambda {
db.load! '1' db.load! '1'
}.should raise_error(CouchPotato::NotFound) }.should raise_error(CouchPotato::NotFound)
end 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 context "when several ids given" do

let(:docs) do let(:docs) do
[ [
DbTestUser.new(:id => '1'), DbTestUser.new(:id => '1'),
DbTestUser.new(:id => '2') DbTestUser.new(:id => '2')
] ]
end end

before(:each) do before(:each) do
db.stub(:load).and_return(docs) db.stub(:load).and_return(docs)
end end

it "raises an exception when not all documents could be found" do it "raises an exception when not all documents could be found" do
lambda { lambda {
db.load! ['1', '2', '3'] db.load! ['1', '2', '3']
}.should raise_error(CouchPotato::NotFound, ['3']) }.should raise_error(CouchPotato::NotFound, ['3'])
end end

it "raises no exception when all documents are found" do it "raises no exception when all documents are found" do
docs << DbTestUser.new(:id => '3') docs << DbTestUser.new(:id => '3')
lambda { lambda {
Expand Down

0 comments on commit 6414e4e

Please sign in to comment.