Reduce `.times` usage in AccountSearch spec, use constant for default limit (#27946)

shrike
Matt Jankowski 2023-11-20 04:08:22 -05:00 committed by GitHub
parent 00c6ebd86f
commit d2aacea8da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -106,6 +106,8 @@ module AccountSearch
LIMIT :limit OFFSET :offset LIMIT :limit OFFSET :offset
SQL SQL
DEFAULT_LIMIT = 10
def searchable_text def searchable_text
PlainTextFormatter.new(note, local?).to_s if discoverable? PlainTextFormatter.new(note, local?).to_s if discoverable?
end end
@ -118,7 +120,7 @@ module AccountSearch
end end
class_methods do class_methods do
def search_for(terms, limit: 10, offset: 0) def search_for(terms, limit: DEFAULT_LIMIT, offset: 0)
tsquery = generate_query_for_search(terms) tsquery = generate_query_for_search(terms)
find_by_sql([BASIC_SEARCH_SQL, { limit: limit, offset: offset, tsquery: tsquery }]).tap do |records| find_by_sql([BASIC_SEARCH_SQL, { limit: limit, offset: offset, tsquery: tsquery }]).tap do |records|
@ -126,7 +128,7 @@ module AccountSearch
end end
end end
def advanced_search_for(terms, account, limit: 10, following: false, offset: 0) def advanced_search_for(terms, account, limit: DEFAULT_LIMIT, following: false, offset: 0)
tsquery = generate_query_for_search(terms) tsquery = generate_query_for_search(terms)
sql_template = following ? ADVANCED_SEARCH_WITH_FOLLOWING : ADVANCED_SEARCH_WITHOUT_FOLLOWING sql_template = following ? ADVANCED_SEARCH_WITH_FOLLOWING : ADVANCED_SEARCH_WITHOUT_FOLLOWING

View File

@ -450,10 +450,11 @@ RSpec.describe Account do
expect(results).to eq [match] expect(results).to eq [match]
end end
it 'limits by 10 by default' do it 'limits via constant by default' do
11.times.each { Fabricate(:account, display_name: 'Display Name') } stub_const('AccountSearch::DEFAULT_LIMIT', 1)
2.times.each { Fabricate(:account, display_name: 'Display Name') }
results = described_class.search_for('display') results = described_class.search_for('display')
expect(results.size).to eq 10 expect(results.size).to eq 1
end end
it 'accepts arbitrary limits' do it 'accepts arbitrary limits' do
@ -594,9 +595,10 @@ RSpec.describe Account do
end end
it 'limits by 10 by default' do it 'limits by 10 by default' do
11.times { Fabricate(:account, display_name: 'Display Name') } stub_const('AccountSearch::DEFAULT_LIMIT', 1)
2.times { Fabricate(:account, display_name: 'Display Name') }
results = described_class.advanced_search_for('display', account) results = described_class.advanced_search_for('display', account)
expect(results.size).to eq 10 expect(results.size).to eq 1
end end
it 'accepts arbitrary limits' do it 'accepts arbitrary limits' do