sunspot_matchers 1.1.0.0

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.
@@ -0,0 +1,651 @@
1
+ require 'sunspot'
2
+ require 'sunspot_matchers'
3
+
4
+ class Post; end
5
+ class Blog; end
6
+
7
+ Sunspot.setup(Post) do
8
+ text :body
9
+ string :author_name
10
+ integer :blog_id
11
+ integer :category_ids
12
+ integer :popularity
13
+ time :published_at
14
+ float :average_rating
15
+ end
16
+
17
+ Sunspot.setup(Blog) do
18
+ text :body
19
+ string :name
20
+ end
21
+
22
+ describe "Sunspot Matchers" do
23
+ include SunspotMatchers
24
+
25
+ before do
26
+ Sunspot.session = SunspotMatchers::SunspotSessionSpy.new(Sunspot.session)
27
+ end
28
+
29
+ describe "have_search_params" do
30
+ it "should allow you to specify your search" do
31
+ Sunspot.search(Post) do
32
+ keywords 'great pizza'
33
+ end
34
+ Sunspot.search(Blog) do
35
+ keywords 'bad pizza'
36
+ end
37
+ Sunspot.session.searches.first.should have_search_params(:keywords, 'great pizza')
38
+ Sunspot.session.searches.last.should have_search_params(:keywords, 'bad pizza')
39
+ end
40
+
41
+ describe "keyword matcher" do
42
+ it "should match if search matches" do
43
+ Sunspot.search(Post) do
44
+ keywords 'great pizza'
45
+ end
46
+ Sunspot.session.should have_search_params(:keywords, 'great pizza')
47
+ end
48
+
49
+ it "should not match if search does not match" do
50
+ Sunspot.search(Post) do
51
+ keywords 'terrible pizza'
52
+ end
53
+ Sunspot.session.should_not have_search_params(:keywords, 'great pizza')
54
+ end
55
+
56
+ it "should match for multiple keywords" do
57
+ Sunspot.search(Post) do
58
+ keywords 'great pizza'
59
+ keywords 'terrible pizza'
60
+ end
61
+ Sunspot.session.should have_search_params(:keywords, Proc.new {
62
+ keywords 'great pizza'
63
+ keywords 'terrible pizza'
64
+ })
65
+ end
66
+
67
+ it "should work with any_param match" do
68
+ Sunspot.search(Post) do
69
+ keywords 'great pizza'
70
+ end
71
+ Sunspot.session.should have_search_params(:keywords, any_param)
72
+ end
73
+
74
+ it "should work with any_param negative match" do
75
+ Sunspot.search(Post) do
76
+ with :blog_id, 4
77
+ end
78
+ Sunspot.session.should_not have_search_params(:keywords, any_param)
79
+ end
80
+ end
81
+
82
+ describe "with matcher" do
83
+ it "should match if search matches" do
84
+ Sunspot.search(Post) do
85
+ with :author_name, 'Mark Twain'
86
+ end
87
+ Sunspot.session.should have_search_params(:with, :author_name, 'Mark Twain')
88
+ end
89
+
90
+ it "should not match if search does not match" do
91
+ Sunspot.search(Post) do
92
+ with :author_name, 'Mark Twain'
93
+ end
94
+ Sunspot.session.should_not have_search_params(:with, :author_name, 'John Twain')
95
+ end
96
+
97
+ it "should match for multiple with" do
98
+ Sunspot.search(Post) do
99
+ with :author_name, 'Mark Twain'
100
+ with :author_name, 'John Twain'
101
+ end
102
+ Sunspot.session.should have_search_params(:with, Proc.new {
103
+ with :author_name, 'Mark Twain'
104
+ with :author_name, 'John Twain'
105
+ })
106
+ end
107
+
108
+ it "should match for greater_than" do
109
+ Sunspot.search(Post) do
110
+ with(:category_ids).greater_than(1)
111
+ end
112
+ Sunspot.session.should have_search_params(:with, Proc.new {
113
+ with(:category_ids).greater_than(1)
114
+ })
115
+ end
116
+
117
+ it "should match for less_than" do
118
+ Sunspot.search(Post) do
119
+ with(:category_ids).less_than(1)
120
+ end
121
+ Sunspot.session.should have_search_params(:with, Proc.new {
122
+ with(:category_ids).less_than(1)
123
+ })
124
+ end
125
+
126
+ it "should match for range" do
127
+ Sunspot.search(Post) do
128
+ with :category_ids, 1..3
129
+ end
130
+ Sunspot.session.should have_search_params(:with, Proc.new {
131
+ with :category_ids, 1..3
132
+ })
133
+ end
134
+
135
+ it "should match any_of" do
136
+ Sunspot.search(Post) do
137
+ with(:category_ids).any_of [1,2]
138
+ end
139
+ Sunspot.session.should have_search_params(:with, Proc.new {
140
+ with(:category_ids).any_of [1,2]
141
+ })
142
+ end
143
+
144
+ it "should match any_of multiline" do
145
+ Sunspot.search(Post) do
146
+ any_of do
147
+ with :category_ids, 1
148
+ with :category_ids, 2
149
+ end
150
+ end
151
+ Sunspot.session.should have_search_params(:with, Proc.new {
152
+ any_of do
153
+ with :category_ids, 1
154
+ with :category_ids, 2
155
+ end
156
+ })
157
+ end
158
+
159
+ it "should match any_of and all_of" do
160
+ Sunspot.search(Post) do
161
+ any_of do
162
+ with :category_ids, 1
163
+ all_of do
164
+ with :category_ids, 2
165
+ with :category_ids, 3
166
+ end
167
+ end
168
+ end
169
+ Sunspot.session.should have_search_params(:with, Proc.new {
170
+ any_of do
171
+ with :category_ids, 1
172
+ all_of do
173
+ with :category_ids, 2
174
+ with :category_ids, 3
175
+ end
176
+ end
177
+ })
178
+ end
179
+
180
+ it "should work with any_param match" do
181
+ Sunspot.search(Post) do
182
+ with :blog_id, 4
183
+ end
184
+ Sunspot.session.should have_search_params(:with, :blog_id, any_param)
185
+ end
186
+
187
+ it "should work with any_param negative match no with at all" do
188
+ Sunspot.search(Post) do
189
+ keywords 'great pizza'
190
+ end
191
+ Sunspot.session.should_not have_search_params(:with, :blog_id, any_param)
192
+ end
193
+
194
+ it "should work with any_param negative match different field with query" do
195
+ Sunspot.search(Post) do
196
+ with :category_ids, 7
197
+ end
198
+ Sunspot.session.should_not have_search_params(:with, :blog_id, any_param)
199
+ end
200
+ end
201
+
202
+ describe "without matcher" do
203
+ it "should match if search matches" do
204
+ Sunspot.search(Post) do
205
+ without :author_name, 'Mark Twain'
206
+ end
207
+ Sunspot.session.should have_search_params(:without, :author_name, 'Mark Twain')
208
+ end
209
+
210
+ it "should not match if search does not match" do
211
+ Sunspot.search(Post) do
212
+ without :author_name, 'Mark Twain'
213
+ end
214
+ Sunspot.session.should_not have_search_params(:without, :author_name, 'John Twain')
215
+ end
216
+
217
+ it "should match for multiple without" do
218
+ Sunspot.search(Post) do
219
+ without :author_name, 'Mark Twain'
220
+ without :author_name, 'John Twain'
221
+ end
222
+ Sunspot.session.should have_search_params(:without, Proc.new {
223
+ without :author_name, 'Mark Twain'
224
+ without :author_name, 'John Twain'
225
+ })
226
+ end
227
+
228
+ it "should match for greater_than" do
229
+ Sunspot.search(Post) do
230
+ without(:category_ids).greater_than(1)
231
+ end
232
+ Sunspot.session.should have_search_params(:without, Proc.new {
233
+ without(:category_ids).greater_than(1)
234
+ })
235
+ end
236
+
237
+ it "should match for less_than" do
238
+ Sunspot.search(Post) do
239
+ without(:category_ids).less_than(1)
240
+ end
241
+ Sunspot.session.should have_search_params(:without, Proc.new {
242
+ without(:category_ids).less_than(1)
243
+ })
244
+ end
245
+
246
+ it "should match for range" do
247
+ Sunspot.search(Post) do
248
+ without :category_ids, 1..3
249
+ end
250
+ Sunspot.session.should have_search_params(:without, Proc.new {
251
+ without :category_ids, 1..3
252
+ })
253
+ end
254
+
255
+ it "should match any_of" do
256
+ Sunspot.search(Post) do
257
+ without(:category_ids).any_of [1,2]
258
+ end
259
+ Sunspot.session.should have_search_params(:without, Proc.new {
260
+ without(:category_ids).any_of [1,2]
261
+ })
262
+ end
263
+
264
+ it "should match any_of multiline" do
265
+ Sunspot.search(Post) do
266
+ any_of do
267
+ without :category_ids, 1
268
+ without :category_ids, 2
269
+ end
270
+ end
271
+ Sunspot.session.should have_search_params(:without, Proc.new {
272
+ any_of do
273
+ without :category_ids, 1
274
+ without :category_ids, 2
275
+ end
276
+ })
277
+ end
278
+
279
+ it "should match any_of and all_of" do
280
+ Sunspot.search(Post) do
281
+ any_of do
282
+ without :category_ids, 1
283
+ all_of do
284
+ without :category_ids, 2
285
+ without :category_ids, 3
286
+ end
287
+ end
288
+ end
289
+ Sunspot.session.should have_search_params(:without, Proc.new {
290
+ any_of do
291
+ without :category_ids, 1
292
+ all_of do
293
+ without :category_ids, 2
294
+ without :category_ids, 3
295
+ end
296
+ end
297
+ })
298
+ end
299
+
300
+ it "should work with any_param match" do
301
+ Sunspot.search(Post) do
302
+ without :blog_id, 4
303
+ end
304
+ Sunspot.session.should have_search_params(:without, :blog_id, any_param)
305
+ end
306
+
307
+ it "should work with any_param negative match no without at all" do
308
+ Sunspot.search(Post) do
309
+ keywords 'great pizza'
310
+ end
311
+ Sunspot.session.should_not have_search_params(:without, :blog_id, any_param)
312
+ end
313
+
314
+ it "should work with any_param negative match different field without query" do
315
+ Sunspot.search(Post) do
316
+ without :category_ids, 4
317
+ end
318
+ Sunspot.session.should_not have_search_params(:without, :blog_id, any_param)
319
+ end
320
+ end
321
+
322
+ describe "paginate matcher" do
323
+ it "should match if search matches" do
324
+ Sunspot.search(Post) do
325
+ paginate :page => 3, :per_page => 15
326
+ end
327
+ Sunspot.session.should have_search_params(:paginate, :page => 3, :per_page => 15)
328
+ end
329
+
330
+ it "should match if search matches, page only" do
331
+ Sunspot.search(Post) do
332
+ paginate :page => 3
333
+ end
334
+ Sunspot.session.should have_search_params(:paginate, :page => 3)
335
+ end
336
+
337
+ it "should match if search matches, per_page only" do
338
+ Sunspot.search(Post) do
339
+ paginate :per_page => 15
340
+ end
341
+ Sunspot.session.should have_search_params(:paginate, :per_page => 15)
342
+ end
343
+
344
+ it "should not match if per_page does not match" do
345
+ Sunspot.search(Post) do
346
+ paginate :page => 3, :per_page => 30
347
+ end
348
+ Sunspot.session.should_not have_search_params(:paginate, :page => 3, :per_page => 15)
349
+ end
350
+
351
+ it "should not match if page does not match" do
352
+ Sunspot.search(Post) do
353
+ paginate :page => 5, :per_page => 15
354
+ end
355
+ Sunspot.session.should_not have_search_params(:paginate, :page => 3, :per_page => 15)
356
+ end
357
+ end
358
+
359
+ describe "order_by matcher" do
360
+ it "should match if search matches" do
361
+ Sunspot.search(Post) do
362
+ order_by :published_at, :desc
363
+ end
364
+ Sunspot.session.should have_search_params(:order_by, :published_at, :desc)
365
+ end
366
+
367
+ it "should not match if search does not match" do
368
+ Sunspot.search(Post) do
369
+ order_by :published_at, :asc
370
+ end
371
+ Sunspot.session.should_not have_search_params(:order_by, :published_at, :desc)
372
+ end
373
+
374
+ it "should match for multiple orderings" do
375
+ Sunspot.search(Post) do
376
+ order_by :published_at, :asc
377
+ order_by :average_rating, :asc
378
+ end
379
+ Sunspot.session.should have_search_params(:order_by, Proc.new {
380
+ order_by :published_at, :asc
381
+ order_by :average_rating, :asc
382
+ })
383
+ end
384
+
385
+ it "should not match if multiple orderings are reversed" do
386
+ Sunspot.search(Post) do
387
+ order_by :average_rating, :asc
388
+ order_by :published_at, :asc
389
+ end
390
+ Sunspot.session.should_not have_search_params(:order_by, Proc.new {
391
+ order_by :published_at, :asc
392
+ order_by :average_rating, :asc
393
+ })
394
+ end
395
+
396
+ it "should match when using random" do
397
+ Sunspot.search(Post) do
398
+ order_by :average_rating, :asc
399
+ order_by :random
400
+ end
401
+ Sunspot.session.should have_search_params(:order_by, Proc.new {
402
+ order_by :average_rating, :asc
403
+ order_by :random
404
+ })
405
+ end
406
+
407
+ it "should not match when random is missing" do
408
+ Sunspot.search(Post) do
409
+ order_by :average_rating, :asc
410
+ order_by :random
411
+ end
412
+ Sunspot.session.should_not have_search_params(:order_by, Proc.new {
413
+ order_by :average_rating, :asc
414
+ })
415
+ end
416
+
417
+ it "should match when using score" do
418
+ Sunspot.search(Post) do
419
+ order_by :average_rating, :asc
420
+ order_by :score
421
+ end
422
+ Sunspot.session.should have_search_params(:order_by, Proc.new {
423
+ order_by :average_rating, :asc
424
+ order_by :score
425
+ })
426
+ end
427
+
428
+ it "should not match when score is missing" do
429
+ Sunspot.search(Post) do
430
+ order_by :average_rating, :asc
431
+ order_by :score
432
+ end
433
+ Sunspot.session.should_not have_search_params(:order_by, Proc.new {
434
+ order_by :average_rating, :asc
435
+ })
436
+ end
437
+ end
438
+
439
+ describe "facet matcher" do
440
+ describe "field facets" do
441
+ it "should match if facet exists" do
442
+ Sunspot.search(Post) do
443
+ facet :category_ids
444
+ end
445
+ Sunspot.session.should have_search_params(:facet, :category_ids)
446
+ end
447
+
448
+ it "should not match if facet does not exist" do
449
+ Sunspot.search(Post) do
450
+ paginate :page => 5, :per_page => 15
451
+ end
452
+ Sunspot.session.should_not have_search_params(:facet, :category_ids)
453
+ end
454
+
455
+ it "should match when excluding filters" do
456
+ Sunspot.search(Post) do
457
+ category_filter = with(:category_ids, 2)
458
+ facet(:category_ids, :exclude => category_filter)
459
+ end
460
+ Sunspot.session.should have_search_params(:facet, Proc.new {
461
+ category_filter = with(:category_ids, 2)
462
+ facet(:category_ids, :exclude => category_filter)
463
+ })
464
+ end
465
+ end
466
+
467
+ describe "query facets" do
468
+ it "should match if facet exists" do
469
+ Sunspot.search(Post) do
470
+ facet(:average_rating) do
471
+ row(1.0..2.0) do
472
+ with(:average_rating, 1.0..2.0)
473
+ end
474
+ row(2.0..3.0) do
475
+ with(:average_rating, 2.0..3.0)
476
+ end
477
+ end
478
+ end
479
+ Sunspot.session.should have_search_params(:facet, Proc.new {
480
+ facet(:average_rating) do
481
+ row(1.0..2.0) do
482
+ with(:average_rating, 1.0..2.0)
483
+ end
484
+ row(2.0..3.0) do
485
+ with(:average_rating, 2.0..3.0)
486
+ end
487
+ end
488
+ })
489
+ end
490
+
491
+ it "should not match if actual search is missing a facet" do
492
+ Sunspot.search(Post) do
493
+ facet(:average_rating) do
494
+ row(1.0..2.0) do
495
+ with(:average_rating, 1.0..2.0)
496
+ end
497
+ end
498
+ end
499
+ Sunspot.session.should_not have_search_params(:facet, Proc.new {
500
+ facet(:average_rating) do
501
+ row(1.0..2.0) do
502
+ with(:average_rating, 1.0..2.0)
503
+ end
504
+ row(2.0..3.0) do
505
+ with(:average_rating, 2.0..3.0)
506
+ end
507
+ end
508
+ })
509
+ end
510
+
511
+ it "should not match if facet query is different" do
512
+ Sunspot.search(Post) do
513
+ facet(:average_rating) do
514
+ row(1.0..2.0) do
515
+ with(:average_rating, 1.0..2.0)
516
+ end
517
+ row(2.0..3.0) do
518
+ with(:average_rating, 2.0..3.0)
519
+ end
520
+ end
521
+ end
522
+ Sunspot.session.should_not have_search_params(:facet, Proc.new {
523
+ facet(:average_rating) do
524
+ row(1.0..2.0) do
525
+ with(:average_rating, 1.0..2.0)
526
+ end
527
+ row(2.0..3.0) do
528
+ with(:average_rating, 2.0..4.0)
529
+ end
530
+ end
531
+ })
532
+ end
533
+ end
534
+ end
535
+
536
+ describe "boost matcher" do
537
+ it "should match if field boost matches" do
538
+ Sunspot.search(Post) do
539
+ keywords 'great pizza' do
540
+ boost_fields :body => 2.0
541
+ end
542
+ end
543
+ Sunspot.session.should have_search_params(:boost, Proc.new {
544
+ keywords 'great pizza' do
545
+ boost_fields :body => 2.0
546
+ end
547
+ })
548
+ end
549
+
550
+ it "should not match if field boost does not match" do
551
+ Sunspot.search(Post) do
552
+ keywords 'great pizza' do
553
+ boost_fields :body => 2.0
554
+ end
555
+ end
556
+ Sunspot.session.should_not have_search_params(:boost, Proc.new {
557
+ keywords 'great pizza' do
558
+ boost_fields :body => 3.0
559
+ end
560
+ })
561
+ end
562
+
563
+ it "should match if boost query matches" do
564
+ Sunspot.search(Post) do
565
+ keywords 'great pizza' do
566
+ boost(2.0) do
567
+ with :blog_id, 4
568
+ end
569
+ end
570
+ end
571
+ Sunspot.session.should have_search_params(:boost, Proc.new {
572
+ keywords 'great pizza' do
573
+ boost(2.0) do
574
+ with :blog_id, 4
575
+ end
576
+ end
577
+ })
578
+ end
579
+
580
+ it "should not match if boost query does not match" do
581
+ Sunspot.search(Post) do
582
+ keywords 'great pizza' do
583
+ boost(2.0) do
584
+ with :blog_id, 4
585
+ end
586
+ end
587
+ end
588
+ Sunspot.session.should_not have_search_params(:boost, Proc.new {
589
+ keywords 'great pizza' do
590
+ boost(2.0) do
591
+ with :blog_id, 5
592
+ end
593
+ end
594
+ })
595
+ end
596
+
597
+ it "should match if boost function matches" do
598
+ Sunspot.search(Post) do
599
+ keywords 'great pizza' do
600
+ boost(function { sum(:average_rating, product(:popularity, 10)) })
601
+ end
602
+ end
603
+ Sunspot.session.should have_search_params(:boost, Proc.new {
604
+ keywords 'great pizza' do
605
+ boost(function { sum(:average_rating, product(:popularity, 10)) })
606
+ end
607
+ })
608
+ end
609
+
610
+ it "should not match if boost function does not match" do
611
+ Sunspot.search(Post) do
612
+ keywords 'great pizza' do
613
+ boost(function { sum(:average_rating, product(:popularity, 10)) })
614
+ end
615
+ end
616
+ Sunspot.session.should_not have_search_params(:boost, Proc.new {
617
+ keywords 'great pizza' do
618
+ boost(function { sum(:average_rating, product(:popularity, 42)) })
619
+ end
620
+ })
621
+ end
622
+ end
623
+
624
+ end
625
+
626
+ describe "be_a_search_for" do
627
+ before do
628
+ Sunspot.search(Post) do
629
+ keywords 'great pizza'
630
+ end
631
+ end
632
+
633
+ it "should succeed if the model is correct" do
634
+ Sunspot.session.should be_a_search_for(Post)
635
+ end
636
+
637
+ it "should fail if the model is incorrect" do
638
+ Sunspot.session.should_not be_a_search_for(Blog)
639
+ end
640
+
641
+ describe "with multiple searches" do
642
+ it "should allow you to choose the search" do
643
+ Sunspot.search(Blog) do
644
+ keywords 'bad pizza'
645
+ end
646
+ Sunspot.session.searches.first.should be_a_search_for(Post)
647
+ Sunspot.session.searches.last.should be_a_search_for(Blog)
648
+ end
649
+ end
650
+ end
651
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/sunspot_matchers/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "sunspot_matchers"
6
+ s.version = SunspotMatchers::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Joseph Palermo"]
9
+ s.email = []
10
+ s.homepage = "https://github.com/pivotal/sunspot_matchers"
11
+ s.summary = "RSpec matchers for testing Sunspot"
12
+ s.description = "These matchers allow you to test what is happening inside the Sunspot Search DSL blocks"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+
16
+ s.add_development_dependency "bundler", ">= 1.0.0"
17
+ s.add_development_dependency "rspec", "~> 2.1.0"
18
+ s.add_development_dependency "sunspot", "~> 1.1.0"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.require_path = 'lib'
22
+ end