elastic_record 4.1.8 → 5.1.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.
Files changed (72) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +15 -8
  3. data/Gemfile +1 -1
  4. data/README.md +29 -21
  5. data/elastic_record.gemspec +1 -1
  6. data/lib/elastic_record.rb +25 -13
  7. data/lib/elastic_record/aggregation_response/aggregation.rb +19 -0
  8. data/lib/elastic_record/aggregation_response/bucket.rb +24 -0
  9. data/lib/elastic_record/aggregation_response/builder.rb +55 -0
  10. data/lib/elastic_record/aggregation_response/has_aggregations.rb +9 -0
  11. data/lib/elastic_record/aggregation_response/multi_bucket_aggregation.rb +9 -0
  12. data/lib/elastic_record/aggregation_response/multi_value_aggregation.rb +6 -0
  13. data/lib/elastic_record/aggregation_response/single_bucket_aggregation.rb +8 -0
  14. data/lib/elastic_record/aggregation_response/single_value_aggregation.rb +7 -0
  15. data/lib/elastic_record/as_document.rb +33 -16
  16. data/lib/elastic_record/callbacks.rb +1 -1
  17. data/lib/elastic_record/config.rb +3 -0
  18. data/lib/elastic_record/connection.rb +4 -4
  19. data/lib/elastic_record/errors.rb +7 -1
  20. data/lib/elastic_record/index.rb +6 -8
  21. data/lib/elastic_record/index/analyze.rb +10 -0
  22. data/lib/elastic_record/index/deferred.rb +2 -2
  23. data/lib/elastic_record/index/documents.rb +42 -29
  24. data/lib/elastic_record/index/manage.rb +6 -8
  25. data/lib/elastic_record/index/mapping.rb +16 -8
  26. data/lib/elastic_record/index/mapping_type.rb +16 -0
  27. data/lib/elastic_record/index/mapping_type_test.rb +18 -0
  28. data/lib/elastic_record/index/settings.rb +6 -17
  29. data/lib/elastic_record/model.rb +2 -14
  30. data/lib/elastic_record/percolator_model.rb +11 -19
  31. data/lib/elastic_record/relation.rb +9 -30
  32. data/lib/elastic_record/relation/batches.rb +5 -3
  33. data/lib/elastic_record/relation/delegation.rb +8 -4
  34. data/lib/elastic_record/relation/finder_methods.rb +8 -0
  35. data/lib/elastic_record/relation/hits.rb +34 -0
  36. data/lib/elastic_record/relation/search_methods.rb +17 -22
  37. data/lib/elastic_record/relation/value_methods.rb +1 -1
  38. data/test/dummy/app/models/project.rb +16 -4
  39. data/test/dummy/app/models/warehouse.rb +5 -16
  40. data/test/dummy/app/models/widget.rb +23 -12
  41. data/test/dummy/app/models/widget_query.rb +1 -0
  42. data/test/dummy/db/migrate/20151211225259_create_warehouses.rb +7 -0
  43. data/test/dummy/db/migrate/20180215140125_create_widgets.rb +11 -0
  44. data/test/dummy/db/schema.rb +10 -3
  45. data/test/elastic_record/aggregation_response/bucket_test.rb +8 -0
  46. data/test/elastic_record/aggregation_response/multi_bucket_aggregation_test.rb +33 -0
  47. data/test/elastic_record/aggregation_response/single_bucket_aggregation_test.rb +15 -0
  48. data/test/elastic_record/as_document_test.rb +55 -29
  49. data/test/elastic_record/callbacks_test.rb +7 -11
  50. data/test/elastic_record/connection_test.rb +3 -16
  51. data/test/elastic_record/index/documents_test.rb +56 -11
  52. data/test/elastic_record/index/manage_test.rb +0 -7
  53. data/test/elastic_record/index/mapping_test.rb +18 -5
  54. data/test/elastic_record/index/settings_test.rb +1 -7
  55. data/test/elastic_record/index_test.rb +0 -4
  56. data/test/elastic_record/integration/active_record_test.rb +6 -9
  57. data/test/elastic_record/model_test.rb +5 -2
  58. data/test/elastic_record/percolator_model_test.rb +25 -11
  59. data/test/elastic_record/relation/batches_test.rb +29 -47
  60. data/test/elastic_record/relation/delegation_test.rb +1 -1
  61. data/test/elastic_record/relation/finder_methods_test.rb +17 -31
  62. data/test/elastic_record/relation/hits_test.rb +49 -0
  63. data/test/elastic_record/relation/search_methods_test.rb +20 -16
  64. data/test/elastic_record/relation_test.rb +19 -50
  65. data/test/helper.rb +7 -3
  66. metadata +20 -9
  67. data/lib/elastic_record/doctype.rb +0 -43
  68. data/lib/elastic_record/json.rb +0 -29
  69. data/lib/elastic_record/name_cache.rb +0 -23
  70. data/test/dummy/db/migrate/20151211225259_create_projects.rb +0 -7
  71. data/test/elastic_record/doctype_test.rb +0 -45
  72. data/test/elastic_record/name_cache_test.rb +0 -16
@@ -7,10 +7,6 @@ class ElasticRecord::IndexTest < MiniTest::Test
7
7
  refute_equal copied.settings.object_id, index.settings.object_id
8
8
  end
9
9
 
10
- def test_doctypes
11
- assert_equal [Widget.doctype], index.doctypes
12
- end
13
-
14
10
  def test_alias_name
15
11
  assert_equal 'widgets', index.alias_name
16
12
  end
@@ -2,20 +2,17 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::ActiveRecordTest < MiniTest::Test
4
4
  def test_ordering
5
- poo_product = Project.create! name: "Poo"
6
- bear_product = Project.create! name: "Bear"
7
- Project.elastic_index.refresh
5
+ poo_product = Warehouse.create! name: "Poo"
6
+ bear_product = Warehouse.create! name: "Bear"
8
7
 
9
- assert_equal [bear_product, poo_product], Project.elastic_relation.order(name: 'asc')
10
- assert_equal [poo_product, bear_product], Project.elastic_relation.order(name: 'desc')
8
+ assert_equal [bear_product, poo_product], Warehouse.elastic_relation.order(name: 'asc')
9
+ assert_equal [poo_product, bear_product], Warehouse.elastic_relation.order(name: 'desc')
11
10
  end
12
11
 
13
12
  def test_update_callback
14
- project = Project.create! name: "Ideas"
15
- Project.elastic_index.refresh
13
+ project = Warehouse.create! name: "Ideas"
16
14
  project.update! name: 'Terrible Stuff'
17
- Project.elastic_index.refresh
18
15
 
19
- assert_equal [project], Project.elastic_relation.filter(name: 'Terrible Stuff')
16
+ assert_equal [project], Warehouse.elastic_relation.filter(name: 'Terrible Stuff')
20
17
  end
21
18
  end
@@ -17,8 +17,11 @@ class ElasticRecord::ModelTest < MiniTest::Test
17
17
  assert_equal Widget, index.model
18
18
  end
19
19
 
20
- def test_elastic_index_inheritence
20
+ def test_elastic_index_inheritance
21
21
  refute_equal Widget.elastic_index.object_id, InheritedModel.elastic_index.object_id
22
- refute_equal Widget.doctype.object_id, InheritedModel.doctype.object_id
22
+ assert_equal Widget.elastic_index.mapping_type, InheritedModel.elastic_index.mapping_type
23
+ assert_equal 'widgets', InheritedModel.elastic_index.alias_name
24
+ assert_equal InheritedModel, InheritedModel.elastic_index.model
25
+ assert_equal 'widget', InheritedModel.elastic_index.mapping_type
23
26
  end
24
27
  end
@@ -2,15 +2,35 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::PercolatorModelTest < MiniTest::Test
4
4
  def test_elastic_index
5
- assert_equal [WidgetQuery.doctype, Widget.doctype], index.doctypes
5
+ index = WidgetQuery.elastic_index
6
+
7
+ expected_mapping = {
8
+ "widget"=> {
9
+ "properties"=> {
10
+ "color"=> {"type" => "keyword" },
11
+ "name"=> {
12
+ "type" => "text",
13
+ "fields" => {
14
+ "raw" => { "type" => "keyword" }
15
+ }
16
+ },
17
+ "price" => { "type" => "long" },
18
+ "query" => { "type" => "percolator" },
19
+ "warehouse_id" => { "type" => "keyword" },
20
+ "widget_part" => {
21
+ "properties" => {
22
+ "name" => { "type" => "keyword" }
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+ assert_equal expected_mapping, index.get_mapping
6
30
  refute index.partial_updates
7
31
  assert_equal({}, index.settings)
8
32
  end
9
33
 
10
- def test_doctype
11
- assert_equal ElasticRecord::Doctype.percolator_doctype, WidgetQuery.doctype
12
- end
13
-
14
34
  def test_as_search_document
15
35
  query = WidgetQuery.new(name: 'foo', color: 'red')
16
36
 
@@ -45,10 +65,4 @@ class ElasticRecord::PercolatorModelTest < MiniTest::Test
45
65
 
46
66
  assert_equal [query], WidgetQuery.percolate(should_hit)
47
67
  end
48
-
49
- private
50
-
51
- def index
52
- @index ||= WidgetQuery.elastic_index
53
- end
54
68
  end
@@ -3,79 +3,61 @@ require 'helper'
3
3
  class ElasticRecord::Relation::BatchesTest < MiniTest::Test
4
4
  def setup
5
5
  super
6
- create_widgets
6
+ @red_widget = Widget.create!(color: 'red')
7
+ @blue_widget = Widget.create!(color: 'blue')
8
+ @green_widget = Widget.create!(color: 'green')
7
9
  end
8
10
 
9
- def test_find_each
10
- results = []
11
- Widget.elastic_relation.find_each do |widget|
12
- results << widget.id
13
- end
14
- assert_equal ['5', '10', '15'].to_set, results.to_set
15
- end
11
+ # def test_find_each
12
+ # results = []
13
+ # Widget.elastic_relation.find_each do |widget|
14
+ # results << widget
15
+ # end
16
+ # assert_equal [@red_widget, @blue_widget, @green_widget].to_set, results.to_set
17
+ # end
16
18
 
17
19
  def test_find_ids_in_batches
18
20
  results = []
19
21
  Widget.elastic_relation.find_ids_in_batches do |ids|
20
22
  results << ids
21
23
  end
22
- assert_equal [['5', '10', '15'].to_set], results.map(&:to_set)
24
+ assert_equal [[@red_widget.id.to_s, @blue_widget.id.to_s, @green_widget.id.to_s].to_set], results.map(&:to_set)
23
25
  end
24
26
 
25
- def test_find_ids_in_batches_with_order
27
+ def test_find_in_batches
26
28
  results = []
27
- Widget.elastic_relation.order(color: :asc).find_ids_in_batches do |ids|
28
- results << ids
29
+ Widget.elastic_relation.find_in_batches do |widgets|
30
+ results << widgets
29
31
  end
30
-
31
- assert_equal [['10', '15', '5']], results
32
+ assert_equal [[@red_widget, @blue_widget, @green_widget].to_set], results.map(&:to_set)
32
33
  end
33
34
 
34
- def test_find_ids_in_batches_with_size
35
+ def test_find_in_batches_with_order
35
36
  results = []
36
- Widget.elastic_relation.find_ids_in_batches(batch_size: 2) do |ids|
37
- results << ids
37
+ Widget.elastic_relation.order(color: :asc).find_in_batches do |records|
38
+ results << records
38
39
  end
39
40
 
40
- assert_equal 2, results.size
41
- assert_equal 2, results[0].size
42
- assert_equal 1, results[1].size
43
- assert_equal ['5', '10', '15'].to_set, results.flatten.to_set
41
+ assert_equal [[@blue_widget, @green_widget, @red_widget]], results
44
42
  end
45
43
 
46
- def test_find_in_batches
44
+ def test_find_in_batches_with_size
47
45
  results = []
48
- Widget.elastic_relation.find_in_batches do |widgets|
49
- results << widgets.map(&:id)
46
+ Widget.elastic_relation.find_in_batches(batch_size: 2) do |records|
47
+ results << records
50
48
  end
51
- assert_equal [['5', '10', '15'].to_set], results.map(&:to_set)
49
+
50
+ assert_equal 2, results.size
51
+ assert_equal 2, results[0].size
52
+ assert_equal 1, results[1].size
53
+ assert_equal [@red_widget, @blue_widget, @green_widget].to_set, results.flatten.to_set
52
54
  end
53
55
 
54
56
  def test_find_in_batches_with_scope
55
57
  results = []
56
58
  Widget.elastic_relation.filter(color: %w(red blue)).find_in_batches do |widgets|
57
- results << widgets.map(&:id)
58
- end
59
- assert_equal [['5', '10'].to_set], results.map(&:to_set)
60
- end
61
-
62
- def test_find_with_batch_size
63
- results = []
64
- Widget.elastic_relation.find_ids_in_batches(batch_size: 1) do |ids|
65
- results << ids
59
+ results << widgets
66
60
  end
67
-
68
- assert_equal 3, results.size
69
- results.each { |r| assert_equal 1, r.size }
70
- assert_equal ['5', '10', '15'].to_set, results.flatten.to_set
61
+ assert_equal [[@red_widget, @blue_widget].to_set], results.map(&:to_set)
71
62
  end
72
-
73
- private
74
- def create_widgets
75
- Widget.elastic_index.bulk_add [
76
- Widget.new(id: 5, color: 'red'),
77
- Widget.new(id: 10, color: 'blue'),
78
- Widget.new(id: 15, color: 'green')
79
- ]
80
- end
81
63
  end
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::Relation::DelegationTest < MiniTest::Test
4
4
  def test_delegate_to_array
5
- Widget.elastic_index.index_document('5', color: 'red')
5
+ Widget.create! color: 'red'
6
6
 
7
7
  records = []
8
8
  Widget.elastic_relation.each do |record|
@@ -3,23 +3,23 @@ require 'helper'
3
3
  class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Test
4
4
  def setup
5
5
  super
6
- create_widgets
6
+ @red_widget = Widget.create(color: 'red')
7
+ @blue_widget = Widget.create(color: 'blue')
7
8
  end
8
9
 
9
10
  def test_find
10
- refute_nil Widget.elastic_relation.find('05')
11
- refute_nil Widget.elastic_relation.filter('color' => 'red').find('05')
11
+ refute_nil Widget.elastic_relation.find(@red_widget.id)
12
+ refute_nil Widget.elastic_relation.filter('color' => 'red').find(@red_widget.id)
12
13
 
13
14
  assert_raises ActiveRecord::RecordNotFound do
14
- Widget.elastic_relation.filter('color' => 'blue').find('05')
15
+ Widget.elastic_relation.filter('color' => 'blue').find(@red_widget.id)
15
16
  end
16
17
  end
17
18
 
18
19
  def test_find_passed_an_array
19
- assert_equal 2, Widget.elastic_relation.find(['05', '10']).size
20
- assert_equal 2, Widget.elastic_relation.filter('color' => ['red', 'blue']).find(['05', '10']).size
21
- assert_equal 0, Widget.elastic_relation.find(['15', '20']).size
22
- assert_equal 0, Widget.elastic_relation.filter('color' => ['purple', 'gold']).find(['05', '10']).size
20
+ assert_equal 2, Widget.elastic_relation.find([@red_widget.id, @blue_widget.id]).size
21
+ assert_equal 2, Widget.elastic_relation.filter('color' => ['red', 'blue']).find([@red_widget.id, @blue_widget.id]).size
22
+ assert_equal 0, Widget.elastic_relation.filter('color' => ['purple', 'gold']).find([@red_widget.id, @blue_widget.id]).size
23
23
  end
24
24
 
25
25
  def test_find_passed_an_empty_args
@@ -29,23 +29,23 @@ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Test
29
29
  end
30
30
 
31
31
  def test_first
32
- assert_equal '10', Widget.elastic_relation.order('color').first.id
33
- assert_equal '05', Widget.elastic_relation.order('color').filter('color' => 'red').first.id
34
- assert_equal '10', Widget.elastic_relation.order('color').filter('color' => 'blue').first.id
32
+ assert_equal 'blue', Widget.elastic_relation.order('color').first.color
33
+ assert_equal 'red', Widget.elastic_relation.order('color').filter('color' => 'red').first.color
34
+ assert_equal 'blue', Widget.elastic_relation.order('color').filter('color' => 'blue').first.color
35
35
  assert_nil Widget.elastic_relation.filter('color' => 'green').first
36
36
  end
37
37
 
38
38
  def test_first_with_bang
39
- assert_equal '10', Widget.elastic_relation.order('color').first!.id
39
+ assert_equal 'blue', Widget.elastic_relation.order('color').first!.color
40
40
  assert_raises ActiveRecord::RecordNotFound do
41
41
  Widget.elastic_relation.filter('color' => 'green').first!
42
42
  end
43
43
  end
44
44
 
45
45
  def test_last
46
- assert_equal '05', Widget.elastic_relation.order('color').last.id
47
- assert_equal '05', Widget.elastic_relation.order('color' => 'asc').last.id
48
- assert_equal '10', Widget.elastic_relation.order('color' => 'desc').last.id
46
+ assert_equal 'red', Widget.elastic_relation.order('color').last.color
47
+ assert_equal 'red', Widget.elastic_relation.order('color' => 'asc').last.color
48
+ assert_equal 'blue', Widget.elastic_relation.order('color' => 'desc').last.color
49
49
  end
50
50
 
51
51
  def test_all
@@ -54,24 +54,10 @@ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Test
54
54
  end
55
55
 
56
56
  def test_find_by
57
- Widget.elastic_index.bulk_add [
58
- Widget.new(color: 'red', id: '05'),
59
- Widget.new(color: 'blue', id: '10'),
60
- ]
61
-
62
- assert_equal '05', Widget.elastic_relation.find_by(color: 'red').id
63
- assert_equal '05', Widget.elastic_relation.find_by!(color: 'red').id
57
+ assert_equal 'red', Widget.elastic_relation.find_by(color: 'red').color
58
+ assert_equal 'red', Widget.elastic_relation.find_by!(color: 'red').color
64
59
  assert_raises ActiveRecord::RecordNotFound do
65
60
  Widget.elastic_relation.find_by!(color: 'green')
66
61
  end
67
62
  end
68
-
69
- private
70
-
71
- def create_widgets
72
- Widget.elastic_index.bulk_add [
73
- Widget.new(color: 'red', id: '05'),
74
- Widget.new(color: 'blue', id: '10'),
75
- ]
76
- end
77
63
  end
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ class ElasticRecord::Relation::HitsTest < MiniTest::Test
4
+ def test_to_ids
5
+ red_widget = Widget.create(color: 'red')
6
+ blue_widget = Widget.create(color: 'red')
7
+
8
+ assert_equal [red_widget.id.to_s, blue_widget.id.to_s].to_set, Widget.elastic_relation.to_ids.to_set
9
+ end
10
+
11
+ def test_to_a
12
+ red_widget = Widget.create(color: 'red')
13
+ blue_widget = Widget.create(color: 'red')
14
+
15
+ array = Widget.elastic_relation.to_a
16
+
17
+ assert_equal 2, array.size
18
+ assert array.first.is_a?(Widget)
19
+ end
20
+
21
+ def test_to_a_from_source
22
+ warehouses = [Project.new(name: 'Latte'), Project.new(name: 'Americano')]
23
+ result = Project.elastic_index.bulk_add(warehouses)
24
+
25
+ array = Project.elastic_relation.to_a
26
+
27
+ assert_equal 2, array.size
28
+ assert array.first.is_a?(Project)
29
+ names = array.map(&:name)
30
+ assert_includes names, 'Latte'
31
+ assert_includes names, 'Americano'
32
+ end
33
+
34
+ def test_search_hits
35
+ coffees = [Project.new(name: 'Latte'), Project.new(name: 'Americano')]
36
+ Project.elastic_index.bulk_add(coffees)
37
+
38
+ array = Project.elastic_relation.search_hits
39
+ assert_equal %w(Latte Americano).to_set, array.map { |hit| hit["_source"]["name"] }.to_set
40
+ end
41
+
42
+ def test_search_results
43
+ coffees = [Project.new(name: 'Latte'), Project.new(name: 'Americano')]
44
+ Project.elastic_index.bulk_add(coffees)
45
+
46
+ results = Project.elastic_relation.search_results
47
+ %w(took timed_out _shards hits).each { |key| assert results.key?(key) }
48
+ end
49
+ end
@@ -198,29 +198,33 @@ class ElasticRecord::Relation::SearchMethodsTest < MiniTest::Test
198
198
  end
199
199
 
200
200
  def test_select
201
- selectable_klass = Widget.anon do
202
- def self.select(values)
203
- @latest_select = values
204
- self
205
- end
206
- end
201
+ scope = relation.select 'foo'
207
202
 
208
- relation = selectable_klass.elastic_relation.select 'foo'
209
- relation.to_a
210
-
211
- assert_equal ['foo'], selectable_klass.instance_variable_get('@latest_select')
203
+ assert_equal ['foo'], scope.select_values
212
204
  end
213
205
 
214
206
  def test_select_with_block
215
- Widget.elastic_index.bulk_add [
216
- Widget.new(id: 5, color: 'red'),
217
- Widget.new(id: 10, color: 'blue')
218
- ]
207
+ red_widget = Widget.create(color: 'red')
208
+ blue_widget = Widget.create(color: 'blue')
219
209
 
220
- records = relation.select { |record| record.id == '10' }
210
+ records = relation.select { |record| record.id == blue_widget.id }
221
211
 
222
212
  assert_equal 1, records.size
223
- assert_equal '10', records.first.id
213
+ assert_equal blue_widget, records.first
214
+ end
215
+
216
+ def test_includes
217
+ warehouse = Warehouse.create! name: 'Boeing'
218
+ widget = Widget.create! name: '747', color: 'red', warehouse: warehouse
219
+ widget = Widget.create! name: '747', color: 'green', warehouse: warehouse
220
+
221
+ widgets = relation.filter(color: 'red').includes(:warehouse)
222
+ assert_equal 1, widgets.count
223
+ assert widgets.first.association(:warehouse).loaded?
224
+
225
+ widgets = relation.filter(color: 'red')
226
+ assert_equal 1, widgets.count
227
+ refute widgets.first.association(:warehouse).loaded?
224
228
  end
225
229
 
226
230
  def test_extending_with_block
@@ -3,73 +3,47 @@ require 'helper'
3
3
  class ElasticRecord::RelationTest < MiniTest::Test
4
4
  def test_count
5
5
  original_count = Widget.elastic_relation.count
6
- create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
6
+ Widget.create(color: 'red')
7
+ Widget.create(color: 'blue')
7
8
 
8
9
  assert_equal 2, Widget.elastic_relation.count - original_count
9
10
  end
10
11
 
11
12
  def test_aggregations
12
- create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
13
+ Widget.create(color: 'red', price: 5)
14
+ Widget.create(color: 'blue', price: 10)
13
15
 
14
16
  aggregations = Widget.elastic_relation.aggregate('popular_colors' => {'terms' => {'field' => 'color'}}).aggregations
15
17
 
16
- assert_equal 2, aggregations['popular_colors']['buckets'].size
17
- assert_equal %w(red blue).to_set, aggregations['popular_colors']['buckets'].map { |bucket| bucket['key'] }.to_set
18
+ assert_equal 2, aggregations['popular_colors'].buckets.size
19
+ assert_equal %w(red blue).to_set, aggregations['popular_colors'].buckets.map(&:key).to_set
20
+
21
+ aggregations = Widget.elastic_relation.aggregate('avg_price' => {'avg' => {'field' => 'price'}}).aggregations
22
+ assert_equal 7.5, aggregations['avg_price'].value
18
23
  end
19
24
 
20
25
  def test_explain
21
- create_widgets [Widget.new(id: 10, color: 'blue')]
26
+ Widget.create(color: 'blue')
22
27
 
23
28
  # explain = Widget.elastic_relation.filter(color: 'blue').explain('10')
24
29
  end
25
30
 
26
- def test_to_hits
27
- # assert Widget.elastic_relation.search_results.is_a?(ElasticSearch::Api::Hits)
28
- end
29
-
30
- def test_to_ids
31
- create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
32
-
33
- assert_equal ['5', '10'].to_set, Widget.elastic_relation.to_ids.to_set
34
- end
35
-
36
- def test_to_a
37
- create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
38
-
39
- array = Widget.elastic_relation.to_a
40
-
41
- assert_equal 2, array.size
42
- assert array.first.is_a?(Widget)
43
- end
44
-
45
- def test_to_a_from_source
46
- warehouses = [Warehouse.new(name: 'Amazon'), Warehouse.new(name: 'Walmart')]
47
- result = Warehouse.elastic_index.bulk_add(warehouses)
48
-
49
- array = Warehouse.elastic_relation.to_a
50
-
51
- assert_equal 2, array.size
52
- assert array.first.is_a?(Warehouse)
53
- names = array.map(&:name)
54
- assert_includes names, 'Amazon'
55
- assert_includes names, 'Walmart'
56
- end
57
-
58
31
  def test_delete_all
59
- project_red = Project.create! name: 'Red'
60
- project_blue = Project.create! name: 'Blue'
32
+ project_red = Warehouse.create! name: 'Red'
33
+ project_blue = Warehouse.create! name: 'Blue'
61
34
 
62
- Project.elastic_relation.filter(name: 'Red').delete_all
35
+ Warehouse.elastic_relation.filter(name: 'Red').delete_all
63
36
 
64
- assert_nil Project.find_by(id: project_red.id)
65
- assert_equal 0, Project.elastic_relation.filter(name: 'Red').count
37
+ assert_nil Warehouse.find_by(id: project_red.id)
38
+ assert_equal 0, Warehouse.elastic_relation.filter(name: 'Red').count
66
39
 
67
- refute_nil Project.find_by(id: project_blue.id)
68
- assert_equal 1, Project.elastic_relation.filter(name: 'Blue').count
40
+ refute_nil Warehouse.find_by(id: project_blue.id)
41
+ assert_equal 1, Warehouse.elastic_relation.filter(name: 'Blue').count
69
42
  end
70
43
 
71
44
  def test_equal
72
- create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
45
+ Widget.create(color: 'red')
46
+ Widget.create(color: 'blue')
73
47
 
74
48
  assert_equal Widget.elastic_relation.filter(color: 'red'), Widget.elastic_relation.filter(color: 'red')
75
49
  refute_equal Widget.elastic_relation.filter(color: 'red'), Widget.elastic_relation.filter(color: 'blue')
@@ -80,9 +54,4 @@ class ElasticRecord::RelationTest < MiniTest::Test
80
54
  def test_inspect
81
55
  assert_equal [].inspect, Widget.elastic_relation.filter(color: 'magenta').inspect
82
56
  end
83
-
84
- private
85
- def create_widgets(widgets)
86
- Widget.elastic_index.bulk_add(widgets)
87
- end
88
57
  end