Skip to content

Commit 5b57c6b

Browse files
authored
Merge pull request #350 from ellnix/refactor-integration-spec
Split `spec/integration_spec.rb`
2 parents 40a6abb + 487c780 commit 5b57c6b

32 files changed

+1391
-1410
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
--color
2+
--require spec_helper

.rubocop_todo.yml

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-04-08 13:44:25 UTC using RuboCop version 1.27.0.
3+
# on 2024-05-27 18:22:30 UTC using RuboCop version 1.27.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
# This cop supports safe auto-correction (--auto-correct).
11-
# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include.
12-
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
13-
Bundler/OrderedGems:
14-
Exclude:
15-
- 'Gemfile'
16-
17-
# Offense count: 1
18-
# This cop supports safe auto-correction (--auto-correct).
19-
# Configuration parameters: Include.
20-
# Include: **/*.gemspec
21-
Gemspec/RequireMFA:
22-
Exclude:
23-
- 'meilisearch-rails.gemspec'
24-
259
# Offense count: 1
2610
# This cop supports safe auto-correction (--auto-correct).
2711
# Configuration parameters: EnforcedStyle.
@@ -30,13 +14,12 @@ Layout/EmptyLinesAroundModuleBody:
3014
Exclude:
3115
- 'lib/meilisearch-rails.rb'
3216

33-
# Offense count: 3
17+
# Offense count: 1
3418
# This cop supports safe auto-correction (--auto-correct).
3519
# Configuration parameters: EnforcedStyle.
3620
# SupportedStyles: symmetrical, new_line, same_line
3721
Layout/MultilineMethodCallBraceLayout:
3822
Exclude:
39-
- 'spec/integration_spec.rb'
4023
- 'spec/ms_clean_up_job_spec.rb'
4124

4225
# Offense count: 1
@@ -70,7 +53,7 @@ Lint/UnusedMethodArgument:
7053
# Offense count: 12
7154
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
7255
Metrics/AbcSize:
73-
Max: 104
56+
Max: 103
7457

7558
# Offense count: 1
7659
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -81,7 +64,7 @@ Metrics/BlockLength:
8164
# Offense count: 1
8265
# Configuration parameters: CountComments, CountAsOne.
8366
Metrics/ClassLength:
84-
Max: 157
67+
Max: 169
8568

8669
# Offense count: 8
8770
# Configuration parameters: IgnoredMethods.
@@ -91,12 +74,12 @@ Metrics/CyclomaticComplexity:
9174
# Offense count: 18
9275
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
9376
Metrics/MethodLength:
94-
Max: 103
77+
Max: 102
9578

9679
# Offense count: 1
9780
# Configuration parameters: CountComments, CountAsOne.
9881
Metrics/ModuleLength:
99-
Max: 449
82+
Max: 437
10083

10184
# Offense count: 8
10285
# Configuration parameters: IgnoredMethods.
@@ -123,47 +106,61 @@ Naming/MethodParameterName:
123106
Exclude:
124107
- 'lib/meilisearch-rails.rb'
125108

126-
# Offense count: 20
109+
# Offense count: 5
127110
RSpec/BeforeAfterAll:
128111
Exclude:
129-
- 'spec/integration_spec.rb'
112+
- 'spec/integration/active_record/record_has_associations_spec.rb'
113+
- 'spec/pagination/kaminari_spec.rb'
114+
- 'spec/pagination/will_paginate_spec.rb'
115+
- 'spec/settings_spec.rb'
116+
- 'spec/system/tech_shop_spec.rb'
117+
118+
# Offense count: 2
119+
# Configuration parameters: Prefixes.
120+
# Prefixes: when, with, without
121+
RSpec/ContextWording:
122+
Exclude:
123+
- 'spec/options_spec.rb'
124+
- 'spec/system/tech_shop_spec.rb'
130125

131-
# Offense count: 56
126+
# Offense count: 54
132127
# Configuration parameters: CountAsOne.
133128
RSpec/ExampleLength:
134-
Max: 19
129+
Max: 16
135130

136-
# Offense count: 3
131+
# Offense count: 6
137132
# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
138133
# Include: **/*_spec*rb*, **/spec/**/*
139134
RSpec/FilePath:
140135
Exclude:
141136
- 'spec/configuration_spec.rb'
137+
- 'spec/integration/active_record/meilisearch_calls_are_deactivated.rb'
138+
- 'spec/meilisearch/activation_spec.rb'
139+
- 'spec/safe_index_spec.rb'
142140
- 'spec/settings_spec.rb'
143141
- 'spec/utilities_spec.rb'
144142

145-
# Offense count: 25
143+
# Offense count: 29
146144
# Configuration parameters: AssignmentOnly.
147145
RSpec/InstanceVariable:
148146
Exclude:
149-
- 'spec/integration_spec.rb'
147+
- 'spec/system/tech_shop_spec.rb'
150148

151149
# Offense count: 1
152150
RSpec/MultipleDescribes:
153151
Exclude:
154-
- 'spec/integration_spec.rb'
152+
- 'spec/search_spec.rb'
153+
154+
# Offense count: 2
155+
RSpec/NestedGroups:
156+
Max: 4
155157

156158
# Offense count: 1
157159
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
158160
RSpec/VerifiedDoubles:
159161
Exclude:
160162
- 'spec/configuration_spec.rb'
161163

162-
# Offense count: 1
163-
Security/MarshalLoad:
164-
Exclude:
165-
- 'spec/integration_spec.rb'
166-
167164
# Offense count: 2
168165
# This cop supports safe auto-correction (--auto-correct).
169166
# Configuration parameters: EnforcedStyle.
@@ -172,13 +169,13 @@ Style/Alias:
172169
Exclude:
173170
- 'lib/meilisearch-rails.rb'
174171

175-
# Offense count: 2
172+
# Offense count: 3
176173
# Configuration parameters: MinBodyLength.
177174
Style/GuardClause:
178175
Exclude:
179176
- 'lib/meilisearch-rails.rb'
180177

181-
# Offense count: 9
178+
# Offense count: 8
182179
# This cop supports safe auto-correction (--auto-correct).
183180
Style/IfUnlessModifier:
184181
Exclude:
@@ -212,26 +209,17 @@ Style/OptionalBooleanParameter:
212209
Exclude:
213210
- 'lib/meilisearch-rails.rb'
214211

215-
# Offense count: 11
212+
# Offense count: 5
216213
# This cop supports safe auto-correction (--auto-correct).
217214
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
218215
# SupportedStyles: single_quotes, double_quotes
219216
Style/StringLiterals:
220217
Exclude:
221-
- 'spec/integration_spec.rb'
222218
- 'spec/ms_clean_up_job_spec.rb'
223219

224-
# Offense count: 2
225-
# This cop supports safe auto-correction (--auto-correct).
226-
# Configuration parameters: EnforcedStyleForMultiline.
227-
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
228-
Style/TrailingCommaInArguments:
229-
Exclude:
230-
- 'spec/integration_spec.rb'
231-
232-
# Offense count: 20
220+
# Offense count: 16
233221
# This cop supports safe auto-correction (--auto-correct).
234222
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
235223
# URISchemes: http, https
236224
Layout/LineLength:
237-
Max: 178
225+
Max: 170

lib/meilisearch-rails.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ class IndexSettings
8484
def initialize(options, &block)
8585
@options = options
8686
instance_exec(&block) if block_given?
87+
warn_searchable_missing_attributes
88+
end
89+
90+
def warn_searchable_missing_attributes
91+
searchables = get_setting(:searchable_attributes)
92+
attrs = get_setting(:attributes)&.keys
93+
94+
if searchables.present? && attrs.present?
95+
(searchables.map(&:to_s) - attrs.map(&:to_s)).each do |missing_searchable|
96+
MeiliSearch::Rails.logger.warn(
97+
"[meilisearch-rails] #{missing_searchable} declared in searchable_attributes but not in attributes. Please add it to attributes if it should be searchable."
98+
)
99+
end
100+
end
87101
end
88102

89103
def use_serializer(serializer)
@@ -464,8 +478,6 @@ def meilisearch(options = {}, &block)
464478
after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
465479
end
466480
end
467-
468-
warn_searchable_missing_attributes
469481
end
470482

471483
def ms_without_auto_index(&block)
@@ -901,19 +913,6 @@ def ms_attribute_changed?(document, attr_name)
901913
# We don't know if the attribute has changed, so conservatively assume it has
902914
true
903915
end
904-
905-
def warn_searchable_missing_attributes
906-
searchables = meilisearch_settings.get_setting(:searchable_attributes)
907-
attrs = meilisearch_settings.get_setting(:attributes)&.keys
908-
909-
if searchables.present? && attrs.present?
910-
(searchables.map(&:to_s) - attrs.map(&:to_s)).each do |missing_searchable|
911-
MeiliSearch::Rails.logger.warn(
912-
"[meilisearch-rails] #{name}##{missing_searchable} declared in searchable_attributes but not in attributes. Please add it to attributes if it should be searchable."
913-
)
914-
end
915-
end
916-
end
917916
end
918917

919918
# these are the instance methods included

spec/configuration_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require 'spec_helper'
2+
require 'support/models/book'
3+
require 'support/models/color'
24

35
describe MeiliSearch::Rails::Configuration do
46
before { stub_const('MeiliSearch::Rails::VERSION', '0.0.1') }
@@ -63,6 +65,20 @@
6365
end
6466
end
6567

68+
context 'with per_environment' do
69+
# per_environment is already enabled in testing
70+
# no setup is required
71+
72+
it 'adds a Rails env-based index suffix' do
73+
expect(Color.index_uid).to eq(safe_index_uid('Color') + "_#{Rails.env}")
74+
end
75+
76+
it 'uses suffix in the additional index as well' do
77+
index = Book.index(safe_index_uid('Book'))
78+
expect(index.uid).to eq("#{safe_index_uid('Book')}_#{Rails.env}")
79+
end
80+
end
81+
6682
context 'when use Meilisearch without configuration' do
6783
around do |example|
6884
config = MeiliSearch::Rails.configuration

spec/instance_methods_spec.rb

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
require 'support/models/book'
2+
require 'support/models/animals'
3+
require 'support/models/people'
4+
require 'support/models/movie'
5+
require 'support/models/queued_models'
6+
7+
describe 'Instance methods' do
8+
describe '#ms_entries' do
9+
it 'includes conditionally enabled indexes' do
10+
book = Book.create!(
11+
name: 'Frankenstein', author: 'Mary Shelley',
12+
premium: false, released: true
13+
)
14+
15+
expect(book.ms_entries).to contain_exactly(
16+
a_hash_including('index_uid' => /SecuredBook/),
17+
a_hash_including('index_uid' => /BookAuthor/),
18+
a_hash_including('index_uid' => /Book/)
19+
)
20+
end
21+
22+
it 'includes conditionally disabled indexes' do
23+
# non public book
24+
book = Book.create!(
25+
name: 'Frankenstein', author: 'Mary Shelley',
26+
premium: false, released: false
27+
)
28+
29+
expect(book.ms_entries).to contain_exactly(
30+
a_hash_including('index_uid' => /SecuredBook/),
31+
a_hash_including('index_uid' => /BookAuthor/),
32+
# also includes book's id as if it was a public book
33+
a_hash_including('index_uid' => /Book/)
34+
)
35+
end
36+
37+
context 'when models share an index' do
38+
it 'does not return instances of other models' do
39+
TestUtil.reset_animals!
40+
41+
toby_dog = Dog.create!(name: 'Toby the Dog')
42+
taby_cat = Cat.create!(name: 'Taby the Cat')
43+
44+
expect(toby_dog.ms_entries).to contain_exactly(
45+
a_hash_including('primary_key' => /dog_\d+/)
46+
)
47+
48+
expect(taby_cat.ms_entries).to contain_exactly(
49+
a_hash_including('primary_key' => /cat_\d+/)
50+
)
51+
end
52+
end
53+
end
54+
55+
describe '#ms_index!' do
56+
it 'returns array with single task with single index' do
57+
TestUtil.reset_movies!
58+
59+
task = Movie.create(title: 'Harry Potter').ms_index!
60+
61+
expect(task).to contain_exactly(a_hash_including('taskUid'))
62+
end
63+
64+
it 'returns array of tasks with multiple indexes' do
65+
TestUtil.reset_books!
66+
67+
moby_dick = Book.create! name: 'Moby Dick', author: 'Herman Melville', premium: false, released: true
68+
69+
tasks = moby_dick.ms_index!
70+
71+
expect(tasks).to contain_exactly(
72+
a_hash_including('uid'),
73+
a_hash_including('taskUid'),
74+
a_hash_including('taskUid')
75+
)
76+
end
77+
78+
it 'throws error on non-persisted instances' do
79+
expect { Color.new(name: 'purple').index!(true) }.to raise_error(ArgumentError)
80+
end
81+
82+
it 'returns empty array when indexing is disabled' do
83+
doc = DisabledEnqueuedDocument.create! name: 'test'
84+
85+
expect(doc.ms_index!).to be_empty
86+
end
87+
end
88+
89+
describe '#ms_remove_from_index!' do
90+
it 'throws error on non-persisted instances' do
91+
expect { Color.new(name: 'purple').remove_from_index!(true) }.to raise_error(ArgumentError)
92+
end
93+
94+
context 'when :auto_remove is disabled' do
95+
it 'is able to remove manually' do
96+
TestUtil.reset_people!
97+
98+
bob = People.create(first_name: 'Bob', last_name: 'Sponge', card_number: 75_801_889)
99+
100+
result = People.raw_search('Bob')
101+
expect(result['hits']).to be_one
102+
103+
bob.remove_from_index!
104+
105+
result = People.raw_search('Bob')
106+
expect(result['hits']).to be_empty
107+
end
108+
end
109+
end
110+
end

0 commit comments

Comments
 (0)