sunspot_matchers 1.2.1.0 → 1.2.1.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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sunspot_matchers (1.2.1.0)
4
+ sunspot_matchers (1.2.1.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.markdown CHANGED
@@ -58,13 +58,17 @@ the `with` restrictions are simply ignored.
58
58
 
59
59
  ### wildcard matching
60
60
 
61
- keywords, with, and without support wildcard expectations using the `any_param` parameter:
61
+ keywords, with, without, and order_by support wildcard expectations using the `any_param` parameter:
62
62
 
63
63
  Sunspot.search(Post) do
64
64
  with :blog_id, 4
65
+ order_by :blog_id, :desc
65
66
  end
66
67
 
67
68
  Sunspot.session.should have_search_params(:with, :blog_id, any_param)
69
+ Sunspot.session.should have_search_params(:order_by, :blog_id, any_param)
70
+ Sunspot.session.should have_search_params(:order_by, any_param)
71
+ Sunspot.session.should_not have_search_params(:order_by, :category_ids, any_param)
68
72
 
69
73
  ### :keywords
70
74
 
@@ -1,8 +1,21 @@
1
1
  module SunspotMatchers
2
2
  class BaseMatcher
3
- def initialize(actual_search, comparison_search, args)
3
+ attr_accessor :args
4
+
5
+ def initialize(actual, args)
6
+ @actual = actual
4
7
  @args = args
5
- @actual_search, @comparison_search = actual_search, comparison_search
8
+ build_comparison_search
9
+ end
10
+
11
+ def build_comparison_search
12
+ @comparison_search = if(@args.last.is_a?(Proc))
13
+ SunspotMatchers::SunspotSessionSpy.new(nil).build_search(search_types, &args.last)
14
+ else
15
+ SunspotMatchers::SunspotSessionSpy.new(nil).build_search(search_types) do
16
+ send(search_method, *args)
17
+ end
18
+ end
6
19
  end
7
20
 
8
21
  def search_tuple
@@ -32,7 +45,7 @@ module SunspotMatchers
32
45
  end
33
46
 
34
47
  def actual_params
35
- @actual_params ||= query_params_for_search(@actual_search)
48
+ @actual_params ||= query_params_for_search(actual_search)
36
49
  end
37
50
 
38
51
  def comparison_params
@@ -83,7 +96,7 @@ module SunspotMatchers
83
96
  def compare_single_value(actual, comparison)
84
97
  if comparison.is_a?(Regexp)
85
98
  return [] if comparison =~ actual
86
- return [comparison]
99
+ return [comparison.source]
87
100
  end
88
101
  return [comparison] unless actual == comparison
89
102
  []
@@ -97,10 +110,6 @@ module SunspotMatchers
97
110
  end
98
111
  end
99
112
 
100
- def normalize_value(value)
101
-
102
- end
103
-
104
113
  def filter_values(values)
105
114
  return values unless wildcard?
106
115
  field_matcher = Regexp.new(field.to_s)
@@ -112,7 +121,7 @@ module SunspotMatchers
112
121
  end
113
122
  end
114
123
 
115
- class HaveSearchParams < BaseMatcher
124
+ class HaveSearchParams
116
125
  def initialize(method, *args)
117
126
  @method = method
118
127
  @args = args
@@ -120,7 +129,7 @@ module SunspotMatchers
120
129
 
121
130
  def matches?(actual)
122
131
  @actual = actual
123
- @matcher = build_matcher
132
+ @matcher = get_matcher.new(@actual, @args)
124
133
  @matcher.match?
125
134
  end
126
135
 
@@ -132,20 +141,6 @@ module SunspotMatchers
132
141
  @matcher.unexpected_match_error_message
133
142
  end
134
143
 
135
- def build_matcher
136
- comparison_search = if(@args.last.is_a?(Proc))
137
- SunspotMatchers::SunspotSessionSpy.new(nil).build_search(search_types, &@args.last)
138
- else
139
- method = @method
140
- args = @args
141
- SunspotMatchers::SunspotSessionSpy.new(nil).build_search(search_types) do
142
- send(method, *args)
143
- end
144
- end
145
-
146
- get_matcher.new(actual_search, comparison_search, @args)
147
- end
148
-
149
144
  def get_matcher
150
145
  case @method
151
146
  when :with, :without
@@ -157,7 +152,7 @@ module SunspotMatchers
157
152
  when :facet
158
153
  FacetMatcher
159
154
  when :order_by
160
- SortMatcher
155
+ OrderByMatcher
161
156
  when :paginate
162
157
  PaginationMatcher
163
158
  end
@@ -169,12 +164,20 @@ module SunspotMatchers
169
164
  end
170
165
 
171
166
  class WithMatcher < BaseMatcher
167
+ def search_method
168
+ :with
169
+ end
170
+
172
171
  def keys_to_compare
173
172
  [:fq]
174
173
  end
175
174
  end
176
175
 
177
176
  class KeywordsMatcher < BaseMatcher
177
+ def search_method
178
+ :keywords
179
+ end
180
+
178
181
  def keys_to_compare
179
182
  [:q, :qf]
180
183
  end
@@ -185,30 +188,79 @@ module SunspotMatchers
185
188
  end
186
189
 
187
190
  class BoostMatcher < BaseMatcher
191
+ def search_method
192
+ :boost
193
+ end
194
+
195
+
188
196
  def keys_to_compare
189
197
  [:qf, :bq, :bf]
190
198
  end
191
199
  end
192
200
 
193
201
  class FacetMatcher < BaseMatcher
202
+ def search_method
203
+ :facet
204
+ end
205
+
194
206
  def keys_to_compare
195
207
  comparison_params.keys.select {|key| /facet/ =~ key.to_s}
196
208
  end
197
209
  end
198
210
 
199
- class SortMatcher < BaseMatcher
211
+ class OrderByMatcher < BaseMatcher
212
+ def search_method
213
+ :order_by
214
+ end
215
+
200
216
  def keys_to_compare
201
217
  [:sort]
202
218
  end
219
+
220
+ def wildcard_matcher_for_keys
221
+ return {:sort => /./} if field_wildcard?
222
+ param = comparison_params[:sort]
223
+ regex = Regexp.new(param.gsub(any_param, '.*'))
224
+ {:sort => regex}
225
+ end
226
+
227
+ def field_wildcard?
228
+ @args.first == any_param
229
+ end
230
+
231
+ def direction_wildcard?
232
+ @args.length == 2 && @args.last == any_param
233
+ end
234
+
235
+ def args
236
+ return @args unless direction_wildcard?
237
+ @args[0...-1] + [:asc]
238
+ end
239
+
240
+ def build_comparison_search
241
+ if field_wildcard?
242
+ @comparison_params = {:sort => any_param}
243
+ elsif direction_wildcard?
244
+ super
245
+ @comparison_params = comparison_params
246
+ @comparison_params[:sort].gsub!("asc", any_param)
247
+ else
248
+ super
249
+ end
250
+ end
203
251
  end
204
252
 
205
253
  class PaginationMatcher < BaseMatcher
254
+ def search_method
255
+ :paginate
256
+ end
257
+
206
258
  def keys_to_compare
207
259
  [:rows, :start]
208
260
  end
209
261
  end
210
262
 
211
- class BeASearchFor < BaseMatcher
263
+ class BeASearchFor
212
264
  def initialize(expected_class)
213
265
  @expected_class = expected_class
214
266
  end
@@ -218,6 +270,16 @@ module SunspotMatchers
218
270
  search_types.include?(@expected_class)
219
271
  end
220
272
 
273
+ def search_tuple
274
+ search_tuple = @actual.is_a?(Array) ? @actual : @actual.searches.last
275
+ raise 'no search found' unless search_tuple
276
+ search_tuple
277
+ end
278
+
279
+ def search_types
280
+ search_tuple.first
281
+ end
282
+
221
283
  def failure_message_for_should
222
284
  "expected search class: #{search_types.join(' and ')} to match expected class: #{@expected_class}"
223
285
  end
@@ -1,3 +1,3 @@
1
1
  module SunspotMatchers
2
- VERSION = "1.2.1.0"
2
+ VERSION = "1.2.1.1"
3
3
  end
@@ -6,6 +6,7 @@ class Blog; end
6
6
 
7
7
  Sunspot.setup(Post) do
8
8
  text :body
9
+ text :name
9
10
  string :author_name
10
11
  integer :blog_id
11
12
  integer :category_ids
@@ -434,6 +435,48 @@ describe "Sunspot Matchers" do
434
435
  order_by :average_rating, :asc
435
436
  })
436
437
  end
438
+
439
+ it "should work with any_param match on direction" do
440
+ Sunspot.search(Post) do
441
+ order_by :average_rating, :asc
442
+ end
443
+ Sunspot.session.should have_search_params(:order_by, :average_rating, any_param)
444
+ end
445
+
446
+ it "should work with any_param match on field" do
447
+ Sunspot.search(Post) do
448
+ order_by :average_rating, :asc
449
+ end
450
+ Sunspot.session.should have_search_params(:order_by, any_param)
451
+ end
452
+
453
+ it "should work with any_param negative match on direction" do
454
+ Sunspot.search(Post) do
455
+ order_by :score
456
+ end
457
+ Sunspot.session.should_not have_search_params(:order_by, :average_rating, any_param)
458
+ end
459
+
460
+ it "should work with any_param negative match on field" do
461
+ Sunspot.search(Post) do
462
+ keywords 'great pizza'
463
+ end
464
+ Sunspot.session.should_not have_search_params(:order_by, any_param)
465
+ end
466
+
467
+ it "should work with any_param match on field and direction" do
468
+ Sunspot.search(Post) do
469
+ order_by :score, :desc
470
+ end
471
+ Sunspot.session.should have_search_params(:order_by, any_param, any_param)
472
+ end
473
+
474
+ it "should work with any_param negative match on field and direction" do
475
+ Sunspot.search(Post) do
476
+ keywords 'great pizza'
477
+ end
478
+ Sunspot.session.should_not have_search_params(:order_by, any_param, any_param)
479
+ end
437
480
  end
438
481
 
439
482
  describe "facet matcher" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunspot_matchers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 75
4
+ hash: 73
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
9
  - 1
10
- - 0
11
- version: 1.2.1.0
10
+ - 1
11
+ version: 1.2.1.1
12
12
  platform: ruby
13
13
  authors:
14
14
  - Joseph Palermo
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-01-08 00:00:00 -08:00
19
+ date: 2011-02-13 00:00:00 +08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency