/
search_proxy_spec.rb
228 lines (191 loc) · 7.25 KB
/
search_proxy_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
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
describe "SearchProxy" do
context "initialization" do
it "should require a class" do
lambda { Searchlogic::SearchProxy.new }.should raise_error(ArgumentError)
end
it "should set the conditions" do
search = User.search(:username => "bjohnson")
search.conditions.should == {:username => "bjohnson"}
end
end
context "conditions" do
it "should set the conditions and be accessible individually" do
search = User.search
search.conditions = {:username => "bjohnson"}
search.username.should == "bjohnson"
end
it "should set the conditions and allow string keys" do
search = User.search
search.conditions = {"username" => "bjohnson"}
search.username.should == "bjohnson"
end
it "should ignore blank values" do
search = User.search
search.conditions = {"username" => ""}
search.username.should be_nil
end
end
context "condition accessors" do
it "should allow setting exact columns individually" do
search = User.search
search.username = "bjohnson"
search.username.should == "bjohnson"
end
it "should allow setting local column conditions individually" do
search = User.search
search.username_gt = "bjohnson"
search.username_gt.should == "bjohnson"
end
it "should allow setting association conditions" do
search = User.search
search.orders_total_gt = 10
search.orders_total_gt.should == 10
end
it "should allow using custom conditions" do
User.named_scope(:four_year_olds, { :conditions => { :age => 4 } })
search = User.search
search.four_year_olds = true
search.four_year_olds.should == true
search.proxy_options.should == User.four_year_olds.proxy_options
end
it "should not merge conflicting conditions into one value" do
# This class should JUST be a proxy. It should not do anything more than that.
# A user would be allowed to call both named scopes if they wanted.
search = User.search
search.username_greater_than = "bjohnson1"
search.username_gt = "bjohnson2"
search.username_greater_than.should == "bjohnson1"
search.username_gt.should == "bjohnson2"
end
it "should allow setting custom conditions individually" do
User.named_scope(:four_year_olds, :conditions => {:age => 4})
search = User.search
search.four_year_olds = true
search.four_year_olds.should == true
end
it "should not allow setting conditions that are not scopes" do
search = User.search
lambda { search.unknown = true }.should raise_error(Searchlogic::SearchProxy::UnknownConditionError)
end
context "type casting" do
it "should be a Boolean given true" do
search = User.search
search.id_nil = true
search.id_nil.should == true
end
it "should be a Boolean given 'true'" do
search = User.search
search.id_nil = "true"
search.id_nil.should == true
end
it "should be a Boolean given '1'" do
search = User.search
search.id_nil = "1"
search.id_nil.should == true
end
it "should be a Boolean given false" do
search = User.search
search.id_nil = false
search.id_nil.should == false
end
it "should be a Boolean given 'false'" do
search = User.search
search.id_nil = "false"
search.id_nil.should == false
end
it "should be a Boolean given '0'" do
search = User.search
search.id_nil = "0"
search.id_nil.should == false
end
it "should be an Integer given 1" do
search = User.search
search.id_gt = 1
search.id_gt.should == 1
end
it "should be an Integer given '1'" do
search = User.search
search.id_gt = "1"
search.id_gt.should == 1
end
it "should be a Float given 1.0" do
search = Order.search
search.total_gt = 1.0
search.total_gt.should == 1.0
end
it "should be a Float given '1'" do
search = Order.search
search.total_gt = "1"
search.total_gt.should == 1.0
end
it "should be a Float given '1.5'" do
search = Order.search
search.total_gt = "1.5"
search.total_gt.should == 1.5
end
it "should be a Date given 'Jan 1, 2009'" do
search = Order.search
search.shipped_on_after = "Jan 1, 2009"
search.shipped_on_after.should == Date.parse("Jan 1, 2009")
end
it "should be a Time given 'Jan 1, 2009'" do
search = Order.search
search.created_at_after = "Jan 1, 2009"
search.created_at_after.should == Time.parse("Jan 1, 2009")
end
it "should be a Time given 'Jan 1, 2009 9:33AM'" do
search = Order.search
search.created_at_after = "Jan 1, 2009 9:33AM"
search.created_at_after.should == Time.parse("Jan 1, 2009 9:33AM")
end
it "should be an Array and cast it's values given ['1', '2', '3']" do
search = Order.search
search.id_equals_any = ["1", "2", "3"]
search.id_equals_any.should == [1, 2, 3]
end
it "should type cast association conditions" do
search = User.search
search.orders_total_gt = "10"
search.orders_total_gt.should == 10
end
it "should type cast deep association conditions" do
search = Company.search
search.users_orders_total_gt = "10"
search.users_orders_total_gt.should == 10
end
end
end
context "taking action" do
it "should return all when not given any conditions" do
3.times { User.create }
User.search.all.length.should == 3
end
it "should implement the current scope based on an association" do
User.create
company = Company.create
user = company.users.create
company.users.search.all.should == [user]
end
it "should implement the current scope based on a named scope" do
User.named_scope(:four_year_olds, :conditions => {:age => 4})
(3..5).each { |age| User.create(:age => age) }
User.four_year_olds.search.all.should == User.find_all_by_age(4)
end
it "should call named scopes for conditions" do
User.search(:age_less_than => 5).proxy_options.should == User.age_less_than(5).proxy_options
end
it "should alias exact column names to use equals" do
User.search(:username => "joe").proxy_options.should == User.username_equals("joe").proxy_options
end
it "should recognize conditions with a value of true where the named scope has an arity of 0" do
User.search(:username_nil => true).proxy_options.should == User.username_nil.proxy_options
end
it "should ignore conditions with a value of false where the named scope has an arity of 0" do
User.search(:username_nil => false).proxy_options.should == {}
end
it "should recognize the order condition" do
User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
end
end
end