forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_module_test.rb
146 lines (115 loc) · 4.82 KB
/
repository_module_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'test_helper'
class Elasticsearch::Persistence::RepositoryModuleTest < Test::Unit::TestCase
context "The repository module" do
class DummyModel
def initialize(attributes={})
@attributes = attributes
end
def to_hash
@attributes
end
def inspect
"<Note #{@attributes.inspect}>"
end
end
setup do
class DummyRepository
include Elasticsearch::Persistence::Repository
end
end
teardown do
Elasticsearch::Persistence::RepositoryModuleTest.__send__ :remove_const, :DummyRepository
end
context "when included" do
should "set up the gateway for the class and instance" do
assert_respond_to DummyRepository, :gateway
assert_respond_to DummyRepository.new, :gateway
assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.gateway
assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.new.gateway
end
should "proxy repository methods from the class to the gateway" do
class DummyRepository
include Elasticsearch::Persistence::Repository
index :foobar
klass DummyModel
type :my_dummy_model
mapping { indexes :title, analyzer: 'snowball' }
end
repository = DummyRepository.new
assert_equal :foobar, DummyRepository.index
assert_equal DummyModel, DummyRepository.klass
assert_equal :my_dummy_model, DummyRepository.type
assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
assert_equal :foobar, repository.index
assert_equal DummyModel, repository.klass
assert_equal :my_dummy_model, repository.type
assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
end
should "correctly delegate to the gateway" do
repository = DummyRepository.new
assert_instance_of Method, repository.method(:index)
end
should "proxy repository methods from the instance to the gateway" do
class DummyRepository
include Elasticsearch::Persistence::Repository
end
repository = DummyRepository.new
repository.index :foobar
repository.klass DummyModel
repository.type :my_dummy_model
repository.mapping { indexes :title, analyzer: 'snowball' }
assert_equal :foobar, DummyRepository.index
assert_equal DummyModel, DummyRepository.klass
assert_equal :my_dummy_model, DummyRepository.type
assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
assert_equal :foobar, repository.index
assert_equal DummyModel, repository.klass
assert_equal :my_dummy_model, repository.type
assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
end
should "allow to define gateway methods in the class definition via block passed to the gateway method" do
class DummyRepositoryWithGatewaySerialize
include Elasticsearch::Persistence::Repository
gateway do
def serialize(document)
'FAKE!'
end
end
end
repository = DummyRepositoryWithGatewaySerialize.new
repository.client.transport.logger = Logger.new(STDERR)
client = mock
client.expects(:index).with do |arguments|
assert_equal('xxx', arguments[:id])
assert_equal('FAKE!', arguments[:body])
true
end
repository.gateway.expects(:client).returns(client)
repository.gateway.expects(:__get_id_from_document).returns('xxx')
repository.save( id: '123', foo: 'bar' )
end
end
should "allow to define gateway methods in the class definition via regular method definition" do
class DummyRepositoryWithDirectSerialize
include Elasticsearch::Persistence::Repository
def serialize(document)
'FAKE IN CLASS!'
end
end
repository = DummyRepositoryWithDirectSerialize.new
repository.client.transport.logger = Logger.new(STDERR)
client = mock
client.expects(:index).with do |arguments|
assert_equal('xxx', arguments[:id])
assert_equal('FAKE IN CLASS!', arguments[:body])
true
end
repository.gateway.expects(:client).returns(client)
repository.gateway.expects(:__get_id_from_document).returns('xxx')
repository.save( id: '123', foo: 'bar' )
end
should "configure the index name in the shortcut initializer" do
assert_equal 'repository', Elasticsearch::Persistence::Repository.new.index_name
end
end
end