forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_basic_test.rb
157 lines (114 loc) · 4.63 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
require 'test_helper'
require 'elasticsearch/persistence/model'
module Elasticsearch
module Persistence
class PersistenceModelBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
class ::Person
include Elasticsearch::Persistence::Model
settings index: { number_of_shards: 1 }
attribute :name, String,
mapping: { fields: {
name: { type: 'string', analyzer: 'snowball' },
raw: { type: 'string', analyzer: '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')
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: 'person', id: person.id
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 "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 "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