elasticsearch-persistence 0.0.0 → 0.0.1

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 (39) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE.txt +10 -19
  3. data/README.md +432 -14
  4. data/Rakefile +56 -0
  5. data/elasticsearch-persistence.gemspec +45 -17
  6. data/examples/sinatra/.gitignore +7 -0
  7. data/examples/sinatra/Gemfile +28 -0
  8. data/examples/sinatra/README.markdown +36 -0
  9. data/examples/sinatra/application.rb +238 -0
  10. data/examples/sinatra/config.ru +7 -0
  11. data/examples/sinatra/test.rb +118 -0
  12. data/lib/elasticsearch/persistence.rb +88 -2
  13. data/lib/elasticsearch/persistence/client.rb +51 -0
  14. data/lib/elasticsearch/persistence/repository.rb +75 -0
  15. data/lib/elasticsearch/persistence/repository/class.rb +71 -0
  16. data/lib/elasticsearch/persistence/repository/find.rb +73 -0
  17. data/lib/elasticsearch/persistence/repository/naming.rb +115 -0
  18. data/lib/elasticsearch/persistence/repository/response/results.rb +90 -0
  19. data/lib/elasticsearch/persistence/repository/search.rb +60 -0
  20. data/lib/elasticsearch/persistence/repository/serialize.rb +31 -0
  21. data/lib/elasticsearch/persistence/repository/store.rb +95 -0
  22. data/lib/elasticsearch/persistence/version.rb +1 -1
  23. data/test/integration/repository/custom_class_test.rb +85 -0
  24. data/test/integration/repository/customized_class_test.rb +82 -0
  25. data/test/integration/repository/default_class_test.rb +108 -0
  26. data/test/integration/repository/virtus_model_test.rb +114 -0
  27. data/test/test_helper.rb +46 -0
  28. data/test/unit/persistence_test.rb +32 -0
  29. data/test/unit/repository_class_test.rb +51 -0
  30. data/test/unit/repository_client_test.rb +32 -0
  31. data/test/unit/repository_find_test.rb +375 -0
  32. data/test/unit/repository_indexing_test.rb +37 -0
  33. data/test/unit/repository_module_test.rb +144 -0
  34. data/test/unit/repository_naming_test.rb +146 -0
  35. data/test/unit/repository_response_results_test.rb +98 -0
  36. data/test/unit/repository_search_test.rb +97 -0
  37. data/test/unit/repository_serialize_test.rb +57 -0
  38. data/test/unit/repository_store_test.rb +287 -0
  39. metadata +288 -20
@@ -0,0 +1,146 @@
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
@@ -0,0 +1,98 @@
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 Hashie::Mash" 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
@@ -0,0 +1,97 @@
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
+
25
+ assert_equal({foo: 'bar'}, arguments[:body])
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
+
39
+ assert_equal({foo: 'bar'}, arguments[:body])
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
+
54
+ assert_equal({foo: 'bar'}, arguments[:body])
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
+ end
68
+
69
+ assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
70
+ subject.search( {foo: 'bar'}, { routing: 'bambam' } )
71
+ assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
72
+ subject.search( 'foobar', { routing: 'bambam' } )
73
+ end
74
+
75
+ should "search with simple search" do
76
+ subject.expects(:klass).returns(nil).at_least_once
77
+ subject.expects(:__get_type_from_class).never
78
+
79
+ @client.expects(:search).with do |arguments|
80
+ assert_equal 'foobar', arguments[:q]
81
+ end
82
+
83
+ assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
84
+ subject.search('foobar')
85
+ end
86
+
87
+ should "raise error for incorrect search definitions" do
88
+ subject.expects(:klass).returns(nil).at_least_once
89
+ subject.expects(:__get_type_from_class).never
90
+
91
+ assert_raise ArgumentError do
92
+ subject.search 123
93
+ end
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch::Persistence::RepositorySerializeTest < Test::Unit::TestCase
4
+ context "The repository serialization" do
5
+ class DummyDocument
6
+ def to_hash
7
+ { foo: 'bar' }
8
+ end
9
+ end
10
+
11
+ class MyDocument; end
12
+
13
+ setup do
14
+ @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Serialize }.new
15
+ end
16
+
17
+ context "serialize" do
18
+ should "call #to_hash on passed object" do
19
+ document = DummyDocument.new
20
+ assert_equal( { foo: 'bar' }, subject.serialize(document))
21
+ end
22
+ end
23
+
24
+ context "deserialize" do
25
+ should "get the class name from #klass" do
26
+ subject.expects(:klass)
27
+ .returns(MyDocument)
28
+
29
+ MyDocument.expects(:new)
30
+
31
+ subject.deserialize( {} )
32
+ end
33
+
34
+ should "get the class name from Elasticsearch _type" do
35
+ subject.expects(:klass)
36
+ .returns(nil)
37
+
38
+ subject.expects(:__get_klass_from_type)
39
+ .returns(MyDocument)
40
+
41
+ MyDocument.expects(:new)
42
+
43
+ subject.deserialize( {} )
44
+ end
45
+
46
+ should "create the class instance with _source attributes" do
47
+ subject.expects(:klass).returns(nil)
48
+
49
+ subject.expects(:__get_klass_from_type).returns(MyDocument)
50
+
51
+ MyDocument.expects(:new).with({ 'foo' => 'bar' })
52
+
53
+ subject.deserialize( {'_source' => { 'foo' => 'bar' } } )
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,287 @@
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch::Persistence::RepositoryStoreTest < Test::Unit::TestCase
4
+ context "The repository store" do
5
+ class MyDocument; end
6
+
7
+ setup do
8
+ @shoulda_subject = Class.new() do
9
+ include Elasticsearch::Persistence::Repository::Store
10
+ include Elasticsearch::Persistence::Repository::Naming
11
+ end.new
12
+ @shoulda_subject.stubs(:index_name).returns('test')
13
+ end
14
+
15
+ context "save" do
16
+ should "serialize the document, get type from klass and index it" do
17
+ subject.expects(:serialize).returns({foo: 'bar'})
18
+ subject.expects(:document_type).returns(nil)
19
+ subject.expects(:klass).at_least_once.returns(MyDocument)
20
+ subject.expects(:__get_type_from_class).with(MyDocument).at_least_once.returns('my_document')
21
+ subject.expects(:__get_id_from_document).returns('1')
22
+
23
+ client = mock
24
+ client.expects(:index).with do |arguments|
25
+ assert_equal 'my_document', arguments[:type]
26
+ assert_equal '1', arguments[:id]
27
+ assert_equal({foo: 'bar'}, arguments[:body])
28
+ end
29
+ subject.expects(:client).returns(client)
30
+
31
+ subject.save({foo: 'bar'})
32
+ end
33
+
34
+ should "serialize the document, get type from document class and index it" do
35
+ subject.expects(:serialize).returns({foo: 'bar'})
36
+ subject.expects(:document_type).returns(nil)
37
+ subject.expects(:klass).at_least_once.returns(nil)
38
+ subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
39
+ subject.expects(:__get_id_from_document).returns('1')
40
+
41
+ client = mock
42
+ client.expects(:index).with do |arguments|
43
+ assert_equal 'my_document', arguments[:type]
44
+ assert_equal '1', arguments[:id]
45
+ assert_equal({foo: 'bar'}, arguments[:body])
46
+ end
47
+ subject.expects(:client).returns(client)
48
+
49
+ subject.save(MyDocument.new)
50
+ end
51
+
52
+ should "serialize the document, get type from document_type and index it" do
53
+ subject.expects(:serialize).returns({foo: 'bar'})
54
+
55
+ subject.expects(:document_type).returns('my_document')
56
+
57
+ subject.expects(:klass).never
58
+ subject.expects(:__get_type_from_class).never
59
+
60
+ subject.expects(:__get_id_from_document).returns('1')
61
+
62
+ client = mock
63
+ client.expects(:index).with do |arguments|
64
+ assert_equal 'my_document', arguments[:type]
65
+ assert_equal '1', arguments[:id]
66
+ assert_equal({foo: 'bar'}, arguments[:body])
67
+ end
68
+ subject.expects(:client).returns(client)
69
+
70
+ subject.save(MyDocument.new)
71
+ end
72
+
73
+ should "pass the options to the client" do
74
+ subject.expects(:serialize).returns({foo: 'bar'})
75
+ subject.expects(:document_type).returns(nil)
76
+ subject.expects(:klass).at_least_once.returns(MyDocument)
77
+ subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
78
+ subject.expects(:__get_id_from_document).returns('1')
79
+
80
+ client = mock
81
+ client.expects(:index).with do |arguments|
82
+ assert_equal 'foobarbam', arguments[:index]
83
+ assert_equal 'bambam', arguments[:routing]
84
+ end
85
+ subject.expects(:client).returns(client)
86
+
87
+ subject.save({foo: 'bar'}, { index: 'foobarbam', routing: 'bambam' })
88
+ end
89
+ end
90
+
91
+ context "update" do
92
+ should "get the ID from first argument and :doc from options" do
93
+ subject.expects(:serialize).never
94
+ subject.expects(:document_type).returns('mydoc')
95
+ subject.expects(:__extract_id_from_document).never
96
+
97
+ client = mock
98
+ client.expects(:update).with do |arguments|
99
+ assert_equal '1', arguments[:id]
100
+ assert_equal 'mydoc', arguments[:type]
101
+ assert_equal({doc: { foo: 'bar' }}, arguments[:body])
102
+ end
103
+ subject.expects(:client).returns(client)
104
+
105
+ subject.update('1', doc: { foo: 'bar' })
106
+ end
107
+
108
+ should "get the ID from first argument and :script from options" do
109
+ subject.expects(:document_type).returns('mydoc')
110
+ subject.expects(:__extract_id_from_document).never
111
+
112
+ client = mock
113
+ client.expects(:update).with do |arguments|
114
+ assert_equal '1', arguments[:id]
115
+ assert_equal 'mydoc', arguments[:type]
116
+ assert_equal({script: 'ctx._source.foo += 1'}, arguments[:body])
117
+ end
118
+ subject.expects(:client).returns(client)
119
+
120
+ subject.update('1', script: 'ctx._source.foo += 1')
121
+ end
122
+
123
+ should "get the ID from first argument and :script with :upsert from options" do
124
+ subject.expects(:document_type).returns('mydoc')
125
+ subject.expects(:__extract_id_from_document).never
126
+
127
+ client = mock
128
+ client.expects(:update).with do |arguments|
129
+ assert_equal '1', arguments[:id]
130
+ assert_equal 'mydoc', arguments[:type]
131
+ assert_equal({script: 'ctx._source.foo += 1', upsert: { foo: 1 }}, arguments[:body])
132
+ end
133
+ subject.expects(:client).returns(client)
134
+
135
+ subject.update('1', script: 'ctx._source.foo += 1', upsert: { foo: 1 })
136
+ end
137
+
138
+ should "get the ID and :doc from document" do
139
+ subject.expects(:document_type).returns('mydoc')
140
+
141
+ client = mock
142
+ client.expects(:update).with do |arguments|
143
+ assert_equal '1', arguments[:id]
144
+ assert_equal 'mydoc', arguments[:type]
145
+ assert_equal({doc: { foo: 'bar' }}, arguments[:body])
146
+ end
147
+ subject.expects(:client).returns(client)
148
+
149
+ subject.update(id: '1', foo: 'bar')
150
+ end
151
+
152
+ should "get the ID and :script from document" do
153
+ subject.expects(:document_type).returns('mydoc')
154
+
155
+ client = mock
156
+ client.expects(:update).with do |arguments|
157
+ assert_equal '1', arguments[:id]
158
+ assert_equal 'mydoc', arguments[:type]
159
+ assert_equal({script: 'ctx._source.foo += 1'}, arguments[:body])
160
+ end
161
+ subject.expects(:client).returns(client)
162
+
163
+ subject.update(id: '1', script: 'ctx._source.foo += 1')
164
+ end
165
+
166
+ should "get the ID and :script with :upsert from document" do
167
+ subject.expects(:document_type).returns('mydoc')
168
+
169
+ client = mock
170
+ client.expects(:update).with do |arguments|
171
+ assert_equal '1', arguments[:id]
172
+ assert_equal 'mydoc', arguments[:type]
173
+ assert_equal({script: 'ctx._source.foo += 1', upsert: { foo: 1 } }, arguments[:body])
174
+ end
175
+ subject.expects(:client).returns(client)
176
+
177
+ subject.update(id: '1', script: 'ctx._source.foo += 1', upsert: { foo: 1 })
178
+ end
179
+
180
+ should "override the type from params" do
181
+ subject.expects(:document_type).never
182
+
183
+ client = mock
184
+ client.expects(:update).with do |arguments|
185
+ assert_equal '1', arguments[:id]
186
+ assert_equal 'foo', arguments[:type]
187
+ assert_equal({script: 'ctx._source.foo += 1'}, arguments[:body])
188
+ end
189
+ subject.expects(:client).returns(client)
190
+
191
+ subject.update(id: '1', script: 'ctx._source.foo += 1', type: 'foo')
192
+ end
193
+
194
+ should "raise an exception when passed incorrect argument" do
195
+ assert_raise(ArgumentError) { subject.update(MyDocument.new, foo: 'bar') }
196
+ end
197
+ end
198
+
199
+ context "delete" do
200
+ should "get type from klass when passed only ID" do
201
+ subject.expects(:serialize).never
202
+ subject.expects(:document_type).returns(nil)
203
+ subject.expects(:klass).at_least_once.returns(MyDocument)
204
+ subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
205
+ subject.expects(:__get_id_from_document).never
206
+
207
+ client = mock
208
+ client.expects(:delete).with do |arguments|
209
+ assert_equal 'my_document', arguments[:type]
210
+ assert_equal '1', arguments[:id]
211
+ end
212
+ subject.expects(:client).returns(client)
213
+
214
+ subject.delete('1')
215
+ end
216
+
217
+ should "get ID from document and type from klass when passed a document" do
218
+ subject.expects(:serialize).returns({id: '1', foo: 'bar'})
219
+ subject.expects(:document_type).returns(nil)
220
+ subject.expects(:klass).at_least_once.returns(MyDocument)
221
+ subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
222
+ subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
223
+
224
+ client = mock
225
+ client.expects(:delete).with do |arguments|
226
+ assert_equal 'my_document', arguments[:type]
227
+ assert_equal '1', arguments[:id]
228
+ end
229
+ subject.expects(:client).returns(client)
230
+
231
+ subject.delete({id: '1', foo: 'bar'})
232
+ end
233
+
234
+ should "get ID from document and type from document_type when passed a document" do
235
+ subject.expects(:serialize).returns({id: '1', foo: 'bar'})
236
+
237
+ subject.expects(:document_type).returns('my_document')
238
+
239
+ subject.expects(:klass).never
240
+ subject.expects(:__get_type_from_class).never
241
+
242
+ subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
243
+
244
+ client = mock
245
+ client.expects(:delete).with do |arguments|
246
+ assert_equal 'my_document', arguments[:type]
247
+ assert_equal '1', arguments[:id]
248
+ end
249
+ subject.expects(:client).returns(client)
250
+
251
+ subject.delete({id: '1', foo: 'bar'})
252
+ end
253
+
254
+ should "get ID and type from document when passed a document" do
255
+ subject.expects(:serialize).returns({id: '1', foo: 'bar'})
256
+ subject.expects(:document_type).returns(nil)
257
+ subject.expects(:klass).at_least_once.returns(nil)
258
+ subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
259
+ subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
260
+
261
+ client = mock
262
+ client.expects(:delete).with do |arguments|
263
+ assert_equal 'my_document', arguments[:type]
264
+ assert_equal '1', arguments[:id]
265
+ end
266
+ subject.expects(:client).returns(client)
267
+
268
+ subject.delete(MyDocument.new)
269
+ end
270
+
271
+ should "pass the options to the client" do
272
+ subject.expects(:document_type).returns(nil)
273
+ subject.expects(:klass).at_least_once.returns(MyDocument)
274
+ subject.expects(:__get_type_from_class).returns('my_document')
275
+
276
+ client = mock
277
+ client.expects(:delete).with do |arguments|
278
+ assert_equal 'foobarbam', arguments[:index]
279
+ assert_equal 'bambam', arguments[:routing]
280
+ end
281
+ subject.expects(:client).returns(client)
282
+
283
+ subject.delete('1', index: 'foobarbam', routing: 'bambam')
284
+ end
285
+ end
286
+ end
287
+ end