sunspot_matchers 1.3.0.0 → 1.3.0.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 +1 -1
- data/README.markdown +49 -2
- data/Rakefile +9 -4
- data/lib/sunspot_matchers/matchers.rb +2 -1
- data/lib/sunspot_matchers/test_helper.rb +88 -0
- data/lib/sunspot_matchers/version.rb +1 -1
- data/sunspot_matchers.gemspec +2 -2
- data/test/sunspot_matchers_test.rb +703 -0
- metadata +8 -6
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -7,7 +7,7 @@ without doing full integration tests.
|
|
7
7
|
The goal of these matchers are to make it easier to unit test search logic without having to construct the individual
|
8
8
|
fixture scenarios inside of Solr and then actually perform a search against Solr.
|
9
9
|
|
10
|
-
# Installation
|
10
|
+
# Installation for RSpec
|
11
11
|
|
12
12
|
You will need to replace the Sunspot Session object with the spy provided. You can do this globally by putting the
|
13
13
|
following in your spec_helper.
|
@@ -28,6 +28,19 @@ You will also need to include the matchers in your specs. Again, this can be do
|
|
28
28
|
|
29
29
|
Alternately, you could include them into individual tests if needed.
|
30
30
|
|
31
|
+
# Installation for Test::Unit
|
32
|
+
|
33
|
+
You will need to replace the Sunspot Session object with the spy provided. You can do this by requiring the sunspot test helper
|
34
|
+
|
35
|
+
require 'sunspot_matchers/test_helper'
|
36
|
+
|
37
|
+
and then in the test case that you wish to override, including the SunspotMatchers::TestHelper class and set the sunspot session in your setup.
|
38
|
+
|
39
|
+
include SunspotMatchers::TestHelper
|
40
|
+
def setup
|
41
|
+
Sunspot.session = SunspotMatchers::SunspotSessionSpy.new(Sunspot.session)
|
42
|
+
end
|
43
|
+
|
31
44
|
# Matchers
|
32
45
|
|
33
46
|
## be_a_search_for
|
@@ -230,4 +243,38 @@ Boost function matching:
|
|
230
243
|
keywords 'great pizza' do
|
231
244
|
boost(function { sum(:average_rating, product(:popularity, 10)) })
|
232
245
|
end
|
233
|
-
})
|
246
|
+
})
|
247
|
+
|
248
|
+
# Assertions
|
249
|
+
|
250
|
+
If you are using Test::Unit, the format is similar, but we use
|
251
|
+
|
252
|
+
`assert_has_search_params`
|
253
|
+
`assert_has_no_search_params`
|
254
|
+
`assert_is_search_for`
|
255
|
+
`assert_is_not_search_for`
|
256
|
+
|
257
|
+
These are used like:
|
258
|
+
|
259
|
+
Sunspot.search([ Post, Blog ]) do
|
260
|
+
keywords 'great pizza'
|
261
|
+
end
|
262
|
+
assert_has_search_params Sunspot.session, :keywords, 'great pizza'
|
263
|
+
|
264
|
+
Sunspot.search(Post) do
|
265
|
+
with :category_ids, 1..3
|
266
|
+
end
|
267
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
268
|
+
with :category_ids, 1..3
|
269
|
+
}
|
270
|
+
|
271
|
+
Sunspot.search(Post) { keywords 'great pizza' }
|
272
|
+
Sunspot.search([ Post, Blog ]) do
|
273
|
+
keywords 'great pizza'
|
274
|
+
end
|
275
|
+
|
276
|
+
assert_is_search_for Sunspot.session, Post
|
277
|
+
assert_is_search_for Sunspot.session, Blog
|
278
|
+
assert_is_not_search_for Sunspot.session, Person
|
279
|
+
|
280
|
+
Check out the test/sunspot_matchers_test.rb for more examples
|
data/Rakefile
CHANGED
@@ -5,9 +5,14 @@ require 'rspec/core/rake_task'
|
|
5
5
|
desc "Run all specs"
|
6
6
|
RSpec::Core::RakeTask.new(:spec)
|
7
7
|
|
8
|
-
|
8
|
+
require 'rake/testtask'
|
9
|
+
Rake::TestTask.new(:test) do |test|
|
10
|
+
test.libs << 'lib' << 'test'
|
11
|
+
test.pattern = 'test/*_test.rb'
|
12
|
+
test.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => [:spec, :test]
|
9
16
|
|
10
17
|
desc "Runs the CI build"
|
11
|
-
task :cruise
|
12
|
-
Rake::Task["spec"].execute
|
13
|
-
end
|
18
|
+
task :cruise => :default
|
@@ -106,7 +106,8 @@ module SunspotMatchers
|
|
106
106
|
filter_values(comparison).reject do |value|
|
107
107
|
next false unless actual
|
108
108
|
value_matcher = Regexp.new(Regexp.escape(value))
|
109
|
-
actual.
|
109
|
+
cmp_value = actual.is_a?(String) ? actual.chars : actual
|
110
|
+
cmp_value.any?{ |actual_value| actual_value =~ value_matcher }
|
110
111
|
end
|
111
112
|
end
|
112
113
|
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'sunspot'
|
2
|
+
require 'test/unit'
|
3
|
+
require File.expand_path('../matchers', __FILE__)
|
4
|
+
require File.expand_path('../sunspot_session_spy', __FILE__)
|
5
|
+
module SunspotMatchers
|
6
|
+
class HaveSearchParamsForSession
|
7
|
+
include Test::Unit::Assertions
|
8
|
+
|
9
|
+
def initialize(session, method, *args)
|
10
|
+
@session = session
|
11
|
+
@method = method
|
12
|
+
@args = args
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_matcher
|
16
|
+
matcher_class = case @method
|
17
|
+
when :with
|
18
|
+
WithMatcher
|
19
|
+
when :without
|
20
|
+
WithoutMatcher
|
21
|
+
when :keywords
|
22
|
+
KeywordsMatcher
|
23
|
+
when :boost
|
24
|
+
BoostMatcher
|
25
|
+
when :facet
|
26
|
+
FacetMatcher
|
27
|
+
when :order_by
|
28
|
+
OrderByMatcher
|
29
|
+
when :paginate
|
30
|
+
PaginationMatcher
|
31
|
+
end
|
32
|
+
matcher_class.new(@session, @args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
class BeASearchForSession
|
38
|
+
def initialize(session, expected_class)
|
39
|
+
@session = session
|
40
|
+
@expected_class = expected_class
|
41
|
+
end
|
42
|
+
|
43
|
+
def match?
|
44
|
+
search_types.include?(@expected_class)
|
45
|
+
end
|
46
|
+
|
47
|
+
def search_tuple
|
48
|
+
search_tuple = @session.is_a?(Array) ? @session : @session.searches.last
|
49
|
+
raise 'no search found' unless search_tuple
|
50
|
+
search_tuple
|
51
|
+
end
|
52
|
+
|
53
|
+
def search_types
|
54
|
+
search_tuple.first
|
55
|
+
end
|
56
|
+
|
57
|
+
def failure_message_for_should
|
58
|
+
"expected search class: #{search_types.join(' and ')} to match expected class: #{@expected_class}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def failure_message_for_should_not
|
62
|
+
"expected search class: #{search_types.join(' and ')} NOT to match expected class: #{@expected_class}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
module TestHelper
|
66
|
+
def assert_has_search_params(session, *method_and_args)
|
67
|
+
method, *args = method_and_args
|
68
|
+
matcher = HaveSearchParamsForSession.new(session, method, *args).get_matcher
|
69
|
+
assert matcher.match?, matcher.missing_param_error_message
|
70
|
+
end
|
71
|
+
|
72
|
+
def assert_has_no_search_params(session, *method_and_args)
|
73
|
+
method, *args = method_and_args
|
74
|
+
matcher = HaveSearchParamsForSession.new(session, method, *args).get_matcher
|
75
|
+
assert !matcher.match?, matcher.unexpected_match_error_message
|
76
|
+
end
|
77
|
+
|
78
|
+
def assert_is_search_for(session, expected_class)
|
79
|
+
matcher = BeASearchForSession.new(session, expected_class)
|
80
|
+
assert matcher.match?, matcher.failure_message_for_should
|
81
|
+
end
|
82
|
+
|
83
|
+
def assert_is_not_search_for(session, expected_class)
|
84
|
+
matcher = BeASearchForSession.new(session, expected_class)
|
85
|
+
assert !matcher.match?, matcher.failure_message_for_should_not
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/sunspot_matchers.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Joseph Palermo"]
|
9
9
|
s.email = []
|
10
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"
|
11
|
+
s.summary = "RSpec matchers and Test::Unit assertions for testing Sunspot"
|
12
|
+
s.description = "These matchers and assertions allow you to test what is happening inside the Sunspot Search DSL blocks"
|
13
13
|
s.license = "MIT"
|
14
14
|
|
15
15
|
s.required_rubygems_version = ">= 1.3.6"
|
@@ -0,0 +1,703 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'sunspot_matchers/test_helper'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class Post; end
|
7
|
+
class Blog; end
|
8
|
+
class Person; end
|
9
|
+
|
10
|
+
Sunspot.setup(Post) do
|
11
|
+
text :body
|
12
|
+
text :name
|
13
|
+
string :author_name
|
14
|
+
integer :blog_id
|
15
|
+
integer :category_ids
|
16
|
+
integer :popularity
|
17
|
+
time :published_at
|
18
|
+
float :average_rating
|
19
|
+
end
|
20
|
+
|
21
|
+
Sunspot.setup(Blog) do
|
22
|
+
text :body
|
23
|
+
string :name
|
24
|
+
end
|
25
|
+
|
26
|
+
class SunspotMatchersTest < Test::Unit::TestCase
|
27
|
+
include SunspotMatchers::TestHelper
|
28
|
+
|
29
|
+
def setup
|
30
|
+
Sunspot.session = SunspotMatchers::SunspotSessionSpy.new(Sunspot.session)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_specify_search_params
|
34
|
+
Sunspot.search(Post) do
|
35
|
+
keywords 'great pizza'
|
36
|
+
end
|
37
|
+
Sunspot.search(Blog) do
|
38
|
+
keywords 'bad pizza'
|
39
|
+
end
|
40
|
+
assert_has_search_params Sunspot.session.searches.first, :keywords, 'great pizza'
|
41
|
+
assert_has_search_params Sunspot.session.searches.last, :keywords, 'bad pizza'
|
42
|
+
end
|
43
|
+
|
44
|
+
# should allow you to specify your search on multiple models
|
45
|
+
def test_specify_search_params_multiple_models
|
46
|
+
Sunspot.search([ Post, Blog ]) do
|
47
|
+
keywords 'great pizza'
|
48
|
+
end
|
49
|
+
assert_has_search_params Sunspot.session, :keywords, 'great pizza'
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_match_keywords
|
53
|
+
Sunspot.search(Post) do
|
54
|
+
keywords 'great pizza'
|
55
|
+
end
|
56
|
+
assert_has_search_params Sunspot.session, :keywords, 'great pizza'
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_match_keywords_nomatch
|
60
|
+
Sunspot.search(Post) do
|
61
|
+
keywords 'terrible pizza'
|
62
|
+
end
|
63
|
+
assert_has_no_search_params Sunspot.session, :keywords, 'great pizza'
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_match_multiple_keywords
|
67
|
+
Sunspot.search(Post) do
|
68
|
+
keywords 'great pizza'
|
69
|
+
keywords 'terrible pizza'
|
70
|
+
end
|
71
|
+
|
72
|
+
assert_has_search_params Sunspot.session, :keywords, Proc.new {
|
73
|
+
keywords 'great pizza'
|
74
|
+
keywords 'terrible pizza'
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_allow_any_match_keyword
|
79
|
+
Sunspot.search(Post) do
|
80
|
+
keywords 'great pizza'
|
81
|
+
end
|
82
|
+
assert_has_search_params Sunspot.session, :keywords, any_param
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_allow_any_match_keyword_negative
|
86
|
+
Sunspot.search(Post) do
|
87
|
+
with :blog_id, 4
|
88
|
+
end
|
89
|
+
assert_has_no_search_params Sunspot.session, :keywords, any_param
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_with_matcher_matches
|
93
|
+
Sunspot.search(Post) do
|
94
|
+
with :author_name, 'Mark Twain'
|
95
|
+
end
|
96
|
+
assert_has_search_params Sunspot.session, :with, :author_name, 'Mark Twain'
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_with_matcher_doesnt_match_search
|
100
|
+
Sunspot.search(Post) do
|
101
|
+
with :author_name, 'Mark Twain'
|
102
|
+
end
|
103
|
+
assert_has_no_search_params Sunspot.session, :with, :author_name, 'John Twain'
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_with_matcher_matches_multiple_with
|
107
|
+
Sunspot.search(Post) do
|
108
|
+
with :author_name, 'Mark Twain'
|
109
|
+
with :author_name, 'John Twain'
|
110
|
+
end
|
111
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
112
|
+
with :author_name, 'Mark Twain'
|
113
|
+
with :author_name, 'John Twain'
|
114
|
+
}
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_with_matcher_matches_greater_than
|
118
|
+
Sunspot.search(Post) do
|
119
|
+
with(:category_ids).greater_than(1)
|
120
|
+
end
|
121
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
122
|
+
with(:category_ids).greater_than(1)
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_with_matcher_matches_less_than
|
127
|
+
Sunspot.search(Post) do
|
128
|
+
with(:category_ids).less_than(1)
|
129
|
+
end
|
130
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
131
|
+
with(:category_ids).less_than(1)
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_with_matcher_matches_range
|
136
|
+
Sunspot.search(Post) do
|
137
|
+
with :category_ids, 1..3
|
138
|
+
end
|
139
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
140
|
+
with :category_ids, 1..3
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_with_matcher_matches_any_of
|
145
|
+
Sunspot.search(Post) do
|
146
|
+
with(:category_ids).any_of [ 1, 2 ]
|
147
|
+
end
|
148
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
149
|
+
with(:category_ids).any_of [ 1, 2 ]
|
150
|
+
}
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_with_matcher_matches_any_of_multiline
|
154
|
+
Sunspot.search(Post) do
|
155
|
+
any_of do
|
156
|
+
with :category_ids, 1
|
157
|
+
with :category_ids, 2
|
158
|
+
end
|
159
|
+
end
|
160
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
161
|
+
any_of do
|
162
|
+
with :category_ids, 1
|
163
|
+
with :category_ids, 2
|
164
|
+
end
|
165
|
+
}
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_with_matcher_matches_any_of_and_all_of
|
169
|
+
Sunspot.search(Post) do
|
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
|
+
end
|
178
|
+
assert_has_search_params Sunspot.session, :with, Proc.new {
|
179
|
+
any_of do
|
180
|
+
with :category_ids, 1
|
181
|
+
all_of do
|
182
|
+
with :category_ids, 2
|
183
|
+
with :category_ids, 3
|
184
|
+
end
|
185
|
+
end
|
186
|
+
}
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_with_matcher_matches_any_param_match
|
190
|
+
Sunspot.search(Post) do
|
191
|
+
with :blog_id, 4
|
192
|
+
end
|
193
|
+
assert_has_search_params Sunspot.session, :with, :blog_id, any_param
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_with_matcher_matches_any_param_negative_no_with
|
197
|
+
Sunspot.search(Post) do
|
198
|
+
keywords 'great pizza'
|
199
|
+
end
|
200
|
+
assert_has_no_search_params Sunspot.session, :with, :blog_id, any_param
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_with_matcher_matches_any_param_negative_diff_field
|
204
|
+
Sunspot.search(Post) do
|
205
|
+
with :category_ids, 7
|
206
|
+
end
|
207
|
+
assert_has_no_search_params Sunspot.session, :with, :blog_id, any_param
|
208
|
+
end
|
209
|
+
|
210
|
+
# 'without' matcher
|
211
|
+
|
212
|
+
def test_without_matcher_matches
|
213
|
+
Sunspot.search(Post) do
|
214
|
+
without :author_name, 'Mark Twain'
|
215
|
+
end
|
216
|
+
assert_has_search_params Sunspot.session, :without, :author_name, 'Mark Twain'
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_without_matcher_doesnt_match_search
|
220
|
+
Sunspot.search(Post) do
|
221
|
+
without :author_name, 'Mark Twain'
|
222
|
+
end
|
223
|
+
assert_has_no_search_params Sunspot.session, :without, :author_name, 'John Twain'
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_without_matcher_doesnt_match_with_search
|
227
|
+
Sunspot.search(Post) do
|
228
|
+
with :author_name, 'Mark Twain'
|
229
|
+
end
|
230
|
+
assert_has_no_search_params Sunspot.session, :without, :author_name, 'Mark Twain'
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_without_matcher_matches_multiple_with
|
234
|
+
Sunspot.search(Post) do
|
235
|
+
without :author_name, 'Mark Twain'
|
236
|
+
without :author_name, 'John Twain'
|
237
|
+
end
|
238
|
+
assert_has_search_params Sunspot.session, :without, Proc.new {
|
239
|
+
without :author_name, 'Mark Twain'
|
240
|
+
without :author_name, 'John Twain'
|
241
|
+
}
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_without_matcher_matches_greater_than
|
245
|
+
Sunspot.search(Post) do
|
246
|
+
without(:category_ids).greater_than(1)
|
247
|
+
end
|
248
|
+
assert_has_search_params Sunspot.session, :without, Proc.new {
|
249
|
+
without(:category_ids).greater_than(1)
|
250
|
+
}
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_without_matcher_matches_less_than
|
254
|
+
Sunspot.search(Post) do
|
255
|
+
without(:category_ids).less_than(1)
|
256
|
+
end
|
257
|
+
assert_has_search_params Sunspot.session, :without, Proc.new {
|
258
|
+
without(:category_ids).less_than(1)
|
259
|
+
}
|
260
|
+
end
|
261
|
+
|
262
|
+
def test_without_matcher_matches_range
|
263
|
+
Sunspot.search(Post) do
|
264
|
+
without :category_ids, 1..3
|
265
|
+
end
|
266
|
+
assert_has_search_params Sunspot.session, :without, Proc.new {
|
267
|
+
without :category_ids, 1..3
|
268
|
+
}
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_without_matcher_matches_any_of
|
272
|
+
Sunspot.search(Post) do
|
273
|
+
without(:category_ids).any_of [ 1, 2 ]
|
274
|
+
end
|
275
|
+
assert_has_search_params Sunspot.session, :without, Proc.new {
|
276
|
+
without(:category_ids).any_of [ 1, 2 ]
|
277
|
+
}
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_without_matcher_matches_any_of_multiline
|
281
|
+
Sunspot.search(Post) do
|
282
|
+
any_of do
|
283
|
+
without :category_ids, 1
|
284
|
+
without :category_ids, 2
|
285
|
+
end
|
286
|
+
end
|
287
|
+
assert_has_search_params Sunspot.session, :without, Proc.new {
|
288
|
+
any_of do
|
289
|
+
without :category_ids, 1
|
290
|
+
without :category_ids, 2
|
291
|
+
end
|
292
|
+
}
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_without_matcher_matches_any_of_and_all_of
|
296
|
+
Sunspot.search(Post) do
|
297
|
+
any_of do
|
298
|
+
without :category_ids, 1
|
299
|
+
all_of do
|
300
|
+
without :category_ids, 2
|
301
|
+
without :category_ids, 3
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
assert_has_search_params Sunspot.session, :without, Proc.new {
|
306
|
+
any_of do
|
307
|
+
without :category_ids, 1
|
308
|
+
all_of do
|
309
|
+
without :category_ids, 2
|
310
|
+
without :category_ids, 3
|
311
|
+
end
|
312
|
+
end
|
313
|
+
}
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_without_matcher_matches_any_param_match
|
317
|
+
Sunspot.search(Post) do
|
318
|
+
without :blog_id, 4
|
319
|
+
end
|
320
|
+
assert_has_search_params Sunspot.session, :without, :blog_id, any_param
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_without_matcher_matches_any_param_negative_no_without
|
324
|
+
Sunspot.search(Post) do
|
325
|
+
keywords 'great pizza'
|
326
|
+
end
|
327
|
+
assert_has_no_search_params Sunspot.session, :without, :blog_id, any_param
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_without_matcher_matches_any_param_negative_diff_field
|
331
|
+
Sunspot.search(Post) do
|
332
|
+
without :category_ids, 7
|
333
|
+
end
|
334
|
+
assert_has_no_search_params Sunspot.session, :without, :blog_id, any_param
|
335
|
+
end
|
336
|
+
|
337
|
+
# 'paginate' matcher
|
338
|
+
|
339
|
+
def test_paginate_matcher_matches
|
340
|
+
Sunspot.search(Post) do
|
341
|
+
paginate :page => 3, :per_page => 15
|
342
|
+
end
|
343
|
+
assert_has_search_params Sunspot.session, :paginate, { :page => 3, :per_page => 15 }
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_paginate_matcher_matches_page_only
|
347
|
+
Sunspot.search(Post) do
|
348
|
+
paginate :page => 3
|
349
|
+
end
|
350
|
+
assert_has_search_params Sunspot.session, :paginate, { :page => 3 }
|
351
|
+
end
|
352
|
+
|
353
|
+
def test_paginate_matcher_matches_per_page_only
|
354
|
+
Sunspot.search(Post) do
|
355
|
+
paginate :per_page => 15
|
356
|
+
end
|
357
|
+
assert_has_search_params Sunspot.session, :paginate, { :per_page => 15 }
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_paginate_matcher_doesnt_match_without_per_page
|
361
|
+
Sunspot.search(Post) do
|
362
|
+
paginate :page => 3, :per_page => 30
|
363
|
+
end
|
364
|
+
assert_has_no_search_params Sunspot.session, :paginate, { :page => 3, :per_page => 15 }
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_paginate_matcher_doesnt_match_without_page
|
368
|
+
Sunspot.search(Post) do
|
369
|
+
paginate :page => 5, :per_page => 15
|
370
|
+
end
|
371
|
+
assert_has_no_search_params Sunspot.session, :paginate, { :page => 3, :per_page => 15 }
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_order_by_matcher_matches
|
375
|
+
Sunspot.search(Post) do
|
376
|
+
order_by :published_at, :desc
|
377
|
+
end
|
378
|
+
assert_has_search_params Sunspot.session, :order_by, :published_at, :desc
|
379
|
+
end
|
380
|
+
|
381
|
+
def test_order_by_matcher_doesnt_match
|
382
|
+
Sunspot.search(Post) do
|
383
|
+
order_by :published_at, :asc
|
384
|
+
end
|
385
|
+
assert_has_no_search_params Sunspot.session, :order_by, :published_at, :desc
|
386
|
+
end
|
387
|
+
|
388
|
+
def test_order_by_matcher_matches_multiple
|
389
|
+
Sunspot.search(Post) do
|
390
|
+
order_by :published_at, :asc
|
391
|
+
order_by :average_rating, :asc
|
392
|
+
end
|
393
|
+
assert_has_search_params Sunspot.session, :order_by, Proc.new {
|
394
|
+
order_by :published_at, :asc
|
395
|
+
order_by :average_rating, :asc
|
396
|
+
}
|
397
|
+
end
|
398
|
+
|
399
|
+
def test_order_by_matcher_doesnt_match_multiple_reversed
|
400
|
+
Sunspot.search(Post) do
|
401
|
+
order_by :average_rating, :asc
|
402
|
+
order_by :published_at, :asc
|
403
|
+
end
|
404
|
+
assert_has_no_search_params Sunspot.session, :order_by, Proc.new {
|
405
|
+
order_by :published_at, :asc
|
406
|
+
order_by :average_rating, :asc
|
407
|
+
}
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_order_by_matcher_matches_on_random
|
411
|
+
Sunspot.search(Post) do
|
412
|
+
order_by :average_rating, :asc
|
413
|
+
order_by :random
|
414
|
+
end
|
415
|
+
assert_has_search_params Sunspot.session, :order_by, Proc.new {
|
416
|
+
order_by :average_rating, :asc
|
417
|
+
order_by :random
|
418
|
+
}
|
419
|
+
end
|
420
|
+
|
421
|
+
def test_order_by_matcher_doesnt_match_without_random
|
422
|
+
Sunspot.search(Post) do
|
423
|
+
order_by :average_rating, :asc
|
424
|
+
order_by :random
|
425
|
+
end
|
426
|
+
assert_has_no_search_params Sunspot.session, :order_by, Proc.new {
|
427
|
+
order_by :average_rating, :asc
|
428
|
+
}
|
429
|
+
end
|
430
|
+
|
431
|
+
def test_order_by_matcher_with_score
|
432
|
+
Sunspot.search(Post) do
|
433
|
+
order_by :average_rating, :asc
|
434
|
+
order_by :score
|
435
|
+
end
|
436
|
+
assert_has_search_params Sunspot.session, :order_by, Proc.new {
|
437
|
+
order_by :average_rating, :asc
|
438
|
+
order_by :score
|
439
|
+
}
|
440
|
+
end
|
441
|
+
|
442
|
+
def test_order_by_matcher_doesnt_match_without_score
|
443
|
+
Sunspot.search(Post) do
|
444
|
+
order_by :average_rating, :asc
|
445
|
+
order_by :score
|
446
|
+
end
|
447
|
+
assert_has_no_search_params Sunspot.session, :order_by, Proc.new {
|
448
|
+
order_by :average_rating, :asc
|
449
|
+
}
|
450
|
+
end
|
451
|
+
|
452
|
+
def test_order_by_matcher_respects_any_param_on_direction
|
453
|
+
Sunspot.search(Post) do
|
454
|
+
order_by :average_rating, :asc
|
455
|
+
end
|
456
|
+
assert_has_search_params Sunspot.session, :order_by, :average_rating, any_param
|
457
|
+
end
|
458
|
+
|
459
|
+
def test_order_by_matcher_respects_any_param_on_field
|
460
|
+
Sunspot.search(Post) do
|
461
|
+
order_by :average_rating, :asc
|
462
|
+
end
|
463
|
+
assert_has_search_params Sunspot.session, :order_by, any_param
|
464
|
+
end
|
465
|
+
|
466
|
+
def test_order_by_matcher_doesnt_respect_any_param_on_direction
|
467
|
+
Sunspot.search(Post) do
|
468
|
+
order_by :score
|
469
|
+
end
|
470
|
+
assert_has_no_search_params Sunspot.session, :order_by, :average_rating, any_param
|
471
|
+
end
|
472
|
+
|
473
|
+
def test_order_by_matcher_doesnt_respect_any_param_on_field
|
474
|
+
Sunspot.search(Post) do
|
475
|
+
keywords 'great pizza'
|
476
|
+
end
|
477
|
+
assert_has_no_search_params Sunspot.session, :order_by, any_param
|
478
|
+
end
|
479
|
+
|
480
|
+
def test_order_by_matcher_respects_any_param_on_field_and_dir
|
481
|
+
Sunspot.search(Post) do
|
482
|
+
order_by :score, :desc
|
483
|
+
end
|
484
|
+
assert_has_search_params Sunspot.session, :order_by, any_param, any_param
|
485
|
+
end
|
486
|
+
|
487
|
+
def test_order_by_matcher_respects_any_param_on_field_and_dir
|
488
|
+
Sunspot.search(Post) do
|
489
|
+
keywords 'great pizza'
|
490
|
+
end
|
491
|
+
assert_has_no_search_params Sunspot.session, :order_by, any_param, any_param
|
492
|
+
end
|
493
|
+
|
494
|
+
# 'facet' matcher
|
495
|
+
|
496
|
+
def test_facet_matcher_matches_field_facets
|
497
|
+
Sunspot.search(Post) do
|
498
|
+
facet :category_ids
|
499
|
+
end
|
500
|
+
assert_has_search_params Sunspot.session, :facet, :category_ids
|
501
|
+
end
|
502
|
+
|
503
|
+
def test_facet_matcher_doesnt_match_nonexistent_facet
|
504
|
+
Sunspot.search(Post) do
|
505
|
+
paginate :page => 5, :per_page => 15
|
506
|
+
end
|
507
|
+
assert_has_no_search_params Sunspot.session, :facet, :category_ids
|
508
|
+
end
|
509
|
+
|
510
|
+
def test_facet_matcher_matches_excluding_filters
|
511
|
+
Sunspot.search(Post) do
|
512
|
+
category_filter = with(:category_ids, 2)
|
513
|
+
facet(:category_ids, :exclude => category_filter)
|
514
|
+
end
|
515
|
+
assert_has_search_params Sunspot.session, :facet, Proc.new {
|
516
|
+
category_filter = with(:category_ids, 2)
|
517
|
+
facet(:category_ids, :exclude => category_filter)
|
518
|
+
}
|
519
|
+
end
|
520
|
+
|
521
|
+
def test_query_facet_matcher_matches
|
522
|
+
Sunspot.search(Post) do
|
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..3.0)
|
529
|
+
end
|
530
|
+
end
|
531
|
+
end
|
532
|
+
assert_has_search_params Sunspot.session, :facet, Proc.new {
|
533
|
+
facet(:average_rating) do
|
534
|
+
row(1.0..2.0) do
|
535
|
+
with(:average_rating, 1.0..2.0)
|
536
|
+
end
|
537
|
+
row(2.0..3.0) do
|
538
|
+
with(:average_rating, 2.0..3.0)
|
539
|
+
end
|
540
|
+
end
|
541
|
+
}
|
542
|
+
end
|
543
|
+
|
544
|
+
def test_query_facet_matcher_doesnt_match_missing_facet
|
545
|
+
Sunspot.search(Post) do
|
546
|
+
facet(:average_rating) do
|
547
|
+
row(1.0..2.0) do
|
548
|
+
with(:average_rating, 1.0..2.0)
|
549
|
+
end
|
550
|
+
end
|
551
|
+
end
|
552
|
+
assert_has_no_search_params Sunspot.session, :facet, Proc.new {
|
553
|
+
facet(:average_rating) do
|
554
|
+
row(1.0..2.0) do
|
555
|
+
with(:average_rating, 1.0..2.0)
|
556
|
+
end
|
557
|
+
row(2.0..3.0) do
|
558
|
+
with(:average_rating, 2.0..3.0)
|
559
|
+
end
|
560
|
+
end
|
561
|
+
}
|
562
|
+
end
|
563
|
+
|
564
|
+
def test_query_facet_matcher_doesnt_match_different_query
|
565
|
+
Sunspot.search(Post) do
|
566
|
+
facet(:average_rating) do
|
567
|
+
row(1.0..2.0) do
|
568
|
+
with(:average_rating, 1.0..2.0)
|
569
|
+
end
|
570
|
+
row(2.0..3.0) do
|
571
|
+
with(:average_rating, 2.0..3.0)
|
572
|
+
end
|
573
|
+
end
|
574
|
+
end
|
575
|
+
assert_has_no_search_params Sunspot.session, :facet, Proc.new {
|
576
|
+
facet(:average_rating) do
|
577
|
+
row(1.0..2.0) do
|
578
|
+
with(:average_rating, 1.0..2.0)
|
579
|
+
end
|
580
|
+
row(2.0..3.0) do
|
581
|
+
with(:average_rating, 2.0..4.0)
|
582
|
+
end
|
583
|
+
end
|
584
|
+
}
|
585
|
+
end
|
586
|
+
|
587
|
+
# 'boost' matcher
|
588
|
+
|
589
|
+
def test_boost_matcher_matches
|
590
|
+
Sunspot.search(Post) do
|
591
|
+
keywords 'great pizza' do
|
592
|
+
boost_fields :body => 2.0
|
593
|
+
end
|
594
|
+
end
|
595
|
+
assert_has_search_params Sunspot.session, :boost, Proc.new {
|
596
|
+
keywords 'great pizza' do
|
597
|
+
boost_fields :body => 2.0
|
598
|
+
end
|
599
|
+
}
|
600
|
+
end
|
601
|
+
|
602
|
+
def test_boost_matcher_doesnt_match_on_boost_mismatch
|
603
|
+
Sunspot.search(Post) do
|
604
|
+
keywords 'great pizza' do
|
605
|
+
boost_fields :body => 2.0
|
606
|
+
end
|
607
|
+
end
|
608
|
+
assert_has_no_search_params Sunspot.session, :boost, Proc.new {
|
609
|
+
keywords 'great pizza' do
|
610
|
+
boost_fields :body => 3.0
|
611
|
+
end
|
612
|
+
}
|
613
|
+
end
|
614
|
+
|
615
|
+
def test_boost_matcher_matches_boost_query
|
616
|
+
Sunspot.search(Post) do
|
617
|
+
keywords 'great pizza' do
|
618
|
+
boost(2.0) do
|
619
|
+
with :blog_id, 4
|
620
|
+
end
|
621
|
+
end
|
622
|
+
end
|
623
|
+
assert_has_search_params Sunspot.session, :boost, Proc.new {
|
624
|
+
keywords 'great pizza' do
|
625
|
+
boost(2.0) do
|
626
|
+
with :blog_id, 4
|
627
|
+
end
|
628
|
+
end
|
629
|
+
}
|
630
|
+
end
|
631
|
+
|
632
|
+
def test_boost_matcher_doesnt_match_on_boost_query_mismatch
|
633
|
+
Sunspot.search(Post) do
|
634
|
+
keywords 'great pizza' do
|
635
|
+
boost(2.0) do
|
636
|
+
with :blog_id, 4
|
637
|
+
end
|
638
|
+
end
|
639
|
+
end
|
640
|
+
assert_has_no_search_params Sunspot.session, :boost, Proc.new {
|
641
|
+
keywords 'great pizza' do
|
642
|
+
boost(2.0) do
|
643
|
+
with :blog_id, 5
|
644
|
+
end
|
645
|
+
end
|
646
|
+
}
|
647
|
+
end
|
648
|
+
|
649
|
+
def test_boost_matcher_matches_boost_function
|
650
|
+
Sunspot.search(Post) do
|
651
|
+
keywords 'great pizza' do
|
652
|
+
boost(function { sum(:average_rating, product(:popularity, 10)) })
|
653
|
+
end
|
654
|
+
end
|
655
|
+
assert_has_search_params Sunspot.session, :boost, Proc.new {
|
656
|
+
keywords 'great pizza' do
|
657
|
+
boost(function { sum(:average_rating, product(:popularity, 10)) })
|
658
|
+
end
|
659
|
+
}
|
660
|
+
end
|
661
|
+
|
662
|
+
def test_boost_matcher_doesnt_match_on_boost_function_mismatch
|
663
|
+
Sunspot.search(Post) do
|
664
|
+
keywords 'great pizza' do
|
665
|
+
boost(function { sum(:average_rating, product(:popularity, 10)) })
|
666
|
+
end
|
667
|
+
end
|
668
|
+
assert_has_no_search_params Sunspot.session, :boost, Proc.new {
|
669
|
+
keywords 'great pizza' do
|
670
|
+
boost(function { sum(:average_rating, product(:popularity, 42)) })
|
671
|
+
end
|
672
|
+
}
|
673
|
+
end
|
674
|
+
|
675
|
+
# 'be a search for'
|
676
|
+
|
677
|
+
def test_be_a_search_for
|
678
|
+
Sunspot.search(Post) { keywords 'great pizza' }
|
679
|
+
assert_is_search_for Sunspot.session, Post
|
680
|
+
end
|
681
|
+
|
682
|
+
def test_be_a_search_for_model_mismatch
|
683
|
+
Sunspot.search(Post) { keywords 'great pizza' }
|
684
|
+
assert_is_not_search_for Sunspot.session, Blog
|
685
|
+
end
|
686
|
+
|
687
|
+
def test_be_a_search_for_multiple_models
|
688
|
+
Sunspot.search(Post) { keywords 'great pizza' }
|
689
|
+
Sunspot.search([ Post, Blog ]) do
|
690
|
+
keywords 'great pizza'
|
691
|
+
end
|
692
|
+
assert_is_search_for Sunspot.session, Post
|
693
|
+
assert_is_search_for Sunspot.session, Blog
|
694
|
+
assert_is_not_search_for Sunspot.session, Person
|
695
|
+
end
|
696
|
+
|
697
|
+
def test_be_a_search_for_multiple_searches
|
698
|
+
Sunspot.search(Post) { keywords 'great pizza' }
|
699
|
+
Sunspot.search(Blog) { keywords 'bad pizza' }
|
700
|
+
assert_is_search_for Sunspot.session.searches.first, Post
|
701
|
+
assert_is_search_for Sunspot.session.searches.last, Blog
|
702
|
+
end
|
703
|
+
end
|
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:
|
4
|
+
hash: 69
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 3
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
version: 1.3.0.
|
10
|
+
- 1
|
11
|
+
version: 1.3.0.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-
|
19
|
+
date: 2011-12-02 00:00:00 -08:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
version: "0"
|
82
82
|
type: :development
|
83
83
|
version_requirements: *id004
|
84
|
-
description: These matchers allow you to test what is happening inside the Sunspot Search DSL blocks
|
84
|
+
description: These matchers and assertions allow you to test what is happening inside the Sunspot Search DSL blocks
|
85
85
|
email: []
|
86
86
|
|
87
87
|
executables: []
|
@@ -100,9 +100,11 @@ files:
|
|
100
100
|
- lib/sunspot_matchers.rb
|
101
101
|
- lib/sunspot_matchers/matchers.rb
|
102
102
|
- lib/sunspot_matchers/sunspot_session_spy.rb
|
103
|
+
- lib/sunspot_matchers/test_helper.rb
|
103
104
|
- lib/sunspot_matchers/version.rb
|
104
105
|
- spec/sunspot_matchers_spec.rb
|
105
106
|
- sunspot_matchers.gemspec
|
107
|
+
- test/sunspot_matchers_test.rb
|
106
108
|
has_rdoc: true
|
107
109
|
homepage: https://github.com/pivotal/sunspot_matchers
|
108
110
|
licenses:
|
@@ -138,6 +140,6 @@ rubyforge_project:
|
|
138
140
|
rubygems_version: 1.3.7
|
139
141
|
signing_key:
|
140
142
|
specification_version: 3
|
141
|
-
summary: RSpec matchers for testing Sunspot
|
143
|
+
summary: RSpec matchers and Test::Unit assertions for testing Sunspot
|
142
144
|
test_files: []
|
143
145
|
|