/
conditions_spec.rb
274 lines (219 loc) · 9.88 KB
/
conditions_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "Conditions" do
it "should be dynamically created and then cached" do
User.should_not respond_to(:age_less_than)
User.age_less_than(5)
User.should respond_to(:age_less_than)
end
it "should not allow conditions on non columns" do
lambda { User.whatever_equals(2) }.should raise_error(NoMethodError)
end
context "comparison conditions" do
it "should have equals" do
(5..7).each { |age| User.create(:age => age) }
User.age_equals(6).all.should == User.find_all_by_age(6)
end
it "should have does not equal" do
(5..7).each { |age| User.create(:age => age) }
User.age_does_not_equal(6).all.should == User.find_all_by_age([5,7])
end
it "should have less than" do
(5..7).each { |age| User.create(:age => age) }
User.age_less_than(6).all.should == User.find_all_by_age(5)
end
it "should have less than or equal to" do
(5..7).each { |age| User.create(:age => age) }
User.age_less_than_or_equal_to(6).all.should == User.find_all_by_age([5, 6])
end
it "should have greater than" do
(5..7).each { |age| User.create(:age => age) }
User.age_greater_than(6).all.should == User.find_all_by_age(7)
end
it "should have greater than or equal to" do
(5..7).each { |age| User.create(:age => age) }
User.age_greater_than_or_equal_to(6).all.should == User.find_all_by_age([6, 7])
end
end
context "wildcard conditions" do
it "should have like" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_like("john").all.should == User.find_all_by_username("bjohnson")
end
it "should have not like" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_not_like("john").all.should == User.find_all_by_username("thunt")
end
it "should have begins with" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_begins_with("bj").all.should == User.find_all_by_username("bjohnson")
end
it "should have not begin with" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_not_begin_with("bj").all.should == User.find_all_by_username("thunt")
end
it "should have ends with" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_ends_with("son").all.should == User.find_all_by_username("bjohnson")
end
it "should have not end with" do
%w(bjohnson thunt).each { |username| User.create(:username => username) }
User.username_not_end_with("son").all.should == User.find_all_by_username("thunt")
end
end
context "boolean conditions" do
it "should have null" do
["bjohnson", nil].each { |username| User.create(:username => username) }
User.username_null.all.should == User.find_all_by_username(nil)
end
it "should have not null" do
["bjohnson", nil].each { |username| User.create(:username => username) }
User.username_not_null.all.should == User.find_all_by_username("bjohnson")
end
it "should have empty" do
["bjohnson", ""].each { |username| User.create(:username => username) }
User.username_empty.all.should == User.find_all_by_username("")
end
end
context "any and all conditions" do
it "should do nothing if no arguments are passed" do
User.username_equals_any.proxy_options.should == {}
end
it "should have equals any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_equals_any("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
end
it "should have equals all" do
%w(bjohnson thunt dainor).each { |username| User.create(:username => username) }
User.username_equals_all("bjohnson", "thunt").all == []
end
it "should have does not equal any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_does_not_equal_any("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
end
it "should have does not equal all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_does_not_equal_all("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
end
it "should have less than any" do
(5..7).each { |age| User.create(:age => age) }
User.age_less_than_any(7,6).all == User.find_all_by_age([5, 6])
end
it "should have less than all" do
(5..7).each { |age| User.create(:age => age) }
User.age_less_than_all(7,6).all == User.find_all_by_age(5)
end
it "should have less than or equal to any" do
(5..7).each { |age| User.create(:age => age) }
User.age_less_than_or_equal_to_any(7,6).all == User.find_all_by_age([5, 6, 7])
end
it "should have less than or equal to all" do
(5..7).each { |age| User.create(:age => age) }
User.age_less_than_or_equal_to_all(7,6).all == User.find_all_by_age([5, 6])
end
it "should have less than any" do
(5..7).each { |age| User.create(:age => age) }
User.age_greater_than_any(5,6).all == User.find_all_by_age([6, 7])
end
it "should have greater than all" do
(5..7).each { |age| User.create(:age => age) }
User.age_greater_than_all(5,6).all == User.find_all_by_age(7)
end
it "should have greater than or equal to any" do
(5..7).each { |age| User.create(:age => age) }
User.age_greater_than_or_equal_to_any(5,6).all == User.find_all_by_age([5, 6, 7])
end
it "should have greater than or equal to all" do
(5..7).each { |age| User.create(:age => age) }
User.age_greater_than_or_equal_to_all(5,6).all == User.find_all_by_age([6, 7])
end
it "should have like all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_like_all("bjohnson", "thunt").all == []
User.username_like_all("n", "o").all == User.find_all_by_username(["bjohnson", "thunt"])
end
it "should have like any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_like_all("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
end
it "should have begins with all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_begins_with_all("bjohnson", "thunt").all == []
end
it "should have begins with any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_begins_with_any("bj", "th").all == User.find_all_by_username(["bjohnson", "thunt"])
end
it "should have ends with all" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_ends_with_all("n", "r").all == []
end
it "should have ends with any" do
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
User.username_ends_with_any("n", "r").all == User.find_all_by_username(["bjohnson", "dgainor"])
end
end
context "alias conditions" do
it "should have is" do
User.age_is(5).proxy_options.should == User.age_equals(5).proxy_options
end
it "should have eq" do
User.age_eq(5).proxy_options.should == User.age_equals(5).proxy_options
end
it "should have not_equal_to" do
User.age_not_equal_to(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
end
it "should have is_not" do
User.age_is_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
end
it "should have not" do
User.age_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
end
it "should have ne" do
User.age_ne(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
end
it "should have lt" do
User.age_lt(5).proxy_options.should == User.age_less_than(5).proxy_options
end
it "should have lte" do
User.age_lte(5).proxy_options.should == User.age_less_than_or_equal_to(5).proxy_options
end
it "should have gt" do
User.age_gt(5).proxy_options.should == User.age_greater_than(5).proxy_options
end
it "should have gte" do
User.age_gte(5).proxy_options.should == User.age_greater_than_or_equal_to(5).proxy_options
end
it "should have contains" do
User.username_contains(5).proxy_options.should == User.username_like(5).proxy_options
end
it "should have contains" do
User.username_includes(5).proxy_options.should == User.username_like(5).proxy_options
end
it "should have bw" do
User.username_bw(5).proxy_options.should == User.username_begins_with(5).proxy_options
end
it "should have ew" do
User.username_ew(5).proxy_options.should == User.username_ends_with(5).proxy_options
end
it "should have nil" do
User.username_nil.proxy_options.should == User.username_nil.proxy_options
end
end
context "searchlogic lambda" do
it "should be a string" do
User.username_like("test")
User.named_scope_options(:username_like).searchlogic_arg_type.should == :string
end
it "should be an integer" do
User.id_gt(10)
User.named_scope_options(:id_gt).searchlogic_arg_type.should == :integer
end
it "should be a float" do
Order.total_gt(10)
Order.named_scope_options(:total_gt).searchlogic_arg_type.should == :float
end
end
it "should have priorty to columns over conflicting association conditions" do
Company.users_count_gt(10)
end
end