|
641 | 641 | end
|
642 | 642 | end
|
643 | 643 |
|
| 644 | + describe "#take" do |
| 645 | + |
| 646 | + let(:hobrecht) do |
| 647 | + Address.new(street: "hobrecht") |
| 648 | + end |
| 649 | + |
| 650 | + let(:friedel) do |
| 651 | + Address.new(street: "friedel") |
| 652 | + end |
| 653 | + |
| 654 | + let(:criteria) do |
| 655 | + Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit| |
| 656 | + crit.documents = [ hobrecht, friedel ] |
| 657 | + end |
| 658 | + end |
| 659 | + |
| 660 | + let(:context) do |
| 661 | + described_class.new(criteria) |
| 662 | + end |
| 663 | + |
| 664 | + it "returns the first matching document" do |
| 665 | + expect(context.take).to eq(hobrecht) |
| 666 | + end |
| 667 | + |
| 668 | + it "returns an array when passing a limit" do |
| 669 | + expect(context.take(2)).to eq([ hobrecht, friedel ]) |
| 670 | + end |
| 671 | + |
| 672 | + it "returns an array when passing a limit as 1" do |
| 673 | + expect(context.take(1)).to eq([ hobrecht ]) |
| 674 | + end |
| 675 | + |
| 676 | + context 'when there is a collation on the criteria' do |
| 677 | + |
| 678 | + let(:criteria) do |
| 679 | + Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit| |
| 680 | + crit.documents = [ hobrecht, friedel ] |
| 681 | + end.collation(locale: 'en_US', strength: 2) |
| 682 | + end |
| 683 | + |
| 684 | + it "raises an exception" do |
| 685 | + expect { |
| 686 | + context.take |
| 687 | + }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported) |
| 688 | + end |
| 689 | + end |
| 690 | + end |
| 691 | + |
| 692 | + describe "#take!" do |
| 693 | + |
| 694 | + let(:hobrecht) do |
| 695 | + Address.new(street: "hobrecht") |
| 696 | + end |
| 697 | + |
| 698 | + let(:friedel) do |
| 699 | + Address.new(street: "friedel") |
| 700 | + end |
| 701 | + |
| 702 | + let(:criteria) do |
| 703 | + Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit| |
| 704 | + crit.documents = [ hobrecht, friedel ] |
| 705 | + end |
| 706 | + end |
| 707 | + |
| 708 | + let(:context) do |
| 709 | + described_class.new(criteria) |
| 710 | + end |
| 711 | + |
| 712 | + it "returns the first matching document" do |
| 713 | + expect(context.take!).to eq(hobrecht) |
| 714 | + end |
| 715 | + |
| 716 | + context "when the criteria is empty" do |
| 717 | + let(:criteria) do |
| 718 | + Address.where(street: "bogus").tap do |crit| |
| 719 | + crit.documents = [] |
| 720 | + end |
| 721 | + end |
| 722 | + |
| 723 | + it "raise an error" do |
| 724 | + expect do |
| 725 | + context.take! |
| 726 | + end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Address./) |
| 727 | + end |
| 728 | + end |
| 729 | + |
| 730 | + context 'when there is a collation on the criteria' do |
| 731 | + |
| 732 | + let(:criteria) do |
| 733 | + Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit| |
| 734 | + crit.documents = [ hobrecht, friedel ] |
| 735 | + end.collation(locale: 'en_US', strength: 2) |
| 736 | + end |
| 737 | + |
| 738 | + it "raises an exception" do |
| 739 | + expect { |
| 740 | + context.take |
| 741 | + }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported) |
| 742 | + end |
| 743 | + end |
| 744 | + end |
| 745 | + |
644 | 746 | describe "#initialize" do
|
645 | 747 |
|
646 | 748 | context "when the criteria has no options" do
|
|
0 commit comments