skanev-searchlogic 2.1.8.1

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,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Association Ordering" do
4
+ it "should allow ascending" do
5
+ Company.ascend_by_users_username.proxy_options.should == User.ascend_by_username.proxy_options.merge(:joins => :users)
6
+ end
7
+
8
+ it "should allow descending" do
9
+ Company.descend_by_users_username.proxy_options.should == User.descend_by_username.proxy_options.merge(:joins => :users)
10
+ end
11
+
12
+ it "should allow deep ascending" do
13
+ Company.ascend_by_users_orders_total.proxy_options.should == Order.ascend_by_total.proxy_options.merge(:joins => {:users => :orders})
14
+ end
15
+
16
+ it "should allow deep descending" do
17
+ Company.descend_by_users_orders_total.proxy_options.should == Order.descend_by_total.proxy_options.merge(:joins => {:users => :orders})
18
+ end
19
+
20
+ it "should work through #order" do
21
+ Company.order('ascend_by_users_username').proxy_options.should == Company.ascend_by_users_username.proxy_options
22
+ end
23
+ end
@@ -0,0 +1,277 @@
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 not like" do
53
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
54
+ User.username_not_like("john").all.should == User.find_all_by_username("thunt")
55
+ end
56
+
57
+ it "should have begins with" do
58
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
59
+ User.username_begins_with("bj").all.should == User.find_all_by_username("bjohnson")
60
+ end
61
+
62
+ it "should have not begin with" do
63
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
64
+ User.username_not_begin_with("bj").all.should == User.find_all_by_username("thunt")
65
+ end
66
+
67
+ it "should have ends with" do
68
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
69
+ User.username_ends_with("son").all.should == User.find_all_by_username("bjohnson")
70
+ end
71
+
72
+ it "should have not end with" do
73
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
74
+ User.username_not_end_with("son").all.should == User.find_all_by_username("thunt")
75
+ end
76
+ end
77
+
78
+ context "boolean conditions" do
79
+ it "should have null" do
80
+ ["bjohnson", nil].each { |username| User.create(:username => username) }
81
+ User.username_null.all.should == User.find_all_by_username(nil)
82
+ end
83
+
84
+ it "should have not null" do
85
+ ["bjohnson", nil].each { |username| User.create(:username => username) }
86
+ User.username_not_null.all.should == User.find_all_by_username("bjohnson")
87
+ end
88
+
89
+ it "should have empty" do
90
+ ["bjohnson", ""].each { |username| User.create(:username => username) }
91
+ User.username_empty.all.should == User.find_all_by_username("")
92
+ end
93
+ end
94
+
95
+ context "any and all conditions" do
96
+ it "should do nothing if no arguments are passed" do
97
+ User.username_equals_any.proxy_options.should == {}
98
+ end
99
+
100
+ it "should have equals any" do
101
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
102
+ User.username_equals_any("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
103
+ end
104
+
105
+ it "should have equals all" do
106
+ %w(bjohnson thunt dainor).each { |username| User.create(:username => username) }
107
+ User.username_equals_all("bjohnson", "thunt").all == []
108
+ end
109
+
110
+ it "should have does not equal any" do
111
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
112
+ User.username_does_not_equal_any("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
113
+ end
114
+
115
+ it "should have does not equal all" do
116
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
117
+ User.username_does_not_equal_all("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
118
+ end
119
+
120
+ it "should have less than any" do
121
+ (5..7).each { |age| User.create(:age => age) }
122
+ User.age_less_than_any(7,6).all == User.find_all_by_age([5, 6])
123
+ end
124
+
125
+ it "should have less than all" do
126
+ (5..7).each { |age| User.create(:age => age) }
127
+ User.age_less_than_all(7,6).all == User.find_all_by_age(5)
128
+ end
129
+
130
+ it "should have less than or equal to any" do
131
+ (5..7).each { |age| User.create(:age => age) }
132
+ User.age_less_than_or_equal_to_any(7,6).all == User.find_all_by_age([5, 6, 7])
133
+ end
134
+
135
+ it "should have less than or equal to all" do
136
+ (5..7).each { |age| User.create(:age => age) }
137
+ User.age_less_than_or_equal_to_all(7,6).all == User.find_all_by_age([5, 6])
138
+ end
139
+
140
+ it "should have less than any" do
141
+ (5..7).each { |age| User.create(:age => age) }
142
+ User.age_greater_than_any(5,6).all == User.find_all_by_age([6, 7])
143
+ end
144
+
145
+ it "should have greater than all" do
146
+ (5..7).each { |age| User.create(:age => age) }
147
+ User.age_greater_than_all(5,6).all == User.find_all_by_age(7)
148
+ end
149
+
150
+ it "should have greater than or equal to any" do
151
+ (5..7).each { |age| User.create(:age => age) }
152
+ User.age_greater_than_or_equal_to_any(5,6).all == User.find_all_by_age([5, 6, 7])
153
+ end
154
+
155
+ it "should have greater than or equal to all" do
156
+ (5..7).each { |age| User.create(:age => age) }
157
+ User.age_greater_than_or_equal_to_all(5,6).all == User.find_all_by_age([6, 7])
158
+ end
159
+
160
+ it "should have like all" do
161
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
162
+ User.username_like_all("bjohnson", "thunt").all == []
163
+ User.username_like_all("n", "o").all == User.find_all_by_username(["bjohnson", "thunt"])
164
+ end
165
+
166
+ it "should have like any" do
167
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
168
+ User.username_like_all("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
169
+ end
170
+
171
+ it "should have begins with all" do
172
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
173
+ User.username_begins_with_all("bjohnson", "thunt").all == []
174
+ end
175
+
176
+ it "should have begins with any" do
177
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
178
+ User.username_begins_with_any("bj", "th").all == User.find_all_by_username(["bjohnson", "thunt"])
179
+ end
180
+
181
+ it "should have ends with all" do
182
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
183
+ User.username_ends_with_all("n", "r").all == []
184
+ end
185
+
186
+ it "should have ends with any" do
187
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
188
+ User.username_ends_with_any("n", "r").all == User.find_all_by_username(["bjohnson", "dgainor"])
189
+ end
190
+ end
191
+
192
+ context "alias conditions" do
193
+ it "should have is" do
194
+ User.age_is(5).proxy_options.should == User.age_equals(5).proxy_options
195
+ end
196
+
197
+ it "should have eq" do
198
+ User.age_eq(5).proxy_options.should == User.age_equals(5).proxy_options
199
+ end
200
+
201
+ it "should have not_equal_to" do
202
+ User.age_not_equal_to(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
203
+ end
204
+
205
+ it "should have is_not" do
206
+ # This is matching "not" first. How do you give priority in a regex? Because it's matching the
207
+ # 'not' condition and thinking the column is 'age_is'.
208
+ pending
209
+ User.age_is_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
210
+ end
211
+
212
+ it "should have not" do
213
+ User.age_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
214
+ end
215
+
216
+ it "should have ne" do
217
+ User.age_ne(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
218
+ end
219
+
220
+ it "should have lt" do
221
+ User.age_lt(5).proxy_options.should == User.age_less_than(5).proxy_options
222
+ end
223
+
224
+ it "should have lte" do
225
+ User.age_lte(5).proxy_options.should == User.age_less_than_or_equal_to(5).proxy_options
226
+ end
227
+
228
+ it "should have gt" do
229
+ User.age_gt(5).proxy_options.should == User.age_greater_than(5).proxy_options
230
+ end
231
+
232
+ it "should have gte" do
233
+ User.age_gte(5).proxy_options.should == User.age_greater_than_or_equal_to(5).proxy_options
234
+ end
235
+
236
+ it "should have contains" do
237
+ User.username_contains(5).proxy_options.should == User.username_like(5).proxy_options
238
+ end
239
+
240
+ it "should have contains" do
241
+ User.username_includes(5).proxy_options.should == User.username_like(5).proxy_options
242
+ end
243
+
244
+ it "should have bw" do
245
+ User.username_bw(5).proxy_options.should == User.username_begins_with(5).proxy_options
246
+ end
247
+
248
+ it "should have ew" do
249
+ User.username_ew(5).proxy_options.should == User.username_ends_with(5).proxy_options
250
+ end
251
+
252
+ it "should have nil" do
253
+ User.username_nil.proxy_options.should == User.username_nil.proxy_options
254
+ end
255
+ end
256
+
257
+ context "searchlogic lambda" do
258
+ it "should be a string" do
259
+ User.username_like("test")
260
+ User.named_scope_options(:username_like).searchlogic_arg_type.should == :string
261
+ end
262
+
263
+ it "should be an integer" do
264
+ User.id_gt(10)
265
+ User.named_scope_options(:id_gt).searchlogic_arg_type.should == :integer
266
+ end
267
+
268
+ it "should be a float" do
269
+ Order.total_gt(10)
270
+ Order.named_scope_options(:total_gt).searchlogic_arg_type.should == :float
271
+ end
272
+ end
273
+
274
+ it "should have priorty to columns over conflicting association conditions" do
275
+ Company.users_count_gt(10)
276
+ end
277
+ end
@@ -0,0 +1,27 @@
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 priorty to columns over conflicting association columns" do
25
+ Company.ascend_by_users_count
26
+ end
27
+ end
@@ -0,0 +1,295 @@
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 convert the time to the current zone" do
230
+ search = Order.search
231
+ now = Time.now
232
+ search.created_at_after = now
233
+ search.created_at_after.should == now.in_time_zone
234
+ end
235
+
236
+ it "should be an Array and cast it's values given ['1', '2', '3']" do
237
+ search = Order.search
238
+ search.id_equals_any = ["1", "2", "3"]
239
+ search.id_equals_any.should == [1, 2, 3]
240
+ end
241
+
242
+ it "should type cast association conditions" do
243
+ search = User.search
244
+ search.orders_total_gt = "10"
245
+ search.orders_total_gt.should == 10
246
+ end
247
+
248
+ it "should type cast deep association conditions" do
249
+ search = Company.search
250
+ search.users_orders_total_gt = "10"
251
+ search.users_orders_total_gt.should == 10
252
+ end
253
+ end
254
+ end
255
+
256
+ context "taking action" do
257
+ it "should return all when not given any conditions" do
258
+ 3.times { User.create }
259
+ User.search.all.length.should == 3
260
+ end
261
+
262
+ it "should implement the current scope based on an association" do
263
+ User.create
264
+ company = Company.create
265
+ user = company.users.create
266
+ company.users.search.all.should == [user]
267
+ end
268
+
269
+ it "should implement the current scope based on a named scope" do
270
+ User.named_scope(:four_year_olds, :conditions => {:age => 4})
271
+ (3..5).each { |age| User.create(:age => age) }
272
+ User.four_year_olds.search.all.should == User.find_all_by_age(4)
273
+ end
274
+
275
+ it "should call named scopes for conditions" do
276
+ User.search(:age_less_than => 5).proxy_options.should == User.age_less_than(5).proxy_options
277
+ end
278
+
279
+ it "should alias exact column names to use equals" do
280
+ User.search(:username => "joe").proxy_options.should == User.username_equals("joe").proxy_options
281
+ end
282
+
283
+ it "should recognize conditions with a value of true where the named scope has an arity of 0" do
284
+ User.search(:username_nil => true).proxy_options.should == User.username_nil.proxy_options
285
+ end
286
+
287
+ it "should ignore conditions with a value of false where the named scope has an arity of 0" do
288
+ User.search(:username_nil => false).proxy_options.should == {}
289
+ end
290
+
291
+ it "should recognize the order condition" do
292
+ User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
293
+ end
294
+ end
295
+ end