search_flip 2.0.0.beta2 → 2.0.0.beta3

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.
@@ -1,35 +0,0 @@
1
-
2
- require File.expand_path("../test_helper", __dir__)
3
-
4
- class SearchFlip::HTTPClientTest < SearchFlip::TestCase
5
- [:get, :put, :delete, :post, :head].each do |method|
6
- define_method :"test_#{method}" do
7
- stub_request(method, "http://localhost/path").with(body: "body", query: { key: "value" }).to_return(body: "success")
8
-
9
- assert_equal "success", SearchFlip::HTTPClient.send(method, "http://localhost/path", body: "body", params: { key: "value" }).body.to_s
10
- end
11
- end
12
-
13
- def test_headers
14
- stub_request(:get, "http://localhost/path").with(headers: { "X-Key" => "Value" }).to_return(body: "success")
15
-
16
- assert_equal "success", SearchFlip::HTTPClient.headers("X-Key" => "Value").get("http://localhost/path").body.to_s
17
- end
18
-
19
- def test_connection_error
20
- stub_request(:get, "http://localhost/path").to_raise(HTTP::ConnectionError)
21
-
22
- assert_raises SearchFlip::ConnectionError do
23
- SearchFlip::HTTPClient.get("http://localhost/path")
24
- end
25
- end
26
-
27
- def test_response_error
28
- stub_request(:get, "http://localhost/path").to_return(status: 500)
29
-
30
- assert_raises SearchFlip::ResponseError do
31
- SearchFlip::HTTPClient.get("http://localhost/path")
32
- end
33
- end
34
- end
35
-
@@ -1,451 +0,0 @@
1
-
2
- require File.expand_path("../test_helper", __dir__)
3
-
4
- class SearchFlip::IndexTest < SearchFlip::TestCase
5
- should_delegate_methods :profile, :where, :where_not, :filter, :range, :match_all, :exists,
6
- :exists_not, :post_where, :post_where_not, :post_filter, :post_must, :post_must_not,
7
- :post_should, :post_range, :post_exists, :post_exists_not, :aggregate, :scroll, :source,
8
- :includes, :eager_load, :preload, :sort, :resort, :order, :reorder, :offset, :limit,
9
- :paginate, :page, :per, :search, :find_in_batches, :highlight, :suggest, :custom, :find_each,
10
- :failsafe, :total_entries, :total_count, :terminate_after, :timeout, :should, :should_not,
11
- :must, :must_not, to: :criteria, subject: ProductIndex
12
-
13
- def test_serialize_exception
14
- klass = Class.new do
15
- include SearchFlip::Index
16
- end
17
-
18
- assert_raises SearchFlip::MethodNotImplemented do
19
- klass.serialize(Hashie::Mash)
20
- end
21
- end
22
-
23
- def test_type_name_exception
24
- klass = Class.new do
25
- include SearchFlip::Index
26
- end
27
-
28
- assert_raises SearchFlip::MethodNotImplemented do
29
- klass.serialize(Hashie::Mash)
30
- end
31
- end
32
-
33
- def test_create_index_works
34
- assert TestIndex.create_index
35
- ensure
36
- TestIndex.delete_index if TestIndex.index_exists?
37
- end
38
-
39
- def test_create_index_delegates_to_connection
40
- TestIndex.connection.expects(:create_index).with("test", {})
41
- TestIndex.create_index
42
- end
43
-
44
- def test_create_index_passes_index_settings_to_connection
45
- TestIndex.stubs(:index_settings).returns(settings: { number_of_shards: 3 })
46
- assert TestIndex.create_index
47
-
48
- assert "3", TestIndex.get_index_settings["test"]["settings"]["index"]["number_of_replicas"]
49
- ensure
50
- TestIndex.delete_index if TestIndex.index_exists?
51
- end
52
-
53
- def test_create_index_passes_index_settings_delegates_to_connection
54
- TestIndex.stubs(:index_settings).returns(settings: { number_of_shards: 3 })
55
-
56
- TestIndex.connection.expects(:create_index).with("test", settings: { number_of_shards: 3 })
57
- TestIndex.create_index
58
- end
59
-
60
- def test_create_index_passes_mapping_if_specified
61
- TestIndex.stubs(:mapping).returns(test: { properties: { id: { type: "long" } } })
62
- assert TestIndex.create_index
63
- ensure
64
- TestIndex.delete_index if TestIndex.index_exists?
65
- end
66
-
67
- def test_create_index_passes_mapping_if_specified_delegates_to_connection
68
- TestIndex.stubs(:mapping).returns(test: { properties: { id: { type: "long" } } })
69
-
70
- TestIndex.connection.expects(:create_index).with("test", mappings: { test: { properties: { id: { type: "long" } } } })
71
- TestIndex.create_index(include_mapping: true)
72
- end
73
-
74
- def test_update_index_settings_works
75
- TestIndex.create_index
76
- TestIndex.stubs(:index_settings).returns(settings: { number_of_replicas: 3 })
77
- TestIndex.update_index_settings
78
- ensure
79
- TestIndex.delete_index if TestIndex.index_exists?
80
- end
81
-
82
- def test_update_index_settings_delegates_to_connection
83
- index_settings = { settings: { number_of_replicas: 3 } }
84
-
85
- TestIndex.stubs(:index_settings).returns(settings: { number_of_replicas: 3 })
86
-
87
- TestIndex.connection.expects(:update_index_settings).with("test", index_settings)
88
- TestIndex.update_index_settings
89
- end
90
-
91
- def test_get_index_settings_works
92
- TestIndex.create_index
93
- assert TestIndex.get_index_settings
94
- ensure
95
- TestIndex.delete_index if TestIndex.index_exists?
96
- end
97
-
98
- def test_get_index_settings_delegates_to_connection
99
- TestIndex.connection.expects(:get_index_settings).with("test")
100
- TestIndex.get_index_settings
101
- end
102
-
103
- def test_index_exists_works
104
- TestIndex.create_index
105
- assert TestIndex.index_exists?
106
- ensure
107
- TestIndex.delete_index if TestIndex.index_exists?
108
- end
109
-
110
- def test_index_exists_delegates_to_connection
111
- TestIndex.connection.expects(:index_exists?).with("test")
112
- TestIndex.index_exists?
113
- end
114
-
115
- def test_delete_index_works
116
- TestIndex.create_index
117
- assert TestIndex.delete_index
118
- ensure
119
- TestIndex.delete_index if TestIndex.index_exists?
120
- end
121
-
122
- def test_delete_index_delegates_to_connection
123
- TestIndex.connection.expects(:delete_index).with("test")
124
- TestIndex.delete_index
125
- end
126
-
127
- def test_update_mapping_works
128
- TestIndex.stubs(:mapping).returns(test: { properties: { id: { type: "long" } } })
129
-
130
- TestIndex.create_index
131
- TestIndex.update_mapping
132
- ensure
133
- TestIndex.delete_index if TestIndex.index_exists?
134
- end
135
-
136
- def test_update_mapping_delegates_to_connection
137
- mapping = { test: { properties: { id: { type: "long" } } } }
138
-
139
- TestIndex.stubs(:mapping).returns(mapping)
140
-
141
- TestIndex.connection.expects(:update_mapping).with("test", "test", mapping)
142
- TestIndex.update_mapping
143
- end
144
-
145
- def test_get_mapping_works
146
- TestIndex.create_index
147
- TestIndex.update_mapping
148
-
149
- assert TestIndex.get_mapping
150
- ensure
151
- TestIndex.delete_index if TestIndex.index_exists?
152
- end
153
-
154
- def test_get_mapping_delegates_to_connection
155
- TestIndex.connection.expects(:get_mapping).with("test", "test")
156
- TestIndex.get_mapping
157
- end
158
-
159
- def test_refresh_works
160
- TestIndex.create_index
161
- TestIndex.refresh
162
- ensure
163
- TestIndex.delete_index if TestIndex.index_exists?
164
- end
165
-
166
- def test_refresh_delegates_to_connection
167
- TestIndex.connection.expects(:refresh).with("test")
168
- TestIndex.refresh
169
- end
170
-
171
- def test_index_url
172
- assert TestIndex.index_url
173
- end
174
-
175
- def test_index_url_delegates_to_connection
176
- TestIndex.connection.expects(:index_url).with("test")
177
- TestIndex.index_url
178
-
179
- SearchFlip::Config[:index_prefix] = "prefix-"
180
- TestIndex.connection.expects(:index_url).with("prefix-test")
181
- TestIndex.index_url
182
- ensure
183
- SearchFlip::Config[:index_prefix] = nil
184
- end
185
-
186
- def test_type_url
187
- assert TestIndex.type_url
188
- end
189
-
190
- def test_type_url_delegates_to_connection
191
- TestIndex.connection.expects(:type_url).with("test", "test")
192
- TestIndex.type_url
193
- end
194
-
195
- def test_import_object
196
- assert_difference "ProductIndex.total_entries" do
197
- ProductIndex.import create(:product)
198
- end
199
- end
200
-
201
- def test_import_array
202
- assert_difference "ProductIndex.total_entries", 2 do
203
- ProductIndex.import [create(:product), create(:product)]
204
- end
205
- end
206
-
207
- def test_import_scope
208
- create_list :product, 2
209
-
210
- assert_difference "ProductIndex.total_entries", 2 do
211
- ProductIndex.import Product.all
212
- end
213
- end
214
-
215
- def test_import_with_param_options
216
- products = create_list(:product, 2)
217
-
218
- assert_difference "ProductIndex.total_entries", 2 do
219
- ProductIndex.import products, {}, version: 1, version_type: "external"
220
- end
221
-
222
- assert_no_difference "ProductIndex.total_entries" do
223
- ProductIndex.import products, {}, version: 2, version_type: "external"
224
- ProductIndex.import products, { ignore_errors: [409] }, version: 2, version_type: "external"
225
-
226
- assert_raises SearchFlip::Bulk::Error do
227
- ProductIndex.import products, {}, version: 2, version_type: "external"
228
- end
229
- end
230
- end
231
-
232
- def test_import_with_class_options
233
- products = create_list(:product, 2)
234
-
235
- ProductIndex.stubs(:index_options).returns(version: 3, version_type: "external")
236
-
237
- assert_difference "ProductIndex.total_entries", 2 do
238
- ProductIndex.import products
239
- end
240
-
241
- actual = products.map { |product| ProductIndex.get(product.id)["_version"] }
242
-
243
- assert_equal [3, 3], actual
244
- end
245
-
246
- def test_index_array
247
- assert_difference "ProductIndex.total_entries", 2 do
248
- ProductIndex.index create_list(:product, 2)
249
- end
250
- end
251
-
252
- def test_index_scope
253
- temp_product_index = Class.new(ProductIndex)
254
-
255
- temp_product_index.define_singleton_method(:index_scope) do |scope|
256
- scope.where("price < 80")
257
- end
258
-
259
- products = [create(:product, price: 20), create(:product, price: 50), create(:product, price: 100)]
260
-
261
- assert_difference "ProductIndex.total_entries", 2 do
262
- temp_product_index.index Product.where(id: products.map(&:id))
263
- end
264
- end
265
-
266
- def test_create_array
267
- products = create_list(:product, 2)
268
-
269
- assert_difference "ProductIndex.total_entries", 2 do
270
- ProductIndex.create products
271
- end
272
-
273
- assert_no_difference "ProductIndex.total_entries" do
274
- assert_raises SearchFlip::Bulk::Error do
275
- ProductIndex.create products
276
- end
277
- end
278
- end
279
-
280
- def test_create_scope
281
- assert_difference "ProductIndex.total_entries", 2 do
282
- ProductIndex.create Product.where(id: create_list(:product, 2).map(&:id))
283
- end
284
- end
285
-
286
- def test_update_array
287
- products = create_list(:product, 2)
288
-
289
- assert_difference "ProductIndex.total_entries", 2 do
290
- ProductIndex.import products
291
- end
292
-
293
- assert_no_difference "ProductIndex.total_entries" do
294
- ProductIndex.update products
295
- end
296
- end
297
-
298
- def test_update_scope
299
- products = create_list(:product, 2)
300
-
301
- assert_difference "ProductIndex.total_entries", 2 do
302
- ProductIndex.import products
303
- end
304
-
305
- assert_no_difference "ProductIndex.total_entries" do
306
- ProductIndex.update Product.where(id: products.map(&:id))
307
- end
308
- end
309
-
310
- def test_delete_array
311
- products = create_list(:product, 2)
312
-
313
- assert_difference "ProductIndex.total_entries", 2 do
314
- ProductIndex.import products
315
- end
316
-
317
- assert_difference "ProductIndex.total_entries", -2 do
318
- ProductIndex.delete products
319
- end
320
- end
321
-
322
- def test_delete_scope
323
- products = create_list(:product, 2)
324
-
325
- assert_difference "ProductIndex.total_entries", 2 do
326
- ProductIndex.import products
327
- end
328
-
329
- assert_difference "ProductIndex.total_entries", -2 do
330
- ProductIndex.delete Product.where(id: Product.where(id: products.map(&:id)))
331
- end
332
- end
333
-
334
- def test_create_already_created
335
- products = create_list(:product, 2)
336
-
337
- assert_difference "ProductIndex.total_entries", 2 do
338
- ProductIndex.create products
339
- end
340
-
341
- assert_raises SearchFlip::Bulk::Error do
342
- ProductIndex.create products
343
- end
344
- end
345
-
346
- def test_create_with_param_options
347
- products = create_list(:product, 2)
348
-
349
- assert_difference "ProductIndex.total_entries", 2 do
350
- ProductIndex.create products
351
- end
352
-
353
- assert_no_difference "ProductIndex.total_entries" do
354
- ProductIndex.create products, ignore_errors: [409]
355
- end
356
-
357
- products = create_list(:product, 2)
358
-
359
- if ProductIndex.connection.version.to_i >= 5
360
- assert_difference "ProductIndex.total_entries", 2 do
361
- ProductIndex.create products, {}, routing: "r1"
362
- end
363
-
364
- actual = ProductIndex.get(products.first.id, routing: "r1")["_routing"]
365
-
366
- assert_equal "r1", actual
367
- else
368
- assert_difference "ProductIndex.total_entries", 2 do
369
- ProductIndex.create products, {}, version: 2, version_type: "external"
370
- end
371
-
372
- actual = products.map { |product| ProductIndex.get(product.id)["_version"] }
373
-
374
- assert_equal [2, 2], actual
375
- end
376
- end
377
-
378
- def test_create_with_class_options
379
- products = create_list(:product, 2)
380
-
381
- if ProductIndex.connection.version.to_i >= 5
382
- ProductIndex.stubs(:index_options).returns(routing: "r1")
383
-
384
- assert_difference "ProductIndex.total_entries", 2 do
385
- ProductIndex.create products
386
- end
387
-
388
- actual = products.map { |product| ProductIndex.get(product.id, routing: "r1")["_routing"] }
389
-
390
- assert_equal ["r1", "r1"], actual
391
- else
392
- ProductIndex.stubs(:index_options).returns(version: 2, version_type: "external")
393
-
394
- assert_difference "ProductIndex.total_entries", 2 do
395
- ProductIndex.create products
396
- end
397
-
398
- actual = products.map { |product| ProductIndex.get(product.id)["_version"] }
399
-
400
- assert_equal [2, 2], actual
401
- end
402
- end
403
-
404
- def test_get
405
- # Already tested
406
- end
407
-
408
- def test_scope
409
- temp_product_index = Class.new(ProductIndex)
410
-
411
- temp_product_index.scope(:with_title) { |title| where(title: title) }
412
-
413
- expected = create(:product, title: "expected")
414
- rejected = create(:product, title: "rejected")
415
-
416
- temp_product_index.import [expected, rejected]
417
-
418
- results = temp_product_index.with_title("expected").records
419
-
420
- assert_includes results, expected
421
- refute_includes results, rejected
422
- end
423
-
424
- def test_bulk
425
- assert_difference "ProductIndex.total_entries", 2 do
426
- ProductIndex.bulk do |indexer|
427
- indexer.index 1, id: 1
428
- indexer.index 2, id: 2
429
- end
430
- end
431
-
432
- assert_no_difference "ProductIndex.total_entries" do
433
- assert_raises "SearchFlip::Bulk::Error" do
434
- ProductIndex.bulk do |indexer|
435
- indexer.index 1, { id: 1 }, version: 1, version_type: "external"
436
- indexer.index 2, { id: 2 }, version: 1, version_type: "external"
437
- end
438
- end
439
-
440
- ProductIndex.bulk ignore_errors: [409] do |indexer|
441
- indexer.index 1, { id: 1 }, version: 1, version_type: "external"
442
- indexer.index 2, { id: 2 }, version: 1, version_type: "external"
443
- end
444
- end
445
- end
446
-
447
- def test_connection
448
- assert_equal "http://127.0.0.1:9200", ProductIndex.connection.base_url
449
- end
450
- end
451
-
@@ -1,39 +0,0 @@
1
-
2
- require File.expand_path("../test_helper", __dir__)
3
-
4
- class SearchFlip::ModelTest < SearchFlip::TestCase
5
- class TestProduct < Product
6
- include SearchFlip::Model
7
-
8
- notifies_index(ProductIndex)
9
- end
10
-
11
- def test_save
12
- assert_equal 0, ProductIndex.total_count
13
-
14
- TestProduct.create!
15
-
16
- assert_equal 1, ProductIndex.total_count
17
- end
18
-
19
- def test_destroy
20
- test_product = TestProduct.create!
21
-
22
- assert_equal 1, ProductIndex.total_count
23
-
24
- test_product.destroy
25
-
26
- assert_equal 0, ProductIndex.total_count
27
- end
28
-
29
- def test_touch
30
- test_product = Timecop.freeze(Time.parse("2016-01-01 12:00:00")) { TestProduct.create! }
31
-
32
- updated_at = ProductIndex.match_all.results.first.updated_at
33
-
34
- Timecop.freeze(Time.parse("2017-01-01 12:00:00")) { test_product.touch }
35
-
36
- refute_equal updated_at, ProductIndex.match_all.results.first.updated_at
37
- end
38
- end
39
-
@@ -1,137 +0,0 @@
1
-
2
- require File.expand_path("../test_helper", __dir__)
3
-
4
- class SearchFlip::ResponseTest < SearchFlip::TestCase
5
- def test_total_entries
6
- ProductIndex.import create_list(:product, 3)
7
-
8
- assert_equal 3, ProductIndex.total_entries
9
- end
10
-
11
- def test_curent_page
12
- assert_equal 1, ProductIndex.match_all.current_page
13
-
14
- ProductIndex.import create_list(:product, 3)
15
-
16
- assert_equal 1, ProductIndex.paginate(page: 1, per_page: 2).current_page
17
- assert_equal 2, ProductIndex.paginate(page: 2, per_page: 2).current_page
18
- assert_equal 3, ProductIndex.paginate(page: 3, per_page: 2).current_page
19
- end
20
-
21
- def test_total_pages
22
- assert_equal 1, ProductIndex.paginate(page: 1, per_page: 2).total_pages
23
-
24
- ProductIndex.import create_list(:product, 3)
25
-
26
- assert_equal 2, ProductIndex.paginate(page: 1, per_page: 2).total_pages
27
- end
28
-
29
- def test_previous_page
30
- ProductIndex.import create_list(:product, 3)
31
-
32
- assert_nil ProductIndex.paginate(page: 1, per_page: 2).previous_page
33
- assert_equal 1, ProductIndex.paginate(page: 2, per_page: 2).previous_page
34
- assert_equal 2, ProductIndex.paginate(page: 3, per_page: 2).previous_page
35
- end
36
-
37
- def test_next_page
38
- ProductIndex.import create_list(:product, 3)
39
-
40
- assert_equal 2, ProductIndex.paginate(page: 1, per_page: 2).next_page
41
- assert_nil ProductIndex.paginate(page: 2, per_page: 2).next_page
42
- end
43
-
44
- def test_first_page?
45
- ProductIndex.import create(:product)
46
-
47
- assert ProductIndex.paginate(page: 1).first_page?
48
- refute ProductIndex.paginate(page: 2).first_page?
49
- end
50
-
51
- def test_last_page?
52
- ProductIndex.import create_list(:product, 31)
53
-
54
- assert ProductIndex.paginate(page: 2).last_page?
55
- refute ProductIndex.paginate(page: 1).last_page?
56
- end
57
-
58
- def test_out_of_range?
59
- ProductIndex.import create(:product)
60
-
61
- assert ProductIndex.paginate(page: 2).out_of_range?
62
- refute ProductIndex.paginate(page: 1).out_of_range?
63
- end
64
-
65
- def test_results
66
- products = create_list(:product, 3)
67
-
68
- ProductIndex.import products
69
-
70
- assert_equal products.map(&:id).to_set, ProductIndex.match_all.results.map(&:id).to_set
71
- end
72
-
73
- def test_hits
74
- ProductIndex.import create_list(:product, 3)
75
-
76
- response = ProductIndex.match_all.response
77
-
78
- assert_present response.hits
79
- assert_equal response.raw_response["hits"], response.hits
80
- end
81
-
82
- def test_scroll_id
83
- ProductIndex.import create_list(:product, 3)
84
-
85
- response = ProductIndex.scroll.response
86
-
87
- assert_present response.scroll_id
88
- assert_equal response.raw_response["_scroll_id"], response.scroll_id
89
- end
90
-
91
- def test_records
92
- products = create_list(:product, 3)
93
-
94
- ProductIndex.import products
95
-
96
- assert_equal products.to_set, ProductIndex.match_all.records.to_set
97
- end
98
-
99
- def test_ids
100
- products = create_list(:product, 3)
101
-
102
- ProductIndex.import products
103
-
104
- response = ProductIndex.match_all.response
105
-
106
- assert_equal products.map(&:id).map(&:to_s).to_set, response.ids.to_set
107
- assert_equal response.raw_response["hits"]["hits"].map { |hit| hit["_id"] }, response.ids
108
- end
109
-
110
- def test_took
111
- ProductIndex.import create_list(:product, 3)
112
-
113
- response = ProductIndex.match_all.response
114
-
115
- assert_present response.took
116
- assert_equal response.raw_response["took"], response.took
117
- end
118
-
119
- def test_aggregations
120
- product1 = create(:product, price: 10, category: "category1")
121
- product2 = create(:product, price: 20, category: "category2")
122
- product3 = create(:product, price: 30, category: "category1")
123
-
124
- ProductIndex.import [product1, product2, product3]
125
-
126
- query = ProductIndex.aggregate(:category) do |aggregation|
127
- aggregation.aggregate(price_sum: { sum: { field: "price" } })
128
- end
129
-
130
- assert_equal Hash["category1" => 2, "category2" => 1],
131
- query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
132
-
133
- assert_equal 40, query.aggregations(:category)["category1"].price_sum.value
134
- assert_equal 20, query.aggregations(:category)["category2"].price_sum.value
135
- end
136
- end
137
-
@@ -1,30 +0,0 @@
1
-
2
- require File.expand_path("../test_helper", __dir__)
3
- require "search_flip/to_json"
4
-
5
- class SearchFlip::ToJsonTest < SearchFlip::TestCase
6
- def test_time
7
- Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
8
- assert_equal "\"2018-01-01T12:00:00.000000Z\"", Time.now.utc.to_json
9
- end
10
- end
11
-
12
- def test_date
13
- Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
14
- assert_equal "\"2018-01-01\"", Date.today.to_json
15
- end
16
- end
17
-
18
- def test_date_time
19
- Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
20
- assert_equal "\"2018-01-01T12:00:00.000000Z\"", Time.now.utc.to_json
21
- end
22
- end
23
-
24
- def test_time_with_zone
25
- Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
26
- assert_equal "\"2018-01-01T12:00:00.000000Z\"", Time.find_zone("UTC").now.to_json
27
- end
28
- end
29
- end
30
-