-
Notifications
You must be signed in to change notification settings - Fork 54
Split spec/integration_spec.rb
#350
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
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| require 'support/models/book' | ||
|
|
||
| describe 'Instance methods' do | ||
| describe '#ms_entries' do | ||
| it 'includes conditionally enabled indexes' do | ||
| book = Book.create!( | ||
| name: 'Frankenstein', author: 'Mary Shelley', | ||
| premium: false, released: true | ||
| ) | ||
|
|
||
| expect(book.ms_entries).to contain_exactly( | ||
| a_hash_including("index_uid" => safe_index_uid('SecuredBook')), | ||
| a_hash_including("index_uid" => safe_index_uid('BookAuthor')), | ||
| a_hash_including("index_uid" => safe_index_uid('Book')), | ||
| ) | ||
| end | ||
|
|
||
| it 'includes conditionally disabled indexes' do | ||
| # non public book | ||
| book = Book.create!( | ||
| name: 'Frankenstein', author: 'Mary Shelley', | ||
| premium: false, released: false | ||
| ) | ||
|
|
||
| expect(book.ms_entries).to contain_exactly( | ||
| a_hash_including("index_uid" => safe_index_uid('SecuredBook')), | ||
| a_hash_including("index_uid" => safe_index_uid('BookAuthor')), | ||
| # also includes book's id as if it was a public book | ||
| a_hash_including("index_uid" => safe_index_uid('Book')), | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| describe '#ms_index!' do | ||
| it 'returns array of tasks' do | ||
| TestUtil.reset_books! | ||
|
|
||
| moby_dick = Book.create! name: 'Moby Dick', author: 'Herman Melville', premium: false, released: true | ||
|
|
||
| tasks = moby_dick.ms_index! | ||
|
|
||
| expect(tasks).to contain_exactly( | ||
| a_hash_including('uid'), | ||
| a_hash_including('taskUid'), | ||
| a_hash_including('taskUid') | ||
| ) | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| require 'support/models/book' | ||
|
|
||
| describe 'When record is updated' do | ||
| it 'automatically removes document from conditional indexes' do | ||
| TestUtil.reset_books! | ||
|
|
||
| # add a new public book which is public (not premium but released) | ||
| book = Book.create! name: 'Public book', author: 'me', premium: false, released: true | ||
|
|
||
| # should be searchable in the 'Book' index | ||
| index = Book.index(safe_index_uid('Book')) | ||
| results = index.search('Public book') | ||
| expect(results['hits']).to be_one | ||
|
|
||
| # update the book and make it non-public anymore (not premium, not released) | ||
| book.update released: false | ||
|
|
||
| # should be removed from the index | ||
| results = index.search('Public book') | ||
| expect(results['hits']).to be_empty | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,10 +158,6 @@ | |
| expect(Color.search('').size).to eq(1) | ||
| end | ||
|
|
||
| it 'has a Rails env-based index name' do | ||
| expect(Color.index_uid).to eq(safe_index_uid('Color') + "_#{Rails.env}") | ||
| 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 | ||
|
|
@@ -359,167 +355,6 @@ | |
| end | ||
| end | ||
|
|
||
| describe 'Book' do | ||
| before do | ||
| Book.clear_index!(true) | ||
| Book.index(safe_index_uid('BookAuthor')).delete_all_documents | ||
| Book.index(safe_index_uid('Book')).delete_all_documents | ||
| end | ||
|
|
||
| it 'returns array of tasks on #ms_index!' 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 |
||
| moby_dick = Book.create! name: 'Moby Dick', author: 'Herman Melville', premium: false, released: true | ||
|
|
||
| tasks = moby_dick.ms_index! | ||
|
|
||
| expect(tasks).to contain_exactly( | ||
| a_hash_including('uid'), | ||
| a_hash_including('taskUid'), | ||
| a_hash_including('taskUid') | ||
| ) | ||
| end | ||
|
|
||
| it 'indexes the book in 2 indexes of 3' 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 |
||
| steve_jobs = Book.create! name: 'Steve Jobs', author: 'Walter Isaacson', premium: true, released: true | ||
| results = Book.search('steve') | ||
| expect(results.size).to eq(1) | ||
| expect(results).to include(steve_jobs) | ||
|
|
||
| index_author = Book.index(safe_index_uid('BookAuthor')) | ||
| expect(index_author).not_to be_nil | ||
| results = index_author.search('steve') | ||
| expect(results['hits'].length).to eq(0) | ||
| results = index_author.search('walter') | ||
| expect(results['hits'].length).to eq(1) | ||
|
|
||
| # premium -> not part of the public index | ||
| index_book = Book.index(safe_index_uid('Book')) | ||
| expect(index_book).not_to be_nil | ||
| results = index_book.search('steve') | ||
| expect(results['hits'].length).to eq(0) | ||
| end | ||
|
|
||
| it 'sanitizes attributes' 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 |
||
| _hack = Book.create! name: '"><img src=x onerror=alert(1)> hack0r', | ||
| author: '<script type="text/javascript">alert(1)</script>', premium: true, released: true | ||
| b = Book.raw_search('hack', { attributes_to_highlight: ['*'] }) | ||
| expect(b['hits'].length).to eq(1) | ||
| begin | ||
| expect(b['hits'][0]['name']).to eq('"> hack0r').and_raise(StandardError) | ||
| expect(b['hits'][0]['author']).to eq('alert(1)') | ||
| expect(b['hits'][0]['_formatted']['name']).to eq('"> <em>hack</em>0r') | ||
| rescue StandardError | ||
| # rails 4.2's sanitizer | ||
|
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. Kept only the last sanitization format. |
||
| begin | ||
| expect(b['hits'][0]['name']).to eq('"> hack0r').and_raise(StandardError) | ||
| expect(b['hits'][0]['author']).to eq('') | ||
| expect(b['hits'][0]['_formatted']['name']).to eq('"> <em>hack</em>0r') | ||
| rescue StandardError | ||
| # jruby | ||
| expect(b['hits'][0]['name']).to eq('"> hack0r') | ||
| expect(b['hits'][0]['author']).to eq('') | ||
| expect(b['hits'][0]['_formatted']['name']).to eq('"> <em>hack</em>0r') | ||
| end | ||
| end | ||
| end | ||
|
|
||
| it 'handles removal in an extra index' 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 |
||
| # add a new public book which (not premium but released) | ||
| book = Book.create! name: 'Public book', author: 'me', premium: false, released: true | ||
|
|
||
| # should be searchable in the 'Book' index | ||
| index = Book.index(safe_index_uid('Book')) | ||
| results = index.search('Public book') | ||
| expect(results['hits'].size).to eq(1) | ||
|
|
||
| # update the book and make it non-public anymore (not premium, not released) | ||
| book.update released: false | ||
|
|
||
| # should be removed from the index | ||
| results = index.search('Public book') | ||
| expect(results['hits'].size).to eq(0) | ||
| end | ||
|
|
||
| it 'uses the per_environment option in the additional index as well' 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 |
||
| index = Book.index(safe_index_uid('Book')) | ||
| expect(index.uid).to eq("#{safe_index_uid('Book')}_#{Rails.env}") | ||
| end | ||
|
|
||
| it 'searches with one typo min size' 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 |
||
| Book.create! name: 'The Lord of the Rings', author: 'me', premium: false, released: true | ||
| results = Book.search('Lrod') | ||
| expect(results.size).to eq(0) | ||
|
|
||
| results = Book.search('Rnigs') | ||
| expect(results.size).to eq(1) | ||
| end | ||
|
|
||
| it 'searches with two typo min size' do | ||
| Book.create! name: 'Dracula', author: 'me', premium: false, released: true | ||
| results = Book.search('Darclua') | ||
| expect(results.size).to eq(0) | ||
|
|
||
| Book.create! name: 'Frankenstein', author: 'me', premium: false, released: true | ||
| results = Book.search('Farnkenstien') | ||
| expect(results.size).to eq(1) | ||
| end | ||
|
|
||
| describe '#ms_entries' 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 |
||
| it 'returns all 3 indexes for a public book' do | ||
| book = Book.create!( | ||
| name: 'Frankenstein', author: 'Mary Shelley', | ||
| premium: false, released: true | ||
| ) | ||
|
|
||
| expect(book.ms_entries).to contain_exactly( | ||
| a_hash_including("index_uid" => safe_index_uid('SecuredBook')), | ||
| a_hash_including("index_uid" => safe_index_uid('BookAuthor')), | ||
| a_hash_including("index_uid" => safe_index_uid('Book')), | ||
| ) | ||
| end | ||
|
|
||
| it 'returns all 3 indexes for a non-public book' do | ||
| book = Book.create!( | ||
| name: 'Frankenstein', author: 'Mary Shelley', | ||
| premium: false, released: false | ||
| ) | ||
|
|
||
| expect(book.ms_entries).to contain_exactly( | ||
| a_hash_including("index_uid" => safe_index_uid('SecuredBook')), | ||
| a_hash_including("index_uid" => safe_index_uid('BookAuthor')), | ||
| a_hash_including("index_uid" => safe_index_uid('Book')), | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| it 'returns facets using max values per facet' 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 |
||
| 10.times do | ||
| Book.create! name: Faker::Book.title, author: Faker::Book.author, genre: Faker::Book.genre | ||
| end | ||
|
|
||
| genres = Book.distinct.pluck(:genre) | ||
|
|
||
| results = Book.search('', { facets: ['genre'] }) | ||
|
|
||
| expect(genres.size).to be > 3 | ||
| expect(results.facets_distribution['genre'].size).to eq(3) | ||
| end | ||
|
|
||
| it 'does not error on facet_search' 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 new |
||
| genres = %w[Legend Fiction Crime].cycle | ||
| authors = %w[A B C].cycle | ||
|
|
||
| 5.times do | ||
| Book.create! name: Faker::Book.title, author: authors.next, genre: genres.next | ||
| end | ||
|
|
||
| expect do | ||
| Book.index.facet_search('genre', 'Fic', filter: 'author = A') | ||
| Book.index.facet_search('genre', filter: 'author = A') | ||
| Book.index.facet_search('genre') | ||
| end.not_to raise_error | ||
| end | ||
| end | ||
|
|
||
| describe 'Movie' do | ||
| before(:all) do | ||
| Movie.clear_index!(true) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| require 'support/models/color' | ||
| require 'support/models/book' | ||
|
|
||
| describe 'meilisearch_options' do | ||
| describe ':if' do | ||
| it 'only indexes the record in the valid indexes' do | ||
| TestUtil.reset_books! | ||
|
|
||
| Book.create! name: 'Steve Jobs', author: 'Walter Isaacson', | ||
| premium: true, released: true | ||
|
|
||
| results = Book.search('steve') | ||
| expect(results).to be_one | ||
|
|
||
| results = Book.index(safe_index_uid('BookAuthor')).search('walter') | ||
| expect(results['hits']).to be_one | ||
|
|
||
| # premium -> not part of the public index | ||
| results = Book.index(safe_index_uid('Book')).search('steve') | ||
| expect(results['hits']).to be_empty | ||
| end | ||
| end | ||
|
|
||
| describe ':auto_index' do | ||
| it 'auto indexes by default' do | ||
| Color.clear_index!(true) | ||
| Color.delete_all | ||
|
|
||
| Color.create!(name: 'blue', short_name: 'b', hex: 0xFF0000) | ||
| results = Color.raw_search('blue') | ||
| expect(results['hits'].size).to eq(1) | ||
| expect(results['estimatedTotalHits']).to eq(1) | ||
| end | ||
| end | ||
|
|
||
| describe ':sanitize' do | ||
| context 'when true' do | ||
| it 'sanitizes attributes' do | ||
| TestUtil.reset_books! | ||
|
|
||
| Book.create! name: '"><img src=x onerror=alert(1)> hack0r', | ||
| author: '<script type="text/javascript">alert(1)</script>', premium: true, released: true | ||
|
|
||
| b = Book.raw_search('hack') | ||
|
|
||
| expect(b['hits'][0]).to include( | ||
| 'name' => '"> hack0r', | ||
| 'author' => '', | ||
| ) | ||
| end | ||
|
|
||
| it 'keeps _formatted emphasis' do | ||
| TestUtil.reset_books! | ||
|
|
||
| Book.create! name: '"><img src=x onerror=alert(1)> hack0r', | ||
| author: '<script type="text/javascript">alert(1)</script>', premium: true, released: true | ||
|
|
||
| b = Book.raw_search('hack', { attributes_to_highlight: ['*'] }) | ||
|
|
||
| expect(b['hits'][0]['_formatted']).to include( | ||
| 'name' => '"> <em>hack</em>0r', | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| require 'support/models/book' | ||
|
|
||
| describe MeiliSearch::Rails::SafeIndex do | ||
| describe '#facet_search' do | ||
| it 'accepts all params without error' do | ||
| TestUtil.reset_books! | ||
|
|
||
| genres = %w[Legend Fiction Crime].cycle | ||
| authors = %w[A B C].cycle | ||
|
|
||
| 5.times do | ||
| Book.create! name: Faker::Book.title, author: authors.next, genre: genres.next | ||
| end | ||
|
|
||
| expect do | ||
| Book.index.facet_search('genre', 'Fic', filter: 'author = A') | ||
| Book.index.facet_search('genre', filter: 'author = A') | ||
| Book.index.facet_search('genre') | ||
| end.not_to raise_error | ||
| end | ||
| 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.
Moved to
configuration_spec.rb, seemed more appropriate.