elasticsearch-model 0.1.9 → 7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -2
  3. data/Gemfile +22 -0
  4. data/LICENSE.txt +199 -10
  5. data/README.md +96 -43
  6. data/Rakefile +49 -35
  7. data/elasticsearch-model.gemspec +53 -41
  8. data/examples/activerecord_article.rb +18 -1
  9. data/examples/activerecord_associations.rb +86 -17
  10. data/examples/activerecord_custom_analyzer.rb +152 -0
  11. data/examples/activerecord_mapping_completion.rb +33 -16
  12. data/examples/activerecord_mapping_edge_ngram.rb +118 -0
  13. data/examples/couchbase_article.rb +17 -0
  14. data/examples/datamapper_article.rb +28 -1
  15. data/examples/mongoid_article.rb +17 -0
  16. data/examples/ohm_article.rb +17 -0
  17. data/examples/riak_article.rb +17 -0
  18. data/gemfiles/3.0.gemfile +23 -1
  19. data/gemfiles/4.0.gemfile +25 -1
  20. data/gemfiles/5.0.gemfile +35 -0
  21. data/gemfiles/6.0.gemfile +36 -0
  22. data/lib/elasticsearch/model/adapter.rb +17 -0
  23. data/lib/elasticsearch/model/adapters/active_record.rb +31 -27
  24. data/lib/elasticsearch/model/adapters/default.rb +17 -0
  25. data/lib/elasticsearch/model/adapters/mongoid.rb +27 -3
  26. data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
  27. data/lib/elasticsearch/model/callbacks.rb +17 -0
  28. data/lib/elasticsearch/model/client.rb +17 -0
  29. data/lib/elasticsearch/model/ext/active_record.rb +17 -0
  30. data/lib/elasticsearch/model/hash_wrapper.rb +32 -0
  31. data/lib/elasticsearch/model/importing.rb +61 -17
  32. data/lib/elasticsearch/model/indexing.rb +87 -49
  33. data/lib/elasticsearch/model/multimodel.rb +17 -0
  34. data/lib/elasticsearch/model/naming.rb +33 -2
  35. data/lib/elasticsearch/model/proxy.rb +61 -18
  36. data/lib/elasticsearch/model/response/aggregations.rb +55 -0
  37. data/lib/elasticsearch/model/response/base.rb +25 -3
  38. data/lib/elasticsearch/model/response/pagination/kaminari.rb +126 -0
  39. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +112 -0
  40. data/lib/elasticsearch/model/response/pagination.rb +19 -192
  41. data/lib/elasticsearch/model/response/records.rb +17 -1
  42. data/lib/elasticsearch/model/response/result.rb +19 -2
  43. data/lib/elasticsearch/model/response/results.rb +17 -0
  44. data/lib/elasticsearch/model/response/suggestions.rb +20 -1
  45. data/lib/elasticsearch/model/response.rb +28 -10
  46. data/lib/elasticsearch/model/searching.rb +17 -0
  47. data/lib/elasticsearch/model/serializing.rb +17 -0
  48. data/lib/elasticsearch/model/version.rb +18 -1
  49. data/lib/elasticsearch/model.rb +36 -39
  50. data/spec/elasticsearch/model/adapter_spec.rb +136 -0
  51. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +351 -0
  52. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +395 -0
  53. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +35 -0
  54. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +193 -0
  55. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +127 -0
  56. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +55 -0
  57. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +332 -0
  58. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +92 -0
  59. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +78 -0
  60. data/spec/elasticsearch/model/adapters/active_record_spec.rb +224 -0
  61. data/spec/elasticsearch/model/adapters/default_spec.rb +58 -0
  62. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +284 -0
  63. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +83 -0
  64. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +252 -0
  65. data/spec/elasticsearch/model/adapters/multiple_spec.rb +142 -0
  66. data/spec/elasticsearch/model/callbacks_spec.rb +50 -0
  67. data/spec/elasticsearch/model/client_spec.rb +83 -0
  68. data/spec/elasticsearch/model/hash_wrapper_spec.rb +29 -0
  69. data/spec/elasticsearch/model/importing_spec.rb +243 -0
  70. data/spec/elasticsearch/model/indexing_spec.rb +1014 -0
  71. data/spec/elasticsearch/model/module_spec.rb +94 -0
  72. data/spec/elasticsearch/model/multimodel_spec.rb +72 -0
  73. data/spec/elasticsearch/model/naming_spec.rb +203 -0
  74. data/spec/elasticsearch/model/proxy_spec.rb +124 -0
  75. data/spec/elasticsearch/model/response/aggregations_spec.rb +83 -0
  76. data/spec/elasticsearch/model/response/base_spec.rb +107 -0
  77. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +472 -0
  78. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +279 -0
  79. data/spec/elasticsearch/model/response/records_spec.rb +135 -0
  80. data/spec/elasticsearch/model/response/response_spec.rb +148 -0
  81. data/spec/elasticsearch/model/response/result_spec.rb +139 -0
  82. data/spec/elasticsearch/model/response/results_spec.rb +73 -0
  83. data/spec/elasticsearch/model/searching_search_request_spec.rb +129 -0
  84. data/spec/elasticsearch/model/searching_spec.rb +66 -0
  85. data/spec/elasticsearch/model/serializing_spec.rb +39 -0
  86. data/spec/spec_helper.rb +193 -0
  87. data/spec/support/app/answer.rb +50 -0
  88. data/spec/support/app/article.rb +39 -0
  89. data/spec/support/app/article_for_pagination.rb +29 -0
  90. data/spec/support/app/article_no_type.rb +37 -0
  91. data/spec/support/app/article_with_custom_serialization.rb +30 -0
  92. data/spec/support/app/article_with_dynamic_index_name.rb +32 -0
  93. data/spec/support/app/author.rb +26 -0
  94. data/spec/support/app/authorship.rb +21 -0
  95. data/spec/support/app/category.rb +20 -0
  96. data/spec/support/app/comment.rb +20 -0
  97. data/spec/support/app/episode.rb +28 -0
  98. data/spec/support/app/image.rb +36 -0
  99. data/spec/support/app/import_article.rb +29 -0
  100. data/spec/support/app/mongoid_article.rb +38 -0
  101. data/spec/support/app/namespaced_book.rb +27 -0
  102. data/spec/support/app/parent_and_child_searchable.rb +41 -0
  103. data/spec/support/app/post.rb +31 -0
  104. data/spec/support/app/question.rb +44 -0
  105. data/spec/support/app/searchable.rb +65 -0
  106. data/spec/support/app/series.rb +28 -0
  107. data/spec/support/app.rb +46 -0
  108. data/spec/support/model.json +1 -0
  109. data/{test → spec}/support/model.yml +0 -0
  110. metadata +175 -121
  111. data/test/integration/active_record_associations_parent_child.rb +0 -139
  112. data/test/integration/active_record_associations_test.rb +0 -326
  113. data/test/integration/active_record_basic_test.rb +0 -234
  114. data/test/integration/active_record_custom_serialization_test.rb +0 -62
  115. data/test/integration/active_record_import_test.rb +0 -109
  116. data/test/integration/active_record_namespaced_model_test.rb +0 -49
  117. data/test/integration/active_record_pagination_test.rb +0 -145
  118. data/test/integration/dynamic_index_name_test.rb +0 -47
  119. data/test/integration/mongoid_basic_test.rb +0 -177
  120. data/test/integration/multiple_models_test.rb +0 -172
  121. data/test/support/model.json +0 -1
  122. data/test/test_helper.rb +0 -93
  123. data/test/unit/adapter_active_record_test.rb +0 -157
  124. data/test/unit/adapter_default_test.rb +0 -41
  125. data/test/unit/adapter_mongoid_test.rb +0 -104
  126. data/test/unit/adapter_multiple_test.rb +0 -106
  127. data/test/unit/adapter_test.rb +0 -69
  128. data/test/unit/callbacks_test.rb +0 -31
  129. data/test/unit/client_test.rb +0 -27
  130. data/test/unit/importing_test.rb +0 -203
  131. data/test/unit/indexing_test.rb +0 -650
  132. data/test/unit/module_test.rb +0 -57
  133. data/test/unit/multimodel_test.rb +0 -38
  134. data/test/unit/naming_test.rb +0 -103
  135. data/test/unit/proxy_test.rb +0 -100
  136. data/test/unit/response_base_test.rb +0 -40
  137. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  138. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  139. data/test/unit/response_records_test.rb +0 -91
  140. data/test/unit/response_result_test.rb +0 -90
  141. data/test/unit/response_results_test.rb +0 -31
  142. data/test/unit/response_test.rb +0 -104
  143. data/test/unit/searching_search_request_test.rb +0 -78
  144. data/test/unit/searching_test.rb +0 -41
  145. data/test/unit/serializing_test.rb +0 -17
@@ -1 +0,0 @@
1
- { "baz": "qux" }
data/test/test_helper.rb DELETED
@@ -1,93 +0,0 @@
1
- RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
2
-
3
- exit(0) if RUBY_1_8
4
-
5
- require 'simplecov' and SimpleCov.start { add_filter "/test|test_/" } if ENV["COVERAGE"]
6
-
7
- # Register `at_exit` handler for integration tests shutdown.
8
- # MUST be called before requiring `test/unit`.
9
- at_exit { Elasticsearch::Test::IntegrationTestCase.__run_at_exit_hooks }
10
-
11
- puts '-'*80
12
-
13
- if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
14
- require 'test-unit'
15
- require 'mocha/test_unit'
16
- else
17
- require 'minitest/autorun'
18
- require 'mocha/mini_test'
19
- end
20
-
21
- require 'shoulda-context'
22
-
23
- require 'turn' unless ENV["TM_FILEPATH"] || ENV["NOTURN"] || defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
24
-
25
- require 'ansi'
26
- require 'oj'
27
-
28
- require 'active_model'
29
-
30
- require 'kaminari'
31
-
32
- require 'elasticsearch/model'
33
-
34
- require 'elasticsearch/extensions/test/cluster'
35
- require 'elasticsearch/extensions/test/startup_shutdown'
36
-
37
- module Elasticsearch
38
- module Test
39
- class IntegrationTestCase < ::Test::Unit::TestCase
40
- extend Elasticsearch::Extensions::Test::StartupShutdown
41
-
42
- startup { Elasticsearch::Extensions::Test::Cluster.start(nodes: 1) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running? }
43
- shutdown { Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] && started? }
44
- context "IntegrationTest" do; should "noop on Ruby 1.8" do; end; end if RUBY_1_8
45
-
46
- def setup
47
- ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
48
- logger = ::Logger.new(STDERR)
49
- logger.formatter = lambda { |s, d, p, m| "\e[2;36m#{m}\e[0m\n" }
50
- ActiveRecord::Base.logger = logger unless ENV['QUIET']
51
-
52
- ActiveRecord::LogSubscriber.colorize_logging = false
53
- ActiveRecord::Migration.verbose = false
54
-
55
- tracer = ::Logger.new(STDERR)
56
- tracer.formatter = lambda { |s, d, p, m| "#{m.gsub(/^.*$/) { |n| ' ' + n }.ansi(:faint)}\n" }
57
-
58
- Elasticsearch::Model.client = Elasticsearch::Client.new host: "localhost:#{(ENV['TEST_CLUSTER_PORT'] || 9250)}",
59
- tracer: (ENV['QUIET'] ? nil : tracer)
60
- end
61
- end
62
- end
63
- end
64
-
65
- class Mongo
66
- def self.setup!
67
- begin
68
- require 'mongoid'
69
- session = Moped::Connection.new("localhost", 27017, 0.5)
70
- session.connect
71
- ENV['MONGODB_AVAILABLE'] = 'yes'
72
- rescue LoadError, Moped::Errors::ConnectionFailure => e
73
- $stderr.puts "MongoDB not installed or running: #{e}"
74
- end
75
- end
76
-
77
- def self.available?
78
- !!ENV['MONGODB_AVAILABLE']
79
- end
80
-
81
- def self.connect_to(source)
82
- $stderr.puts "Mongoid #{Mongoid::VERSION}", '-'*80
83
-
84
- logger = ::Logger.new($stderr)
85
- logger.formatter = lambda { |s, d, p, m| " #{m.ansi(:faint, :cyan)}\n" }
86
- logger.level = ::Logger::DEBUG
87
-
88
- Mongoid.logger = logger unless ENV['QUIET']
89
- Moped.logger = logger unless ENV['QUIET']
90
-
91
- Mongoid.connect_to source
92
- end
93
- end
@@ -1,157 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::AdapterActiveRecordTest < Test::Unit::TestCase
4
- context "Adapter ActiveRecord module: " do
5
- class ::DummyClassForActiveRecord
6
- RESPONSE = Struct.new('DummyActiveRecordResponse') do
7
- def response
8
- { 'hits' => {'hits' => [ {'_id' => 2}, {'_id' => 1} ]} }
9
- end
10
- end.new
11
-
12
- def response
13
- RESPONSE
14
- end
15
-
16
- def ids
17
- [2, 1]
18
- end
19
- end
20
-
21
- RESPONSE = { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [] } }
22
-
23
- setup do
24
- @records = [ stub(id: 1, inspect: '<Model-1>'), stub(id: 2, inspect: '<Model-2>') ]
25
- @records.stubs(:load).returns(true)
26
- @records.stubs(:exec_queries).returns(true)
27
- end
28
-
29
- should "have the register condition" do
30
- assert_not_nil Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::ActiveRecord]
31
- assert_equal false, Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::ActiveRecord].call(DummyClassForActiveRecord)
32
- end
33
-
34
- context "Records" do
35
- setup do
36
- DummyClassForActiveRecord.__send__ :include, Elasticsearch::Model::Adapter::ActiveRecord::Records
37
- end
38
-
39
- should "have the implementation" do
40
- assert_instance_of Module, Elasticsearch::Model::Adapter::ActiveRecord::Records
41
-
42
- instance = DummyClassForActiveRecord.new
43
- instance.expects(:klass).returns(mock('class', primary_key: :some_key, where: @records)).at_least_once
44
-
45
- assert_equal @records, instance.records
46
- end
47
-
48
- should "load the records" do
49
- instance = DummyClassForActiveRecord.new
50
- instance.expects(:records).returns(@records)
51
- instance.load
52
- end
53
-
54
- should "load the records with its submodels when using :includes" do
55
- klass = mock('class', primary_key: :some_key, where: @records)
56
- @records.expects(:includes).with([:submodel]).at_least_once
57
-
58
- instance = DummyClassForActiveRecord.new
59
- instance.expects(:klass).returns(klass).at_least_once
60
- instance.options[:includes] = [:submodel]
61
- instance.records
62
- end
63
-
64
- should "reorder the records based on hits order" do
65
- @records.instance_variable_set(:@records, @records)
66
-
67
- instance = DummyClassForActiveRecord.new
68
- instance.expects(:klass).returns(mock('class', primary_key: :some_key, where: @records)).at_least_once
69
-
70
- assert_equal [1, 2], @records. to_a.map(&:id)
71
- assert_equal [2, 1], instance.records.to_a.map(&:id)
72
- end
73
-
74
- should "not reorder records when SQL order is present" do
75
- @records.instance_variable_set(:@records, @records)
76
-
77
- instance = DummyClassForActiveRecord.new
78
- instance.expects(:klass).returns(stub('class', primary_key: :some_key, where: @records)).at_least_once
79
- instance.records.expects(:order).returns(@records)
80
-
81
- assert_equal [2, 1], instance.records. to_a.map(&:id)
82
- assert_equal [1, 2], instance.order(:foo).to_a.map(&:id)
83
- end
84
- end
85
-
86
- context "Callbacks" do
87
- should "register hooks for automatically updating the index" do
88
- DummyClassForActiveRecord.expects(:after_commit).times(3)
89
-
90
- Elasticsearch::Model::Adapter::ActiveRecord::Callbacks.included(DummyClassForActiveRecord)
91
- end
92
- end
93
-
94
- context "Importing" do
95
- setup do
96
- DummyClassForActiveRecord.__send__ :extend, Elasticsearch::Model::Adapter::ActiveRecord::Importing
97
- end
98
-
99
- should "raise an exception when passing an invalid scope" do
100
- assert_raise NoMethodError do
101
- DummyClassForActiveRecord.__find_in_batches(scope: :not_found_method) do; end
102
- end
103
- end
104
-
105
- should "implement the __find_in_batches method" do
106
- DummyClassForActiveRecord.expects(:find_in_batches).returns([])
107
- DummyClassForActiveRecord.__find_in_batches do; end
108
- end
109
-
110
- should "limit the relation to a specific scope" do
111
- DummyClassForActiveRecord.expects(:find_in_batches).returns([])
112
- DummyClassForActiveRecord.expects(:published).returns(DummyClassForActiveRecord)
113
-
114
- DummyClassForActiveRecord.__find_in_batches(scope: :published) do; end
115
- end
116
-
117
- should "limit the relation to a specific query" do
118
- DummyClassForActiveRecord.expects(:find_in_batches).returns([])
119
- DummyClassForActiveRecord.expects(:where).returns(DummyClassForActiveRecord)
120
-
121
- DummyClassForActiveRecord.__find_in_batches(query: -> { where(color: "red") }) do; end
122
- end
123
-
124
- should "preprocess the batch if option provided" do
125
- class << DummyClassForActiveRecord
126
- # Updates/transforms the batch while fetching it from the database
127
- # (eg. with information from an external system)
128
- #
129
- def update_batch(batch)
130
- batch.collect { |b| b.to_s + '!' }
131
- end
132
- end
133
-
134
- DummyClassForActiveRecord.expects(:__find_in_batches).returns( [:a, :b] )
135
-
136
- DummyClassForActiveRecord.__find_in_batches(preprocess: :update_batch) do |batch|
137
- assert_same_elements ["a!", "b!"], batch
138
- end
139
- end
140
-
141
- context "when transforming models" do
142
- setup do
143
- @transform = DummyClassForActiveRecord.__transform
144
- end
145
-
146
- should "provide an object that responds to #call" do
147
- assert_respond_to @transform, :call
148
- end
149
-
150
- should "provide default transformation" do
151
- model = mock("model", id: 1, __elasticsearch__: stub(as_indexed_json: {}))
152
- assert_equal @transform.call(model), { index: { _id: 1, data: {} } }
153
- end
154
- end
155
- end
156
- end
157
- end
@@ -1,41 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::AdapterDefaultTest < Test::Unit::TestCase
4
- context "Adapter default module" do
5
- class ::DummyClassForDefaultAdapter; end
6
-
7
- should "have the default Records implementation" do
8
- assert_instance_of Module, Elasticsearch::Model::Adapter::Default::Records
9
-
10
- DummyClassForDefaultAdapter.__send__ :include, Elasticsearch::Model::Adapter::Default::Records
11
-
12
- instance = DummyClassForDefaultAdapter.new
13
- klass = mock('class', find: [1])
14
- instance.expects(:klass).returns(klass)
15
- instance.records
16
- end
17
-
18
- should "have the default Callbacks implementation" do
19
- assert_instance_of Module, Elasticsearch::Model::Adapter::Default::Callbacks
20
- end
21
-
22
- context "concerning abstract methods" do
23
- setup do
24
- DummyClassForDefaultAdapter.__send__ :include, Elasticsearch::Model::Adapter::Default::Importing
25
- end
26
-
27
- should "have the default Importing implementation" do
28
- assert_raise Elasticsearch::Model::NotImplemented do
29
- DummyClassForDefaultAdapter.new.__find_in_batches
30
- end
31
- end
32
-
33
- should "have the default transform implementation" do
34
- assert_raise Elasticsearch::Model::NotImplemented do
35
- DummyClassForDefaultAdapter.new.__transform
36
- end
37
- end
38
- end
39
-
40
- end
41
- end
@@ -1,104 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::AdapterMongoidTest < Test::Unit::TestCase
4
- context "Adapter Mongoid module: " do
5
- class ::DummyClassForMongoid
6
- RESPONSE = Struct.new('DummyMongoidResponse') do
7
- def response
8
- { 'hits' => {'hits' => [ {'_id' => 2}, {'_id' => 1} ]} }
9
- end
10
- end.new
11
-
12
- def response
13
- RESPONSE
14
- end
15
-
16
- def ids
17
- [2, 1]
18
- end
19
- end
20
-
21
- setup do
22
- @records = [ stub(id: 1, inspect: '<Model-1>'), stub(id: 2, inspect: '<Model-2>') ]
23
- ::Symbol.any_instance.stubs(:in).returns(@records)
24
- end
25
-
26
- should "have the register condition" do
27
- assert_not_nil Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid]
28
- assert_equal false, Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid].call(DummyClassForMongoid)
29
- end
30
-
31
- context "Records" do
32
- setup do
33
- DummyClassForMongoid.__send__ :include, Elasticsearch::Model::Adapter::Mongoid::Records
34
- end
35
-
36
- should "have the implementation" do
37
- assert_instance_of Module, Elasticsearch::Model::Adapter::Mongoid::Records
38
-
39
- instance = DummyClassForMongoid.new
40
- instance.expects(:klass).returns(mock('class', where: @records))
41
-
42
- assert_equal @records, instance.records
43
- end
44
-
45
- should "reorder the records based on hits order" do
46
- @records.instance_variable_set(:@records, @records)
47
-
48
- instance = DummyClassForMongoid.new
49
- instance.expects(:klass).returns(mock('class', where: @records))
50
-
51
- assert_equal [1, 2], @records. to_a.map(&:id)
52
- assert_equal [2, 1], instance.records.to_a.map(&:id)
53
- end
54
-
55
- should "not reorder records when SQL order is present" do
56
- @records.instance_variable_set(:@records, @records)
57
-
58
- instance = DummyClassForMongoid.new
59
- instance.expects(:klass).returns(stub('class', where: @records)).at_least_once
60
- instance.records.expects(:asc).returns(@records)
61
-
62
- assert_equal [2, 1], instance.records.to_a.map(&:id)
63
- assert_equal [1, 2], instance.asc.to_a.map(&:id)
64
- end
65
- end
66
-
67
- context "Callbacks" do
68
- should "register hooks for automatically updating the index" do
69
- DummyClassForMongoid.expects(:after_create)
70
- DummyClassForMongoid.expects(:after_update)
71
- DummyClassForMongoid.expects(:after_destroy)
72
-
73
- Elasticsearch::Model::Adapter::Mongoid::Callbacks.included(DummyClassForMongoid)
74
- end
75
- end
76
-
77
- context "Importing" do
78
- should "implement the __find_in_batches method" do
79
- relation = mock()
80
- relation.stubs(:no_timeout).returns([])
81
- DummyClassForMongoid.expects(:all).returns(relation)
82
-
83
- DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
84
- DummyClassForMongoid.__find_in_batches do; end
85
- end
86
-
87
- context "when transforming models" do
88
- setup do
89
- @transform = DummyClassForMongoid.__transform
90
- end
91
-
92
- should "provide an object that responds to #call" do
93
- assert_respond_to @transform, :call
94
- end
95
-
96
- should "provide basic transformation" do
97
- model = mock("model", id: 1, as_indexed_json: {})
98
- assert_equal @transform.call(model), { index: { _id: "1", data: {} } }
99
- end
100
- end
101
- end
102
-
103
- end
104
- end
@@ -1,106 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::MultipleTest < Test::Unit::TestCase
4
-
5
- context "Adapter for multiple models" do
6
-
7
- class ::DummyOne
8
- include Elasticsearch::Model
9
-
10
- index_name 'dummy'
11
- document_type 'dummy_one'
12
-
13
- def self.find(ids)
14
- ids.map { |id| new(id) }
15
- end
16
-
17
- attr_reader :id
18
-
19
- def initialize(id)
20
- @id = id.to_i
21
- end
22
- end
23
-
24
- module ::Namespace
25
- class DummyTwo
26
- include Elasticsearch::Model
27
-
28
- index_name 'dummy'
29
- document_type 'dummy_two'
30
-
31
- def self.find(ids)
32
- ids.map { |id| new(id) }
33
- end
34
-
35
- attr_reader :id
36
-
37
- def initialize(id)
38
- @id = id.to_i
39
- end
40
- end
41
- end
42
-
43
- class ::DummyTwo
44
- include Elasticsearch::Model
45
-
46
- index_name 'other_index'
47
- document_type 'dummy_two'
48
-
49
- def self.find(ids)
50
- ids.map { |id| new(id) }
51
- end
52
-
53
- attr_reader :id
54
-
55
- def initialize(id)
56
- @id = id.to_i
57
- end
58
- end
59
-
60
- HITS = [{_index: 'dummy',
61
- _type: 'dummy_two',
62
- _id: '2',
63
- }, {
64
- _index: 'dummy',
65
- _type: 'dummy_one',
66
- _id: '2',
67
- }, {
68
- _index: 'other_index',
69
- _type: 'dummy_two',
70
- _id: '1',
71
- }, {
72
- _index: 'dummy',
73
- _type: 'dummy_two',
74
- _id: '1',
75
- }, {
76
- _index: 'dummy',
77
- _type: 'dummy_one',
78
- _id: '3'}]
79
-
80
- setup do
81
- @multimodel = Elasticsearch::Model::Multimodel.new(DummyOne, DummyTwo, Namespace::DummyTwo)
82
- end
83
-
84
- context "when returning records" do
85
- setup do
86
- @multimodel.class.send :include, Elasticsearch::Model::Adapter::Multiple::Records
87
- @multimodel.expects(:response).at_least_once.returns(stub(response: { 'hits' => { 'hits' => HITS } }))
88
- end
89
-
90
- should "keep the order from response" do
91
- assert_instance_of Module, Elasticsearch::Model::Adapter::Multiple::Records
92
- records = @multimodel.records
93
-
94
- assert_equal 5, records.count
95
-
96
- assert_kind_of ::Namespace::DummyTwo, records[0]
97
- assert_kind_of ::DummyOne, records[1]
98
- assert_kind_of ::DummyTwo, records[2]
99
- assert_kind_of ::Namespace::DummyTwo, records[3]
100
- assert_kind_of ::DummyOne, records[4]
101
-
102
- assert_equal [2, 2, 1, 1, 3], records.map(&:id)
103
- end
104
- end
105
- end
106
- end
@@ -1,69 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::AdapterTest < Test::Unit::TestCase
4
- context "Adapter module" do
5
- class ::DummyAdapterClass; end
6
- class ::DummyAdapterClassWithAdapter; end
7
- class ::DummyAdapter
8
- Records = Module.new
9
- Callbacks = Module.new
10
- Importing = Module.new
11
- end
12
-
13
- should "return an Adapter instance" do
14
- assert_instance_of Elasticsearch::Model::Adapter::Adapter,
15
- Elasticsearch::Model::Adapter.from_class(DummyAdapterClass)
16
- end
17
-
18
- should "return a list of adapters" do
19
- Elasticsearch::Model::Adapter::Adapter.expects(:adapters)
20
- Elasticsearch::Model::Adapter.adapters
21
- end
22
-
23
- should "register an adapter" do
24
- begin
25
- Elasticsearch::Model::Adapter::Adapter.expects(:register)
26
- Elasticsearch::Model::Adapter.register(:foo, lambda { |c| false })
27
- ensure
28
- Elasticsearch::Model::Adapter::Adapter.instance_variable_set(:@adapters, {})
29
- end
30
- end
31
- end
32
-
33
- context "Adapter class" do
34
- should "register an adapter" do
35
- begin
36
- Elasticsearch::Model::Adapter::Adapter.register(:foo, lambda { |c| false })
37
- assert Elasticsearch::Model::Adapter::Adapter.adapters[:foo]
38
- ensure
39
- Elasticsearch::Model::Adapter::Adapter.instance_variable_set(:@adapters, {})
40
- end
41
- end
42
-
43
- should "return the default adapter" do
44
- adapter = Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClass)
45
- assert_equal Elasticsearch::Model::Adapter::Default, adapter.adapter
46
- end
47
-
48
- should "return a specific adapter" do
49
- Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
50
- lambda { |c| c == DummyAdapterClassWithAdapter })
51
-
52
- adapter = Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
53
- assert_equal DummyAdapter, adapter.adapter
54
- end
55
-
56
- should "return the modules" do
57
- assert_nothing_raised do
58
- Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
59
- lambda { |c| c == DummyAdapterClassWithAdapter })
60
-
61
- adapter = Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
62
-
63
- assert_instance_of Module, adapter.records_mixin
64
- assert_instance_of Module, adapter.callbacks_mixin
65
- assert_instance_of Module, adapter.importing_mixin
66
- end
67
- end
68
- end
69
- end
@@ -1,31 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::CallbacksTest < Test::Unit::TestCase
4
- context "Callbacks module" do
5
- class ::DummyCallbacksModel
6
- end
7
-
8
- module DummyCallbacksAdapter
9
- module CallbacksMixin
10
- end
11
-
12
- def callbacks_mixin
13
- CallbacksMixin
14
- end; module_function :callbacks_mixin
15
- end
16
-
17
- should "include the callbacks mixin from adapter" do
18
- Elasticsearch::Model::Adapter.expects(:from_class)
19
- .with(DummyCallbacksModel)
20
- .returns(DummyCallbacksAdapter)
21
-
22
- ::DummyCallbacksModel.expects(:__send__).with do |method, parameter|
23
- assert_equal :include, method
24
- assert_equal DummyCallbacksAdapter::CallbacksMixin, parameter
25
- true
26
- end
27
-
28
- Elasticsearch::Model::Callbacks.included(DummyCallbacksModel)
29
- end
30
- end
31
- end
@@ -1,27 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::ClientTest < Test::Unit::TestCase
4
- context "Client module" do
5
- class ::DummyClientModel
6
- extend Elasticsearch::Model::Client::ClassMethods
7
- include Elasticsearch::Model::Client::InstanceMethods
8
- end
9
-
10
- should "have the default client method" do
11
- assert_instance_of Elasticsearch::Transport::Client, DummyClientModel.client
12
- assert_instance_of Elasticsearch::Transport::Client, DummyClientModel.new.client
13
- end
14
-
15
- should "set the client for the model" do
16
- DummyClientModel.client = 'foobar'
17
- assert_equal 'foobar', DummyClientModel.client
18
- assert_equal 'foobar', DummyClientModel.new.client
19
- end
20
-
21
- should "set the client for a model instance" do
22
- instance = DummyClientModel.new
23
- instance.client = 'moobam'
24
- assert_equal 'moobam', instance.client
25
- end
26
- end
27
- end