elasticsearch-model 0.1.9 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,29 @@
1
1
  require 'test_helper'
2
2
  require 'active_record'
3
3
 
4
+ # Needed for ActiveRecord 3.x ?
5
+ ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
6
+
7
+ ::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
8
+
4
9
  module Elasticsearch
5
10
  module Model
6
11
  class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase
7
- context "ActiveRecord pagination" do
8
- setup do
9
- class ::ArticleForPagination < ActiveRecord::Base
10
- include Elasticsearch::Model
12
+ class ::ArticleForPagination < ActiveRecord::Base
13
+ include Elasticsearch::Model
11
14
 
12
- scope :published, -> { where(published: true) }
15
+ scope :published, -> { where(published: true) }
13
16
 
14
- settings index: { number_of_shards: 1, number_of_replicas: 0 } do
15
- mapping do
16
- indexes :title, type: 'string', analyzer: 'snowball'
17
- indexes :created_at, type: 'date'
18
- end
19
- end
17
+ settings index: { number_of_shards: 1, number_of_replicas: 0 } do
18
+ mapping do
19
+ indexes :title, type: 'string', analyzer: 'snowball'
20
+ indexes :created_at, type: 'date'
20
21
  end
22
+ end
23
+ end
21
24
 
25
+ context "ActiveRecord pagination" do
26
+ setup do
22
27
  ActiveRecord::Schema.define(:version => 1) do
23
28
  create_table ::ArticleForPagination.table_name do |t|
24
29
  t.string :title
@@ -27,7 +32,7 @@ module Elasticsearch
27
32
  end
28
33
  end
29
34
 
30
- Kaminari::Hooks.init
35
+ Kaminari::Hooks.init if defined?(Kaminari::Hooks)
31
36
 
32
37
  ArticleForPagination.delete_all
33
38
  ArticleForPagination.__elasticsearch__.create_index! force: true
@@ -87,12 +92,11 @@ module Elasticsearch
87
92
 
88
93
  assert_equal 0, records.size
89
94
  assert_equal 6, records.current_page
90
- assert_equal 5, records.prev_page
95
+
91
96
  assert_equal nil, records.next_page
92
97
  assert_equal 3, records.total_pages
93
98
 
94
99
  assert ! records.first_page?, "Should NOT be the first page"
95
- assert records.last_page?, "Should be the last page"
96
100
  assert records.out_of_range?, "Should be out of range"
97
101
  end
98
102
 
@@ -1,6 +1,11 @@
1
1
  require 'test_helper'
2
2
  require 'active_record'
3
3
 
4
+ # Needed for ActiveRecord 3.x ?
5
+ ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
6
+
7
+ ::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
8
+
4
9
  module Elasticsearch
5
10
  module Model
6
11
  class DynamicIndexNameTest < Elasticsearch::Test::IntegrationTestCase
@@ -1,11 +1,40 @@
1
1
  require 'test_helper'
2
2
  require 'active_record'
3
3
 
4
+ # Needed for ActiveRecord 3.x ?
5
+ ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
6
+
7
+ ::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
8
+
4
9
  Mongo.setup!
5
10
 
6
11
  module Elasticsearch
7
12
  module Model
8
13
  class MultipleModelsIntegration < Elasticsearch::Test::IntegrationTestCase
14
+ module ::NameSearch
15
+ extend ActiveSupport::Concern
16
+
17
+ included do
18
+ include Elasticsearch::Model
19
+ include Elasticsearch::Model::Callbacks
20
+
21
+ settings index: {number_of_shards: 1, number_of_replicas: 0} do
22
+ mapping do
23
+ indexes :name, type: 'string', analyzer: 'snowball'
24
+ indexes :created_at, type: 'date'
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ class ::Episode < ActiveRecord::Base
31
+ include NameSearch
32
+ end
33
+
34
+ class ::Series < ActiveRecord::Base
35
+ include NameSearch
36
+ end
37
+
9
38
  context "Multiple models" do
10
39
  setup do
11
40
  ActiveRecord::Schema.define(:version => 1) do
@@ -20,30 +49,6 @@ module Elasticsearch
20
49
  end
21
50
  end
22
51
 
23
- module ::NameSearch
24
- extend ActiveSupport::Concern
25
-
26
- included do
27
- include Elasticsearch::Model
28
- include Elasticsearch::Model::Callbacks
29
-
30
- settings index: {number_of_shards: 1, number_of_replicas: 0} do
31
- mapping do
32
- indexes :name, type: 'string', analyzer: 'snowball'
33
- indexes :created_at, type: 'date'
34
- end
35
- end
36
- end
37
- end
38
-
39
- class ::Episode < ActiveRecord::Base
40
- include NameSearch
41
- end
42
-
43
- class ::Series < ActiveRecord::Base
44
- include NameSearch
45
- end
46
-
47
52
  [::Episode, ::Series].each do |model|
48
53
  model.delete_all
49
54
  model.__elasticsearch__.create_index! force: true
@@ -52,7 +57,6 @@ module Elasticsearch
52
57
  model.create name: "The greatest #{model.name}"
53
58
  model.__elasticsearch__.refresh_index!
54
59
  end
55
-
56
60
  end
57
61
 
58
62
  should "find matching documents across multiple models" do
@@ -423,7 +423,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
423
423
  end
424
424
 
425
425
  context "Checking for index existence" do
426
- context "the index exists" do
426
+ context "when the index exists" do
427
427
  should "return true" do
428
428
  indices = mock('indices', exists: true)
429
429
  client = stub('client', indices: indices)
@@ -434,7 +434,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
434
434
  end
435
435
  end
436
436
 
437
- context "the index does not exists" do
437
+ context "when the index does not exists" do
438
438
  should "return false" do
439
439
  indices = mock('indices', exists: false)
440
440
  client = stub('client', indices: indices)
@@ -445,7 +445,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
445
445
  end
446
446
  end
447
447
 
448
- context "the indices raises" do
448
+ context "when the indices API raises an error" do
449
449
  should "return false" do
450
450
  client = stub('client')
451
451
  client.expects(:indices).raises(StandardError)
@@ -456,7 +456,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
456
456
  end
457
457
  end
458
458
 
459
- context "the indices raises" do
459
+ context "the indices.exists API raises an error" do
460
460
  should "return false" do
461
461
  indices = stub('indices')
462
462
  client = stub('client')
@@ -53,5 +53,16 @@ class Elasticsearch::Model::ModuleTest < Test::Unit::TestCase
53
53
  end
54
54
  end
55
55
 
56
+ context "settings" do
57
+ should "access the settings" do
58
+ assert_not_nil Elasticsearch::Model.settings
59
+ end
60
+
61
+ should "allow to set settings" do
62
+ assert_nothing_raised { Elasticsearch::Model.settings[:foo] = 'bar' }
63
+ assert_equal 'bar', Elasticsearch::Model.settings[:foo]
64
+ end
65
+ end
66
+
56
67
  end
57
68
  end
@@ -0,0 +1,94 @@
1
+ require "test_helper"
2
+
3
+ class Elasticsearch::Model::NamingInheritanceTest < Test::Unit::TestCase
4
+ def setup
5
+ Elasticsearch::Model.settings[:inheritance_enabled] = true
6
+ end
7
+
8
+ def teardown
9
+ Elasticsearch::Model.settings[:inheritance_enabled] = false
10
+ end
11
+
12
+ context "Naming module with inheritance" do
13
+ class ::TestBase
14
+ extend ActiveModel::Naming
15
+
16
+ extend Elasticsearch::Model::Naming::ClassMethods
17
+ include Elasticsearch::Model::Naming::InstanceMethods
18
+ end
19
+
20
+ class ::Animal < ::TestBase
21
+ extend ActiveModel::Naming
22
+
23
+ extend Elasticsearch::Model::Naming::ClassMethods
24
+ include Elasticsearch::Model::Naming::InstanceMethods
25
+
26
+ index_name "mammals"
27
+ document_type "mammal"
28
+ end
29
+
30
+ class ::Dog < ::Animal
31
+ end
32
+
33
+ module ::MyNamespace
34
+ class Dog < ::Animal
35
+ end
36
+ end
37
+
38
+ class ::Cat < ::Animal
39
+ extend ActiveModel::Naming
40
+
41
+ extend Elasticsearch::Model::Naming::ClassMethods
42
+ include Elasticsearch::Model::Naming::InstanceMethods
43
+
44
+ index_name "cats"
45
+ document_type "cat"
46
+ end
47
+
48
+ should "return the default index_name" do
49
+ assert_equal "test_bases", TestBase.index_name
50
+ assert_equal "test_bases", TestBase.new.index_name
51
+ end
52
+
53
+ should "return the explicit index_name" do
54
+ assert_equal "mammals", Animal.index_name
55
+ assert_equal "mammals", Animal.new.index_name
56
+
57
+ assert_equal "cats", Cat.index_name
58
+ assert_equal "cats", Cat.new.index_name
59
+ end
60
+
61
+ should "return the ancestor index_name" do
62
+ assert_equal "mammals", Dog.index_name
63
+ assert_equal "mammals", Dog.new.index_name
64
+ end
65
+
66
+ should "return the ancestor index_name for namespaced model" do
67
+ assert_equal "mammals", ::MyNamespace::Dog.index_name
68
+ assert_equal "mammals", ::MyNamespace::Dog.new.index_name
69
+ end
70
+
71
+ should "return the default document_type" do
72
+ assert_equal "test_base", TestBase.document_type
73
+ assert_equal "test_base", TestBase.new.document_type
74
+ end
75
+
76
+ should "return the explicit document_type" do
77
+ assert_equal "mammal", Animal.document_type
78
+ assert_equal "mammal", Animal.new.document_type
79
+
80
+ assert_equal "cat", Cat.document_type
81
+ assert_equal "cat", Cat.new.document_type
82
+ end
83
+
84
+ should "return the ancestor document_type" do
85
+ assert_equal "mammal", Dog.document_type
86
+ assert_equal "mammal", Dog.new.document_type
87
+ end
88
+
89
+ should "return the ancestor document_type for namespaced model" do
90
+ assert_equal "mammal", ::MyNamespace::Dog.document_type
91
+ assert_equal "mammal", ::MyNamespace::Dog.new.document_type
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch::Model::ResponseAggregationsTest < Test::Unit::TestCase
4
+ context "Response aggregations" do
5
+ class OriginClass
6
+ def self.index_name; 'foo'; end
7
+ def self.document_type; 'bar'; end
8
+ end
9
+
10
+ RESPONSE = {
11
+ 'aggregations' => {
12
+ 'foo' => {'bar' => 10 },
13
+ 'price' => { 'doc_count' => 123,
14
+ 'min' => { 'value' => 1.0},
15
+ 'max' => { 'value' => 99 }
16
+ }
17
+ }
18
+ }
19
+
20
+ setup do
21
+ @search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
22
+ @search.stubs(:execute!).returns(RESPONSE)
23
+ end
24
+
25
+ should "access the aggregations" do
26
+ @search.expects(:execute!).returns(RESPONSE)
27
+
28
+ response = Elasticsearch::Model::Response::Response.new OriginClass, @search
29
+ assert_respond_to response, :aggregations
30
+ assert_kind_of Elasticsearch::Model::Response::Aggregations, response.aggregations
31
+ assert_kind_of Hashie::Mash, response.aggregations.foo
32
+ assert_equal 10, response.aggregations.foo.bar
33
+ end
34
+
35
+ should "properly return min and max values" do
36
+ @search.expects(:execute!).returns(RESPONSE)
37
+
38
+ response = Elasticsearch::Model::Response::Response.new OriginClass, @search
39
+
40
+ assert_equal 123, response.aggregations.price.doc_count
41
+ assert_equal 1, response.aggregations.price.min.value
42
+ assert_equal 99, response.aggregations.price.max.value
43
+ end
44
+
45
+ end
46
+ end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'active_support/json/encoding'
2
3
 
3
4
  class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
4
5
  context "Response result" do
@@ -80,7 +81,6 @@ class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
80
81
  end
81
82
 
82
83
  should "delegate as_json to @result even when ActiveSupport changed half of Ruby" do
83
- require 'active_support/json/encoding'
84
84
  result = Elasticsearch::Model::Response::Result.new foo: 'bar'
85
85
 
86
86
  result.instance_variable_get(:@result).expects(:as_json)
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-05 00:00:00.000000000 Z
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.4'
19
+ version: '1.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">"
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.4'
26
+ version: '1.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "<"
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '11.0'
75
+ version: '11.1'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "<"
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '11.0'
82
+ version: '11.1'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: elasticsearch-extensions
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - ">"
116
116
  - !ruby/object:Gem::Version
117
- version: '3.0'
117
+ version: '3'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">"
123
123
  - !ruby/object:Gem::Version
124
- version: '3.0'
124
+ version: '3'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: oj
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -168,18 +168,18 @@ dependencies:
168
168
  name: minitest
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
- - - "~>"
171
+ - - ">="
172
172
  - !ruby/object:Gem::Version
173
- version: '4.2'
173
+ version: '0'
174
174
  type: :development
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
- - - "~>"
178
+ - - ">="
179
179
  - !ruby/object:Gem::Version
180
- version: '4.2'
180
+ version: '0'
181
181
  - !ruby/object:Gem::Dependency
182
- name: shoulda-context
182
+ name: test-unit
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - ">="
@@ -193,7 +193,7 @@ dependencies:
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  - !ruby/object:Gem::Dependency
196
- name: mocha
196
+ name: shoulda-context
197
197
  requirement: !ruby/object:Gem::Requirement
198
198
  requirements:
199
199
  - - ">="
@@ -207,7 +207,7 @@ dependencies:
207
207
  - !ruby/object:Gem::Version
208
208
  version: '0'
209
209
  - !ruby/object:Gem::Dependency
210
- name: turn
210
+ name: mocha
211
211
  requirement: !ruby/object:Gem::Requirement
212
212
  requirements:
213
213
  - - ">="
@@ -221,7 +221,7 @@ dependencies:
221
221
  - !ruby/object:Gem::Version
222
222
  version: '0'
223
223
  - !ruby/object:Gem::Dependency
224
- name: yard
224
+ name: turn
225
225
  requirement: !ruby/object:Gem::Requirement
226
226
  requirements:
227
227
  - - ">="
@@ -235,7 +235,7 @@ dependencies:
235
235
  - !ruby/object:Gem::Version
236
236
  version: '0'
237
237
  - !ruby/object:Gem::Dependency
238
- name: ruby-prof
238
+ name: yard
239
239
  requirement: !ruby/object:Gem::Requirement
240
240
  requirements:
241
241
  - - ">="
@@ -249,7 +249,7 @@ dependencies:
249
249
  - !ruby/object:Gem::Version
250
250
  version: '0'
251
251
  - !ruby/object:Gem::Dependency
252
- name: pry
252
+ name: ruby-prof
253
253
  requirement: !ruby/object:Gem::Requirement
254
254
  requirements:
255
255
  - - ">="
@@ -263,19 +263,19 @@ dependencies:
263
263
  - !ruby/object:Gem::Version
264
264
  version: '0'
265
265
  - !ruby/object:Gem::Dependency
266
- name: ci_reporter
266
+ name: pry
267
267
  requirement: !ruby/object:Gem::Requirement
268
268
  requirements:
269
- - - "~>"
269
+ - - ">="
270
270
  - !ruby/object:Gem::Version
271
- version: '1.9'
271
+ version: '0'
272
272
  type: :development
273
273
  prerelease: false
274
274
  version_requirements: !ruby/object:Gem::Requirement
275
275
  requirements:
276
- - - "~>"
276
+ - - ">="
277
277
  - !ruby/object:Gem::Version
278
- version: '1.9'
278
+ version: '0'
279
279
  - !ruby/object:Gem::Dependency
280
280
  name: simplecov
281
281
  requirement: !ruby/object:Gem::Requirement
@@ -344,6 +344,7 @@ files:
344
344
  - examples/riak_article.rb
345
345
  - gemfiles/3.0.gemfile
346
346
  - gemfiles/4.0.gemfile
347
+ - gemfiles/5.0.gemfile
347
348
  - lib/elasticsearch/model.rb
348
349
  - lib/elasticsearch/model/adapter.rb
349
350
  - lib/elasticsearch/model/adapters/active_record.rb
@@ -359,6 +360,7 @@ files:
359
360
  - lib/elasticsearch/model/naming.rb
360
361
  - lib/elasticsearch/model/proxy.rb
361
362
  - lib/elasticsearch/model/response.rb
363
+ - lib/elasticsearch/model/response/aggregations.rb
362
364
  - lib/elasticsearch/model/response/base.rb
363
365
  - lib/elasticsearch/model/response/pagination.rb
364
366
  - lib/elasticsearch/model/response/records.rb
@@ -392,8 +394,10 @@ files:
392
394
  - test/unit/indexing_test.rb
393
395
  - test/unit/module_test.rb
394
396
  - test/unit/multimodel_test.rb
397
+ - test/unit/naming_inheritance_test.rb
395
398
  - test/unit/naming_test.rb
396
399
  - test/unit/proxy_test.rb
400
+ - test/unit/response_aggregations_test.rb
397
401
  - test/unit/response_base_test.rb
398
402
  - test/unit/response_pagination_kaminari_test.rb
399
403
  - test/unit/response_pagination_will_paginate_test.rb
@@ -425,7 +429,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
425
429
  version: '0'
426
430
  requirements: []
427
431
  rubyforge_project:
428
- rubygems_version: 2.2.2
432
+ rubygems_version: 2.6.10
429
433
  signing_key:
430
434
  specification_version: 4
431
435
  summary: ActiveModel/Record integrations for Elasticsearch.
@@ -454,8 +458,10 @@ test_files:
454
458
  - test/unit/indexing_test.rb
455
459
  - test/unit/module_test.rb
456
460
  - test/unit/multimodel_test.rb
461
+ - test/unit/naming_inheritance_test.rb
457
462
  - test/unit/naming_test.rb
458
463
  - test/unit/proxy_test.rb
464
+ - test/unit/response_aggregations_test.rb
459
465
  - test/unit/response_base_test.rb
460
466
  - test/unit/response_pagination_kaminari_test.rb
461
467
  - test/unit/response_pagination_will_paginate_test.rb
@@ -466,4 +472,3 @@ test_files:
466
472
  - test/unit/searching_search_request_test.rb
467
473
  - test/unit/searching_test.rb
468
474
  - test/unit/serializing_test.rb
469
- has_rdoc: