bento_search 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ require 'cgi'
5
5
  class DoajArticlesEngineTest < ActiveSupport::TestCase
6
6
  extend TestWithCassette
7
7
 
8
- def setup
8
+ def setup
9
9
  @engine = BentoSearch::DoajArticlesEngine.new
10
10
  # tell it not to send our bad API key
11
11
  end
@@ -13,15 +13,15 @@ class DoajArticlesEngineTest < ActiveSupport::TestCase
13
13
  test_with_cassette("basic search", :doaj_articles) do
14
14
  results = @engine.search("Breast cancer patients with lobular cancer more commonly have a father than a mother diagnosed with cancer")
15
15
 
16
- assert_kind_of BentoSearch::Results, results
16
+ assert_kind_of BentoSearch::Results, results
17
17
  assert ! results.failed?
18
18
 
19
19
  assert_not_nil results.total_items
20
20
  assert_equal 0, results.start
21
21
  assert_equal 10, results.per_page
22
-
22
+
23
23
  assert_not_empty results
24
-
24
+
25
25
  first = results.first
26
26
 
27
27
  assert_present first.unique_id
@@ -52,11 +52,11 @@ class DoajArticlesEngineTest < ActiveSupport::TestCase
52
52
 
53
53
  test_with_cassette("pagination", :doaj_articles) do
54
54
  results = @engine.search("cancer", :per_page => 20, :page => 3)
55
-
55
+
56
56
  assert ! results.failed?
57
-
57
+
58
58
  assert_equal 20, results.length
59
-
59
+
60
60
  assert_equal 20, results.size
61
61
  assert_equal 40, results.start
62
62
  assert_equal 20, results.per_page
@@ -70,7 +70,7 @@ class DoajArticlesEngineTest < ActiveSupport::TestCase
70
70
  assert results.failed?
71
71
  assert_kind_of Hash, results.error
72
72
  assert_present results.error[:message]
73
- assert_present results.error[:status]
73
+ assert_present results.error[:status]
74
74
  end
75
75
 
76
76
  test_with_cassette("live #get(identifier) round trip", :doaj_articles) do
@@ -197,4 +197,4 @@ class DoajArticlesEngineTest < ActiveSupport::TestCase
197
197
  assert_equal ["article.created_date:desc"], query["sort"]
198
198
  end
199
199
 
200
- end
200
+ end
@@ -5,22 +5,22 @@ class ParseSearchArgumentsTest < ActiveSupport::TestCase
5
5
 
6
6
  class Dummy
7
7
  include BentoSearch::SearchEngine
8
-
8
+
9
9
  def search_implementation(args)
10
10
  # no-op for now
11
11
  BentoSearch::Results.new
12
12
  end
13
-
13
+
14
14
  def test_parse(*args)
15
15
  # original method is protected, this is a lame way
16
- # to expose it.
16
+ # to expose it.
17
17
  parse_search_arguments(*args)
18
18
  end
19
-
19
+
20
20
  def max_per_page
21
21
  40
22
22
  end
23
-
23
+
24
24
  def search_field_definitions
25
25
  {
26
26
  "my_title" => {:semantic => :title},
@@ -28,114 +28,114 @@ class ParseSearchArgumentsTest < ActiveSupport::TestCase
28
28
  "my_other" => nil
29
29
  }
30
30
  end
31
-
32
- end
33
-
34
-
31
+
32
+ end
33
+
34
+
35
35
  def test_single_arg
36
36
  d = Dummy.new
37
-
37
+
38
38
  args = d.test_parse("query")
39
-
40
- assert_equal( {:query => "query", :per_page => BentoSearch::SearchEngine::DefaultPerPage }, args )
39
+
40
+ assert_equal( {:query => "query", :per_page => BentoSearch::SearchEngine::DefaultPerPage }, args )
41
41
  end
42
-
42
+
43
43
  def test_two_arg
44
44
  d = Dummy.new
45
-
45
+
46
46
  args = d.test_parse("query", :arg => "1")
47
-
47
+
48
48
  assert_equal( {:query => "query", :arg => "1", :per_page => BentoSearch::SearchEngine::DefaultPerPage }, args )
49
49
  end
50
-
51
-
52
-
50
+
51
+
52
+
53
53
  def test_convert_page_to_start
54
54
  d = Dummy.new
55
-
55
+
56
56
  args = d.test_parse(:query => "query", :page => 1, :per_page => 20)
57
-
57
+
58
58
  assert_equal 0, args[:start]
59
59
  assert_equal 1, args[:page]
60
60
  assert_equal 20, args[:per_page]
61
-
61
+
62
62
  args = d.test_parse(:query => "query", :page => 3, :per_page => 20)
63
63
 
64
64
  assert_equal 40, args[:start]
65
65
  assert_equal 3, args[:page]
66
- assert_equal 20, args[:per_page]
66
+ assert_equal 20, args[:per_page]
67
67
  end
68
-
68
+
69
69
  def test_convert_start_to_page
70
70
  d = Dummy.new
71
-
72
- # rounds down to get closest 'page' if need be.
71
+
72
+ # rounds down to get closest 'page' if need be.
73
73
  args = d.test_parse(:query => "query", :start => '19', :per_page => '20')
74
-
74
+
75
75
  assert_equal 19, args[:start]
76
76
  assert_equal 1, args[:page]
77
-
77
+
78
78
  args = d.test_parse(:query => "query", :start => '20', :per_page => '20')
79
79
  assert_equal 2, args[:page]
80
80
  end
81
-
82
-
81
+
82
+
83
83
  def test_pagination_to_integer
84
84
  d = Dummy.new
85
-
85
+
86
86
  args = d.test_parse(:query => "query", :page => "1", :per_page => "20")
87
87
  assert_equal 0, args[:start]
88
88
  assert_equal 1, args[:page]
89
89
  assert_equal 20, args[:per_page]
90
-
90
+
91
91
  args = d.test_parse(:query => "query", :start => "20", :per_page => "20")
92
- assert_equal 20, args[:start]
92
+ assert_equal 20, args[:start]
93
93
  assert_equal 20, args[:per_page]
94
-
94
+
95
95
  end
96
-
96
+
97
97
  def test_ignore_blank_pagination_args
98
98
  d = Dummy.new
99
-
99
+
100
100
  args = d.test_parse(:query => "query", :page => "", :per_page => "", :start => "")
101
-
101
+
102
102
  assert ! (args.has_key? :page)
103
103
  assert ! (args.has_key? :start)
104
- assert (args.has_key? :per_page) # default per page always provided
104
+ assert (args.has_key? :per_page) # default per page always provided
105
105
  end
106
-
106
+
107
107
  def test_enforce_max_per_page
108
108
  d = Dummy.new
109
-
110
- assert_raise(ArgumentError) { d.test_parse(:query => "query", :per_page => 1000) }
109
+
110
+ assert_raise(ArgumentError) { d.test_parse(:query => "query", :per_page => 1000) }
111
111
  end
112
-
113
- def test_search_field_keys
112
+
113
+ def test_search_field_keys
114
114
  assert_equal ["my_title", "my_author", "my_other"], Dummy.new.search_keys
115
115
  assert_equal ["title", "author"], Dummy.new.semantic_search_keys
116
116
  end
117
-
117
+
118
118
  def test_semantic_search_map
119
- assert_equal( {"title" => "my_title", "author" => "my_author"},
119
+ assert_equal( {"title" => "my_title", "author" => "my_author"},
120
120
  Dummy.new.semantic_search_map)
121
121
  end
122
-
122
+
123
123
  def test_translate_search_field_semantics
124
124
  d = Dummy.new
125
-
125
+
126
126
  args = d.test_parse(:query => "query", :semantic_search_field => :title)
127
-
127
+
128
128
  assert ! (args.has_key? :semantic_search_field), "translates semantic_search_field to search_field"
129
129
  assert_equal "my_title", args[:search_field]
130
-
130
+
131
131
  assert_raise(ArgumentError, "Raises for undefined semantic_search_field when asked") do
132
132
  d.test_parse(:query => "query", :semantic_search_field => :subject, :unrecognized_search_field => :raise)
133
133
  end
134
134
  # without the :unrecognized_search_field => :raise, ignore
135
135
  args = d.test_parse(:query => "query", :semantic_search_field => :subject)
136
- assert_nil args[:search_field]
136
+ assert_nil args[:search_field]
137
137
  end
138
-
138
+
139
139
  def test_unrecognized_search_field
140
140
  d = Dummy.new
141
141
  assert_raise(ArgumentError, "Raises for undefined search field when asked") do
@@ -144,7 +144,7 @@ class ParseSearchArgumentsTest < ActiveSupport::TestCase
144
144
  assert_nothing_raised do
145
145
  d.test_parse(:query => "query", :search_field => "I_made_this_up")
146
146
  end
147
-
147
+
148
148
  # combine config and args
149
149
  engine = BentoSearch::MockEngine.new(:unrecognized_search_field => :raise)
150
150
  assert_raise(ArgumentError, "Raises for undefined search field when asked") do
@@ -153,7 +153,7 @@ class ParseSearchArgumentsTest < ActiveSupport::TestCase
153
153
  assert_nothing_raised do
154
154
  engine.normalized_search_arguments(:query => "query", :search_field => "I_made_this_up", :unrecognized_search_field => :ignore)
155
155
  end
156
-
156
+
157
157
  end
158
158
 
159
159
 
@@ -170,26 +170,26 @@ class ParseSearchArgumentsTest < ActiveSupport::TestCase
170
170
  end
171
171
 
172
172
  it "converts semantic search fields" do
173
- engine = MockEngine.new(:multi_field_search => true,
173
+ engine = MockEngine.new(:multi_field_search => true,
174
174
  :search_field_definitions => {
175
175
  "internal_title_field" => {:semantic => :title},
176
- "internal_author_field" => {:semantic => :author}
176
+ "internal_author_field" => {:semantic => :author}
177
177
  })
178
178
 
179
179
  engine.search(:query => {:title => "title query", :author => "author query"})
180
180
 
181
181
  assert_equal(
182
- { "internal_title_field" => "title query",
182
+ { "internal_title_field" => "title query",
183
183
  "internal_author_field" => "author query"},
184
184
  engine.last_args[:query]
185
185
  )
186
186
  end
187
187
 
188
188
  it "passes through other fields" do
189
- engine = MockEngine.new(:multi_field_search => true,
189
+ engine = MockEngine.new(:multi_field_search => true,
190
190
  :search_field_definitions => {
191
191
  "internal_title_field" => {:semantic => :title},
192
- "internal_author_field" => {:semantic => :author}
192
+ "internal_author_field" => {:semantic => :author}
193
193
  })
194
194
 
195
195
  engine.search(:query => {"internal_title_field" => "query", "other field" => "query"})
@@ -201,81 +201,81 @@ class ParseSearchArgumentsTest < ActiveSupport::TestCase
201
201
  end
202
202
 
203
203
  it "complains on unrecognized field if configured" do
204
- engine = MockEngine.new(:multi_field_search => true,
204
+ engine = MockEngine.new(:multi_field_search => true,
205
205
  :unrecognized_search_field => "raise",
206
206
  :search_field_definitions => {
207
207
  "internal_title_field" => {:semantic => :title},
208
- "internal_author_field" => {:semantic => :author}
209
- })
208
+ "internal_author_field" => {:semantic => :author}
209
+ })
210
210
  assert_raises(ArgumentError) do
211
211
  engine.search(:query => {"internal_title_field" => "query", "other field" => "query"})
212
212
  end
213
213
  end
214
214
  end
215
-
215
+
216
216
  def test_semantic_blank_ignored
217
217
  d = Dummy.new
218
-
218
+
219
219
  args1 = d.test_parse(:query => "query", :semantic_search_field => nil)
220
220
  args2 = d.test_parse(:query => "query", :semantic_search_field => nil)
221
-
221
+
222
222
  assert_nil args1[:search_field]
223
223
  assert_nil args2[:search_field]
224
224
  end
225
-
225
+
226
226
  def test_semantic_string_or_symbol
227
227
  d = Dummy.new
228
-
228
+
229
229
  args1 = d.test_parse(:query => "query", :semantic_search_field => :title)
230
230
  args2 = d.test_parse(:query => "query", :semantic_search_field => "title")
231
-
232
- assert_equal args1, args2
231
+
232
+ assert_equal args1, args2
233
233
  end
234
-
234
+
235
235
  def test_converts_sort_to_string
236
236
  d = Dummy.new
237
-
237
+
238
238
  args = d.test_parse(:query => "query", :sort => :title_asc)
239
-
239
+
240
240
  assert_equal "title_asc", args[:sort]
241
241
  end
242
-
242
+
243
243
  def test_sets_timing
244
244
  d = Dummy.new
245
245
  results = d.search("foo")
246
-
246
+
247
247
  assert_not_nil results.timing
248
- assert_not_nil results.timing_ms
248
+ assert_not_nil results.timing_ms
249
249
  end
250
-
250
+
251
251
  def test_passes_arbitrary_keys
252
252
  d = Dummy.new
253
253
  args = d.test_parse(:query => "foo", :custom_auth => true)
254
-
254
+
255
255
  assert_present args[:custom_auth]
256
256
  assert_equal true, args[:custom_auth]
257
-
257
+
258
258
  end
259
-
259
+
260
260
  def test_rescues_exceptions
261
261
  horrible_engine = Class.new do
262
262
  include BentoSearch::SearchEngine
263
-
263
+
264
264
  def search_implementation(args)
265
- raise TimeoutError.new("I am a horrible engine")
265
+ raise BentoSearch::RubyTimeoutClass.new("I am a horrible engine")
266
266
  end
267
267
  end
268
-
268
+
269
269
  engine = horrible_engine.new
270
-
271
- results = engine.search("cancer")
272
-
270
+
271
+ results = engine.search("cancer")
272
+
273
273
  assert_not_nil results
274
274
  assert results.failed?, "results marked failed"
275
275
  assert_not_nil results.error[:exception], "results.error has exception"
276
-
277
- assert_equal "I am a horrible engine", results.error[:exception].message, "results.error has right exception"
276
+
277
+ assert_equal "I am a horrible engine", results.error[:exception].message, "results.error has right exception"
278
278
  end
279
-
279
+
280
280
  end
281
281
 
@@ -4,50 +4,50 @@ require 'test_helper'
4
4
 
5
5
  class SearchEngineTest < ActiveSupport::TestCase
6
6
  MockEngine = BentoSearch::MockEngine
7
-
7
+
8
8
  test "takes configuration" do
9
9
  conf = Confstruct::Configuration.new( :foo => "foo", :bar => "bar", :top => {:next => "required key"} )
10
10
  engine = MockEngine.new(conf)
11
-
11
+
12
12
  assert_not_nil engine.configuration
13
13
  assert_equal "foo", engine.configuration.foo
14
14
  assert_equal "bar", engine.configuration.bar
15
15
  assert_equal "required key", engine.configuration.top.next
16
16
  end
17
-
17
+
18
18
  test "nested configuration with hash" do
19
19
  # possible bug in Confstruct make sure we're working around
20
- # if needed.
20
+ # if needed.
21
21
  # https://github.com/mbklein/confstruct/issues/14
22
22
  engine = MockEngine.new("top" => {"one" => "two"})
23
-
24
- assert_equal "two", engine.configuration.top.one
23
+
24
+ assert_equal "two", engine.configuration.top.one
25
25
  end
26
-
26
+
27
27
  test "nested required config key" do
28
28
  requires_class = Class.new(MockEngine) do
29
29
  def self.required_configuration
30
30
  ["required.mykey"]
31
31
  end
32
32
  end
33
-
33
+
34
34
  assert_raise ArgumentError do
35
35
  requires_class.new
36
- end
37
-
36
+ end
37
+
38
38
  assert_raise ArgumentError do
39
39
  requires_class.new(:requires => {})
40
40
  end
41
-
41
+
42
42
  assert_raise ArgumentError do
43
43
  requires_class.new(:required => {:mykey => nil})
44
44
  end
45
-
45
+
46
46
  assert_nothing_raised do
47
47
  requires_class.new(:required => {:mykey => "foo"})
48
48
  end
49
49
  end
50
-
50
+
51
51
  test "merges default configuration" do
52
52
  @dummy_class = Class.new do
53
53
  include BentoSearch::SearchEngine
@@ -59,120 +59,120 @@ class SearchEngineTest < ActiveSupport::TestCase
59
59
  }
60
60
  end
61
61
  end
62
-
62
+
63
63
  engine = @dummy_class.new( :two => "new", :array => ["one", "two"], :nested => {:two => "new"}, :required => {:mykey => "required key"} )
64
-
64
+
65
65
  assert_kind_of Confstruct::Configuration, engine.configuration
66
66
  assert_equal "default" , engine.configuration.one
67
67
  assert_equal "new" , engine.configuration.two
68
68
  assert_equal "default" , engine.configuration.nested.one
69
69
  assert_equal "new" , engine.configuration.nested.two
70
- assert_equal ["one", "two"] , engine.configuration.array
70
+ assert_equal ["one", "two"] , engine.configuration.array
71
71
  end
72
-
72
+
73
73
  test "no default configuration" do
74
74
  @dummy_class = Class.new do
75
75
  include BentoSearch::SearchEngine
76
76
  end
77
-
77
+
78
78
  engine = @dummy_class.new( :one => "one" )
79
-
79
+
80
80
  assert_kind_of Confstruct::Configuration, engine.configuration
81
81
  assert_equal "one", engine.configuration.one
82
82
  end
83
-
83
+
84
84
  test "sets metadata on results" do
85
85
  engine = MockEngine.new(:id => "foo")
86
-
86
+
87
87
  results = engine.search(:query => "cancer", :per_page => 20)
88
-
88
+
89
89
  assert_present results.search_args
90
90
  assert_equal "foo", results.engine_id
91
-
91
+
92
92
  pagination = results.pagination
93
93
  assert_present pagination
94
-
94
+
95
95
  assert_equal 20, pagination.per_page
96
96
  assert_equal 1, pagination.current_page
97
97
  assert_equal 1, pagination.start_record
98
98
  assert_equal 20, pagination.end_record
99
99
  assert pagination.first_page?
100
-
100
+
101
101
  assert_present pagination.total_pages
102
102
  assert_present pagination.count_records
103
-
103
+
104
104
  end
105
-
105
+
106
106
  test "sets metadata on items" do
107
- engine = MockEngine.new(:id => "foo", :for_display => {:mykey => "value", :decorator => "Foo"})
107
+ engine = MockEngine.new(:id => "foo", :for_display => {:mykey => "value", :decorator => "Foo"})
108
108
  results = engine.search(:query => "cancer")
109
109
  record = results.first
110
-
110
+
111
111
  assert_present record.engine_id
112
112
  assert_present record.display_configuration
113
- assert_present record.decorator
113
+ assert_present record.decorator
114
114
  end
115
-
115
+
116
116
  test "failed sets metadata on results" do
117
117
  engine = MockEngine.new(:id => "fail_engine", :error => {:message => "failed"}, :for_display => {:foo => "foo"})
118
-
118
+
119
119
  results = engine.search(:query => "cancer", :per_page => 20)
120
-
120
+
121
121
  assert results.failed?
122
122
  assert_present results.error
123
123
  assert_equal "fail_engine", results.engine_id
124
124
  assert_present results.search_args
125
- assert_equal( {:foo => "foo"}, results.display_configuration )
125
+ assert_equal( {:foo => "foo"}, results.display_configuration )
126
126
  end
127
-
127
+
128
128
  test "auto rescued exception, with proper metadata" do
129
- engine = MockEngine.new(:id => "raises", :raise_exception_class => 'TimeoutError', :for_display => {:foo => "foo"})
130
-
129
+ engine = MockEngine.new(:id => "raises", :raise_exception_class => BentoSearch::RubyTimeoutClass.name, :for_display => {:foo => "foo"})
130
+
131
131
  results = engine.search("foo", :per_page => 20)
132
-
132
+
133
133
  assert results.failed?, "marked failed"
134
- assert_present results.error
134
+ assert_present results.error
135
135
  assert_equal "raises", results.engine_id
136
136
  assert_present results.search_args
137
137
  assert_equal "foo", results.search_args[:query]
138
-
138
+
139
139
  assert_equal( {:foo => "foo"}, results.display_configuration )
140
140
  end
141
-
142
-
141
+
142
+
143
143
  test "has empty :for_display config" do
144
144
  engine = MockEngine.new
145
-
145
+
146
146
  assert_not_nil engine.configuration.for_display
147
147
  end
148
-
148
+
149
149
  test "error results still filled out okay" do
150
150
  engine = MockEngine.new(:error => {:msg => "forced error"}, :id => "test")
151
-
151
+
152
152
  results = engine.search("foo")
153
-
153
+
154
154
  assert_present results.search_args
155
155
  assert_equal "test", results.engine_id
156
-
156
+
157
157
  pagination = nil
158
158
  assert_nothing_raised { pagination = results.pagination }
159
-
159
+
160
160
  assert_present pagination
161
161
  assert_equal 0, pagination.count_records
162
-
162
+
163
163
  end
164
-
165
-
164
+
165
+
166
166
  test "carries display configuration over to results" do
167
- engine = MockEngine.new(:id => "foo",
167
+ engine = MockEngine.new(:id => "foo",
168
168
  :for_display => {:foo => "bar", :nested => {"one" => "two"}}
169
169
  )
170
-
170
+
171
171
  results = engine.search("foo")
172
172
 
173
173
  assert_present results.display_configuration
174
- assert_present results.display_configuration.foo
175
- assert_present results.display_configuration.nested.one
174
+ assert_present results.display_configuration.foo
175
+ assert_present results.display_configuration.nested.one
176
176
  end
177
-
177
+
178
178
  end