elasticsearch-persistence 5.1.0 → 6.0.0.pre

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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -0
  3. data/Gemfile +9 -0
  4. data/README.md +164 -323
  5. data/Rakefile +8 -8
  6. data/elasticsearch-persistence.gemspec +4 -5
  7. data/lib/elasticsearch/persistence.rb +2 -110
  8. data/lib/elasticsearch/persistence/repository.rb +212 -53
  9. data/lib/elasticsearch/persistence/repository/dsl.rb +94 -0
  10. data/lib/elasticsearch/persistence/repository/find.rb +27 -10
  11. data/lib/elasticsearch/persistence/repository/response/results.rb +17 -5
  12. data/lib/elasticsearch/persistence/repository/search.rb +15 -4
  13. data/lib/elasticsearch/persistence/repository/serialize.rb +65 -7
  14. data/lib/elasticsearch/persistence/repository/store.rb +38 -44
  15. data/lib/elasticsearch/persistence/version.rb +1 -1
  16. data/spec/repository/find_spec.rb +179 -0
  17. data/spec/repository/response/results_spec.rb +105 -0
  18. data/spec/repository/search_spec.rb +181 -0
  19. data/spec/repository/serialize_spec.rb +53 -0
  20. data/spec/repository/store_spec.rb +327 -0
  21. data/spec/repository_spec.rb +716 -0
  22. data/spec/spec_helper.rb +28 -0
  23. metadata +25 -80
  24. data/lib/elasticsearch/persistence/client.rb +0 -51
  25. data/lib/elasticsearch/persistence/model.rb +0 -153
  26. data/lib/elasticsearch/persistence/model/base.rb +0 -87
  27. data/lib/elasticsearch/persistence/model/errors.rb +0 -8
  28. data/lib/elasticsearch/persistence/model/find.rb +0 -180
  29. data/lib/elasticsearch/persistence/model/rails.rb +0 -47
  30. data/lib/elasticsearch/persistence/model/store.rb +0 -254
  31. data/lib/elasticsearch/persistence/model/utils.rb +0 -0
  32. data/lib/elasticsearch/persistence/repository/class.rb +0 -71
  33. data/lib/elasticsearch/persistence/repository/naming.rb +0 -115
  34. data/lib/rails/generators/elasticsearch/model/model_generator.rb +0 -21
  35. data/lib/rails/generators/elasticsearch/model/templates/model.rb.tt +0 -9
  36. data/lib/rails/generators/elasticsearch_generator.rb +0 -2
  37. data/test/integration/model/model_basic_test.rb +0 -238
  38. data/test/integration/repository/custom_class_test.rb +0 -85
  39. data/test/integration/repository/customized_class_test.rb +0 -82
  40. data/test/integration/repository/default_class_test.rb +0 -116
  41. data/test/integration/repository/virtus_model_test.rb +0 -118
  42. data/test/test_helper.rb +0 -55
  43. data/test/unit/model_base_test.rb +0 -72
  44. data/test/unit/model_find_test.rb +0 -153
  45. data/test/unit/model_gateway_test.rb +0 -101
  46. data/test/unit/model_rails_test.rb +0 -112
  47. data/test/unit/model_store_test.rb +0 -576
  48. data/test/unit/persistence_test.rb +0 -32
  49. data/test/unit/repository_class_test.rb +0 -51
  50. data/test/unit/repository_client_test.rb +0 -32
  51. data/test/unit/repository_find_test.rb +0 -388
  52. data/test/unit/repository_indexing_test.rb +0 -37
  53. data/test/unit/repository_module_test.rb +0 -146
  54. data/test/unit/repository_naming_test.rb +0 -146
  55. data/test/unit/repository_response_results_test.rb +0 -98
  56. data/test/unit/repository_search_test.rb +0 -117
  57. data/test/unit/repository_serialize_test.rb +0 -57
  58. data/test/unit/repository_store_test.rb +0 -303
@@ -1,37 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryIndexingTest < Test::Unit::TestCase
4
- context "The repository index methods" do
5
- class MyDocument; end
6
-
7
- setup do
8
- @shoulda_subject = Class.new() { include Elasticsearch::Model::Indexing::ClassMethods }.new
9
- @shoulda_subject.stubs(:index_name).returns('my_index')
10
- @shoulda_subject.stubs(:document_type).returns('my_document')
11
- end
12
-
13
- should "have the convenience index management methods" do
14
- %w( create_index! delete_index! refresh_index! ).each do |method|
15
- assert_respond_to subject, method
16
- end
17
- end
18
-
19
- context "mappings" do
20
- should "configure the mappings for the type" do
21
- subject.mappings do
22
- indexes :title
23
- end
24
-
25
- assert_equal( {:"my_document"=>{:properties=>{:title=>{:type=>"text"}}}}, subject.mappings.to_hash )
26
- end
27
- end
28
-
29
- context "settings" do
30
- should "configure the settings for the index" do
31
- subject.settings foo: 'bar'
32
- assert_equal( {foo: 'bar'}, subject.settings.to_hash)
33
- end
34
- end
35
-
36
- end
37
- end
@@ -1,146 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryModuleTest < Test::Unit::TestCase
4
- context "The repository module" do
5
-
6
- class DummyModel
7
- def initialize(attributes={})
8
- @attributes = attributes
9
- end
10
-
11
- def to_hash
12
- @attributes
13
- end
14
-
15
- def inspect
16
- "<Note #{@attributes.inspect}>"
17
- end
18
- end
19
-
20
- setup do
21
- class DummyRepository
22
- include Elasticsearch::Persistence::Repository
23
- end
24
- end
25
-
26
- teardown do
27
- Elasticsearch::Persistence::RepositoryModuleTest.__send__ :remove_const, :DummyRepository
28
- end
29
-
30
- context "when included" do
31
- should "set up the gateway for the class and instance" do
32
- assert_respond_to DummyRepository, :gateway
33
- assert_respond_to DummyRepository.new, :gateway
34
-
35
- assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.gateway
36
- assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.new.gateway
37
- end
38
-
39
- should "proxy repository methods from the class to the gateway" do
40
- class DummyRepository
41
- include Elasticsearch::Persistence::Repository
42
-
43
- index :foobar
44
- klass DummyModel
45
- type :my_dummy_model
46
- mapping { indexes :title, analyzer: 'snowball' }
47
- end
48
-
49
- repository = DummyRepository.new
50
-
51
- assert_equal :foobar, DummyRepository.index
52
- assert_equal DummyModel, DummyRepository.klass
53
- assert_equal :my_dummy_model, DummyRepository.type
54
- assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
55
-
56
- assert_equal :foobar, repository.index
57
- assert_equal DummyModel, repository.klass
58
- assert_equal :my_dummy_model, repository.type
59
- assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
60
- end
61
-
62
- should "correctly delegate to the gateway" do
63
- repository = DummyRepository.new
64
- assert_instance_of Method, repository.method(:index)
65
- end
66
-
67
- should "proxy repository methods from the instance to the gateway" do
68
- class DummyRepository
69
- include Elasticsearch::Persistence::Repository
70
- end
71
-
72
- repository = DummyRepository.new
73
- repository.index :foobar
74
- repository.klass DummyModel
75
- repository.type :my_dummy_model
76
- repository.mapping { indexes :title, analyzer: 'snowball' }
77
-
78
- assert_equal :foobar, DummyRepository.index
79
- assert_equal DummyModel, DummyRepository.klass
80
- assert_equal :my_dummy_model, DummyRepository.type
81
- assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
82
-
83
- assert_equal :foobar, repository.index
84
- assert_equal DummyModel, repository.klass
85
- assert_equal :my_dummy_model, repository.type
86
- assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
87
- end
88
-
89
- should "allow to define gateway methods in the class definition via block passed to the gateway method" do
90
- class DummyRepositoryWithGatewaySerialize
91
- include Elasticsearch::Persistence::Repository
92
-
93
- gateway do
94
- def serialize(document)
95
- 'FAKE!'
96
- end
97
- end
98
- end
99
-
100
- repository = DummyRepositoryWithGatewaySerialize.new
101
- repository.client.transport.logger = Logger.new(STDERR)
102
-
103
- client = mock
104
- client.expects(:index).with do |arguments|
105
- assert_equal('xxx', arguments[:id])
106
- assert_equal('FAKE!', arguments[:body])
107
- true
108
- end
109
- repository.gateway.expects(:client).returns(client)
110
-
111
- repository.gateway.expects(:__get_id_from_document).returns('xxx')
112
-
113
- repository.save( id: '123', foo: 'bar' )
114
- end
115
- end
116
-
117
- should "allow to define gateway methods in the class definition via regular method definition" do
118
- class DummyRepositoryWithDirectSerialize
119
- include Elasticsearch::Persistence::Repository
120
-
121
- def serialize(document)
122
- 'FAKE IN CLASS!'
123
- end
124
- end
125
-
126
- repository = DummyRepositoryWithDirectSerialize.new
127
- repository.client.transport.logger = Logger.new(STDERR)
128
-
129
- client = mock
130
- client.expects(:index).with do |arguments|
131
- assert_equal('xxx', arguments[:id])
132
- assert_equal('FAKE IN CLASS!', arguments[:body])
133
- true
134
- end
135
- repository.gateway.expects(:client).returns(client)
136
-
137
- repository.gateway.expects(:__get_id_from_document).returns('xxx')
138
-
139
- repository.save( id: '123', foo: 'bar' )
140
- end
141
-
142
- should "configure the index name in the shortcut initializer" do
143
- assert_equal 'repository', Elasticsearch::Persistence::Repository.new.index_name
144
- end
145
- end
146
- end
@@ -1,146 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryNamingTest < Test::Unit::TestCase
4
- context "The repository naming" do
5
- # Fake class for the naming tests
6
- class ::Foobar; end
7
- class ::FooBar; end
8
- module ::Foo; class Bar; end; end
9
-
10
- setup do
11
- @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Naming }.new
12
- end
13
-
14
- context "get Ruby class from the Elasticsearch type" do
15
- should "get a simple class" do
16
- assert_equal Foobar, subject.__get_klass_from_type('foobar')
17
- end
18
- should "get a camelcased class" do
19
- assert_equal FooBar, subject.__get_klass_from_type('foo_bar')
20
- end
21
- should "get a namespaced class" do
22
- assert_equal Foo::Bar, subject.__get_klass_from_type('foo/bar')
23
- end
24
- should "re-raise a NameError exception" do
25
- assert_raise NameError do
26
- subject.__get_klass_from_type('foobarbazbam')
27
- end
28
- end
29
- end
30
-
31
- context "get Elasticsearch type from the Ruby class" do
32
- should "encode a simple class" do
33
- assert_equal 'foobar', subject.__get_type_from_class(Foobar)
34
- end
35
- should "encode a camelcased class" do
36
- assert_equal 'foo_bar', subject.__get_type_from_class(FooBar)
37
- end
38
- should "encode a namespaced class" do
39
- assert_equal 'foo/bar', subject.__get_type_from_class(Foo::Bar)
40
- end
41
- end
42
-
43
- context "get an ID from the document" do
44
- should "get an ID from Hash" do
45
- assert_equal 1, subject.__get_id_from_document(id: 1)
46
- assert_equal 1, subject.__get_id_from_document(_id: 1)
47
- assert_equal 1, subject.__get_id_from_document('id' => 1)
48
- assert_equal 1, subject.__get_id_from_document('_id' => 1)
49
- end
50
- end
51
-
52
- context "extract an ID from the document" do
53
- should "delete the key from theHash" do
54
- d1 = { :id => 1 }
55
- d2 = { :_id => 1 }
56
- d3 = { 'id' => 1 }
57
- d4 = { '_id' => 1 }
58
-
59
- assert_equal 1, subject.__extract_id_from_document(d1)
60
- assert_nil d1[:id]
61
-
62
- assert_equal 1, subject.__extract_id_from_document(d2)
63
- assert_nil d1[:_id]
64
-
65
- assert_equal 1, subject.__extract_id_from_document(d3)
66
- assert_nil d1['id']
67
-
68
- assert_equal 1, subject.__extract_id_from_document(d4)
69
- assert_nil d1['_id']
70
- end
71
- end
72
-
73
- context "document class name" do
74
- should "be nil by default" do
75
- assert_nil subject.klass
76
- end
77
-
78
- should "be settable" do
79
- subject.klass = Foobar
80
- assert_equal Foobar, subject.klass
81
- end
82
-
83
- should "be settable by DSL" do
84
- subject.klass Foobar
85
- assert_equal Foobar, subject.klass
86
- end
87
- end
88
-
89
- context "index_name" do
90
- should "default to the class name" do
91
- subject.instance_eval do
92
- def self.class
93
- 'FakeRepository'
94
- end
95
- end
96
-
97
- assert_equal 'fake_repository', subject.index_name
98
- end
99
-
100
- should "be settable" do
101
- subject.index_name = 'foobar1'
102
- assert_equal 'foobar1', subject.index_name
103
-
104
- subject.index_name 'foobar2'
105
- assert_equal 'foobar2', subject.index_name
106
- end
107
-
108
- should "be aliased as `index`" do
109
- subject.index_name = 'foobar1'
110
- assert_equal 'foobar1', subject.index
111
- end
112
-
113
- should "be inferred from the host class" do
114
- class ::MySpecialRepository; end
115
- subject.define_singleton_method(:host) { MySpecialRepository }
116
- assert_equal 'my_special_repository', subject.index_name
117
- end
118
- end
119
-
120
- context "document_type" do
121
- should "be nil when no klass is set" do
122
- assert_equal nil, subject.document_type
123
- end
124
-
125
- should "default to klass" do
126
- subject.klass Foobar
127
- assert_equal 'foobar', subject.document_type
128
- end
129
-
130
- should "be aliased as `type`" do
131
- subject.klass Foobar
132
- assert_equal 'foobar', subject.type
133
- end
134
-
135
- should "be settable" do
136
- subject.document_type = 'foobar'
137
- assert_equal 'foobar', subject.document_type
138
- end
139
-
140
- should "be settable by DSL" do
141
- subject.document_type 'foobar'
142
- assert_equal 'foobar', subject.document_type
143
- end
144
- end
145
- end
146
- end
@@ -1,98 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryResponseResultsTest < Test::Unit::TestCase
4
- include Elasticsearch::Persistence
5
- class MyDocument; end
6
-
7
- context "Response results" do
8
- setup do
9
- @repository = Repository.new
10
-
11
- @response = { "took" => 2,
12
- "timed_out" => false,
13
- "_shards" => {"total" => 5, "successful" => 5, "failed" => 0},
14
- "hits" =>
15
- { "total" => 2,
16
- "max_score" => 0.19,
17
- "hits" =>
18
- [{"_index" => "my_index",
19
- "_type" => "note",
20
- "_id" => "1",
21
- "_score" => 0.19,
22
- "_source" => {"id" => 1, "title" => "Test 1"}},
23
-
24
- {"_index" => "my_index",
25
- "_type" => "note",
26
- "_id" => "2",
27
- "_score" => 0.19,
28
- "_source" => {"id" => 2, "title" => "Test 2"}}
29
- ]
30
- }
31
- }
32
-
33
- @shoulda_subject = Repository::Response::Results.new @repository, @response
34
- end
35
-
36
- should "provide the access to the repository" do
37
- assert_instance_of Repository::Class, subject.repository
38
- end
39
-
40
- should "provide the access to the response" do
41
- assert_equal 5, subject.response['_shards']['total']
42
- end
43
-
44
- should "wrap the response in HashWrapper" do
45
- assert_equal 5, subject.response._shards.total
46
- end
47
-
48
- should "return the total" do
49
- assert_equal 2, subject.total
50
- end
51
-
52
- should "return the max_score" do
53
- assert_equal 0.19, subject.max_score
54
- end
55
-
56
- should "delegate methods to results" do
57
- subject.repository
58
- .expects(:deserialize)
59
- .twice
60
- .returns(MyDocument.new)
61
-
62
- assert_equal 2, subject.size
63
- assert_respond_to subject, :each
64
- end
65
-
66
- should "respond to missing" do
67
- assert_instance_of Method, subject.method(:to_a)
68
- end
69
-
70
- should "yield each object with hit" do
71
- @shoulda_subject = Repository::Response::Results.new \
72
- @repository,
73
- { 'hits' => { 'hits' => [{'_id' => '1', 'foo' => 'bar'}] } }
74
-
75
- subject.repository
76
- .expects(:deserialize)
77
- .returns('FOO')
78
-
79
- subject.each_with_hit do |object, hit|
80
- assert_equal 'FOO', object
81
- assert_equal 'bar', hit.foo
82
- end
83
- end
84
-
85
- should "map objects and hits" do
86
- @shoulda_subject = Repository::Response::Results.new \
87
- @repository,
88
- { 'hits' => { 'hits' => [{'_id' => '1', 'foo' => 'bar'}] } }
89
-
90
- subject.repository
91
- .expects(:deserialize)
92
- .returns('FOO')
93
-
94
- assert_equal ['FOO---bar'], subject.map_with_hit { |object, hit| "#{object}---#{hit.foo}" }
95
- end
96
- end
97
-
98
- end
@@ -1,117 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositorySearchTest < Test::Unit::TestCase
4
- class MyDocument; end
5
-
6
- context "The repository search" do
7
- setup do
8
- @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Search }.new
9
-
10
- @client = mock
11
- @shoulda_subject.stubs(:document_type).returns(nil)
12
- @shoulda_subject.stubs(:klass).returns(nil)
13
- @shoulda_subject.stubs(:index_name).returns('test')
14
- @shoulda_subject.stubs(:client).returns(@client)
15
- end
16
-
17
- should "search in type based on klass" do
18
- subject.expects(:klass).returns(MyDocument).at_least_once
19
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
20
-
21
- @client.expects(:search).with do |arguments|
22
- assert_equal 'test', arguments[:index]
23
- assert_equal 'my_document', arguments[:type]
24
- assert_equal({foo: 'bar'}, arguments[:body])
25
- true
26
- end
27
-
28
- subject.search foo: 'bar'
29
- end
30
-
31
- should "search in type based on document_type" do
32
- subject.expects(:document_type).returns('my_special_document').at_least_once
33
- subject.expects(:__get_type_from_class).never
34
-
35
- @client.expects(:search).with do |arguments|
36
- assert_equal 'test', arguments[:index]
37
- assert_equal 'my_special_document', arguments[:type]
38
- assert_equal({foo: 'bar'}, arguments[:body])
39
- true
40
- end
41
-
42
- subject.search foo: 'bar'
43
- end
44
-
45
- should "search across all types" do
46
- subject.expects(:document_type).returns(nil).at_least_once
47
- subject.expects(:klass).returns(nil).at_least_once
48
- subject.expects(:__get_type_from_class).never
49
-
50
- @client.expects(:search).with do |arguments|
51
- assert_equal 'test', arguments[:index]
52
- assert_equal nil, arguments[:type]
53
- assert_equal({foo: 'bar'}, arguments[:body])
54
- true
55
- end
56
-
57
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
58
- subject.search(foo: 'bar')
59
- end
60
-
61
- should "pass options to the client" do
62
- subject.expects(:klass).returns(nil).at_least_once
63
- subject.expects(:__get_type_from_class).never
64
-
65
- @client.expects(:search).twice.with do |arguments|
66
- assert_equal 'bambam', arguments[:routing]
67
- true
68
- end
69
-
70
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
71
- subject.search( {foo: 'bar'}, { routing: 'bambam' } )
72
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
73
- subject.search( 'foobar', { routing: 'bambam' } )
74
- end
75
-
76
- should "search with simple search" do
77
- subject.expects(:klass).returns(nil).at_least_once
78
- subject.expects(:__get_type_from_class).never
79
-
80
- @client.expects(:search).with do |arguments|
81
- assert_equal 'foobar', arguments[:q]
82
- true
83
- end
84
-
85
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
86
- subject.search('foobar')
87
- end
88
-
89
- should "raise error for incorrect search definitions" do
90
- subject.expects(:klass).returns(nil).at_least_once
91
- subject.expects(:__get_type_from_class).never
92
-
93
- assert_raise ArgumentError do
94
- subject.search 123
95
- end
96
- end
97
-
98
- should "return the number of domain objects" do
99
- subject.client.expects(:count).returns({'count' => 1})
100
- assert_equal 1, subject.count
101
- end
102
-
103
- should "pass arguments to count" do
104
- subject.client.expects(:count)
105
- .with do |arguments|
106
- assert_equal 'test', arguments[:index]
107
- assert_equal 'bar', arguments[:body][:query][:match][:foo]
108
- assert_equal true, arguments[:ignore_unavailable]
109
- true
110
- end
111
- .returns({'count' => 1})
112
-
113
- assert_equal 1, subject.count( { query: { match: { foo: 'bar' } } }, { ignore_unavailable: true } )
114
- end
115
- end
116
-
117
- end