Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit bdd4de1

Browse files
authored
Merge pull request #19 from doctolib/fix-find-array
test and solve cases of find with multiple ids
2 parents f44844c + 50f9af8 commit bdd4de1

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

lib/couchbase-orm/base.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ def find(*ids, quiet: false)
254254
records = quiet ? collection.get_multi(ids, transcoder: transcoder) : collection.get_multi!(ids, transcoder: transcoder)
255255
CouchbaseOrm.logger.debug { "Base.find found(#{records})" }
256256
records = records.zip(ids).map { |record, id|
257-
self.new(record, id: id) if record
258-
}
259-
records.compact!
257+
next unless record
258+
next if record.error
259+
self.new(record, id: id)
260+
}.compact
260261
ids.length > 1 ? records : records[0]
261262
end
262263

lib/couchbase-orm/proxies/collection_proxy.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def get_multi!(*ids, **options)
2121
end
2222

2323
def get_multi(*ids, **options)
24-
result = @proxyfied.get_multi(*ids, Couchbase::Options::GetMulti.new(**options))
25-
result.reject(&:error)
24+
@proxyfied.get_multi(*ids, Couchbase::Options::GetMulti.new(**options))
2625
end
2726

2827
def remove!(id, **options)

spec/base_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,27 @@ class BaseTestWithIgnoredProperties < CouchbaseOrm::Base
212212
expect(base.created_at).to be_a(Time)
213213
end
214214

215+
it "should find multiple ids at same time" do
216+
base1 = BaseTest.create!(name: 'joe1')
217+
base2 = BaseTest.create!(name: 'joe2')
218+
base3 = BaseTest.create!(name: 'joe3')
219+
expect(BaseTest.find([base1.id, base2.id, base3.id])).to eq([base1, base2, base3])
220+
end
221+
222+
it "should find multiple ids at same time with a not found id with exception" do
223+
base1 = BaseTest.create!(name: 'joe1')
224+
base2 = BaseTest.create!(name: 'joe2')
225+
base3 = BaseTest.create!(name: 'joe3')
226+
expect { BaseTest.find([base1.id, 't', base3.id]) }.to raise_error(Couchbase::Error::DocumentNotFound)
227+
end
228+
229+
it "should find multiple ids at same time with a not found id without exception" do
230+
base1 = BaseTest.create!(name: 'joe1')
231+
base2 = BaseTest.create!(name: 'joe2')
232+
base3 = BaseTest.create!(name: 'joe3')
233+
expect(BaseTest.find([base1.id, 't', 't', base2.id, base3.id], quiet: true)).to eq([base1, base2, base3])
234+
end
235+
215236
describe BaseTest do
216237
it_behaves_like "ActiveModel"
217238
end

0 commit comments

Comments
 (0)