-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split spec/integration_spec.rb
#350
Changes from 1 commit
fc7bbac
c5990b3
c4644e5
4c62567
0994f36
355250e
093bfbc
26e607c
3fda1a7
8504a7a
b0cc102
b9a278c
4663f91
5c7a6e4
55bf813
91ac1af
4cf7ad7
a437c8e
50ec330
702888b
862262b
5deaef9
208c9cb
88bed6f
596a7b4
b328a69
4f1663b
dcc4c08
de4cf44
121b989
84d355b
eefe6be
79fe56a
300b778
f1b4ec3
1d70659
cf8a17f
487c780
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,64 +112,6 @@ | |
end | ||
end | ||
|
||
describe 'Colors' do | ||
before do | ||
Color.clear_index!(true) | ||
Color.delete_all | ||
end | ||
|
||
it 'auto indexes' do | ||
blue = Color.create!(name: 'blue', short_name: 'b', hex: 0xFF0000) | ||
results = Color.search('blue') | ||
expect(results.size).to eq(1) | ||
expect(results).to include(blue) | ||
end | ||
|
||
|
||
it 'is able to temporarily disable auto-indexing' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
Color.without_auto_index do | ||
Color.create!(name: 'blue', short_name: 'b', hex: 0xFF0000) | ||
end | ||
expect(Color.search('blue').size).to eq(0) | ||
Color.reindex!(MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, true) | ||
expect(Color.search('blue').size).to eq(1) | ||
end | ||
|
||
it 'updates the index if the attribute changed' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
purple = Color.create!(name: 'purple', short_name: 'p') | ||
expect(Color.search('purple').size).to eq(1) | ||
expect(Color.search('pink').size).to eq(0) | ||
purple.name = 'pink' | ||
purple.save | ||
expect(Color.search('purple').size).to eq(0) | ||
expect(Color.search('pink').size).to eq(1) | ||
end | ||
|
||
it 'uses the specified scope' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
Color.create!(name: 'red', short_name: 'r3', hex: 3) | ||
Color.create!(name: 'red', short_name: 'r1', hex: 1) | ||
Color.create!(name: 'red', short_name: 'r2', hex: 2) | ||
Color.create!(name: 'purple', short_name: 'p') | ||
Color.clear_index!(true) | ||
Color.where(name: 'red').reindex!(MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, true) | ||
expect(Color.search('').size).to eq(3) | ||
Color.clear_index!(true) | ||
Color.where(id: Color.first.id).reindex!(MeiliSearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, true) | ||
expect(Color.search('').size).to eq(1) | ||
end | ||
|
||
it 'indexes an array of documents' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
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 | ||
|
||
it 'does not index non-saved document' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
expect { Color.new(name: 'purple').index!(true) }.to raise_error(ArgumentError) | ||
expect { Color.new(name: 'purple').remove_from_index!(true) }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
describe 'An imaginary store' do | ||
before(:all) do | ||
Product.clear_index!(true) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'support/models/color' | ||
require 'support/models/book' | ||
|
||
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 '.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 | ||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,10 @@ def will_save_change_to_short_name? | |
false | ||
end | ||
end | ||
|
||
module TestUtil | ||
def self.reset_colors! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same rationale as |
||
Color.clear_index!(true) | ||
Color.delete_all | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already tested in
options_spec.rb
under:auto_index