Skip to content

Commit

Permalink
Merge pull request #350 from ellnix/refactor-integration-spec
Browse files Browse the repository at this point in the history
Split `spec/integration_spec.rb`
  • Loading branch information
brunoocasali committed Aug 7, 2024
2 parents 40a6abb + 487c780 commit 5b57c6b
Show file tree
Hide file tree
Showing 32 changed files with 1,391 additions and 1,410 deletions.
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

0 comments on commit 5b57c6b

Please sign in to comment.