-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmodel_methods_spec.rb
105 lines (82 loc) · 2.97 KB
/
model_methods_spec.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
require 'support/async_helper'
require 'support/models/color'
require 'support/models/book'
require 'support/models/people'
describe 'Model methods' do
describe '.reindex!' do
it 'uses the specified scope' do
TestUtil.reset_colors!
Color.create!(name: 'red', short_name: 'r3', hex: 3)
Color.create!(name: 'red', short_name: 'r1', hex: 1)
Color.create!(name: 'purple', short_name: 'p')
Color.clear_index!(true)
Color.where(name: 'red').reindex!(3, true)
expect(Color.search('').size).to eq(2)
Color.clear_index!(true)
Color.where(id: Color.first.id).reindex!(3, true)
expect(Color.search('').size).to eq(1)
end
end
describe '.clear_index!' do
context 'when :auto_remove is disabled' do
it 'clears index manually' do
TestUtil.reset_people!
People.create(first_name: 'Jane', last_name: 'Doe', card_number: 75_801_887)
AsyncHelper.await_last_task
results = People.raw_search('')
expect(results['hits']).not_to be_empty
People.clear_index!(true)
results = People.raw_search('')
expect(results['hits']).to be_empty
end
end
end
describe '.without_auto_index' do
it 'disables auto indexing for the model' do
TestUtil.reset_colors!
Color.without_auto_index do
Color.create!(name: 'blue', short_name: 'b', hex: 0xFF0000)
end
expect(Color.search('blue')).to be_empty
Color.reindex!(2, true)
expect(Color.search('blue')).to be_one
end
it 'does not disable auto indexing for other models' do
TestUtil.reset_books!
Color.without_auto_index do
Book.create!(
name: 'Frankenstein', author: 'Mary Shelley',
premium: false, released: true
)
end
expect(Book.search('Frankenstein')).to be_one
end
end
# while this is not a model method, it's tested here since it's logically similar
# to the model method by the same name
describe 'Meilisearch::Rails.without_auto_index' do
it 'disables auto indexing for all models' do
TestUtil.reset_colors!
TestUtil.reset_books!
MeiliSearch::Rails.without_auto_index do
Color.create!(name: 'blue', short_name: 'b', hex: 0xFF0000)
Book.create!(
name: 'Frankenstein', author: 'Mary Shelley',
premium: false, released: true
)
end
expect(Color.search('blue')).to be_empty
expect(Book.search('Frankenstein')).to be_empty
end
end
describe '.index_documents' do
it 'updates existing documents' do
TestUtil.reset_colors!
_blue = Color.create!(name: 'blue', short_name: 'blu', hex: 0x0000FF)
_black = Color.create!(name: 'black', short_name: 'bla', hex: 0x000000)
json = Color.raw_search('')
Color.index_documents Color.limit(1), true # reindex last color, `limit` is incompatible with the reindex! method
expect(json['hits'].count).to eq(Color.raw_search('')['hits'].count)
end
end
end