|
766 | 766 | Address.new(street: "friedel")
|
767 | 767 | end
|
768 | 768 |
|
769 |
| - context "when there are matching documents" do |
| 769 | + let(:criteria) do |
| 770 | + Address.where(street: "hobrecht").tap do |crit| |
| 771 | + crit.documents = [ hobrecht, friedel ] |
| 772 | + end |
| 773 | + end |
770 | 774 |
|
771 |
| - let(:criteria) do |
772 |
| - Address.where(street: "hobrecht").tap do |crit| |
773 |
| - crit.documents = [ hobrecht, friedel ] |
| 775 | + context "when not passing options" do |
| 776 | + |
| 777 | + context "when there are matching documents" do |
| 778 | + |
| 779 | + let(:context) do |
| 780 | + described_class.new(criteria) |
| 781 | + end |
| 782 | + |
| 783 | + it "returns true" do |
| 784 | + expect(context).to be_exists |
774 | 785 | end
|
775 | 786 | end
|
776 | 787 |
|
777 |
| - let(:context) do |
778 |
| - described_class.new(criteria) |
| 788 | + context "when there are no matching documents" do |
| 789 | + |
| 790 | + let(:criteria) do |
| 791 | + Address.where(street: "pfluger").tap do |crit| |
| 792 | + crit.documents = [ hobrecht, friedel ] |
| 793 | + end |
| 794 | + end |
| 795 | + |
| 796 | + let(:context) do |
| 797 | + described_class.new(criteria) |
| 798 | + end |
| 799 | + |
| 800 | + it "returns false" do |
| 801 | + expect(context).to_not be_exists |
| 802 | + end |
779 | 803 | end
|
780 | 804 |
|
781 |
| - it "returns true" do |
782 |
| - expect(context).to be_exists |
| 805 | + context 'when there is a collation on the criteria' do |
| 806 | + |
| 807 | + let(:criteria) do |
| 808 | + Address.where(street: "pfluger").tap do |crit| |
| 809 | + crit.documents = [ hobrecht, friedel ] |
| 810 | + end.collation(locale: 'en_US', strength: 2) |
| 811 | + end |
| 812 | + |
| 813 | + it "raises an exception" do |
| 814 | + expect { |
| 815 | + context |
| 816 | + }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported) |
| 817 | + end |
783 | 818 | end
|
784 | 819 | end
|
785 | 820 |
|
786 |
| - context "when there are no matching documents" do |
| 821 | + context "when passing an _id" do |
787 | 822 |
|
788 |
| - let(:criteria) do |
789 |
| - Address.where(street: "pfluger").tap do |crit| |
790 |
| - crit.documents = [ hobrecht, friedel ] |
| 823 | + context "when its of type BSON::ObjectId" do |
| 824 | + |
| 825 | + context "when calling it on an empty criteria" do |
| 826 | + |
| 827 | + it "returns true" do |
| 828 | + expect(criteria.exists?(hobrecht._id)).to be true |
| 829 | + end |
| 830 | + end |
| 831 | + |
| 832 | + context "when calling it on a criteria that includes the object" do |
| 833 | + |
| 834 | + it "returns true" do |
| 835 | + expect(criteria.where(street: hobrecht.street).exists?(hobrecht._id)).to be true |
| 836 | + end |
| 837 | + end |
| 838 | + |
| 839 | + context "when calling it on a criteria that does not include the object" do |
| 840 | + |
| 841 | + it "returns false" do |
| 842 | + expect(criteria.where(street: "bogus").exists?(hobrecht._id)).to be false |
| 843 | + end |
| 844 | + end |
| 845 | + |
| 846 | + context "when the id does not exist" do |
| 847 | + |
| 848 | + it "returns false" do |
| 849 | + expect(criteria.exists?(BSON::ObjectId.new)).to be false |
| 850 | + end |
791 | 851 | end
|
792 | 852 | end
|
793 | 853 |
|
794 |
| - let(:context) do |
795 |
| - described_class.new(criteria) |
| 854 | + context "when its of type String" do |
| 855 | + |
| 856 | + context "when the id exists" do |
| 857 | + |
| 858 | + it "returns true" do |
| 859 | + expect(criteria.exists?(hobrecht._id.to_s)).to be true |
| 860 | + end |
| 861 | + end |
| 862 | + |
| 863 | + context "when the id does not exist" do |
| 864 | + |
| 865 | + it "returns false" do |
| 866 | + expect(criteria.exists?(BSON::ObjectId.new.to_s)).to be false |
| 867 | + end |
| 868 | + end |
| 869 | + end |
| 870 | + end |
| 871 | + |
| 872 | + context "when passing a hash" do |
| 873 | + |
| 874 | + context "when calling it on an empty criteria" do |
| 875 | + |
| 876 | + it "returns true" do |
| 877 | + expect(criteria.exists?(street: hobrecht.street)).to be true |
| 878 | + end |
| 879 | + end |
| 880 | + |
| 881 | + context "when calling it on a criteria that includes the object" do |
| 882 | + |
| 883 | + it "returns true" do |
| 884 | + expect(criteria.where(_id: hobrecht._id).exists?(street: hobrecht.street)).to be true |
| 885 | + end |
| 886 | + end |
| 887 | + |
| 888 | + context "when calling it on a criteria that does not include the object" do |
| 889 | + |
| 890 | + it "returns false" do |
| 891 | + expect(criteria.where(_id: BSON::ObjectId.new).exists?(street: hobrecht.street)).to be false |
| 892 | + end |
796 | 893 | end
|
797 | 894 |
|
| 895 | + context "when the conditions don't match" do |
| 896 | + |
| 897 | + it "returns false" do |
| 898 | + expect(criteria.exists?(street: "bogus")).to be false |
| 899 | + end |
| 900 | + end |
| 901 | + end |
| 902 | + |
| 903 | + context "when passing false" do |
| 904 | + |
798 | 905 | it "returns false" do
|
799 |
| - expect(context).to_not be_exists |
| 906 | + expect(criteria.exists?(false)).to be false |
800 | 907 | end
|
801 | 908 | end
|
802 | 909 |
|
803 |
| - context 'when there is a collation on the criteria' do |
| 910 | + context "when passing nil" do |
804 | 911 |
|
805 |
| - let(:criteria) do |
806 |
| - Address.where(street: "pfluger").tap do |crit| |
807 |
| - crit.documents = [ hobrecht, friedel ] |
808 |
| - end.collation(locale: 'en_US', strength: 2) |
| 912 | + it "returns false" do |
| 913 | + expect(criteria.exists?(nil)).to be false |
809 | 914 | end
|
| 915 | + end |
810 | 916 |
|
811 |
| - it "raises an exception" do |
812 |
| - expect { |
813 |
| - context |
814 |
| - }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported) |
| 917 | + context "when the limit is 0" do |
| 918 | + |
| 919 | + it "returns false" do |
| 920 | + expect(criteria.limit(0).exists?).to be false |
815 | 921 | end
|
816 | 922 | end
|
817 | 923 | end
|
|
0 commit comments