forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_class_test.rb
85 lines (63 loc) · 2.41 KB
/
custom_class_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
require 'test_helper'
module Elasticsearch
module Persistence
class RepositoryCustomClassIntegrationTest < Elasticsearch::Test::IntegrationTestCase
class ::MyNote
attr_reader :attributes
def initialize(attributes={})
@attributes = Hashie::Mash.new(attributes)
end
def method_missing(method_name, *arguments, &block)
attributes.respond_to?(method_name) ? attributes.__send__(method_name, *arguments, &block) : super
end
def respond_to?(method_name, include_private=false)
attributes.respond_to?(method_name) || super
end
def to_hash
@attributes
end
end
context "A custom repository class" do
setup do
class ::MyNotesRepository
include Elasticsearch::Persistence::Repository
klass MyNote
settings number_of_shards: 1 do
mapping do
indexes :title, analyzer: 'snowball'
end
end
create_index!
def deserialize(document)
klass.new document.merge(document['_source'])
end
end
@repository = MyNotesRepository.new
@repository.client.cluster.health wait_for_status: 'yellow'
end
should "save the object under a correct index and type" do
@repository.save MyNote.new(id: '1', title: 'Test')
result = @repository.find(1)
assert_instance_of MyNote, result
assert_equal 'Test', result.title
assert_not_nil Elasticsearch::Persistence.client.get index: 'my_notes_repository',
type: 'my_note',
id: '1'
end
should "delete the object" do
note = MyNote.new id: 1, title: 'Test'
@repository.save note
assert_not_nil @repository.find(1)
@repository.delete(note)
assert_raise(Elasticsearch::Persistence::Repository::DocumentNotFound) { @repository.find(1) }
end
should "retrieve the object via a search query" do
note = MyNote.new title: 'Testing'
@repository.save note, refresh: true
results = @repository.search query: { match: { title: 'Test' } }
assert_equal 'Testing', results.first.title
end
end
end
end
end