summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Jankowski <matt@jankowski.online>2023-11-20 04:08:22 -0500
committerGitHub <noreply@github.com>2023-11-20 09:08:22 +0000
commitd2aacea8da2b7229dcef2ba1c7d2c8005ad1370e (patch)
treed713199db647c24f255c283c60129a97e3ef358a
parent00c6ebd86f9a084394a7e1017c9e808e4c4b4d77 (diff)
Reduce `.times` usage in AccountSearch spec, use constant for default limit (#27946)
-rw-r--r--app/models/concerns/account_search.rb6
-rw-r--r--spec/models/account_spec.rb12
2 files changed, 11 insertions, 7 deletions
diff --git a/app/models/concerns/account_search.rb b/app/models/concerns/account_search.rb
index 9f7720f11bf..b855727b4dd 100644
--- a/app/models/concerns/account_search.rb
+++ b/app/models/concerns/account_search.rb
@@ -106,6 +106,8 @@ module AccountSearch
LIMIT :limit OFFSET :offset
SQL
+ DEFAULT_LIMIT = 10
+
def searchable_text
PlainTextFormatter.new(note, local?).to_s if discoverable?
end
@@ -118,7 +120,7 @@ module AccountSearch
end
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)
find_by_sql([BASIC_SEARCH_SQL, { limit: limit, offset: offset, tsquery: tsquery }]).tap do |records|
@@ -126,7 +128,7 @@ module AccountSearch
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)
sql_template = following ? ADVANCED_SEARCH_WITH_FOLLOWING : ADVANCED_SEARCH_WITHOUT_FOLLOWING
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index f77ecb055a4..9652ea1910f 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -450,10 +450,11 @@ RSpec.describe Account do
expect(results).to eq [match]
end
- it 'limits by 10 by default' do
- 11.times.each { Fabricate(:account, display_name: 'Display Name') }
+ it 'limits via constant by default' do
+ stub_const('AccountSearch::DEFAULT_LIMIT', 1)
+ 2.times.each { Fabricate(:account, display_name: 'Display Name') }
results = described_class.search_for('display')
- expect(results.size).to eq 10
+ expect(results.size).to eq 1
end
it 'accepts arbitrary limits' do
@@ -594,9 +595,10 @@ RSpec.describe Account do
end
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)
- expect(results.size).to eq 10
+ expect(results.size).to eq 1
end
it 'accepts arbitrary limits' do