Skip to content

Commit c032521

Browse files
p-mongop
andauthored
MONGOID-5147 Add missing find_first & one methods to Mongoid::Contextual::None (#5044)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent aa79423 commit c032521

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

lib/mongoid/contextual/none.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ def length
112112
entries.length
113113
end
114114
alias :size :length
115+
116+
alias :find_first :first
117+
alias :one :first
115118
end
116119
end
117120
end
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# frozen_string_literal: true
2+
# encoding: utf-8
3+
4+
require 'spec_helper'
5+
6+
describe 'Contextual classes when dealing with empty result set' do
7+
shared_examples 'behave as expected' do
8+
context '#exists?' do
9+
it 'is false' do
10+
context.exists?.should be false
11+
end
12+
end
13+
14+
context '#count' do
15+
it 'is 0' do
16+
context.count.should == 0
17+
end
18+
end
19+
20+
context '#length' do
21+
it 'is 0' do
22+
context.length.should == 0
23+
end
24+
end
25+
26+
# #estimated_count only exists for Mongo
27+
28+
context '#distinct' do
29+
it 'is empty array' do
30+
context.distinct(:foo).should == []
31+
end
32+
end
33+
34+
context '#each' do
35+
context 'with block' do
36+
it 'does not invoke the block' do
37+
called = false
38+
context.each do
39+
called = true
40+
end
41+
called.should be false
42+
end
43+
end
44+
45+
context 'without block' do
46+
it 'returns Enumerable' do
47+
context.each.should be_a(Enumerable)
48+
end
49+
50+
it 'returns empty Enumerable' do
51+
context.each.to_a.should == []
52+
end
53+
end
54+
end
55+
56+
context '#map' do
57+
context 'with block' do
58+
it 'does not invoke the block' do
59+
called = false
60+
context.map do
61+
called = true
62+
end
63+
called.should be false
64+
end
65+
end
66+
67+
context 'without block' do
68+
it 'returns empty array' do
69+
skip 'MONGOID-5148'
70+
71+
context.map(:field).should == []
72+
end
73+
end
74+
end
75+
76+
context '#first' do
77+
it 'is nil' do
78+
context.first.should be nil
79+
end
80+
end
81+
82+
context '#find_first' do
83+
it 'is nil' do
84+
context.find_first.should be nil
85+
end
86+
end
87+
88+
context '#one' do
89+
it 'is nil' do
90+
context.one.should be nil
91+
end
92+
end
93+
94+
context '#last' do
95+
it 'is nil' do
96+
context.last.should be nil
97+
end
98+
end
99+
end
100+
101+
let(:context) do
102+
context_cls.new(criteria)
103+
end
104+
105+
before do
106+
# Create an object of the same class used in the Criteria instance
107+
# to verify we are using the Contextual classes.
108+
Mop.create!
109+
end
110+
111+
context 'Mongo' do
112+
let(:context_cls) { Mongoid::Contextual::Mongo }
113+
114+
let(:criteria) do
115+
Mop.and(Mop.where(a: 1), Mop.where(a: 2))
116+
end
117+
118+
include_examples 'behave as expected'
119+
end
120+
121+
context 'Memory' do
122+
let(:context_cls) { Mongoid::Contextual::Memory }
123+
124+
let(:criteria) do
125+
Mop.all.tap do |criteria|
126+
criteria.documents = []
127+
end
128+
end
129+
130+
include_examples 'behave as expected'
131+
end
132+
133+
context 'None' do
134+
let(:context_cls) { Mongoid::Contextual::None }
135+
136+
let(:criteria) do
137+
Mop.none
138+
end
139+
140+
include_examples 'behave as expected'
141+
end
142+
end

0 commit comments

Comments
 (0)