searchlogic-donotuse 2.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Or conditions" do
4
+ it "should define a scope by the exact same name as requested by the code" do
5
+ User.name_or_username_like('Test')
6
+ User.respond_to?(:name_or_username_like).should be_true
7
+ end
8
+
9
+ it "should match username or name" do
10
+ User.username_or_name_like("ben").proxy_options.should == {:conditions => "(users.username LIKE '%ben%') OR (users.name LIKE '%ben%')"}
11
+ end
12
+
13
+ it "should use the specified condition" do
14
+ User.username_begins_with_or_name_like("ben").proxy_options.should == {:conditions => "(users.username LIKE 'ben%') OR (users.name LIKE '%ben%')"}
15
+ end
16
+
17
+ it "should use the last specified condition" do
18
+ User.username_or_name_like_or_id_or_age_lt(10).proxy_options.should == {:conditions => "(users.username LIKE '%10%') OR (users.name LIKE '%10%') OR (users.id < 10) OR (users.age < 10)"}
19
+ end
20
+
21
+ it "should raise an error on unknown conditions" do
22
+ lambda { User.usernme_begins_with_or_name_like("ben") }.should raise_error(Searchlogic::NamedScopes::OrConditions::UnknownConditionError)
23
+ end
24
+
25
+ it "should work well with _or_equal_to" do
26
+ User.id_less_than_or_equal_to_or_age_gt(10).proxy_options.should == {:conditions => "(users.id <= 10) OR (users.age > 10)"}
27
+ end
28
+
29
+ it "should work well with _or_equal_to_any" do
30
+ User.id_less_than_or_equal_to_all_or_age_gt(10).proxy_options.should == {:conditions => "(users.id <= 10) OR (users.age > 10)"}
31
+ end
32
+
33
+ it "should work well with _or_equal_to_all" do
34
+ User.id_less_than_or_equal_to_any_or_age_gt(10).proxy_options.should == {:conditions => "(users.id <= 10) OR (users.age > 10)"}
35
+ end
36
+
37
+ it "should play nice with other scopes" do
38
+ User.username_begins_with("ben").id_gt(10).age_not_nil.username_or_name_ends_with("ben").scope(:find).should ==
39
+ {:conditions => "((users.username LIKE '%ben') OR (users.name LIKE '%ben')) AND ((users.age IS NOT NULL) AND ((users.id > 10) AND (users.username LIKE 'ben%')))"}
40
+ end
41
+
42
+ it "should play nice with scopes on associations" do
43
+ lambda { User.name_or_company_name_like("ben") }.should_not raise_error(Searchlogic::NamedScopes::OrConditions::NoConditionSpecifiedError)
44
+ User.name_or_company_name_like("ben").proxy_options.should == {:joins => :company, :conditions => "(users.name LIKE '%ben%') OR (companies.name LIKE '%ben%')"}
45
+ User.company_name_or_name_like("ben").proxy_options.should == {:joins => :company, :conditions => "(companies.name LIKE '%ben%') OR (users.name LIKE '%ben%')"}
46
+ User.company_name_or_company_description_like("ben").proxy_options.should == {:joins =>[:company], :conditions => "(companies.name LIKE '%ben%') OR (companies.description LIKE '%ben%')"}
47
+ end
48
+
49
+ it "should not get confused by the 'or' in find_or_create_by_* methods" do
50
+ User.create(:name => "Fred")
51
+ User.find_or_create_by_name("Fred").should be_a_kind_of User
52
+ end
53
+
54
+ it "should not get confused by the 'or' in compound find_or_create_by_* methods" do
55
+ User.create(:name => "Fred", :username => "fredb")
56
+ User.find_or_create_by_name_and_username("Fred", "fredb").should be_a_kind_of User
57
+ end
58
+
59
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Ordering" do
4
+ it "should be dynamically created and then cached" do
5
+ User.should_not respond_to(:ascend_by_username)
6
+ User.ascend_by_username
7
+ User.should respond_to(:ascend_by_username)
8
+ end
9
+
10
+ it "should have ascending" do
11
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
12
+ User.ascend_by_username.all.should == User.all(:order => "username ASC")
13
+ end
14
+
15
+ it "should have descending" do
16
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
17
+ User.descend_by_username.all.should == User.all(:order => "username DESC")
18
+ end
19
+
20
+ it "should have order" do
21
+ User.order("ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
22
+ end
23
+
24
+ it "should have order by custom scope" do
25
+ User.column_names.should_not include("custom")
26
+ %w(bjohnson thunt fisons).each { |username| User.create(:username => username) }
27
+ User.named_scope(:ascend_by_custom, :order => "username ASC, name DESC")
28
+ User.order("ascend_by_custom").proxy_options.should == User.ascend_by_custom.proxy_options
29
+ end
30
+
31
+ it "should have priorty to columns over conflicting association columns" do
32
+ Company.ascend_by_users_count
33
+ end
34
+ end
@@ -0,0 +1,369 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe "Search" do
4
+ context "implementation" do
5
+ it "should create a search proxy" do
6
+ User.search(:username => "joe").should be_kind_of(Searchlogic::Search)
7
+ end
8
+
9
+ it "should create a search proxy using the same class" do
10
+ User.search.klass.should == User
11
+ end
12
+
13
+ it "should pass on the current scope to the proxy" do
14
+ company = Company.create
15
+ user = company.users.create
16
+ search = company.users.search
17
+ search.current_scope.should == company.users.scope(:find)
18
+ end
19
+ end
20
+
21
+ context "initialization" do
22
+ it "should require a class" do
23
+ lambda { Searchlogic::Search.new }.should raise_error(ArgumentError)
24
+ end
25
+
26
+ it "should set the conditions" do
27
+ search = User.search(:username => "bjohnson")
28
+ search.conditions.should == {:username => "bjohnson"}
29
+ end
30
+ end
31
+
32
+ it "should clone properly" do
33
+ company = Company.create
34
+ user1 = company.users.create(:age => 5)
35
+ user2 = company.users.create(:age => 25)
36
+ search1 = company.users.search(:age_gt => 10)
37
+ search2 = search1.clone
38
+ search2.age_gt = 1
39
+ search2.all.should == User.all
40
+ search1.all.should == [user2]
41
+ end
42
+
43
+ it "should clone properly without scope" do
44
+ user1 = User.create(:age => 5)
45
+ user2 = User.create(:age => 25)
46
+ search1 = User.search(:age_gt => 10)
47
+ search2 = search1.clone
48
+ search2.age_gt = 1
49
+ search2.all.should == User.all
50
+ search1.all.should == [user2]
51
+ end
52
+
53
+ it "should delete the condition" do
54
+ search = User.search(:username_like => "bjohnson")
55
+ search.delete("username_like")
56
+ search.username_like.should be_nil
57
+ end
58
+
59
+ context "conditions" do
60
+ it "should set the conditions and be accessible individually" do
61
+ search = User.search
62
+ search.conditions = {:username => "bjohnson"}
63
+ search.username.should == "bjohnson"
64
+ end
65
+
66
+ it "should set the conditions and allow string keys" do
67
+ search = User.search
68
+ search.conditions = {"username" => "bjohnson"}
69
+ search.username.should == "bjohnson"
70
+ end
71
+
72
+ it "should ignore blank values" do
73
+ search = User.search
74
+ search.conditions = {"username" => ""}
75
+ search.username.should be_nil
76
+ end
77
+
78
+ it "should use custom scopes before normalizing" do
79
+ User.create(:username => "bjohnson")
80
+ User.named_scope :username, lambda { |value| {:conditions => {:username => value.reverse}} }
81
+ search1 = User.search(:username => "bjohnson")
82
+ search2 = User.search(:username => "nosnhojb")
83
+ search1.count.should == 0
84
+ search2.count.should == 1
85
+ end
86
+
87
+ it "should ignore blank values in arrays" do
88
+ search = User.search
89
+ search.conditions = {"username_equals_any" => [""]}
90
+ search.username_equals_any.should be_blank
91
+ end
92
+ end
93
+
94
+ context "condition accessors" do
95
+ it "should allow setting exact columns individually" do
96
+ search = User.search
97
+ search.username = "bjohnson"
98
+ search.username.should == "bjohnson"
99
+ end
100
+
101
+ it "should allow setting local column conditions individually" do
102
+ search = User.search
103
+ search.username_gt = "bjohnson"
104
+ search.username_gt.should == "bjohnson"
105
+ end
106
+
107
+ it "should allow chaining conditions" do
108
+ user = User.create(:username => "bjohnson", :age => 20)
109
+ User.create(:username => "bjohnson", :age => 5)
110
+ search = User.search
111
+ search.username_equals("bjohnson").age_gt(10)
112
+ search.all.should == [user]
113
+ end
114
+
115
+ it "should allow setting association conditions" do
116
+ search = User.search
117
+ search.orders_total_gt = 10
118
+ search.orders_total_gt.should == 10
119
+ end
120
+
121
+ it "should allow setting pre-existing association conditions" do
122
+ User.named_scope :uname, lambda { |value| {:conditions => ["users.username = ?", value]} }
123
+ search = Company.search
124
+ search.users_uname = "bjohnson"
125
+ search.users_uname.should == "bjohnson"
126
+ end
127
+
128
+ it "should allow setting pre-existing association alias conditions" do
129
+ User.alias_scope :username_has, lambda { |value| User.username_like(value) }
130
+ search = Company.search
131
+ search.users_username_has = "bjohnson"
132
+ search.users_username_has.should == "bjohnson"
133
+ end
134
+
135
+ it "should allow using custom conditions" do
136
+ User.named_scope(:four_year_olds, { :conditions => { :age => 4 } })
137
+ search = User.search
138
+ search.four_year_olds = true
139
+ search.four_year_olds.should == true
140
+ search.proxy_options.should == User.four_year_olds.proxy_options
141
+ end
142
+
143
+ it "should not merge conflicting conditions into one value" do
144
+ # This class should JUST be a proxy. It should not do anything more than that.
145
+ # A user would be allowed to call both named scopes if they wanted.
146
+ search = User.search
147
+ search.username_greater_than = "bjohnson1"
148
+ search.username_gt = "bjohnson2"
149
+ search.username_greater_than.should == "bjohnson1"
150
+ search.username_gt.should == "bjohnson2"
151
+ end
152
+
153
+ it "should allow setting custom conditions individually with an arity of 0" do
154
+ User.named_scope(:four_year_olds, :conditions => {:age => 4})
155
+ search = User.search
156
+ search.four_year_olds = true
157
+ search.four_year_olds.should == true
158
+ end
159
+
160
+ it "should allow setting custom conditions individually with an arity of 1" do
161
+ User.named_scope(:username_should_be, lambda { |u| {:conditions => {:username => u}} })
162
+ search = User.search
163
+ search.username_should_be = "bjohnson"
164
+ search.username_should_be.should == "bjohnson"
165
+ end
166
+
167
+ it "should not allow setting conditions that are not scopes" do
168
+ search = User.search
169
+ lambda { search.unknown = true }.should raise_error(Searchlogic::Search::UnknownConditionError)
170
+ end
171
+
172
+ it "should not allow setting conditions on sensitive methods" do
173
+ search = User.search
174
+ lambda { search.destroy = true }.should raise_error(Searchlogic::Search::UnknownConditionError)
175
+ end
176
+
177
+ it "should not use the ruby implementation of the id method" do
178
+ search = User.search
179
+ search.id.should be_nil
180
+ end
181
+
182
+ context "type casting" do
183
+ it "should be a Boolean given true" do
184
+ search = User.search
185
+ search.id_nil = true
186
+ search.id_nil.should == true
187
+ end
188
+
189
+ it "should be a Boolean given 'true'" do
190
+ search = User.search
191
+ search.id_nil = "true"
192
+ search.id_nil.should == true
193
+ end
194
+
195
+ it "should be a Boolean given '1'" do
196
+ search = User.search
197
+ search.id_nil = "1"
198
+ search.id_nil.should == true
199
+ end
200
+
201
+ it "should be a Boolean given false" do
202
+ search = User.search
203
+ search.id_nil = false
204
+ search.id_nil.should == false
205
+ end
206
+
207
+ it "should be a Boolean given 'false'" do
208
+ search = User.search
209
+ search.id_nil = "false"
210
+ search.id_nil.should == false
211
+ end
212
+
213
+ it "should be a Boolean given '0'" do
214
+ search = User.search
215
+ search.id_nil = "0"
216
+ search.id_nil.should == false
217
+ end
218
+
219
+ it "should be an Integer given 1" do
220
+ search = User.search
221
+ search.id_gt = 1
222
+ search.id_gt.should == 1
223
+ end
224
+
225
+ it "should be an Integer given '1'" do
226
+ search = User.search
227
+ search.id_gt = "1"
228
+ search.id_gt.should == 1
229
+ end
230
+
231
+ it "should be a Float given 1.0" do
232
+ search = Order.search
233
+ search.total_gt = 1.0
234
+ search.total_gt.should == 1.0
235
+ end
236
+
237
+ it "should be a Float given '1'" do
238
+ search = Order.search
239
+ search.total_gt = "1"
240
+ search.total_gt.should == 1.0
241
+ end
242
+
243
+ it "should be a Float given '1.5'" do
244
+ search = Order.search
245
+ search.total_gt = "1.5"
246
+ search.total_gt.should == 1.5
247
+ end
248
+
249
+ it "should be a Range given 1..3" do
250
+ search = Order.search
251
+ search.total_eq = (1..3)
252
+ search.total_eq.should == (1..3)
253
+ end
254
+
255
+ it "should be a Date given 'Jan 1, 2009'" do
256
+ search = Order.search
257
+ search.shipped_on_after = "Jan 1, 2009"
258
+ search.shipped_on_after.should == Date.parse("Jan 1, 2009")
259
+ end
260
+
261
+ it "should be a Time given 'Jan 1, 2009'" do
262
+ search = Order.search
263
+ search.created_at_after = "Jan 1, 2009"
264
+ search.created_at_after.should == Time.parse("Jan 1, 2009")
265
+ end
266
+
267
+ it "should be a Time given 'Jan 1, 2009 9:33AM'" do
268
+ search = Order.search
269
+ search.created_at_after = "Jan 1, 2009 9:33AM"
270
+ search.created_at_after.should == Time.parse("Jan 1, 2009 9:33AM")
271
+ end
272
+
273
+ it "should convert the time to the current zone" do
274
+ search = Order.search
275
+ now = Time.now
276
+ search.created_at_after = now
277
+ search.created_at_after.should == now.in_time_zone
278
+ end
279
+
280
+ it "should be an Array and cast it's values given ['1', '2', '3']" do
281
+ search = Order.search
282
+ search.id_equals_any = ["1", "2", "3"]
283
+ search.id_equals_any.should == [1, 2, 3]
284
+ end
285
+
286
+ it "should type cast association conditions" do
287
+ search = User.search
288
+ search.orders_total_gt = "10"
289
+ search.orders_total_gt.should == 10
290
+ end
291
+
292
+ it "should type cast deep association conditions" do
293
+ search = Company.search
294
+ search.users_orders_total_gt = "10"
295
+ search.users_orders_total_gt.should == 10
296
+ end
297
+ end
298
+ end
299
+
300
+ context "taking action" do
301
+ it "should return all when not given any conditions" do
302
+ 3.times { User.create }
303
+ User.search.all.length.should == 3
304
+ end
305
+
306
+ it "should implement the current scope based on an association" do
307
+ User.create
308
+ company = Company.create
309
+ user = company.users.create
310
+ company.users.search.all.should == [user]
311
+ end
312
+
313
+ it "should implement the current scope based on a named scope" do
314
+ User.named_scope(:four_year_olds, :conditions => {:age => 4})
315
+ (3..5).each { |age| User.create(:age => age) }
316
+ User.four_year_olds.search.all.should == User.find_all_by_age(4)
317
+ end
318
+
319
+ it "should call named scopes for conditions" do
320
+ User.search(:age_less_than => 5).proxy_options.should == User.age_less_than(5).proxy_options
321
+ end
322
+
323
+ it "should alias exact column names to use equals" do
324
+ User.search(:username => "joe").proxy_options.should == User.username_equals("joe").proxy_options
325
+ end
326
+
327
+ it "should recognize conditions with a value of true where the named scope has an arity of 0" do
328
+ User.search(:username_nil => true).proxy_options.should == User.username_nil.proxy_options
329
+ end
330
+
331
+ it "should ignore conditions with a value of false where the named scope has an arity of 0" do
332
+ User.search(:username_nil => false).proxy_options.should == {}
333
+ end
334
+
335
+ it "should not ignore conditions with a value of false where the named scope does not have an arity of 0" do
336
+ User.search(:username_is => false).proxy_options.should == User.username_is(false).proxy_options
337
+ end
338
+
339
+ it "should recognize the order condition" do
340
+ User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
341
+ end
342
+
343
+ it "should pass array values as multiple arguments" do
344
+ User.named_scope(:multiple_args, lambda { |*args|
345
+ raise "This should not be an array, it should be 1" if args.first.is_a?(Array)
346
+ {:conditions => ["id IN (?)", args]}
347
+ })
348
+ User.search(:multiple_args => [1,2]).proxy_options.should == User.multiple_args(1,2).proxy_options
349
+ end
350
+ end
351
+
352
+ context "method delegation" do
353
+ it "should respond to count" do
354
+ User.create(:username => "bjohnson")
355
+ search1 = User.search(:username => "bjohnson")
356
+ search2 = User.search(:username => "nosnhojb")
357
+ search1.count.should == 1
358
+ search2.count.should == 0
359
+ end
360
+
361
+ it "should respond to empty?" do
362
+ User.create(:username => "bjohnson")
363
+ search1 = User.search(:username => "bjohnson")
364
+ search2 = User.search(:username => "nosnhojb")
365
+ search1.empty?.should == false
366
+ search2.empty?.should == true
367
+ end
368
+ end
369
+ end