sunspot_matchers_testunit 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ module SunspotMatchersTestunit
2
+ class SunspotSearchSpy < Sunspot::Search::StandardSearch
3
+ def execute
4
+ self
5
+ end
6
+ def solr_response
7
+ {}
8
+ end
9
+ def facet_response
10
+ {'facet_queries' => {}}
11
+ end
12
+ end
13
+
14
+ class SunspotSessionSpy < Sunspot::Session
15
+ attr_reader :original_session
16
+ attr_reader :current_search_class
17
+
18
+ attr_accessor :searches
19
+
20
+ def initialize(original_session)
21
+ @searches = []
22
+ @original_session = original_session
23
+ @config = Sunspot::Configuration.build
24
+ end
25
+
26
+ def inspect
27
+ 'Solr Search'
28
+ end
29
+
30
+ def index(*objects)
31
+ end
32
+
33
+ def index!(*objects)
34
+ end
35
+
36
+ def remove(*objects)
37
+ end
38
+
39
+ def remove!(*objects)
40
+ end
41
+
42
+ def remove_by_id(clazz, id)
43
+ end
44
+
45
+ def remove_by_id!(clazz, id)
46
+ end
47
+
48
+ def remove_all(clazz = nil)
49
+ end
50
+
51
+ def remove_all!(clazz = nil)
52
+ end
53
+
54
+ def dirty?
55
+ false
56
+ end
57
+
58
+ def delete_dirty?
59
+ false
60
+ end
61
+
62
+ def commit_if_dirty
63
+ end
64
+
65
+ def commit_if_delete_dirty
66
+ end
67
+
68
+ def commit
69
+ end
70
+
71
+ def search(*types, &block)
72
+ new_search(*types, &block)
73
+ end
74
+
75
+ def new_search(*types, &block)
76
+ types.flatten!
77
+ search = build_search(*types, &block)
78
+ @searches << [types, search]
79
+ search
80
+ end
81
+
82
+ def build_search(*types, &block)
83
+ types.flatten!
84
+ search = SunspotSearchSpy.new(
85
+ nil,
86
+ setup_for_types(types),
87
+ Sunspot::Query::StandardQuery.new(types),
88
+ @config
89
+ )
90
+ search.build(&block) if block
91
+ search
92
+ end
93
+
94
+ def setup_for_types(types)
95
+ if types.empty?
96
+ raise(ArgumentError, "You must specify at least one type to search")
97
+ end
98
+ if types.length == 1
99
+ Sunspot::Setup.for(types.first)
100
+ else
101
+ Sunspot::CompositeSetup.for(types)
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ # Support Sunspot random field in test -- Sunspot originally generate a random number for the field
108
+ class Sunspot::Query::Sort::RandomSort < Sunspot::Query::Sort::Abstract
109
+ def to_param
110
+ "random #{direction_for_solr}"
111
+ end
112
+ end
@@ -0,0 +1,3 @@
1
+ module SunspotMatchersTestunit
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/sunspot_matchers_testunit/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "sunspot_matchers_testunit"
6
+ s.version = SunspotMatchersTestunit::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Christine Yen"]
9
+ s.email = ["cyen@christineyen.com"]
10
+ s.homepage = "https://github.com/christineyen/sunspot_matchers_testunit"
11
+ s.summary = "Test::Unit port of RSpec matchers for testing Sunspot"
12
+ s.description = "These matchers allow you to test what is happening inside the Sunspot Search DSL blocks"
13
+ s.license = "MIT"
14
+
15
+ s.add_development_dependency "bundler", ">= 1.0.0"
16
+ s.add_development_dependency "sunspot", "~> 1.2.1"
17
+ s.add_development_dependency "rake"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.require_path = 'lib'
21
+ end
@@ -0,0 +1,702 @@
1
+ require 'sunspot'
2
+ require 'sunspot_matchers_testunit'
3
+ require 'test/unit'
4
+
5
+ class Post; end
6
+ class Blog; end
7
+ class Person; end
8
+
9
+ Sunspot.setup(Post) do
10
+ text :body
11
+ text :name
12
+ string :author_name
13
+ integer :blog_id
14
+ integer :category_ids
15
+ integer :popularity
16
+ time :published_at
17
+ float :average_rating
18
+ end
19
+
20
+ Sunspot.setup(Blog) do
21
+ text :body
22
+ string :name
23
+ end
24
+
25
+ class SunspotMatchersTestunitTest < Test::Unit::TestCase
26
+ include SunspotMatchersTestunit
27
+
28
+ def setup
29
+ Sunspot.session = SunspotMatchersTestunit::SunspotSessionSpy.new(Sunspot.session)
30
+ end
31
+
32
+ def test_specify_search_params
33
+ Sunspot.search(Post) do
34
+ keywords 'great pizza'
35
+ end
36
+ Sunspot.search(Blog) do
37
+ keywords 'bad pizza'
38
+ end
39
+ assert_has_search_params Sunspot.session.searches.first, [ :keywords, 'great pizza' ]
40
+ assert_has_search_params Sunspot.session.searches.last, [ :keywords, 'bad pizza' ]
41
+ end
42
+
43
+ # should allow you to specify your search on multiple models
44
+ def test_specify_search_params_multiple_models
45
+ Sunspot.search([ Post, Blog ]) do
46
+ keywords 'great pizza'
47
+ end
48
+ assert_has_search_params Sunspot.session, [ :keywords, 'great pizza' ]
49
+ end
50
+
51
+ def test_match_keywords
52
+ Sunspot.search(Post) do
53
+ keywords 'great pizza'
54
+ end
55
+ assert_has_search_params Sunspot.session, [ :keywords, 'great pizza' ]
56
+ end
57
+
58
+ def test_match_keywords_nomatch
59
+ Sunspot.search(Post) do
60
+ keywords 'terrible pizza'
61
+ end
62
+ assert_has_no_search_params Sunspot.session, [ :keywords, 'great pizza' ]
63
+ end
64
+
65
+ def test_match_multiple_keywords
66
+ Sunspot.search(Post) do
67
+ keywords 'great pizza'
68
+ keywords 'terrible pizza'
69
+ end
70
+
71
+ assert_has_search_params Sunspot.session, [ :keywords, Proc.new {
72
+ keywords 'great pizza'
73
+ keywords 'terrible pizza'
74
+ } ]
75
+ end
76
+
77
+ def test_allow_any_match_keyword
78
+ Sunspot.search(Post) do
79
+ keywords 'great pizza'
80
+ end
81
+ assert_has_search_params Sunspot.session, [ :keywords, any_param ]
82
+ end
83
+
84
+ def test_allow_any_match_keyword_negative
85
+ Sunspot.search(Post) do
86
+ with :blog_id, 4
87
+ end
88
+ assert_has_no_search_params Sunspot.session, [ :keywords, any_param ]
89
+ end
90
+
91
+ def test_with_matcher_matches
92
+ Sunspot.search(Post) do
93
+ with :author_name, 'Mark Twain'
94
+ end
95
+ assert_has_search_params Sunspot.session, [ :with, :author_name, 'Mark Twain' ]
96
+ end
97
+
98
+ def test_with_matcher_doesnt_match_search
99
+ Sunspot.search(Post) do
100
+ with :author_name, 'Mark Twain'
101
+ end
102
+ assert_has_no_search_params Sunspot.session, [ :with, :author_name, 'John Twain' ]
103
+ end
104
+
105
+ def test_with_matcher_matches_multiple_with
106
+ Sunspot.search(Post) do
107
+ with :author_name, 'Mark Twain'
108
+ with :author_name, 'John Twain'
109
+ end
110
+ assert_has_search_params Sunspot.session, [ :with, Proc.new {
111
+ with :author_name, 'Mark Twain'
112
+ with :author_name, 'John Twain'
113
+ }]
114
+ end
115
+
116
+ def test_with_matcher_matches_greater_than
117
+ Sunspot.search(Post) do
118
+ with(:category_ids).greater_than(1)
119
+ end
120
+ assert_has_search_params Sunspot.session, [ :with, Proc.new {
121
+ with(:category_ids).greater_than(1)
122
+ }]
123
+ end
124
+
125
+ def test_with_matcher_matches_less_than
126
+ Sunspot.search(Post) do
127
+ with(:category_ids).less_than(1)
128
+ end
129
+ assert_has_search_params Sunspot.session, [ :with, Proc.new {
130
+ with(:category_ids).less_than(1)
131
+ }]
132
+ end
133
+
134
+ def test_with_matcher_matches_range
135
+ Sunspot.search(Post) do
136
+ with :category_ids, 1..3
137
+ end
138
+ assert_has_search_params Sunspot.session, [ :with, Proc.new {
139
+ with :category_ids, 1..3
140
+ }]
141
+ end
142
+
143
+ def test_with_matcher_matches_any_of
144
+ Sunspot.search(Post) do
145
+ with(:category_ids).any_of [ 1, 2 ]
146
+ end
147
+ assert_has_search_params Sunspot.session, [ :with, Proc.new {
148
+ with(:category_ids).any_of [ 1, 2 ]
149
+ }]
150
+ end
151
+
152
+ def test_with_matcher_matches_any_of_multiline
153
+ Sunspot.search(Post) do
154
+ any_of do
155
+ with :category_ids, 1
156
+ with :category_ids, 2
157
+ end
158
+ end
159
+ assert_has_search_params Sunspot.session, [ :with, Proc.new {
160
+ any_of do
161
+ with :category_ids, 1
162
+ with :category_ids, 2
163
+ end
164
+ }]
165
+ end
166
+
167
+ def test_with_matcher_matches_any_of_and_all_of
168
+ Sunspot.search(Post) do
169
+ any_of do
170
+ with :category_ids, 1
171
+ all_of do
172
+ with :category_ids, 2
173
+ with :category_ids, 3
174
+ end
175
+ end
176
+ end
177
+ assert_has_search_params Sunspot.session, [ :with, Proc.new {
178
+ any_of do
179
+ with :category_ids, 1
180
+ all_of do
181
+ with :category_ids, 2
182
+ with :category_ids, 3
183
+ end
184
+ end
185
+ }]
186
+ end
187
+
188
+ def test_with_matcher_matches_any_param_match
189
+ Sunspot.search(Post) do
190
+ with :blog_id, 4
191
+ end
192
+ assert_has_search_params Sunspot.session, [ :with, :blog_id, any_param ]
193
+ end
194
+
195
+ def test_with_matcher_matches_any_param_negative_no_with
196
+ Sunspot.search(Post) do
197
+ keywords 'great pizza'
198
+ end
199
+ assert_has_no_search_params Sunspot.session, [ :with, :blog_id, any_param ]
200
+ end
201
+
202
+ def test_with_matcher_matches_any_param_negative_diff_field
203
+ Sunspot.search(Post) do
204
+ with :category_ids, 7
205
+ end
206
+ assert_has_no_search_params Sunspot.session, [ :with, :blog_id, any_param ]
207
+ end
208
+
209
+ # 'without' matcher
210
+
211
+ def test_without_matcher_matches
212
+ Sunspot.search(Post) do
213
+ without :author_name, 'Mark Twain'
214
+ end
215
+ assert_has_search_params Sunspot.session, [ :without, :author_name, 'Mark Twain' ]
216
+ end
217
+
218
+ def test_without_matcher_doesnt_match_search
219
+ Sunspot.search(Post) do
220
+ without :author_name, 'Mark Twain'
221
+ end
222
+ assert_has_no_search_params Sunspot.session, [ :without, :author_name, 'John Twain' ]
223
+ end
224
+
225
+ def test_without_matcher_doesnt_match_with_search
226
+ Sunspot.search(Post) do
227
+ with :author_name, 'Mark Twain'
228
+ end
229
+ assert_has_no_search_params Sunspot.session, [ :without, :author_name, 'Mark Twain' ]
230
+ end
231
+
232
+ def test_without_matcher_matches_multiple_with
233
+ Sunspot.search(Post) do
234
+ without :author_name, 'Mark Twain'
235
+ without :author_name, 'John Twain'
236
+ end
237
+ assert_has_search_params Sunspot.session, [ :without, Proc.new {
238
+ without :author_name, 'Mark Twain'
239
+ without :author_name, 'John Twain'
240
+ }]
241
+ end
242
+
243
+ def test_without_matcher_matches_greater_than
244
+ Sunspot.search(Post) do
245
+ without(:category_ids).greater_than(1)
246
+ end
247
+ assert_has_search_params Sunspot.session, [ :without, Proc.new {
248
+ without(:category_ids).greater_than(1)
249
+ }]
250
+ end
251
+
252
+ def test_without_matcher_matches_less_than
253
+ Sunspot.search(Post) do
254
+ without(:category_ids).less_than(1)
255
+ end
256
+ assert_has_search_params Sunspot.session, [ :without, Proc.new {
257
+ without(:category_ids).less_than(1)
258
+ }]
259
+ end
260
+
261
+ def test_without_matcher_matches_range
262
+ Sunspot.search(Post) do
263
+ without :category_ids, 1..3
264
+ end
265
+ assert_has_search_params Sunspot.session, [ :without, Proc.new {
266
+ without :category_ids, 1..3
267
+ }]
268
+ end
269
+
270
+ def test_without_matcher_matches_any_of
271
+ Sunspot.search(Post) do
272
+ without(:category_ids).any_of [ 1, 2 ]
273
+ end
274
+ assert_has_search_params Sunspot.session, [ :without, Proc.new {
275
+ without(:category_ids).any_of [ 1, 2 ]
276
+ }]
277
+ end
278
+
279
+ def test_without_matcher_matches_any_of_multiline
280
+ Sunspot.search(Post) do
281
+ any_of do
282
+ without :category_ids, 1
283
+ without :category_ids, 2
284
+ end
285
+ end
286
+ assert_has_search_params Sunspot.session, [ :without, Proc.new {
287
+ any_of do
288
+ without :category_ids, 1
289
+ without :category_ids, 2
290
+ end
291
+ }]
292
+ end
293
+
294
+ def test_without_matcher_matches_any_of_and_all_of
295
+ Sunspot.search(Post) do
296
+ any_of do
297
+ without :category_ids, 1
298
+ all_of do
299
+ without :category_ids, 2
300
+ without :category_ids, 3
301
+ end
302
+ end
303
+ end
304
+ assert_has_search_params Sunspot.session, [ :without, Proc.new {
305
+ any_of do
306
+ without :category_ids, 1
307
+ all_of do
308
+ without :category_ids, 2
309
+ without :category_ids, 3
310
+ end
311
+ end
312
+ }]
313
+ end
314
+
315
+ def test_without_matcher_matches_any_param_match
316
+ Sunspot.search(Post) do
317
+ without :blog_id, 4
318
+ end
319
+ assert_has_search_params Sunspot.session, [ :without, :blog_id, any_param ]
320
+ end
321
+
322
+ def test_without_matcher_matches_any_param_negative_no_without
323
+ Sunspot.search(Post) do
324
+ keywords 'great pizza'
325
+ end
326
+ assert_has_no_search_params Sunspot.session, [ :without, :blog_id, any_param ]
327
+ end
328
+
329
+ def test_without_matcher_matches_any_param_negative_diff_field
330
+ Sunspot.search(Post) do
331
+ without :category_ids, 7
332
+ end
333
+ assert_has_no_search_params Sunspot.session, [ :without, :blog_id, any_param ]
334
+ end
335
+
336
+ # 'paginate' matcher
337
+
338
+ def test_paginate_matcher_matches
339
+ Sunspot.search(Post) do
340
+ paginate :page => 3, :per_page => 15
341
+ end
342
+ assert_has_search_params Sunspot.session, [ :paginate, { :page => 3, :per_page => 15 } ]
343
+ end
344
+
345
+ def test_paginate_matcher_matches_page_only
346
+ Sunspot.search(Post) do
347
+ paginate :page => 3
348
+ end
349
+ assert_has_search_params Sunspot.session, [ :paginate, { :page => 3 } ]
350
+ end
351
+
352
+ def test_paginate_matcher_matches_per_page_only
353
+ Sunspot.search(Post) do
354
+ paginate :per_page => 15
355
+ end
356
+ assert_has_search_params Sunspot.session, [ :paginate, { :per_page => 15 } ]
357
+ end
358
+
359
+ def test_paginate_matcher_doesnt_match_without_per_page
360
+ Sunspot.search(Post) do
361
+ paginate :page => 3, :per_page => 30
362
+ end
363
+ assert_has_no_search_params Sunspot.session, [ :paginate, { :page => 3, :per_page => 15 } ]
364
+ end
365
+
366
+ def test_paginate_matcher_doesnt_match_without_page
367
+ Sunspot.search(Post) do
368
+ paginate :page => 5, :per_page => 15
369
+ end
370
+ assert_has_no_search_params Sunspot.session, [ :paginate, { :page => 3, :per_page => 15 } ]
371
+ end
372
+
373
+ def test_order_by_matcher_matches
374
+ Sunspot.search(Post) do
375
+ order_by :published_at, :desc
376
+ end
377
+ assert_has_search_params Sunspot.session, [ :order_by, :published_at, :desc ]
378
+ end
379
+
380
+ def test_order_by_matcher_doesnt_match
381
+ Sunspot.search(Post) do
382
+ order_by :published_at, :asc
383
+ end
384
+ assert_has_no_search_params Sunspot.session, [ :order_by, :published_at, :desc ]
385
+ end
386
+
387
+ def test_order_by_matcher_matches_multiple
388
+ Sunspot.search(Post) do
389
+ order_by :published_at, :asc
390
+ order_by :average_rating, :asc
391
+ end
392
+ assert_has_search_params Sunspot.session, [ :order_by, Proc.new {
393
+ order_by :published_at, :asc
394
+ order_by :average_rating, :asc
395
+ } ]
396
+ end
397
+
398
+ def test_order_by_matcher_doesnt_match_multiple_reversed
399
+ Sunspot.search(Post) do
400
+ order_by :average_rating, :asc
401
+ order_by :published_at, :asc
402
+ end
403
+ assert_has_no_search_params Sunspot.session, [ :order_by, Proc.new {
404
+ order_by :published_at, :asc
405
+ order_by :average_rating, :asc
406
+ } ]
407
+ end
408
+
409
+ def test_order_by_matcher_matches_on_random
410
+ Sunspot.search(Post) do
411
+ order_by :average_rating, :asc
412
+ order_by :random
413
+ end
414
+ assert_has_search_params Sunspot.session, [ :order_by, Proc.new {
415
+ order_by :average_rating, :asc
416
+ order_by :random
417
+ } ]
418
+ end
419
+
420
+ def test_order_by_matcher_doesnt_match_without_random
421
+ Sunspot.search(Post) do
422
+ order_by :average_rating, :asc
423
+ order_by :random
424
+ end
425
+ assert_has_no_search_params Sunspot.session, [ :order_by, Proc.new {
426
+ order_by :average_rating, :asc
427
+ } ]
428
+ end
429
+
430
+ def test_order_by_matcher_with_score
431
+ Sunspot.search(Post) do
432
+ order_by :average_rating, :asc
433
+ order_by :score
434
+ end
435
+ assert_has_search_params Sunspot.session, [ :order_by, Proc.new {
436
+ order_by :average_rating, :asc
437
+ order_by :score
438
+ } ]
439
+ end
440
+
441
+ def test_order_by_matcher_doesnt_match_without_score
442
+ Sunspot.search(Post) do
443
+ order_by :average_rating, :asc
444
+ order_by :score
445
+ end
446
+ assert_has_no_search_params Sunspot.session, [ :order_by, Proc.new {
447
+ order_by :average_rating, :asc
448
+ } ]
449
+ end
450
+
451
+ def test_order_by_matcher_respects_any_param_on_direction
452
+ Sunspot.search(Post) do
453
+ order_by :average_rating, :asc
454
+ end
455
+ assert_has_search_params Sunspot.session, [ :order_by, :average_rating, any_param ]
456
+ end
457
+
458
+ def test_order_by_matcher_respects_any_param_on_field
459
+ Sunspot.search(Post) do
460
+ order_by :average_rating, :asc
461
+ end
462
+ assert_has_search_params Sunspot.session, [ :order_by, any_param ]
463
+ end
464
+
465
+ def test_order_by_matcher_doesnt_respect_any_param_on_direction
466
+ Sunspot.search(Post) do
467
+ order_by :score
468
+ end
469
+ assert_has_no_search_params Sunspot.session, [ :order_by, :average_rating, any_param ]
470
+ end
471
+
472
+ def test_order_by_matcher_doesnt_respect_any_param_on_field
473
+ Sunspot.search(Post) do
474
+ keywords 'great pizza'
475
+ end
476
+ assert_has_no_search_params Sunspot.session, [ :order_by, any_param ]
477
+ end
478
+
479
+ def test_order_by_matcher_respects_any_param_on_field_and_dir
480
+ Sunspot.search(Post) do
481
+ order_by :score, :desc
482
+ end
483
+ assert_has_search_params Sunspot.session, [ :order_by, any_param, any_param ]
484
+ end
485
+
486
+ def test_order_by_matcher_respects_any_param_on_field_and_dir
487
+ Sunspot.search(Post) do
488
+ keywords 'great pizza'
489
+ end
490
+ assert_has_no_search_params Sunspot.session, [ :order_by, any_param, any_param ]
491
+ end
492
+
493
+ # 'facet' matcher
494
+
495
+ def test_facet_matcher_matches_field_facets
496
+ Sunspot.search(Post) do
497
+ facet :category_ids
498
+ end
499
+ assert_has_search_params Sunspot.session, [ :facet, :category_ids ]
500
+ end
501
+
502
+ def test_facet_matcher_doesnt_match_nonexistent_facet
503
+ Sunspot.search(Post) do
504
+ paginate :page => 5, :per_page => 15
505
+ end
506
+ assert_has_no_search_params Sunspot.session, [ :facet, :category_ids ]
507
+ end
508
+
509
+ def test_facet_matcher_matches_excluding_filters
510
+ Sunspot.search(Post) do
511
+ category_filter = with(:category_ids, 2)
512
+ facet(:category_ids, :exclude => category_filter)
513
+ end
514
+ assert_has_search_params Sunspot.session, [ :facet, Proc.new {
515
+ category_filter = with(:category_ids, 2)
516
+ facet(:category_ids, :exclude => category_filter)
517
+ } ]
518
+ end
519
+
520
+ def test_query_facet_matcher_matches
521
+ Sunspot.search(Post) do
522
+ facet(:average_rating) do
523
+ row(1.0..2.0) do
524
+ with(:average_rating, 1.0..2.0)
525
+ end
526
+ row(2.0..3.0) do
527
+ with(:average_rating, 2.0..3.0)
528
+ end
529
+ end
530
+ end
531
+ assert_has_search_params Sunspot.session, [ :facet, Proc.new {
532
+ facet(:average_rating) do
533
+ row(1.0..2.0) do
534
+ with(:average_rating, 1.0..2.0)
535
+ end
536
+ row(2.0..3.0) do
537
+ with(:average_rating, 2.0..3.0)
538
+ end
539
+ end
540
+ } ]
541
+ end
542
+
543
+ def test_query_facet_matcher_doesnt_match_missing_facet
544
+ Sunspot.search(Post) do
545
+ facet(:average_rating) do
546
+ row(1.0..2.0) do
547
+ with(:average_rating, 1.0..2.0)
548
+ end
549
+ end
550
+ end
551
+ assert_has_no_search_params Sunspot.session, [ :facet, Proc.new {
552
+ facet(:average_rating) do
553
+ row(1.0..2.0) do
554
+ with(:average_rating, 1.0..2.0)
555
+ end
556
+ row(2.0..3.0) do
557
+ with(:average_rating, 2.0..3.0)
558
+ end
559
+ end
560
+ } ]
561
+ end
562
+
563
+ def test_query_facet_matcher_doesnt_match_different_query
564
+ Sunspot.search(Post) do
565
+ facet(:average_rating) do
566
+ row(1.0..2.0) do
567
+ with(:average_rating, 1.0..2.0)
568
+ end
569
+ row(2.0..3.0) do
570
+ with(:average_rating, 2.0..3.0)
571
+ end
572
+ end
573
+ end
574
+ assert_has_no_search_params Sunspot.session, [ :facet, Proc.new {
575
+ facet(:average_rating) do
576
+ row(1.0..2.0) do
577
+ with(:average_rating, 1.0..2.0)
578
+ end
579
+ row(2.0..3.0) do
580
+ with(:average_rating, 2.0..4.0)
581
+ end
582
+ end
583
+ } ]
584
+ end
585
+
586
+ # 'boost' matcher
587
+
588
+ def test_boost_matcher_matches
589
+ Sunspot.search(Post) do
590
+ keywords 'great pizza' do
591
+ boost_fields :body => 2.0
592
+ end
593
+ end
594
+ assert_has_search_params Sunspot.session, [ :boost, Proc.new {
595
+ keywords 'great pizza' do
596
+ boost_fields :body => 2.0
597
+ end
598
+ } ]
599
+ end
600
+
601
+ def test_boost_matcher_doesnt_match_on_boost_mismatch
602
+ Sunspot.search(Post) do
603
+ keywords 'great pizza' do
604
+ boost_fields :body => 2.0
605
+ end
606
+ end
607
+ assert_has_no_search_params Sunspot.session, [ :boost, Proc.new {
608
+ keywords 'great pizza' do
609
+ boost_fields :body => 3.0
610
+ end
611
+ } ]
612
+ end
613
+
614
+ def test_boost_matcher_matches_boost_query
615
+ Sunspot.search(Post) do
616
+ keywords 'great pizza' do
617
+ boost(2.0) do
618
+ with :blog_id, 4
619
+ end
620
+ end
621
+ end
622
+ assert_has_search_params Sunspot.session, [ :boost, Proc.new {
623
+ keywords 'great pizza' do
624
+ boost(2.0) do
625
+ with :blog_id, 4
626
+ end
627
+ end
628
+ } ]
629
+ end
630
+
631
+ def test_boost_matcher_doesnt_match_on_boost_query_mismatch
632
+ Sunspot.search(Post) do
633
+ keywords 'great pizza' do
634
+ boost(2.0) do
635
+ with :blog_id, 4
636
+ end
637
+ end
638
+ end
639
+ assert_has_no_search_params Sunspot.session, [ :boost, Proc.new {
640
+ keywords 'great pizza' do
641
+ boost(2.0) do
642
+ with :blog_id, 5
643
+ end
644
+ end
645
+ } ]
646
+ end
647
+
648
+ def test_boost_matcher_matches_boost_function
649
+ Sunspot.search(Post) do
650
+ keywords 'great pizza' do
651
+ boost(function { sum(:average_rating, product(:popularity, 10)) })
652
+ end
653
+ end
654
+ assert_has_search_params Sunspot.session, [ :boost, Proc.new {
655
+ keywords 'great pizza' do
656
+ boost(function { sum(:average_rating, product(:popularity, 10)) })
657
+ end
658
+ } ]
659
+ end
660
+
661
+ def test_boost_matcher_doesnt_match_on_boost_function_mismatch
662
+ Sunspot.search(Post) do
663
+ keywords 'great pizza' do
664
+ boost(function { sum(:average_rating, product(:popularity, 10)) })
665
+ end
666
+ end
667
+ assert_has_no_search_params Sunspot.session, [ :boost, Proc.new {
668
+ keywords 'great pizza' do
669
+ boost(function { sum(:average_rating, product(:popularity, 42)) })
670
+ end
671
+ } ]
672
+ end
673
+
674
+ # 'be a search for'
675
+
676
+ def test_be_a_search_for
677
+ Sunspot.search(Post) { keywords 'great pizza' }
678
+ assert_is_search_for Sunspot.session, Post
679
+ end
680
+
681
+ def test_be_a_search_for_model_mismatch
682
+ Sunspot.search(Post) { keywords 'great pizza' }
683
+ assert_is_not_search_for Sunspot.session, Blog
684
+ end
685
+
686
+ def test_be_a_search_for_multiple_models
687
+ Sunspot.search(Post) { keywords 'great pizza' }
688
+ Sunspot.search([ Post, Blog ]) do
689
+ keywords 'great pizza'
690
+ end
691
+ assert_is_search_for Sunspot.session, Post
692
+ assert_is_search_for Sunspot.session, Blog
693
+ assert_is_not_search_for Sunspot.session, Person
694
+ end
695
+
696
+ def test_be_a_search_for_multiple_searches
697
+ Sunspot.search(Post) { keywords 'great pizza' }
698
+ Sunspot.search(Blog) { keywords 'bad pizza' }
699
+ assert_is_search_for Sunspot.session.searches.first, Post
700
+ assert_is_search_for Sunspot.session.searches.last, Blog
701
+ end
702
+ end