forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_basic_test.rb
233 lines (172 loc) · 7.1 KB
/
model_basic_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require 'test_helper'
require 'elasticsearch/persistence/model'
require 'elasticsearch/persistence/model/rails'
module Elasticsearch
module Persistence
class PersistenceModelBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
class ::Person
include Elasticsearch::Persistence::Model
include Elasticsearch::Persistence::Model::Rails
settings index: { number_of_shards: 1 }
document_type 'human_being'
attribute :name, String,
mapping: { fields: {
name: { type: 'text', analyzer: 'snowball' },
raw: { type: 'keyword' }
} }
attribute :birthday, Date
attribute :department, String
attribute :salary, Integer
attribute :admin, Boolean, default: false
validates :name, presence: true
end
context "A basic persistence model" do
setup do
Person.create_index! force: true
end
should "save the object with custom ID" do
person = Person.new id: 1, name: 'Number One'
person.save
document = Person.find(1)
assert_not_nil document
assert_equal 'Number One', document.name
end
should "create the object with custom ID" do
person = Person.create id: 1, name: 'Number One'
document = Person.find(1)
assert_not_nil document
assert_equal 'Number One', document.name
end
should "save and find the object" do
person = Person.new name: 'John Smith', birthday: Date.parse('1970-01-01')
assert person.save
assert_not_nil person.id
document = Person.find(person.id)
assert_instance_of Person, document
assert_equal 'John Smith', document.name
assert_equal 'John Smith', Person.find(person.id).name
assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'human_being', id: person.id
end
should "not save an invalid object" do
person = Person.new name: nil
assert ! person.save
end
should "save an invalid object with the :validate option" do
person = Person.new name: nil, salary: 100
assert person.save validate: false
assert_not_nil person.id
document = Person.find(person.id)
assert_equal 100, document.salary
end
should "delete the object" do
person = Person.create name: 'John Smith', birthday: Date.parse('1970-01-01')
person.destroy
assert person.frozen?
assert_raise Elasticsearch::Transport::Transport::Errors::NotFound do
Elasticsearch::Persistence.client.get index: 'people', type: 'person', id: person.id
end
end
should "update an object attribute" do
person = Person.create name: 'John Smith'
person.update name: 'UPDATED'
assert_equal 'UPDATED', person.name
assert_equal 'UPDATED', Person.find(person.id).name
end
should "create the model with correct Date form Rails' form attributes" do
params = { "birthday(1i)"=>"2014",
"birthday(2i)"=>"1",
"birthday(3i)"=>"1"
}
person = Person.create params.merge(name: 'TEST')
assert_equal Date.parse('2014-01-01'), person.birthday
assert_equal Date.parse('2014-01-01'), Person.find(person.id).birthday
end
should_eventually "update the model with correct Date form Rails' form attributes" do
params = { "birthday(1i)"=>"2014",
"birthday(2i)"=>"1",
"birthday(3i)"=>"1"
}
person = Person.create params.merge(name: 'TEST')
person.update params.merge('birthday(1i)' => '2015')
assert_equal Date.parse('2015-01-01'), person.birthday
assert_equal Date.parse('2015-01-01'), Person.find(person.id).birthday
end
should "increment an object attribute" do
person = Person.create name: 'John Smith', salary: 1_000
person.increment :salary
assert_equal 1_001, person.salary
assert_equal 1_001, Person.find(person.id).salary
end
should "update the object timestamp" do
person = Person.create name: 'John Smith'
updated_at = person.updated_at
sleep 1
person.touch
assert person.updated_at > updated_at, [person.updated_at, updated_at].inspect
found = Person.find(person.id)
assert found.updated_at > updated_at, [found.updated_at, updated_at].inspect
end
should 'update the object timestamp on save' do
person = Person.create name: 'John Smith'
person.admin = true
sleep 1
person.save
Person.gateway.refresh_index!
found = Person.find(person.id)
# Compare without milliseconds
assert_equal person.updated_at.to_i, found.updated_at.to_i
end
should "respect the version" do
person = Person.create name: 'John Smith'
person.update( { name: 'UPDATE 1' })
assert_raise Elasticsearch::Transport::Transport::Errors::Conflict do
person.update( { name: 'UPDATE 2' }, { version: 1 } )
end
end
should "find all instances" do
Person.create name: 'John Smith'
Person.create name: 'Mary Smith'
Person.gateway.refresh_index!
people = Person.all
assert_equal 2, people.total
assert_equal 2, people.size
end
should "find instances by search" do
Person.create name: 'John Smith'
Person.create name: 'Mary Smith'
Person.gateway.refresh_index!
people = Person.search query: { match: { name: 'smith' } },
highlight: { fields: { name: {} } }
assert_equal 2, people.total
assert_equal 2, people.size
assert people.map_with_hit { |o,h| h._score }.all? { |s| s > 0 }
assert_not_nil people.first.hit
assert_match /smith/i, people.first.hit.highlight['name'].first
end
should "find instances in batches" do
50.times { |i| Person.create name: "John #{i+1}" }
Person.gateway.refresh_index!
@batches = 0
@results = []
Person.find_in_batches(_source_include: 'name') do |batch|
@batches += 1
@results += batch.map(&:name)
end
assert_equal 3, @batches
assert_equal 50, @results.size
assert_contains @results, 'John 1'
end
should "find each instance" do
50.times { |i| Person.create name: "John #{i+1}" }
Person.gateway.refresh_index!
@results = []
Person.find_each(_source_include: 'name') do |person|
@results << person.name
end
assert_equal 50, @results.size
assert_contains @results, 'John 1'
end
end
end
end
end