Skip to content
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

Merged
merged 38 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--color
--require spec_helper
88 changes: 38 additions & 50 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-04-08 13:44:25 UTC using RuboCop version 1.27.0.
# on 2024-05-27 18:22:30 UTC using RuboCop version 1.27.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Bundler/OrderedGems:
Exclude:
- 'Gemfile'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequireMFA:
Exclude:
- 'meilisearch-rails.gemspec'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand All @@ -30,13 +14,12 @@ Layout/EmptyLinesAroundModuleBody:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 3
# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: symmetrical, new_line, same_line
Layout/MultilineMethodCallBraceLayout:
Exclude:
- 'spec/integration_spec.rb'
- 'spec/ms_clean_up_job_spec.rb'

# Offense count: 1
Expand Down Expand Up @@ -70,7 +53,7 @@ Lint/UnusedMethodArgument:
# Offense count: 12
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 104
Max: 103

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Expand All @@ -81,7 +64,7 @@ Metrics/BlockLength:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 157
Max: 169

# Offense count: 8
# Configuration parameters: IgnoredMethods.
Expand All @@ -91,12 +74,12 @@ Metrics/CyclomaticComplexity:
# Offense count: 18
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 103
Max: 102

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 449
Max: 437

# Offense count: 8
# Configuration parameters: IgnoredMethods.
Expand All @@ -123,47 +106,61 @@ Naming/MethodParameterName:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 20
# Offense count: 5
RSpec/BeforeAfterAll:
Exclude:
- 'spec/integration_spec.rb'
- 'spec/integration/active_record/record_has_associations_spec.rb'
- 'spec/pagination/kaminari_spec.rb'
- 'spec/pagination/will_paginate_spec.rb'
- 'spec/settings_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 2
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
- 'spec/options_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 56
# Offense count: 54
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 19
Max: 16

# Offense count: 3
# Offense count: 6
# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
# Include: **/*_spec*rb*, **/spec/**/*
RSpec/FilePath:
Exclude:
- 'spec/configuration_spec.rb'
- 'spec/integration/active_record/meilisearch_calls_are_deactivated.rb'
- 'spec/meilisearch/activation_spec.rb'
- 'spec/safe_index_spec.rb'
- 'spec/settings_spec.rb'
- 'spec/utilities_spec.rb'

# Offense count: 25
# Offense count: 29
# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Exclude:
- 'spec/integration_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 1
RSpec/MultipleDescribes:
Exclude:
- 'spec/integration_spec.rb'
- 'spec/search_spec.rb'

# Offense count: 2
RSpec/NestedGroups:
Max: 4

# Offense count: 1
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Exclude:
- 'spec/configuration_spec.rb'

# Offense count: 1
Security/MarshalLoad:
Exclude:
- 'spec/integration_spec.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand All @@ -172,13 +169,13 @@ Style/Alias:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 2
# Offense count: 3
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 9
# Offense count: 8
# This cop supports safe auto-correction (--auto-correct).
Style/IfUnlessModifier:
Exclude:
Expand Down Expand Up @@ -212,26 +209,17 @@ Style/OptionalBooleanParameter:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 11
# Offense count: 5
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Exclude:
- 'spec/integration_spec.rb'
- 'spec/ms_clean_up_job_spec.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyleForMultiline.
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
Style/TrailingCommaInArguments:
Exclude:
- 'spec/integration_spec.rb'

# Offense count: 20
# Offense count: 16
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 178
Max: 170
29 changes: 14 additions & 15 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ class IndexSettings
def initialize(options, &block)
@options = options
instance_exec(&block) if block_given?
warn_searchable_missing_attributes
end

def warn_searchable_missing_attributes
searchables = get_setting(:searchable_attributes)
attrs = get_setting(:attributes)&.keys

if searchables.present? && attrs.present?
(searchables.map(&:to_s) - attrs.map(&:to_s)).each do |missing_searchable|
MeiliSearch::Rails.logger.warn(
"[meilisearch-rails] #{missing_searchable} declared in searchable_attributes but not in attributes. Please add it to attributes if it should be searchable."
)
end
end
end

def use_serializer(serializer)
Expand Down Expand Up @@ -464,8 +478,6 @@ def meilisearch(options = {}, &block)
after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
end
end

warn_searchable_missing_attributes
end

def ms_without_auto_index(&block)
Expand Down Expand Up @@ -901,19 +913,6 @@ def ms_attribute_changed?(document, attr_name)
# We don't know if the attribute has changed, so conservatively assume it has
true
end

def warn_searchable_missing_attributes
searchables = meilisearch_settings.get_setting(:searchable_attributes)
attrs = meilisearch_settings.get_setting(:attributes)&.keys

if searchables.present? && attrs.present?
(searchables.map(&:to_s) - attrs.map(&:to_s)).each do |missing_searchable|
MeiliSearch::Rails.logger.warn(
"[meilisearch-rails] #{name}##{missing_searchable} declared in searchable_attributes but not in attributes. Please add it to attributes if it should be searchable."
)
end
end
end
end

# these are the instance methods included
Expand Down
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
110 changes: 110 additions & 0 deletions spec/instance_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require 'support/models/book'
require 'support/models/animals'
require 'support/models/people'
require 'support/models/movie'
require 'support/models/queued_models'

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' => /SecuredBook/),
a_hash_including('index_uid' => /BookAuthor/),
a_hash_including('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' => /SecuredBook/),
a_hash_including('index_uid' => /BookAuthor/),
# also includes book's id as if it was a public book
a_hash_including('index_uid' => /Book/)
)
end

context 'when models share an index' do
it 'does not return instances of other models' do
TestUtil.reset_animals!

toby_dog = Dog.create!(name: 'Toby the Dog')
taby_cat = Cat.create!(name: 'Taby the Cat')

expect(toby_dog.ms_entries).to contain_exactly(
a_hash_including('primary_key' => /dog_\d+/)
)

expect(taby_cat.ms_entries).to contain_exactly(
a_hash_including('primary_key' => /cat_\d+/)
)
end
end
end

describe '#ms_index!' do
it 'returns array with single task with single index' do
TestUtil.reset_movies!

task = Movie.create(title: 'Harry Potter').ms_index!

expect(task).to contain_exactly(a_hash_including('taskUid'))
end

it 'returns array of tasks with multiple indexes' 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

it 'throws error on non-persisted instances' do
expect { Color.new(name: 'purple').index!(true) }.to raise_error(ArgumentError)
end

it 'returns empty array when indexing is disabled' do
doc = DisabledEnqueuedDocument.create! name: 'test'

expect(doc.ms_index!).to be_empty
end
end

describe '#ms_remove_from_index!' do
it 'throws error on non-persisted instances' do
expect { Color.new(name: 'purple').remove_from_index!(true) }.to raise_error(ArgumentError)
end

context 'when :auto_remove is disabled' do
it 'is able to remove manually' do
TestUtil.reset_people!

bob = People.create(first_name: 'Bob', last_name: 'Sponge', card_number: 75_801_889)

result = People.raw_search('Bob')
expect(result['hits']).to be_one

bob.remove_from_index!

result = People.raw_search('Bob')
expect(result['hits']).to be_empty
end
end
end
end
Loading