Skip to content
This repository
Browse code

Adding more find specs on criteria

  • Loading branch information...
commit a585a9e4eadbb542ea90cd675717549edc24d852 1 parent 12e4ed5
Durran Jordan durran authored

Showing 2 changed files with 173 additions and 3 deletions. Show diff stats Hide diff stats

  1. +3 3 lib/mongoid/criteria.rb
  2. +170 0 spec/mongoid/criteria_spec.rb
6 lib/mongoid/criteria.rb
@@ -205,10 +205,10 @@ def each(&block)
205 205 # @return [ Document, Array<Document> ] The document(s).
206 206 #
207 207 # @since 2.0.0
208   - def execute_or_raise(args)
  208 + def execute_or_raise(*args)
209 209 ids = args[0]
210   - ids = ids.to_a if ids.is_a?(::Range)
211   - if ids.is_a?(::Array)
  210 + ids = ids.is_a?(::Range) ? ids.to_a : ids.flatten
  211 + if ids.size > 1
212 212 entries.tap do |result|
213 213 if (entries.size < ids.size) && Mongoid.raise_not_found_error
214 214 missing = ids - entries.map(&:_id)
170 spec/mongoid/criteria_spec.rb
@@ -523,6 +523,176 @@
523 523
524 524 context "when using object ids" do
525 525
  526 + let!(:band) do
  527 + Band.create
  528 + end
  529 +
  530 + context "when providing a single id" do
  531 +
  532 + context "when the id matches" do
  533 +
  534 + let(:found) do
  535 + Band.find(band.id)
  536 + end
  537 +
  538 + it "returns the matching document" do
  539 + found.should eq(band)
  540 + end
  541 + end
  542 +
  543 + context "when the id does not match" do
  544 +
  545 + context "when raising a not found error" do
  546 +
  547 + before do
  548 + Mongoid.raise_not_found_error = true
  549 + end
  550 +
  551 + let(:found) do
  552 + Band.find(BSON::ObjectId.new)
  553 + end
  554 +
  555 + it "raises an error" do
  556 + expect {
  557 + found
  558 + }.to raise_error(Mongoid::Errors::DocumentNotFound)
  559 + end
  560 + end
  561 +
  562 + context "when raising no error" do
  563 +
  564 + before do
  565 + Mongoid.raise_not_found_error = false
  566 + end
  567 +
  568 + after do
  569 + Mongoid.raise_not_found_error = true
  570 + end
  571 +
  572 + let(:found) do
  573 + Band.find(BSON::ObjectId.new)
  574 + end
  575 +
  576 + it "returns nil" do
  577 + found.should be_nil
  578 + end
  579 + end
  580 + end
  581 + end
  582 +
  583 + context "when providing a splat of ids" do
  584 +
  585 + let!(:band_two) do
  586 + Band.create(name: "Tool")
  587 + end
  588 +
  589 + context "when all ids match" do
  590 +
  591 + let(:found) do
  592 + Band.find(band.id, band_two.id)
  593 + end
  594 +
  595 + it "returns the matching documents" do
  596 + found.should eq([ band, band_two ])
  597 + end
  598 + end
  599 +
  600 + context "when any id does not match" do
  601 +
  602 + context "when raising a not found error" do
  603 +
  604 + before do
  605 + Mongoid.raise_not_found_error = true
  606 + end
  607 +
  608 + let(:found) do
  609 + Band.find(band.id, BSON::ObjectId.new)
  610 + end
  611 +
  612 + it "raises an error" do
  613 + expect {
  614 + found
  615 + }.to raise_error(Mongoid::Errors::DocumentNotFound)
  616 + end
  617 + end
  618 +
  619 + context "when raising no error" do
  620 +
  621 + before do
  622 + Mongoid.raise_not_found_error = false
  623 + end
  624 +
  625 + after do
  626 + Mongoid.raise_not_found_error = true
  627 + end
  628 +
  629 + let(:found) do
  630 + Band.find(band.id, BSON::ObjectId.new)
  631 + end
  632 +
  633 + it "returns only the matching documents" do
  634 + found.should eq([ band ])
  635 + end
  636 + end
  637 + end
  638 + end
  639 +
  640 + context "when providing an array of ids" do
  641 +
  642 + let!(:band_two) do
  643 + Band.create(name: "Tool")
  644 + end
  645 +
  646 + context "when all ids match" do
  647 +
  648 + let(:found) do
  649 + Band.find([ band.id, band_two.id ])
  650 + end
  651 +
  652 + it "returns the matching documents" do
  653 + found.should eq([ band, band_two ])
  654 + end
  655 + end
  656 +
  657 + context "when any id does not match" do
  658 +
  659 + context "when raising a not found error" do
  660 +
  661 + before do
  662 + Mongoid.raise_not_found_error = true
  663 + end
  664 +
  665 + let(:found) do
  666 + Band.find([ band.id, BSON::ObjectId.new ])
  667 + end
  668 +
  669 + it "raises an error" do
  670 + expect {
  671 + found
  672 + }.to raise_error(Mongoid::Errors::DocumentNotFound)
  673 + end
  674 + end
  675 +
  676 + context "when raising no error" do
  677 +
  678 + before do
  679 + Mongoid.raise_not_found_error = false
  680 + end
  681 +
  682 + after do
  683 + Mongoid.raise_not_found_error = true
  684 + end
  685 +
  686 + let(:found) do
  687 + Band.find([ band.id, BSON::ObjectId.new ])
  688 + end
  689 +
  690 + it "returns only the matching documents" do
  691 + found.should eq([ band ])
  692 + end
  693 + end
  694 + end
  695 + end
526 696 end
527 697
528 698 context "when using string ids" do

0 comments on commit a585a9e

Please sign in to comment.
Something went wrong with that request. Please try again.