forked from sunspot/sunspot
/
scope_examples.rb
263 lines (230 loc) · 7.2 KB
/
scope_examples.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
shared_examples_for "scoped query" do
it 'scopes by exact match with a string' do
search do
with :title, 'My Pet Post'
end
connection.should have_last_search_including(:fq, 'title_ss:My\ Pet\ Post')
end
it 'scopes by exact match with a special string' do
search do
with :title, 'OR'
end
connection.should have_last_search_including(:fq, 'title_ss:"OR"')
end
it 'scopes by exact match with empty string' do
search do
with :title, ''
end
connection.should have_last_search_including(:fq, 'title_ss:[* TO ""]')
end
it 'scopes by exact match with time' do
time = Time.parse('1983-07-08 05:00:00 -0400')
search do
with :published_at, time
end
connection.should have_last_search_including(
:fq,
'published_at_dt:1983\-07\-08T09\:00\:00Z'
)
end
it 'scopes by exact match with date' do
date = Date.new(1983, 7, 8)
search do
with :expire_date, date
end
connection.should have_last_search_including(
:fq,
'expire_date_d:1983\-07\-08T00\:00\:00Z'
)
end
it 'scopes by exact match with boolean' do
search do
with :featured, false
end
connection.should have_last_search_including(:fq, 'featured_bs:false')
end
it 'scopes by less than match with float' do
search do
with(:average_rating).less_than 3.0
end
connection.should have_last_search_including(:fq, 'average_rating_ft:[* TO 3\.0]')
end
it 'should quote string with space in a less than match' do
search do
with(:title).less_than('test value')
end
connection.should have_last_search_including(:fq, 'title_ss:[* TO "test\ value"]')
end
it 'scopes by greater than match with float' do
search do
with(:average_rating).greater_than 3.0
end
connection.should have_last_search_including(:fq, 'average_rating_ft:[3\.0 TO *]')
end
it 'scopes by short-form between match with integers' do
search do
with :blog_id, 2..4
end
connection.should have_last_search_including(:fq, 'blog_id_i:[2 TO 4]')
end
it 'scopes by between match with float' do
search do
with(:average_rating).between 2.0..4.0
end
connection.should have_last_search_including(:fq, 'average_rating_ft:[2\.0 TO 4\.0]')
end
it 'automatically sorts ranges in between matches' do
search do
with(:blog_id).between(4..2)
end
connection.should have_last_search_including(:fq, 'blog_id_i:[2 TO 4]')
end
it 'scopes by any match with integer' do
search do
with(:category_ids).any_of [2, 7, 12]
end
connection.should have_last_search_including(:fq, 'category_ids_im:(2 OR 7 OR 12)')
end
it 'scopes by short-form any-of match with integers' do
search do
with :category_ids, [2, 7, 12]
end
connection.should have_last_search_including(:fq, 'category_ids_im:(2 OR 7 OR 12)')
end
it 'scopes by all match with integer' do
search do
with(:category_ids).all_of [2, 7, 12]
end
connection.should have_last_search_including(:fq, 'category_ids_im:(2 AND 7 AND 12)')
end
it 'scopes by prefix match with string' do
search do
with(:title).starting_with('tes')
end
connection.should have_last_search_including(:fq, 'title_ss:tes*')
end
it 'scopes by not equal match with string' do
search do
without :title, 'Bad Post'
end
connection.should have_last_search_including(:fq, '-title_ss:Bad\ Post')
end
it 'scopes by not less than match with float' do
search do
without(:average_rating).less_than 3.0
end
connection.should have_last_search_including(:fq, '-average_rating_ft:[* TO 3\.0]')
end
it 'scopes by not greater than match with float' do
search do
without(:average_rating).greater_than 3.0
end
connection.should have_last_search_including(:fq, '-average_rating_ft:[3\.0 TO *]')
end
it 'scopes by not between match with shorthand' do
search do
without(:blog_id, 2..4)
end
connection.should have_last_search_including(:fq, '-blog_id_i:[2 TO 4]')
end
it 'scopes by not between match with float' do
search do
without(:average_rating).between 2.0..4.0
end
connection.should have_last_search_including(:fq, '-average_rating_ft:[2\.0 TO 4\.0]')
end
it 'scopes by not any match with integer' do
search do
without(:category_ids).any_of [2, 7, 12]
end
connection.should have_last_search_including(:fq, '-category_ids_im:(2 OR 7 OR 12)')
end
it 'scopes by not all match with integer' do
search do
without(:category_ids).all_of [2, 7, 12]
end
connection.should have_last_search_including(:fq, '-category_ids_im:(2 AND 7 AND 12)')
end
it 'scopes by empty field' do
search do
with :average_rating, nil
end
connection.should have_last_search_including(:fq, '-average_rating_ft:[* TO *]')
end
it 'scopes by non-empty field' do
search do
without :average_rating, nil
end
connection.should have_last_search_including(:fq, 'average_rating_ft:[* TO *]')
end
it 'excludes by object identity' do
post = Post.new
search do
without post
end
connection.should have_last_search_including(:fq, "-id:Post\\ #{post.id}")
end
it 'excludes multiple objects passed as varargs by object identity' do
post1, post2 = Post.new, Post.new
search do
without post1, post2
end
connection.should have_last_search_including(
:fq,
"-id:Post\\ #{post1.id}",
"-id:Post\\ #{post2.id}"
)
end
it 'excludes multiple objects passed as array by object identity' do
posts = [Post.new, Post.new]
search do
without posts
end
connection.should have_last_search_including(
:fq,
"-id:Post\\ #{posts.first.id}",
"-id:Post\\ #{posts.last.id}"
)
end
it 'allows scoping on fields common to all types' do
time = Time.parse('1983-07-08 05:00:00 -0400')
search Post, Namespaced::Comment do
with :published_at, time
end
connection.should have_last_search_including(:fq, 'published_at_dt:1983\-07\-08T09\:00\:00Z')
end
it 'allows scoping on field not common to all types' do
search Post, Namespaced::Comment do
with :blog_id, 1
end
connection.should have_last_search_including(:fq, 'blog_id_i:1')
end
it 'raises Sunspot::UnrecognizedFieldError if search scoped to field configured differently between types' do
lambda do
search Post, Namespaced::Comment do
with :average_rating, 2.2 # this is a float in Post but an integer in Comment
end
end.should raise_error(Sunspot::UnrecognizedFieldError)
end
it 'raises Sunspot::UnrecognizedFieldError for nonexistant fields in block scope' do
lambda do
search do
with :bogus, 'Field'
end
end.should raise_error(Sunspot::UnrecognizedFieldError)
end
it 'raises NoMethodError if bogus operator referenced' do
lambda do
search do
with(:category_ids).resembling :bogus_condition
end
end.should raise_error(NoMethodError)
end
it 'should raise ArgumentError if more than two arguments passed to scope method' do
lambda do
search do
with(:category_ids, 4, 5)
end
end.should raise_error(ArgumentError)
end
end