joost-searchlogic 2.1.5.2

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,253 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Conditions" do
4
+ it "should be dynamically created and then cached" do
5
+ User.should_not respond_to(:age_less_than)
6
+ User.age_less_than(5)
7
+ User.should respond_to(:age_less_than)
8
+ end
9
+
10
+ it "should not allow conditions on non columns" do
11
+ lambda { User.whatever_equals(2) }.should raise_error(NoMethodError)
12
+ end
13
+
14
+ context "comparison conditions" do
15
+ it "should have equals" do
16
+ (5..7).each { |age| User.create(:age => age) }
17
+ User.age_equals(6).all.should == User.find_all_by_age(6)
18
+ end
19
+
20
+ it "should have does not equal" do
21
+ (5..7).each { |age| User.create(:age => age) }
22
+ User.age_does_not_equal(6).all.should == User.find_all_by_age([5,7])
23
+ end
24
+
25
+ it "should have less than" do
26
+ (5..7).each { |age| User.create(:age => age) }
27
+ User.age_less_than(6).all.should == User.find_all_by_age(5)
28
+ end
29
+
30
+ it "should have less than or equal to" do
31
+ (5..7).each { |age| User.create(:age => age) }
32
+ User.age_less_than_or_equal_to(6).all.should == User.find_all_by_age([5, 6])
33
+ end
34
+
35
+ it "should have greater than" do
36
+ (5..7).each { |age| User.create(:age => age) }
37
+ User.age_greater_than(6).all.should == User.find_all_by_age(7)
38
+ end
39
+
40
+ it "should have greater than or equal to" do
41
+ (5..7).each { |age| User.create(:age => age) }
42
+ User.age_greater_than_or_equal_to(6).all.should == User.find_all_by_age([6, 7])
43
+ end
44
+ end
45
+
46
+ context "wildcard conditions" do
47
+ it "should have like" do
48
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
49
+ User.username_like("john").all.should == User.find_all_by_username("bjohnson")
50
+ end
51
+
52
+ it "should have begins with" do
53
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
54
+ User.username_begins_with("bj").all.should == User.find_all_by_username("bjohnson")
55
+ end
56
+
57
+ it "should have ends with" do
58
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
59
+ User.username_ends_with("son").all.should == User.find_all_by_username("bjohnson")
60
+ end
61
+ end
62
+
63
+ context "boolean conditions" do
64
+ it "should have null" do
65
+ ["bjohnson", nil].each { |username| User.create(:username => username) }
66
+ User.username_null.all.should == User.find_all_by_username(nil)
67
+ end
68
+
69
+ it "should have empty" do
70
+ ["bjohnson", ""].each { |username| User.create(:username => username) }
71
+ User.username_empty.all.should == User.find_all_by_username("")
72
+ end
73
+ end
74
+
75
+ context "any and all conditions" do
76
+ it "should do nothing if no arguments are passed" do
77
+ User.username_equals_any.proxy_options.should == {}
78
+ end
79
+
80
+ it "should have equals any" do
81
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
82
+ User.username_equals_any("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
83
+ end
84
+
85
+ it "should have equals all" do
86
+ %w(bjohnson thunt dainor).each { |username| User.create(:username => username) }
87
+ User.username_equals_all("bjohnson", "thunt").all == []
88
+ end
89
+
90
+ it "should have does not equal any" do
91
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
92
+ User.username_does_not_equal_any("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
93
+ end
94
+
95
+ it "should have does not equal all" do
96
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
97
+ User.username_does_not_equal_all("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
98
+ end
99
+
100
+ it "should have less than any" do
101
+ (5..7).each { |age| User.create(:age => age) }
102
+ User.age_less_than_any(7,6).all == User.find_all_by_age([5, 6])
103
+ end
104
+
105
+ it "should have less than all" do
106
+ (5..7).each { |age| User.create(:age => age) }
107
+ User.age_less_than_all(7,6).all == User.find_all_by_age(5)
108
+ end
109
+
110
+ it "should have less than or equal to any" do
111
+ (5..7).each { |age| User.create(:age => age) }
112
+ User.age_less_than_or_equal_to_any(7,6).all == User.find_all_by_age([5, 6, 7])
113
+ end
114
+
115
+ it "should have less than or equal to all" do
116
+ (5..7).each { |age| User.create(:age => age) }
117
+ User.age_less_than_or_equal_to_all(7,6).all == User.find_all_by_age([5, 6])
118
+ end
119
+
120
+ it "should have less than any" do
121
+ (5..7).each { |age| User.create(:age => age) }
122
+ User.age_greater_than_any(5,6).all == User.find_all_by_age([6, 7])
123
+ end
124
+
125
+ it "should have greater than all" do
126
+ (5..7).each { |age| User.create(:age => age) }
127
+ User.age_greater_than_all(5,6).all == User.find_all_by_age(7)
128
+ end
129
+
130
+ it "should have greater than or equal to any" do
131
+ (5..7).each { |age| User.create(:age => age) }
132
+ User.age_greater_than_or_equal_to_any(5,6).all == User.find_all_by_age([5, 6, 7])
133
+ end
134
+
135
+ it "should have greater than or equal to all" do
136
+ (5..7).each { |age| User.create(:age => age) }
137
+ User.age_greater_than_or_equal_to_all(5,6).all == User.find_all_by_age([6, 7])
138
+ end
139
+
140
+ it "should have like all" do
141
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
142
+ User.username_like_all("bjohnson", "thunt").all == []
143
+ User.username_like_all("n", "o").all == User.find_all_by_username(["bjohnson", "thunt"])
144
+ end
145
+
146
+ it "should have like any" do
147
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
148
+ User.username_like_all("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
149
+ end
150
+
151
+ it "should have begins with all" do
152
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
153
+ User.username_begins_with_all("bjohnson", "thunt").all == []
154
+ end
155
+
156
+ it "should have begins with any" do
157
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
158
+ User.username_begins_with_any("bj", "th").all == User.find_all_by_username(["bjohnson", "thunt"])
159
+ end
160
+
161
+ it "should have ends with all" do
162
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
163
+ User.username_ends_with_all("n", "r").all == []
164
+ end
165
+
166
+ it "should have ends with any" do
167
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
168
+ User.username_ends_with_any("n", "r").all == User.find_all_by_username(["bjohnson", "dgainor"])
169
+ end
170
+ end
171
+
172
+ context "alias conditions" do
173
+ it "should have is" do
174
+ User.age_is(5).proxy_options.should == User.age_equals(5).proxy_options
175
+ end
176
+
177
+ it "should have eq" do
178
+ User.age_eq(5).proxy_options.should == User.age_equals(5).proxy_options
179
+ end
180
+
181
+ it "should have not_equal_to" do
182
+ User.age_not_equal_to(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
183
+ end
184
+
185
+ it "should have is_not" do
186
+ # This is matching "not" first. How do you give priority in a regex? Because it's matching the
187
+ # 'not' condition and thinking the column is 'age_is'.
188
+ pending
189
+ User.age_is_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
190
+ end
191
+
192
+ it "should have not" do
193
+ User.age_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
194
+ end
195
+
196
+ it "should have ne" do
197
+ User.age_ne(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
198
+ end
199
+
200
+ it "should have lt" do
201
+ User.age_lt(5).proxy_options.should == User.age_less_than(5).proxy_options
202
+ end
203
+
204
+ it "should have lte" do
205
+ User.age_lte(5).proxy_options.should == User.age_less_than_or_equal_to(5).proxy_options
206
+ end
207
+
208
+ it "should have gt" do
209
+ User.age_gt(5).proxy_options.should == User.age_greater_than(5).proxy_options
210
+ end
211
+
212
+ it "should have gte" do
213
+ User.age_gte(5).proxy_options.should == User.age_greater_than_or_equal_to(5).proxy_options
214
+ end
215
+
216
+ it "should have contains" do
217
+ User.username_contains(5).proxy_options.should == User.username_like(5).proxy_options
218
+ end
219
+
220
+ it "should have contains" do
221
+ User.username_includes(5).proxy_options.should == User.username_like(5).proxy_options
222
+ end
223
+
224
+ it "should have bw" do
225
+ User.username_bw(5).proxy_options.should == User.username_begins_with(5).proxy_options
226
+ end
227
+
228
+ it "should have ew" do
229
+ User.username_ew(5).proxy_options.should == User.username_ends_with(5).proxy_options
230
+ end
231
+
232
+ it "should have nil" do
233
+ User.username_nil.proxy_options.should == User.username_nil.proxy_options
234
+ end
235
+ end
236
+
237
+ context "searchlogic lambda" do
238
+ it "should be a string" do
239
+ User.username_like("test")
240
+ User.named_scope_options(:username_like).searchlogic_arg_type.should == :string
241
+ end
242
+
243
+ it "should be an integer" do
244
+ User.id_gt(10)
245
+ User.named_scope_options(:id_gt).searchlogic_arg_type.should == :integer
246
+ end
247
+
248
+ it "should be a float" do
249
+ Order.total_gt(10)
250
+ Order.named_scope_options(:total_gt).searchlogic_arg_type.should == :float
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,23 @@
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
+ end
@@ -0,0 +1,288 @@
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 delete the condition" do
44
+ search = User.search(:username_like => "bjohnson")
45
+ search.delete("username_like")
46
+ search.username_like.should be_nil
47
+ end
48
+
49
+ context "conditions" do
50
+ it "should set the conditions and be accessible individually" do
51
+ search = User.search
52
+ search.conditions = {:username => "bjohnson"}
53
+ search.username.should == "bjohnson"
54
+ end
55
+
56
+ it "should set the conditions and allow string keys" do
57
+ search = User.search
58
+ search.conditions = {"username" => "bjohnson"}
59
+ search.username.should == "bjohnson"
60
+ end
61
+
62
+ it "should ignore blank values" do
63
+ search = User.search
64
+ search.conditions = {"username" => ""}
65
+ search.username.should be_nil
66
+ end
67
+
68
+ it "should ignore blank values in arrays" do
69
+ search = User.search
70
+ search.conditions = {"username_equals_any" => [""]}
71
+ search.username_equals_any.should be_blank
72
+ end
73
+ end
74
+
75
+ context "condition accessors" do
76
+ it "should allow setting exact columns individually" do
77
+ search = User.search
78
+ search.username = "bjohnson"
79
+ search.username.should == "bjohnson"
80
+ end
81
+
82
+ it "should allow setting local column conditions individually" do
83
+ search = User.search
84
+ search.username_gt = "bjohnson"
85
+ search.username_gt.should == "bjohnson"
86
+ end
87
+
88
+ it "should allow chainging conditions" do
89
+ user = User.create(:username => "bjohnson", :age => 20)
90
+ User.create(:username => "bjohnson", :age => 5)
91
+ search = User.search
92
+ search.username_equals("bjohnson").age_gt(10)
93
+ search.all.should == [user]
94
+ end
95
+
96
+ it "should allow setting association conditions" do
97
+ search = User.search
98
+ search.orders_total_gt = 10
99
+ search.orders_total_gt.should == 10
100
+ end
101
+
102
+ it "should allow using custom conditions" do
103
+ User.named_scope(:four_year_olds, { :conditions => { :age => 4 } })
104
+ search = User.search
105
+ search.four_year_olds = true
106
+ search.four_year_olds.should == true
107
+ search.proxy_options.should == User.four_year_olds.proxy_options
108
+ end
109
+
110
+ it "should not merge conflicting conditions into one value" do
111
+ # This class should JUST be a proxy. It should not do anything more than that.
112
+ # A user would be allowed to call both named scopes if they wanted.
113
+ search = User.search
114
+ search.username_greater_than = "bjohnson1"
115
+ search.username_gt = "bjohnson2"
116
+ search.username_greater_than.should == "bjohnson1"
117
+ search.username_gt.should == "bjohnson2"
118
+ end
119
+
120
+ it "should allow setting custom conditions individually with an arity of 0" do
121
+ User.named_scope(:four_year_olds, :conditions => {:age => 4})
122
+ search = User.search
123
+ search.four_year_olds = true
124
+ search.four_year_olds.should == true
125
+ end
126
+
127
+ it "should allow setting custom conditions individually with an arity of 1" do
128
+ User.named_scope(:username_should_be, lambda { |u| {:conditions => {:username => u}} })
129
+ search = User.search
130
+ search.username_should_be = "bjohnson"
131
+ search.username_should_be.should == "bjohnson"
132
+ end
133
+
134
+ it "should not allow setting conditions that are not scopes" do
135
+ search = User.search
136
+ lambda { search.unknown = true }.should raise_error(Searchlogic::Search::UnknownConditionError)
137
+ end
138
+
139
+ it "should not use the ruby implementation of the id method" do
140
+ search = User.search
141
+ search.id.should be_nil
142
+ end
143
+
144
+ context "type casting" do
145
+ it "should be a Boolean given true" do
146
+ search = User.search
147
+ search.id_nil = true
148
+ search.id_nil.should == true
149
+ end
150
+
151
+ it "should be a Boolean given 'true'" do
152
+ search = User.search
153
+ search.id_nil = "true"
154
+ search.id_nil.should == true
155
+ end
156
+
157
+ it "should be a Boolean given '1'" do
158
+ search = User.search
159
+ search.id_nil = "1"
160
+ search.id_nil.should == true
161
+ end
162
+
163
+ it "should be a Boolean given false" do
164
+ search = User.search
165
+ search.id_nil = false
166
+ search.id_nil.should == false
167
+ end
168
+
169
+ it "should be a Boolean given 'false'" do
170
+ search = User.search
171
+ search.id_nil = "false"
172
+ search.id_nil.should == false
173
+ end
174
+
175
+ it "should be a Boolean given '0'" do
176
+ search = User.search
177
+ search.id_nil = "0"
178
+ search.id_nil.should == false
179
+ end
180
+
181
+ it "should be an Integer given 1" do
182
+ search = User.search
183
+ search.id_gt = 1
184
+ search.id_gt.should == 1
185
+ end
186
+
187
+ it "should be an Integer given '1'" do
188
+ search = User.search
189
+ search.id_gt = "1"
190
+ search.id_gt.should == 1
191
+ end
192
+
193
+ it "should be a Float given 1.0" do
194
+ search = Order.search
195
+ search.total_gt = 1.0
196
+ search.total_gt.should == 1.0
197
+ end
198
+
199
+ it "should be a Float given '1'" do
200
+ search = Order.search
201
+ search.total_gt = "1"
202
+ search.total_gt.should == 1.0
203
+ end
204
+
205
+ it "should be a Float given '1.5'" do
206
+ search = Order.search
207
+ search.total_gt = "1.5"
208
+ search.total_gt.should == 1.5
209
+ end
210
+
211
+ it "should be a Date given 'Jan 1, 2009'" do
212
+ search = Order.search
213
+ search.shipped_on_after = "Jan 1, 2009"
214
+ search.shipped_on_after.should == Date.parse("Jan 1, 2009")
215
+ end
216
+
217
+ it "should be a Time given 'Jan 1, 2009'" do
218
+ search = Order.search
219
+ search.created_at_after = "Jan 1, 2009"
220
+ search.created_at_after.should == Time.parse("Jan 1, 2009")
221
+ end
222
+
223
+ it "should be a Time given 'Jan 1, 2009 9:33AM'" do
224
+ search = Order.search
225
+ search.created_at_after = "Jan 1, 2009 9:33AM"
226
+ search.created_at_after.should == Time.parse("Jan 1, 2009 9:33AM")
227
+ end
228
+
229
+ it "should be an Array and cast it's values given ['1', '2', '3']" do
230
+ search = Order.search
231
+ search.id_equals_any = ["1", "2", "3"]
232
+ search.id_equals_any.should == [1, 2, 3]
233
+ end
234
+
235
+ it "should type cast association conditions" do
236
+ search = User.search
237
+ search.orders_total_gt = "10"
238
+ search.orders_total_gt.should == 10
239
+ end
240
+
241
+ it "should type cast deep association conditions" do
242
+ search = Company.search
243
+ search.users_orders_total_gt = "10"
244
+ search.users_orders_total_gt.should == 10
245
+ end
246
+ end
247
+ end
248
+
249
+ context "taking action" do
250
+ it "should return all when not given any conditions" do
251
+ 3.times { User.create }
252
+ User.search.all.length.should == 3
253
+ end
254
+
255
+ it "should implement the current scope based on an association" do
256
+ User.create
257
+ company = Company.create
258
+ user = company.users.create
259
+ company.users.search.all.should == [user]
260
+ end
261
+
262
+ it "should implement the current scope based on a named scope" do
263
+ User.named_scope(:four_year_olds, :conditions => {:age => 4})
264
+ (3..5).each { |age| User.create(:age => age) }
265
+ User.four_year_olds.search.all.should == User.find_all_by_age(4)
266
+ end
267
+
268
+ it "should call named scopes for conditions" do
269
+ User.search(:age_less_than => 5).proxy_options.should == User.age_less_than(5).proxy_options
270
+ end
271
+
272
+ it "should alias exact column names to use equals" do
273
+ User.search(:username => "joe").proxy_options.should == User.username_equals("joe").proxy_options
274
+ end
275
+
276
+ it "should recognize conditions with a value of true where the named scope has an arity of 0" do
277
+ User.search(:username_nil => true).proxy_options.should == User.username_nil.proxy_options
278
+ end
279
+
280
+ it "should ignore conditions with a value of false where the named scope has an arity of 0" do
281
+ User.search(:username_nil => false).proxy_options.should == {}
282
+ end
283
+
284
+ it "should recognize the order condition" do
285
+ User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
286
+ end
287
+ end
288
+ end