Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding more find specs on criteria

  • Loading branch information...
commit a585a9e4eadbb542ea90cd675717549edc24d852 1 parent 12e4ed5
Durran Jordan durran authored
Showing with 173 additions and 3 deletions.
  1. +3 −3 lib/mongoid/criteria.rb
  2. +170 −0 spec/mongoid/criteria_spec.rb
6 lib/mongoid/criteria.rb
View
@@ -205,10 +205,10 @@ def each(&block)
# @return [ Document, Array<Document> ] The document(s).
#
# @since 2.0.0
- def execute_or_raise(args)
+ def execute_or_raise(*args)
ids = args[0]
- ids = ids.to_a if ids.is_a?(::Range)
- if ids.is_a?(::Array)
+ ids = ids.is_a?(::Range) ? ids.to_a : ids.flatten
+ if ids.size > 1
entries.tap do |result|
if (entries.size < ids.size) && Mongoid.raise_not_found_error
missing = ids - entries.map(&:_id)
170 spec/mongoid/criteria_spec.rb
View
@@ -523,6 +523,176 @@
context "when using object ids" do
+ let!(:band) do
+ Band.create
+ end
+
+ context "when providing a single id" do
+
+ context "when the id matches" do
+
+ let(:found) do
+ Band.find(band.id)
+ end
+
+ it "returns the matching document" do
+ found.should eq(band)
+ end
+ end
+
+ context "when the id does not match" do
+
+ context "when raising a not found error" do
+
+ before do
+ Mongoid.raise_not_found_error = true
+ end
+
+ let(:found) do
+ Band.find(BSON::ObjectId.new)
+ end
+
+ it "raises an error" do
+ expect {
+ found
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
+ end
+ end
+
+ context "when raising no error" do
+
+ before do
+ Mongoid.raise_not_found_error = false
+ end
+
+ after do
+ Mongoid.raise_not_found_error = true
+ end
+
+ let(:found) do
+ Band.find(BSON::ObjectId.new)
+ end
+
+ it "returns nil" do
+ found.should be_nil
+ end
+ end
+ end
+ end
+
+ context "when providing a splat of ids" do
+
+ let!(:band_two) do
+ Band.create(name: "Tool")
+ end
+
+ context "when all ids match" do
+
+ let(:found) do
+ Band.find(band.id, band_two.id)
+ end
+
+ it "returns the matching documents" do
+ found.should eq([ band, band_two ])
+ end
+ end
+
+ context "when any id does not match" do
+
+ context "when raising a not found error" do
+
+ before do
+ Mongoid.raise_not_found_error = true
+ end
+
+ let(:found) do
+ Band.find(band.id, BSON::ObjectId.new)
+ end
+
+ it "raises an error" do
+ expect {
+ found
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
+ end
+ end
+
+ context "when raising no error" do
+
+ before do
+ Mongoid.raise_not_found_error = false
+ end
+
+ after do
+ Mongoid.raise_not_found_error = true
+ end
+
+ let(:found) do
+ Band.find(band.id, BSON::ObjectId.new)
+ end
+
+ it "returns only the matching documents" do
+ found.should eq([ band ])
+ end
+ end
+ end
+ end
+
+ context "when providing an array of ids" do
+
+ let!(:band_two) do
+ Band.create(name: "Tool")
+ end
+
+ context "when all ids match" do
+
+ let(:found) do
+ Band.find([ band.id, band_two.id ])
+ end
+
+ it "returns the matching documents" do
+ found.should eq([ band, band_two ])
+ end
+ end
+
+ context "when any id does not match" do
+
+ context "when raising a not found error" do
+
+ before do
+ Mongoid.raise_not_found_error = true
+ end
+
+ let(:found) do
+ Band.find([ band.id, BSON::ObjectId.new ])
+ end
+
+ it "raises an error" do
+ expect {
+ found
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
+ end
+ end
+
+ context "when raising no error" do
+
+ before do
+ Mongoid.raise_not_found_error = false
+ end
+
+ after do
+ Mongoid.raise_not_found_error = true
+ end
+
+ let(:found) do
+ Band.find([ band.id, BSON::ObjectId.new ])
+ end
+
+ it "returns only the matching documents" do
+ found.should eq([ band ])
+ end
+ end
+ end
+ end
end
context "when using string ids" do
Please sign in to comment.
Something went wrong with that request. Please try again.