Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 6 additions & 13 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-03-08 11:51:39 UTC using RuboCop version 1.27.0.
# on 2025-03-14 19:22:00 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
Expand Down Expand Up @@ -30,13 +30,6 @@ Layout/EmptyLinesAroundModuleBody:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand Down Expand Up @@ -146,7 +139,7 @@ RSpec/ContextWording:
- 'spec/options_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 57
# Offense count: 61
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 16
Expand Down Expand Up @@ -174,7 +167,7 @@ RSpec/MultipleDescribes:
Exclude:
- 'spec/search_spec.rb'

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

Expand All @@ -184,12 +177,12 @@ RSpec/VerifiedDoubles:
Exclude:
- 'spec/configuration_spec.rb'

# Offense count: 1
# Offense count: 3
# Configuration parameters: ForbiddenMethods, AllowedMethods.
# ForbiddenMethods: decrement!, decrement_counter, increment!, increment_counter, insert, insert!, insert_all, insert_all!, toggle!, touch, touch_all, update_all, update_attribute, update_column, update_columns, update_counters, upsert, upsert_all
Rails/SkipsModelValidations:
Exclude:
- 'spec/settings_spec.rb'
- 'spec/multi_search_spec.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
Expand Down Expand Up @@ -247,7 +240,7 @@ Style/StringLiterals:
Exclude:
- 'spec/ms_clean_up_job_spec.rb'

# Offense count: 15
# Offense count: 16
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
112 changes: 66 additions & 46 deletions spec/multi_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@
require 'support/models/color'

describe 'multi-search' do
def reset_indexes
before do
[Book, Color, Product].each do |klass|
klass.delete_all
klass.clear_index!(true)
klass.clear_index!
end
end

before do
reset_indexes

Product.create! name: 'palm pixi plus', href: 'ebay', tags: ['terrible']
Product.create! name: 'lg vortex', href: 'ebay', tags: ['decent']
Product.create! name: 'palmpre', href: 'ebay', tags: ['discontinued', 'worst phone ever']
Product.insert_all([
Copy link
Member

Choose a reason for hiding this comment

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

Inserts multiple records into the database in a single SQL INSERT statement. It does not instantiate any models nor does it trigger Active Record callbacks or validations. Though passed values go through Active Record’s type casting and serialization.

Interesting that you create the records then you reindex the model, very clever!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

It's a method used very often in seed files, you can imagine that with a lot more records it would be a non-trivial amount of memory and computation to instantiate a ActiveRecord object for all of them.

I'm not really very happy with the solution here, I think it's a little bit undercooked, but I'm saving any refactoring for the inevitable PR where I probably make every test file into an rspec behavior so that I can apply it to every storage backend (AR, Sequel, Mongo).

{ name: 'palm pixi plus', href: 'ebay', tags: ['terrible'] },
{ name: 'lg vortex', href: 'ebay', tags: ['decent'] },
{ name: 'palmpre', href: 'ebay', tags: ['discontinued', 'worst phone ever'] }
])
Product.reindex!

Color.create! name: 'blue', short_name: 'blu', hex: 0x0000FF
Color.create! name: 'black', short_name: 'bla', hex: 0x000000
Color.create! name: 'green', short_name: 'gre', hex: 0x00FF00

Book.create! name: 'Steve Jobs', author: 'Walter Isaacson'
Book.create! name: 'Moby Dick', author: 'Herman Melville'
Color.insert_all([
{ name: 'blue', short_name: 'blu', hex: 0x0000FF },
{ name: 'black', short_name: 'bla', hex: 0x000000 },
{ name: 'green', short_name: 'gre', hex: 0x00FF00 }
])
Color.reindex!

Book.insert_all([
{ name: 'Steve Jobs', author: 'Walter Isaacson' },
{ name: 'Moby Dick', author: 'Herman Melville' }
])
Book.reindex!
end

let!(:palm_pixi_plus) { Product.find_by name: 'palm pixi plus' }
let!(:steve_jobs) { Book.find_by name: 'Steve Jobs' }
let!(:blue) { Color.find_by name: 'blue' }
let!(:black) { Color.find_by name: 'black' }
let(:palm_pixi_plus) { Product.find_by name: 'palm pixi plus' }
let(:steve_jobs) { Book.find_by name: 'Steve Jobs' }
let(:blue) { Color.find_by name: 'blue' }
let(:black) { Color.find_by name: 'black' }

context 'with class keys' do
it 'returns ORM records' do
Expand All @@ -40,8 +44,10 @@ def reset_indexes
Color => { q: 'bl' }
)

expect(results).to contain_exactly(
steve_jobs, palm_pixi_plus, blue, black
expect(results.to_h).to match(
Book => [steve_jobs],
Product => [palm_pixi_plus],
Color => contain_exactly(blue, black)
)
end
end
Expand All @@ -65,11 +71,13 @@ def reset_indexes
'colors' => { q: 'bl', index_uid: Color.index.uid }
)

expect(results).to contain_exactly(
a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs'),
a_hash_including('name' => 'palm pixi plus'),
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
a_hash_including('name' => 'black', 'short_name' => 'bla')
expect(results.to_h).to match(
'books' => [a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs')],
'products' => [a_hash_including('name' => 'palm pixi plus')],
'colors' => contain_exactly(
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
a_hash_including('name' => 'black', 'short_name' => 'bla')
)
)
end

Expand All @@ -82,10 +90,10 @@ def reset_indexes
'nature_colors' => { q: 'green', index_uid: index_uid }
)

expect(results).to contain_exactly(
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
a_hash_including('name' => 'black', 'short_name' => 'bla'),
a_hash_including('name' => 'green', 'short_name' => 'gre')
expect(results.to_h).to match(
'bright_colors' => [a_hash_including('name' => 'blue', 'short_name' => 'blu')],
'dark_colors' => [a_hash_including('name' => 'black', 'short_name' => 'bla')],
'nature_colors' => [a_hash_including('name' => 'green', 'short_name' => 'gre')]
)
end

Expand All @@ -97,8 +105,10 @@ def reset_indexes
'colors' => { q: 'bl', index_uid: Color.index.uid, class_name: 'Color' }
)

expect(results).to contain_exactly(
steve_jobs, palm_pixi_plus, blue, black
expect(results.to_h).to match(
'books' => [steve_jobs],
'products' => [palm_pixi_plus],
'colors' => contain_exactly(blue, black)
)
end
end
Expand All @@ -113,11 +123,13 @@ def reset_indexes
Color.index.uid => { q: 'bl' }
)

expect(results).to contain_exactly(
a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs'),
a_hash_including('name' => 'palm pixi plus'),
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
a_hash_including('name' => 'black', 'short_name' => 'bla')
expect(results.to_h).to match(
Book.index.uid => [a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs')],
Product.index.uid.to_sym => [a_hash_including('name' => 'palm pixi plus')],
Color.index.uid => contain_exactly(
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
a_hash_including('name' => 'black', 'short_name' => 'bla')
)
)
end

Expand All @@ -129,8 +141,10 @@ def reset_indexes
Color.index.uid => { q: 'bl', class_name: 'Color' }
)

expect(results).to contain_exactly(
steve_jobs, palm_pixi_plus, blue, black
expect(results.to_h).to match(
Book.index.uid => [steve_jobs],
Product.index.uid.to_sym => [palm_pixi_plus],
Color.index.uid => contain_exactly(blue, black)
)
end

Expand All @@ -154,10 +168,13 @@ def reset_indexes
Color.index.uid => { q: 'bl' }
)

expect(results).to contain_exactly(
steve_jobs, palm_pixi_plus,
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
a_hash_including('name' => 'black', 'short_name' => 'bla')
expect(results.to_h).to match(
Book => [steve_jobs],
Product.index_uid => [palm_pixi_plus],
Color.index.uid => contain_exactly(
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
a_hash_including('name' => 'black', 'short_name' => 'bla')
)
)
end
end
Expand All @@ -172,9 +189,12 @@ def reset_indexes
Color.index.uid => { q: 'bl', page: 1, 'hitsPerPage' => '1' }
)

expect(results).to contain_exactly(
steve_jobs, palm_pixi_plus,
a_hash_including('name' => 'black', 'short_name' => 'bla')
expect(results.to_h).to match(
Book => [steve_jobs],
Product => [palm_pixi_plus],
Color.index_uid => contain_exactly(
a_hash_including('name' => 'black', 'short_name' => 'bla')
)
)

MeiliSearch::Rails.configuration[:pagination_backend] = nil
Expand Down