newellista-searchlogic 2.0.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,283 @@
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
+ context "type casting" do
140
+ it "should be a Boolean given true" do
141
+ search = User.search
142
+ search.id_nil = true
143
+ search.id_nil.should == true
144
+ end
145
+
146
+ it "should be a Boolean given 'true'" do
147
+ search = User.search
148
+ search.id_nil = "true"
149
+ search.id_nil.should == true
150
+ end
151
+
152
+ it "should be a Boolean given '1'" do
153
+ search = User.search
154
+ search.id_nil = "1"
155
+ search.id_nil.should == true
156
+ end
157
+
158
+ it "should be a Boolean given false" do
159
+ search = User.search
160
+ search.id_nil = false
161
+ search.id_nil.should == false
162
+ end
163
+
164
+ it "should be a Boolean given 'false'" do
165
+ search = User.search
166
+ search.id_nil = "false"
167
+ search.id_nil.should == false
168
+ end
169
+
170
+ it "should be a Boolean given '0'" do
171
+ search = User.search
172
+ search.id_nil = "0"
173
+ search.id_nil.should == false
174
+ end
175
+
176
+ it "should be an Integer given 1" do
177
+ search = User.search
178
+ search.id_gt = 1
179
+ search.id_gt.should == 1
180
+ end
181
+
182
+ it "should be an Integer given '1'" do
183
+ search = User.search
184
+ search.id_gt = "1"
185
+ search.id_gt.should == 1
186
+ end
187
+
188
+ it "should be a Float given 1.0" do
189
+ search = Order.search
190
+ search.total_gt = 1.0
191
+ search.total_gt.should == 1.0
192
+ end
193
+
194
+ it "should be a Float given '1'" do
195
+ search = Order.search
196
+ search.total_gt = "1"
197
+ search.total_gt.should == 1.0
198
+ end
199
+
200
+ it "should be a Float given '1.5'" do
201
+ search = Order.search
202
+ search.total_gt = "1.5"
203
+ search.total_gt.should == 1.5
204
+ end
205
+
206
+ it "should be a Date given 'Jan 1, 2009'" do
207
+ search = Order.search
208
+ search.shipped_on_after = "Jan 1, 2009"
209
+ search.shipped_on_after.should == Date.parse("Jan 1, 2009")
210
+ end
211
+
212
+ it "should be a Time given 'Jan 1, 2009'" do
213
+ search = Order.search
214
+ search.created_at_after = "Jan 1, 2009"
215
+ search.created_at_after.should == Time.parse("Jan 1, 2009")
216
+ end
217
+
218
+ it "should be a Time given 'Jan 1, 2009 9:33AM'" do
219
+ search = Order.search
220
+ search.created_at_after = "Jan 1, 2009 9:33AM"
221
+ search.created_at_after.should == Time.parse("Jan 1, 2009 9:33AM")
222
+ end
223
+
224
+ it "should be an Array and cast it's values given ['1', '2', '3']" do
225
+ search = Order.search
226
+ search.id_equals_any = ["1", "2", "3"]
227
+ search.id_equals_any.should == [1, 2, 3]
228
+ end
229
+
230
+ it "should type cast association conditions" do
231
+ search = User.search
232
+ search.orders_total_gt = "10"
233
+ search.orders_total_gt.should == 10
234
+ end
235
+
236
+ it "should type cast deep association conditions" do
237
+ search = Company.search
238
+ search.users_orders_total_gt = "10"
239
+ search.users_orders_total_gt.should == 10
240
+ end
241
+ end
242
+ end
243
+
244
+ context "taking action" do
245
+ it "should return all when not given any conditions" do
246
+ 3.times { User.create }
247
+ User.search.all.length.should == 3
248
+ end
249
+
250
+ it "should implement the current scope based on an association" do
251
+ User.create
252
+ company = Company.create
253
+ user = company.users.create
254
+ company.users.search.all.should == [user]
255
+ end
256
+
257
+ it "should implement the current scope based on a named scope" do
258
+ User.named_scope(:four_year_olds, :conditions => {:age => 4})
259
+ (3..5).each { |age| User.create(:age => age) }
260
+ User.four_year_olds.search.all.should == User.find_all_by_age(4)
261
+ end
262
+
263
+ it "should call named scopes for conditions" do
264
+ User.search(:age_less_than => 5).proxy_options.should == User.age_less_than(5).proxy_options
265
+ end
266
+
267
+ it "should alias exact column names to use equals" do
268
+ User.search(:username => "joe").proxy_options.should == User.username_equals("joe").proxy_options
269
+ end
270
+
271
+ it "should recognize conditions with a value of true where the named scope has an arity of 0" do
272
+ User.search(:username_nil => true).proxy_options.should == User.username_nil.proxy_options
273
+ end
274
+
275
+ it "should ignore conditions with a value of false where the named scope has an arity of 0" do
276
+ User.search(:username_nil => false).proxy_options.should == {}
277
+ end
278
+
279
+ it "should recognize the order condition" do
280
+ User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
281
+ end
282
+ end
283
+ end