Skip to content

Commit ff43886

Browse files
committed
[STORE] Add more tests and update documentation
1 parent 80d6795 commit ff43886

File tree

3 files changed

+135
-45
lines changed

3 files changed

+135
-45
lines changed

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

+24-12
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,58 @@ def self.included(base)
2222
base.send(:extend, Elasticsearch::Model::Indexing::ClassMethods)
2323
end
2424

25+
# These methods are necessary to define at the class-level so that the methods available
26+
# via Elasticsearch::Model::Indexing::ClassMethod have the references they need.
27+
#
28+
# @since 6.0.0
2529
module ClassMethods
2630

27-
# Get the class-level document type setting.
31+
# Get or set the class-level document type setting.
2832
#
2933
# @example
3034
# MyRepository.document_type
3135
#
32-
# @return [ String, Symbol ] The repository's document type.
36+
# @return [ String, Symbol ] _type The repository's document type.
3337
#
3438
# @since 6.0.0
35-
def document_type(type = nil)
36-
@document_type ||= (type || DEFAULT_DOC_TYPE)
39+
def document_type(_type = nil)
40+
@document_type ||= (_type || DEFAULT_DOC_TYPE)
3741
end
3842

39-
# Get the class-level index name setting.
43+
# Get or set the class-level index name setting.
4044
#
4145
# @example
4246
# MyRepository.index_name
4347
#
44-
# @return [ String, Symbol ] The repository's index name.
48+
# @return [ String, Symbol ] _name The repository's index name.
4549
#
4650
# @since 6.0.0
47-
def index_name(name = nil)
48-
@index_name ||= (name || DEFAULT_INDEX_NAME)
51+
def index_name(_name = nil)
52+
@index_name ||= (_name || DEFAULT_INDEX_NAME)
4953
end
5054

51-
# Get the class-level setting for the class used by the repository when deserializing.
55+
# Get or set the class-level setting for the class used by the repository when deserializing.
5256
#
5357
# @example
5458
# MyRepository.klass
5559
#
56-
# @return [ Class ] The repository's klass for deserializing.
60+
# @return [ Class ] _class The repository's klass for deserializing.
5761
#
5862
# @since 6.0.0
5963
def klass(_class = nil)
6064
instance_variables.include?(:@klass) ? @klass : @klass = _class
6165
end
6266

63-
def client(client = client)
64-
@client ||= (client || Elasticsearch::Transport::Client.new)
67+
# Get or set the class-level setting for the client used by the repository.
68+
#
69+
# @example
70+
# MyRepository.client
71+
#
72+
# @return [ Class ] _client The repository's client.
73+
#
74+
# @since 6.0.0
75+
def client(_client = nil)
76+
@client ||= (_client || Elasticsearch::Transport::Client.new)
6577
end
6678
end
6779

elasticsearch-persistence/spec/repository_spec.rb

+104-27
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class UserRepo
1414
end
1515
end
1616

17+
after do
18+
begin; DEFAULT_REPOSITORY.delete_index!; rescue; end
19+
end
20+
1721
describe '#initialize' do
1822

1923
context 'when options are not provided' do
@@ -118,19 +122,13 @@ class UserRepo
118122

119123
describe 'class methods' do
120124

121-
before do
122-
if defined?(NoteRepository)
123-
Object.send(:remove_const, NoteRepository.name)
124-
end
125+
before(:all) do
125126
class NoteRepository
126127
include Elasticsearch::Persistence::Repository
127128

128129
document_type 'note'
129-
130130
index_name 'notes_repo'
131-
132131
klass Hash
133-
134132
client(_class: 'client')
135133

136134
settings number_of_shards: 1, number_of_replicas: 0 do
@@ -144,6 +142,16 @@ class NoteRepository
144142
end
145143
end
146144

145+
after(:all) do
146+
if defined?(NoteRepository)
147+
Object.send(:remove_const, NoteRepository.name)
148+
end
149+
end
150+
151+
after do
152+
begin; NoteRepository.delete_index!; rescue; end
153+
end
154+
147155
context '#client' do
148156

149157
it 'allows the value to be set only once' do
@@ -160,7 +168,7 @@ class NoteRepository
160168
end
161169

162170
it 'allows the value to be overwritten with options on the instance' do
163-
expect(NoteRepository.new(client: double('client', class: 'other_client')).client.class).to eq('other_client')
171+
expect(NoteRepository.new(client: double('client', instance: 'other')).client.instance).to eq('other')
164172
end
165173
end
166174

@@ -239,12 +247,33 @@ class NoteRepository
239247

240248
context 'when the method is called on an instance' do
241249

250+
let(:repository) do
251+
DEFAULT_REPOSITORY
252+
end
253+
242254
before do
243-
DEFAULT_REPOSITORY.create_index!
255+
begin; repository.delete_index!; rescue; end
256+
repository.create_index!
244257
end
245258

246259
it 'creates the index' do
247-
expect(DEFAULT_REPOSITORY.index_exists?).to be(true)
260+
expect(repository.index_exists?).to be(true)
261+
end
262+
end
263+
264+
context 'when the method is called on the class' do
265+
266+
let(:repository) do
267+
DEFAULT_REPOSITORY.class
268+
end
269+
270+
before do
271+
begin; repository.delete_index!; rescue; end
272+
repository.create_index!
273+
end
274+
275+
it 'creates the index' do
276+
expect(repository.index_exists?).to be(true)
248277
end
249278
end
250279
end
@@ -253,13 +282,33 @@ class NoteRepository
253282

254283
context 'when the method is called on an instance' do
255284

285+
let(:repository) do
286+
DEFAULT_REPOSITORY
287+
end
288+
256289
before do
257-
DEFAULT_REPOSITORY.create_index!
258-
DEFAULT_REPOSITORY.delete_index!
290+
repository.create_index!
291+
begin; repository.delete_index!; rescue; end
259292
end
260293

261294
it 'creates the index' do
262-
expect(DEFAULT_REPOSITORY.index_exists?).to be(false)
295+
expect(repository.index_exists?).to be(false)
296+
end
297+
end
298+
299+
context 'when the method is called on the class' do
300+
301+
let(:repository) do
302+
DEFAULT_REPOSITORY.class
303+
end
304+
305+
before do
306+
begin; repository.delete_index!; rescue; end
307+
repository.create_index!
308+
end
309+
310+
it 'creates the index' do
311+
expect(repository.index_exists?).to be(true)
263312
end
264313
end
265314
end
@@ -268,12 +317,31 @@ class NoteRepository
268317

269318
context 'when the method is called on an instance' do
270319

320+
let(:repository) do
321+
DEFAULT_REPOSITORY
322+
end
323+
271324
before do
272-
DEFAULT_REPOSITORY.create_index!
325+
repository.create_index!
273326
end
274327

275328
it 'creates the index' do
276-
expect(DEFAULT_REPOSITORY.refresh_index!['_shards']).to be_a(Hash)
329+
expect(repository.refresh_index!['_shards']).to be_a(Hash)
330+
end
331+
end
332+
333+
context 'when the method is called on the class' do
334+
335+
let(:repository) do
336+
DEFAULT_REPOSITORY.class
337+
end
338+
339+
before do
340+
repository.create_index!
341+
end
342+
343+
it 'creates the index' do
344+
expect(repository.refresh_index!['_shards']).to be_a(Hash)
277345
end
278346
end
279347
end
@@ -282,12 +350,31 @@ class NoteRepository
282350

283351
context 'when the method is called on an instance' do
284352

353+
let(:repository) do
354+
DEFAULT_REPOSITORY
355+
end
356+
285357
before do
286-
DEFAULT_REPOSITORY.create_index!
358+
repository.create_index!
287359
end
288360

289361
it 'creates the index' do
290-
expect(DEFAULT_REPOSITORY.index_exists?).to be(true)
362+
expect(repository.index_exists?).to be(true)
363+
end
364+
end
365+
366+
context 'when the method is called on the class' do
367+
368+
let(:repository) do
369+
DEFAULT_REPOSITORY.class
370+
end
371+
372+
before do
373+
repository.create_index!
374+
end
375+
376+
it 'creates the index' do
377+
expect(repository.index_exists?).to be(true)
291378
end
292379
end
293380
end
@@ -303,11 +390,6 @@ class NoteRepository
303390
}
304391
end
305392

306-
it 'allows the value to be set only once' do
307-
NoteRepository.mapping(dynamic: 'strict') { indexes :foo, type: 'long' }
308-
#expect(NoteRepository.mapping.to_hash).to eq('note')
309-
end
310-
311393
it 'sets the value at the class level' do
312394
expect(NoteRepository.mapping.to_hash).to eq(expected_mapping)
313395
end
@@ -343,11 +425,6 @@ class NoteRepository
343425

344426
describe '#settings' do
345427

346-
it 'allows the value to be set only once' do
347-
NoteRepository.settings(number_of_shards: 3)
348-
#expect(NoteRepository.settings.to_hash).to eq()
349-
end
350-
351428
it 'sets the value at the class level' do
352429
expect(NoteRepository.settings.to_hash).to eq(number_of_shards: 1, number_of_replicas: 0)
353430
end

elasticsearch-persistence/spec/spec_helper.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
config.color = true
66
end
77

8-
class NoteRepository
9-
include Elasticsearch::Persistence::Repository
10-
end
11-
128
# The default client to be used by the repositories.
139
#
1410
# @since 6.0.0
1511
DEFAULT_CLIENT = Elasticsearch::Client.new(host: "localhost:#{(ENV['TEST_CLUSTER_PORT'] || 9250)}",
1612
tracer: (ENV['QUIET'] ? nil : ::Logger.new(STDERR)))
1713

18-
# The default client to be used by the repositories.
14+
class MyTestRepository
15+
include Elasticsearch::Persistence::Repository
16+
client DEFAULT_CLIENT
17+
end
18+
19+
# The default repository to be used by tests.
1920
#
2021
# @since 6.0.0
21-
DEFAULT_REPOSITORY = NoteRepository.new(index_name: 'notes', document_type: 'note', client: DEFAULT_CLIENT)
22+
DEFAULT_REPOSITORY = MyTestRepository.new(index_name: 'my_test_repository', document_type: 'test')

0 commit comments

Comments
 (0)