Skip to content

Commit 5938ef0

Browse files
committed
[STORE] Added the Store module
The `Store` module saves and deletes the documents in Elasticsearch via the `save` and `delete` methods.
1 parent 7165f3d commit 5938ef0

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'elasticsearch/persistence/client'
88
require 'elasticsearch/persistence/repository/naming'
99
require 'elasticsearch/persistence/repository/serialize'
10+
require 'elasticsearch/persistence/repository/store'
1011
require 'elasticsearch/persistence/repository'
1112

1213
require 'elasticsearch/persistence/repository/class'

elasticsearch-persistence/lib/elasticsearch/persistence/repository.rb

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Repository
55
include Elasticsearch::Persistence::Client
66
include Elasticsearch::Persistence::Repository::Naming
77
include Elasticsearch::Persistence::Repository::Serialize
8+
include Elasticsearch::Persistence::Repository::Store
89

910
def new(options={}, &block)
1011
Elasticsearch::Persistence::Repository::Class.new options, &block
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Elasticsearch
2+
module Persistence
3+
module Repository
4+
5+
module Store
6+
def save(document, options={})
7+
serialized = serialize(document)
8+
id = __get_id_from_document(serialized)
9+
type = klass || __get_type_from_class(document.class)
10+
client.index( { index: 'test', type: type, id: id, body: serialized }.merge(options) )
11+
end
12+
13+
def delete(document, options={})
14+
if document.is_a?(String) || document.is_a?(Integer)
15+
id = document
16+
type = klass
17+
else
18+
serialized = serialize(document)
19+
id = __get_id_from_document(serialized)
20+
type = klass || __get_type_from_class(document.class)
21+
end
22+
client.delete( { index: 'test', type: type, id: id }.merge(options) )
23+
end
24+
end
25+
26+
end
27+
end
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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() { include Elasticsearch::Persistence::Repository::Store }.new
9+
end
10+
11+
context "save" do
12+
should "serialize the document, get type from klass and index it" do
13+
subject.expects(:serialize).returns({foo: 'bar'})
14+
subject.expects(:klass).returns('foo_type')
15+
subject.expects(:__get_id_from_document).returns('1')
16+
17+
client = mock
18+
client.expects(:index).with do |arguments|
19+
assert_equal 'foo_type', arguments[:type]
20+
assert_equal '1', arguments[:id]
21+
assert_equal({foo: 'bar'}, arguments[:body])
22+
end
23+
subject.expects(:client).returns(client)
24+
25+
subject.save({foo: 'bar'})
26+
end
27+
28+
should "serialize the document, get type from document class and index it" do
29+
subject.expects(:serialize).returns({foo: 'bar'})
30+
subject.expects(:klass).returns(nil)
31+
subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
32+
subject.expects(:__get_id_from_document).returns('1')
33+
34+
client = mock
35+
client.expects(:index).with do |arguments|
36+
assert_equal 'my_document', arguments[:type]
37+
assert_equal '1', arguments[:id]
38+
assert_equal({foo: 'bar'}, arguments[:body])
39+
end
40+
subject.expects(:client).returns(client)
41+
42+
subject.save(MyDocument.new)
43+
end
44+
45+
should "pass the options to the client" do
46+
subject.expects(:serialize).returns({foo: 'bar'})
47+
subject.expects(:klass).returns('foo')
48+
subject.expects(:__get_id_from_document).returns('1')
49+
50+
client = mock
51+
client.expects(:index).with do |arguments|
52+
assert_equal 'bambam', arguments[:routing]
53+
end
54+
subject.expects(:client).returns(client)
55+
56+
subject.save({foo: 'bar'}, routing: 'bambam')
57+
end
58+
end
59+
60+
context "delete" do
61+
should "get type from klass when passed only ID" do
62+
subject.expects(:serialize).never
63+
subject.expects(:klass).returns('foo_type')
64+
subject.expects(:__get_id_from_document).never
65+
66+
client = mock
67+
client.expects(:delete).with do |arguments|
68+
assert_equal 'foo_type', arguments[:type]
69+
assert_equal '1', arguments[:id]
70+
end
71+
subject.expects(:client).returns(client)
72+
73+
subject.delete('1')
74+
end
75+
76+
should "get ID from document and type from klass when passed a document" do
77+
subject.expects(:serialize).returns({id: '1', foo: 'bar'})
78+
subject.expects(:klass).returns('foo_type')
79+
subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
80+
81+
client = mock
82+
client.expects(:delete).with do |arguments|
83+
assert_equal 'foo_type', arguments[:type]
84+
assert_equal '1', arguments[:id]
85+
end
86+
subject.expects(:client).returns(client)
87+
88+
subject.delete({id: '1', foo: 'bar'})
89+
end
90+
91+
should "get ID and type from document when passed a document" do
92+
subject.expects(:serialize).returns({id: '1', foo: 'bar'})
93+
subject.expects(:klass).returns(nil)
94+
subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
95+
subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
96+
97+
client = mock
98+
client.expects(:delete).with do |arguments|
99+
assert_equal 'my_document', arguments[:type]
100+
assert_equal '1', arguments[:id]
101+
end
102+
subject.expects(:client).returns(client)
103+
104+
subject.delete(MyDocument.new)
105+
end
106+
107+
should "pass the options to the client" do
108+
subject.expects(:klass).returns('foo')
109+
110+
client = mock
111+
client.expects(:delete).with do |arguments|
112+
assert_equal 'bambam', arguments[:routing]
113+
end
114+
subject.expects(:client).returns(client)
115+
116+
subject.delete('1', routing: 'bambam')
117+
end
118+
end
119+
end
120+
end

0 commit comments

Comments
 (0)