Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fc7bbac
Remove redundant Ebook model in specs
ellnix Feb 28, 2024
c5990b3
Remove test of ruby's Marshal
ellnix Feb 29, 2024
c4644e5
Remove test of Rails cache functionality
ellnix Feb 29, 2024
4c62567
Remove unused UniqUser class from specs
ellnix Feb 29, 2024
0994f36
Remove unused NullableId class from specs
ellnix Feb 29, 2024
355250e
Remove flawed MongoDocument specs
ellnix Feb 29, 2024
093bfbc
Remove unnecessary Color tests
ellnix Feb 29, 2024
26e607c
Move Post specs
ellnix Feb 29, 2024
3fda1a7
Move Color search specs
ellnix Feb 29, 2024
8504a7a
Move Book specs from integration_spec.rb
ellnix Mar 1, 2024
b0cc102
Move Color specs from integration_spec.rb
ellnix Mar 1, 2024
b9a278c
Move Cat and Doc specs from integration_spec.rb
ellnix Mar 1, 2024
4663f91
Move People specs from integration_spec.rb
ellnix Mar 1, 2024
5c7a6e4
Move Disabled specs from integration_spec.rb
ellnix Mar 1, 2024
55bf813
Move Movie, Restaurant, and pagination specs
ellnix May 25, 2024
91ac1af
`require --spec-helper` in .rspec
ellnix May 25, 2024
4cf7ad7
Move 'an imaginary store' tests
ellnix May 25, 2024
a437c8e
Refactor tests
ellnix May 25, 2024
50ec330
Move SerializedDocument and EncodedString tests
ellnix May 26, 2024
702888b
Move NamespacedDocument specs
ellnix May 26, 2024
862262b
Remove redundant `#search` method test
ellnix May 26, 2024
5deaef9
Move deactivation specs
ellnix May 27, 2024
208c9cb
Move :raise_on_failure specs
ellnix May 27, 2024
88bed6f
Fix a few more race conditions in tests
ellnix May 27, 2024
596a7b4
Move EnqueuedDocument-type tests
ellnix May 27, 2024
b328a69
Move Song specs
ellnix May 27, 2024
4f1663b
Move NestedItem tests
ellnix May 27, 2024
dcc4c08
Move misconfiguration test
ellnix May 27, 2024
de4cf44
Fix searchable attribute warning and its test
ellnix May 27, 2024
121b989
Fix regression in #ms_entries specs
ellnix May 27, 2024
84d355b
Merge test refactoring into main
ellnix May 27, 2024
eefe6be
Refactor proximity_precision test
ellnix May 27, 2024
79fe56a
Remove integration spec file & private method test
ellnix May 27, 2024
300b778
Hide ActiveRecord's create_table messages
ellnix May 27, 2024
f1b4ec3
Make rubocop happy
ellnix May 27, 2024
1d70659
Merge main again due to rubocop and integration_spec
ellnix May 27, 2024
cf8a17f
Fix will_paginate test race condition
ellnix May 27, 2024
487c780
Merge branch 'main' into refactor-integration-spec
brunoocasali Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'spec_helper'
require 'support/models/book'
require 'support/models/color'

describe MeiliSearch::Rails::Configuration do
before { stub_const('MeiliSearch::Rails::VERSION', '0.0.1') }
Expand Down Expand Up @@ -63,6 +65,20 @@
end
end

context 'with per_environment' do
# per_environment is already enabled in testing
# no setup is required

it 'adds a Rails env-based index suffix' do
expect(Color.index_uid).to eq(safe_index_uid('Color') + "_#{Rails.env}")
end

it 'uses suffix in the additional index as well' do
index = Book.index(safe_index_uid('Book'))
expect(index.uid).to eq("#{safe_index_uid('Book')}_#{Rails.env}")
end
end

context 'when use Meilisearch without configuration' do
around do |example|
config = MeiliSearch::Rails.configuration
Expand Down
49 changes: 49 additions & 0 deletions spec/instance_methods_spec.rb
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
22 changes: 22 additions & 0 deletions spec/integration/active_record/record_is_updated_spec.rb
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
165 changes: 0 additions & 165 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@
expect(Color.search('').size).to eq(1)
end

it 'has a Rails env-based index name' do
Copy link
Contributor Author

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.

expect(Color.index_uid).to eq(safe_index_uid('Color') + "_#{Rails.env}")
end

it 'indexes an array of documents' do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to model_methods_spec.rb

json = Color.raw_search('')
Color.index_documents Color.limit(1), true # reindex last color, `limit` is incompatible with the reindex! method
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to instance_methods_spec.rb since it concerns the interface (return type) of one of the instance methods we add to models.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to options_spec.rb since it tests whether or not the :if option is behaving correctly.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to options_spec.rb since it tests whether or not the :sanitize option is behaving correctly.

_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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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('&quot;&gt; hack0r').and_raise(StandardError)
expect(b['hits'][0]['author']).to eq('')
expect(b['hits'][0]['_formatted']['name']).to eq('&quot;&gt; <em>hack</em>0r')
rescue StandardError
# jruby
expect(b['hits'][0]['name']).to eq('"&gt; hack0r')
expect(b['hits'][0]['author']).to eq('')
expect(b['hits'][0]['_formatted']['name']).to eq('"&gt; <em>hack</em>0r')
end
end
end

it 'handles removal in an extra index' do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to record_is_updated_spec.rb since it tests a callback on an event (record being updated)

# 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
Copy link
Contributor Author

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

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to settings_spec.rb since it tests the typo_tolerance setting

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to instance_methods_spec.rb

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to settings_spec.rb

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to new safe_index_spec.rb since this is a test on whether SafeIndex forwards method arguments correctly

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)
Expand Down
66 changes: 66 additions & 0 deletions spec/options_spec.rb
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' => '"&gt; 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' => '"&gt; <em>hack</em>0r',
)
end
end
end
end
22 changes: 22 additions & 0 deletions spec/safe_index_spec.rb
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
1 change: 0 additions & 1 deletion spec/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
)
end


it 'results include #formatted object' do
Color.create!(name: 'green', short_name: 'b', hex: 0xFF0000)
results = Color.search('gre')
Expand Down
Loading