sunspot_matchers 1.3.0.2 → 2.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ .idea
data/Gemfile.lock CHANGED
@@ -1,17 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sunspot_matchers (1.3.0.2)
4
+ sunspot_matchers (2.0.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- builder (3.0.0)
9
+ builder (3.2.0)
10
10
  diff-lcs (1.1.2)
11
- escape (0.0.4)
12
11
  pr_geohash (1.0.0)
13
12
  rake (0.8.7)
14
- rsolr (1.0.6)
13
+ rsolr (1.0.9)
15
14
  builder (>= 2.1.2)
16
15
  rspec (2.4.0)
17
16
  rspec-core (~> 2.4.0)
@@ -21,10 +20,9 @@ GEM
21
20
  rspec-expectations (2.4.0)
22
21
  diff-lcs (~> 1.1.2)
23
22
  rspec-mocks (2.4.0)
24
- sunspot (1.3.0)
25
- escape (~> 0.0.4)
23
+ sunspot (2.0.0)
26
24
  pr_geohash (~> 1.0)
27
- rsolr (~> 1.0.6)
25
+ rsolr (~> 1.0.7)
28
26
 
29
27
  PLATFORMS
30
28
  ruby
@@ -33,5 +31,5 @@ DEPENDENCIES
33
31
  bundler (>= 1.0.0)
34
32
  rake
35
33
  rspec (~> 2.4.0)
36
- sunspot (~> 1.3.0)
34
+ sunspot (~> 2.0.0)
37
35
  sunspot_matchers!
data/README.markdown CHANGED
@@ -60,6 +60,12 @@ If you want to verify that a model has a searchable field, you can use this matc
60
60
 
61
61
  `Post.should have_searchable_field(:name)`
62
62
 
63
+ You can also use the shorthand syntax:
64
+
65
+ describe User do
66
+ it { should have_searchable_field(:name) }
67
+ end
68
+
63
69
  ## have_search_params
64
70
 
65
71
  This is where the bulk of the functionality lies. There are seven types of search matches you can perform: `keywords` or `fulltext`,
@@ -70,11 +76,15 @@ have a dozen `with` restrictions on a search and still write an expectation on a
70
76
 
71
77
  Negative expectations also work correctly. `should_not` will fail if the search actually includes the expectation.
72
78
 
73
- With all matchers, you can specify a `Proc` as the second argument, and perform multi statement expectations inside the
74
- Proc. Keep in mind, that only the expectation type specified in the first argument will actually be checked. So if
75
- you specify `keywords` and `with` restrictions in the same Proc, but you said `have_search_params(:keywords, ...`
79
+ With all matchers, you can pass a block and perform multi statement expectations inside the
80
+ block. Keep in mind, that only the expectation type specified as the argument will actually be checked. So if
81
+ you specify `keywords` and `with` restrictions in the same block, but you said `have_search_params(:keywords, ...`
76
82
  the `with` restrictions are simply ignored.
77
83
 
84
+ Without creative usage of parentheses you can not use do...end blocks to pass multi statement expectations to the matcher
85
+ though. Because do...end have such a low binding precedent, the block is passed to the wrong method. Curly syntax blocks
86
+ will work though {...}, or you can pass a Proc as the last argument.
87
+
78
88
  ### wildcard matching
79
89
 
80
90
  keywords, with, without, and order_by support wildcard expectations using the `any_param` parameter:
@@ -109,7 +119,7 @@ You can match against a with restriction:
109
119
 
110
120
  Sunspot.session.should have_search_params(:with, :author_name, 'Mark Twain')
111
121
 
112
- Complex conditions can be matched by using a Proc instead of a value. Be aware that order does matter, not for
122
+ Complex conditions can be matched by using a block instead of a value. Be aware that order does matter, not for
113
123
  the actual results that would come out of Solr, but the matcher will fail of the order of `with` restrictions is
114
124
  different.
115
125
 
@@ -120,12 +130,12 @@ different.
120
130
  end
121
131
  end
122
132
 
123
- Sunspot.session.should have_search_params(:with, Proc.new {
133
+ Sunspot.session.should have_search_params(:with) {
124
134
  any_of do
125
135
  with :category_ids, 1
126
136
  with :category_ids, 2
127
137
  end
128
- })
138
+ }
129
139
 
130
140
  ### :without
131
141
 
@@ -149,7 +159,7 @@ You can also specify only page or per_page, both are not required.
149
159
 
150
160
  ### :order_by
151
161
 
152
- Expectations on multiple orderings are supported using using the Proc format mentioned above.
162
+ Expectations on multiple orderings are supported using using the block format mentioned above.
153
163
 
154
164
  Sunspot.search(Post) do
155
165
  order_by :published_at, :desc
@@ -174,10 +184,10 @@ Faceting where a query is excluded:
174
184
  facet(:category_ids, :exclude => category_filter)
175
185
  end
176
186
 
177
- Sunspot.session.should have_search_params(:facet, Proc.new {
187
+ Sunspot.session.should have_search_params(:facet) {
178
188
  category_filter = with(:category_ids, 2)
179
189
  facet(:category_ids, :exclude => category_filter)
180
- })
190
+ }
181
191
 
182
192
  Query faceting:
183
193
 
@@ -192,7 +202,7 @@ Query faceting:
192
202
  end
193
203
  end
194
204
 
195
- Sunspot.session.should have_search_params(:facet, Proc.new {
205
+ Sunspot.session.should have_search_params(:facet) {
196
206
  facet(:average_rating) do
197
207
  row(1.0..2.0) do
198
208
  with(:average_rating, 1.0..2.0)
@@ -201,7 +211,7 @@ Query faceting:
201
211
  with(:average_rating, 2.0..3.0)
202
212
  end
203
213
  end
204
- })
214
+ }
205
215
 
206
216
  ### :boost
207
217
 
@@ -213,11 +223,11 @@ Field boost matching:
213
223
  end
214
224
  end
215
225
 
216
- Sunspot.session.should have_search_params(:boost, Proc.new {
226
+ Sunspot.session.should have_search_params(:boost) {
217
227
  keywords 'great pizza' do
218
228
  boost_fields :body => 2.0
219
229
  end
220
- })
230
+ }
221
231
 
222
232
  Boost query matching:
223
233
 
@@ -229,13 +239,13 @@ Boost query matching:
229
239
  end
230
240
  end
231
241
 
232
- Sunspot.session.should have_search_params(:boost, Proc.new {
242
+ Sunspot.session.should have_search_params(:boost) {
233
243
  keywords 'great pizza' do
234
244
  boost(2.0) do
235
245
  with :blog_id, 4
236
246
  end
237
247
  end
238
- })
248
+ }
239
249
 
240
250
  Boost function matching:
241
251
 
@@ -245,11 +255,11 @@ Boost function matching:
245
255
  end
246
256
  end
247
257
 
248
- Sunspot.session.should have_search_params(:boost, Proc.new {
258
+ Sunspot.session.should have_search_params(:boost) {
249
259
  keywords 'great pizza' do
250
260
  boost(function { sum(:average_rating, product(:popularity, 10)) })
251
261
  end
252
- })
262
+ }
253
263
 
254
264
  # Assertions
255
265
 
@@ -270,7 +280,7 @@ These are used like:
270
280
  Sunspot.search(Post) do
271
281
  with :category_ids, 1..3
272
282
  end
273
- assert_has_search_params Sunspot.session, :with, Proc.new {
283
+ assert_has_search_params Sunspot.session, :with {
274
284
  with :category_ids, 1..3
275
285
  }
276
286
 
@@ -123,9 +123,9 @@ module SunspotMatchers
123
123
  end
124
124
 
125
125
  class HaveSearchParams
126
- def initialize(method, *args)
126
+ def initialize(method, *args, &block)
127
127
  @method = method
128
- @args = args
128
+ @args = [*args, block].compact
129
129
  end
130
130
 
131
131
  def matches?(actual)
@@ -158,12 +158,14 @@ module SunspotMatchers
158
158
  OrderByMatcher
159
159
  when :paginate
160
160
  PaginationMatcher
161
+ when :group
162
+ GroupMatcher
161
163
  end
162
164
  end
163
165
  end
164
166
 
165
- def have_search_params(method, *args)
166
- HaveSearchParams.new(method, *args)
167
+ def have_search_params(method, *args, &block)
168
+ HaveSearchParams.new(method, *args, &block)
167
169
  end
168
170
 
169
171
  class WithMatcher < BaseMatcher
@@ -273,6 +275,16 @@ module SunspotMatchers
273
275
  end
274
276
  end
275
277
 
278
+ class GroupMatcher < BaseMatcher
279
+ def search_method
280
+ :group
281
+ end
282
+
283
+ def keys_to_compare
284
+ comparison_params.keys.select {|key| /group/ =~ key.to_s}
285
+ end
286
+ end
287
+
276
288
  class BeASearchFor
277
289
  def initialize(expected_class)
278
290
  @expected_class = expected_class
@@ -312,11 +324,10 @@ module SunspotMatchers
312
324
  @field = field
313
325
  end
314
326
 
315
- def matches?(klass)
316
- @klass = klass
317
- Sunspot::Setup.for(@klass).text_field_factories.find do |field|
318
- field.name == @field
319
- end
327
+ def matches?(klass_or_object)
328
+ @klass = klass_or_object.class.name == 'Class' ? klass_or_object : klass_or_object.class
329
+ @sunspot = Sunspot::Setup.for(@klass)
330
+ (@sunspot.all_text_fields + @sunspot.fields).collect(&:name).include?(@field)
320
331
  end
321
332
 
322
333
  def failure_message_for_should
@@ -6,10 +6,10 @@ module SunspotMatchers
6
6
  class HaveSearchParamsForSession
7
7
  include Test::Unit::Assertions
8
8
 
9
- def initialize(session, method, *args)
9
+ def initialize(session, method, *args, &block)
10
10
  @session = session
11
11
  @method = method
12
- @args = args
12
+ @args = [*args, block].compact
13
13
  end
14
14
 
15
15
  def get_matcher
@@ -63,15 +63,15 @@ module SunspotMatchers
63
63
  end
64
64
  end
65
65
  module TestHelper
66
- def assert_has_search_params(session, *method_and_args)
66
+ def assert_has_search_params(session, *method_and_args, &block)
67
67
  method, *args = method_and_args
68
- matcher = HaveSearchParamsForSession.new(session, method, *args).get_matcher
68
+ matcher = HaveSearchParamsForSession.new(session, method, *args, &block).get_matcher
69
69
  assert matcher.match?, matcher.missing_param_error_message
70
70
  end
71
71
 
72
- def assert_has_no_search_params(session, *method_and_args)
72
+ def assert_has_no_search_params(session, *method_and_args, &block)
73
73
  method, *args = method_and_args
74
- matcher = HaveSearchParamsForSession.new(session, method, *args).get_matcher
74
+ matcher = HaveSearchParamsForSession.new(session, method, *args, &block).get_matcher
75
75
  assert !matcher.match?, matcher.unexpected_match_error_message
76
76
  end
77
77
 
@@ -1,3 +1,3 @@
1
1
  module SunspotMatchers
2
- VERSION = "1.3.0.2"
2
+ VERSION = "2.0.0.0"
3
3
  end