Fix Performance/TimesMap cop (#24789)

This commit is contained in:
Matt Jankowski 2023-05-02 12:07:16 -04:00 committed by GitHub
parent bae694108a
commit 570079f8ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 24 deletions

View File

@ -365,15 +365,6 @@ Performance/StartWith:
Exclude: Exclude:
- 'app/lib/extractor.rb' - 'app/lib/extractor.rb'
# This cop supports unsafe autocorrection (--autocorrect-all).
Performance/TimesMap:
Exclude:
- 'spec/controllers/api/v1/blocks_controller_spec.rb'
- 'spec/controllers/api/v1/mutes_controller_spec.rb'
- 'spec/lib/feed_manager_spec.rb'
- 'spec/lib/request_pool_spec.rb'
- 'spec/models/account_spec.rb'
# This cop supports unsafe autocorrection (--autocorrect-all). # This cop supports unsafe autocorrection (--autocorrect-all).
Performance/UnfreezeString: Performance/UnfreezeString:
Exclude: Exclude:

View File

@ -13,13 +13,13 @@ RSpec.describe Api::V1::BlocksController, type: :controller do
describe 'GET #index' do describe 'GET #index' do
it 'limits according to limit parameter' do it 'limits according to limit parameter' do
2.times.map { Fabricate(:block, account: user.account) } Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { limit: 1 } get :index, params: { limit: 1 }
expect(body_as_json.size).to eq 1 expect(body_as_json.size).to eq 1
end end
it 'queries blocks in range according to max_id' do it 'queries blocks in range according to max_id' do
blocks = 2.times.map { Fabricate(:block, account: user.account) } blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { max_id: blocks[1] } get :index, params: { max_id: blocks[1] }
@ -28,7 +28,7 @@ RSpec.describe Api::V1::BlocksController, type: :controller do
end end
it 'queries blocks in range according to since_id' do it 'queries blocks in range according to since_id' do
blocks = 2.times.map { Fabricate(:block, account: user.account) } blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { since_id: blocks[0] } get :index, params: { since_id: blocks[0] }
@ -37,7 +37,7 @@ RSpec.describe Api::V1::BlocksController, type: :controller do
end end
it 'sets pagination header for next path' do it 'sets pagination header for next path' do
blocks = 2.times.map { Fabricate(:block, account: user.account) } blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { limit: 1, since_id: blocks[0] } get :index, params: { limit: 1, since_id: blocks[0] }
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_blocks_url(limit: 1, max_id: blocks[1]) expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_blocks_url(limit: 1, max_id: blocks[1])
end end

View File

@ -13,13 +13,13 @@ RSpec.describe Api::V1::MutesController, type: :controller do
describe 'GET #index' do describe 'GET #index' do
it 'limits according to limit parameter' do it 'limits according to limit parameter' do
2.times.map { Fabricate(:mute, account: user.account) } Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { limit: 1 } get :index, params: { limit: 1 }
expect(body_as_json.size).to eq 1 expect(body_as_json.size).to eq 1
end end
it 'queries mutes in range according to max_id' do it 'queries mutes in range according to max_id' do
mutes = 2.times.map { Fabricate(:mute, account: user.account) } mutes = Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { max_id: mutes[1] } get :index, params: { max_id: mutes[1] }
@ -28,7 +28,7 @@ RSpec.describe Api::V1::MutesController, type: :controller do
end end
it 'queries mutes in range according to since_id' do it 'queries mutes in range according to since_id' do
mutes = 2.times.map { Fabricate(:mute, account: user.account) } mutes = Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { since_id: mutes[0] } get :index, params: { since_id: mutes[0] }
@ -37,7 +37,7 @@ RSpec.describe Api::V1::MutesController, type: :controller do
end end
it 'sets pagination header for next path' do it 'sets pagination header for next path' do
mutes = 2.times.map { Fabricate(:mute, account: user.account) } mutes = Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { limit: 1, since_id: mutes[0] } get :index, params: { limit: 1, since_id: mutes[0] }
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_mutes_url(limit: 1, max_id: mutes[1]) expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_mutes_url(limit: 1, max_id: mutes[1])
end end

View File

@ -188,7 +188,7 @@ RSpec.describe FeedManager do
it 'trims timelines if they will have more than FeedManager::MAX_ITEMS' do it 'trims timelines if they will have more than FeedManager::MAX_ITEMS' do
account = Fabricate(:account) account = Fabricate(:account)
status = Fabricate(:status) status = Fabricate(:status)
members = FeedManager::MAX_ITEMS.times.map { |count| [count, count] } members = Array.new(FeedManager::MAX_ITEMS) { |count| [count, count] }
redis.zadd("feed:home:#{account.id}", members) redis.zadd("feed:home:#{account.id}", members)
FeedManager.instance.push_to_home(account, status) FeedManager.instance.push_to_home(account, status)
@ -233,7 +233,7 @@ RSpec.describe FeedManager do
it 'does not save a new reblog of a recently-reblogged status' do it 'does not save a new reblog of a recently-reblogged status' do
account = Fabricate(:account) account = Fabricate(:account)
reblogged = Fabricate(:status) reblogged = Fabricate(:status)
reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) } reblogs = Array.new(2) { Fabricate(:status, reblog: reblogged) }
# The first reblog will be accepted # The first reblog will be accepted
FeedManager.instance.push_to_home(account, reblogs.first) FeedManager.instance.push_to_home(account, reblogs.first)
@ -262,7 +262,7 @@ RSpec.describe FeedManager do
it 'does not save a new reblog of a multiply-reblogged-then-unreblogged status' do it 'does not save a new reblog of a multiply-reblogged-then-unreblogged status' do
account = Fabricate(:account) account = Fabricate(:account)
reblogged = Fabricate(:status) reblogged = Fabricate(:status)
reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) } reblogs = Array.new(3) { Fabricate(:status, reblog: reblogged) }
# Accept the reblogs # Accept the reblogs
FeedManager.instance.push_to_home(account, reblogs[0]) FeedManager.instance.push_to_home(account, reblogs[0])
@ -278,7 +278,7 @@ RSpec.describe FeedManager do
it 'saves a new reblog of a long-ago-reblogged status' do it 'saves a new reblog of a long-ago-reblogged status' do
account = Fabricate(:account) account = Fabricate(:account)
reblogged = Fabricate(:status) reblogged = Fabricate(:status)
reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) } reblogs = Array.new(2) { Fabricate(:status, reblog: reblogged) }
# The first reblog will be accepted # The first reblog will be accepted
FeedManager.instance.push_to_home(account, reblogs.first) FeedManager.instance.push_to_home(account, reblogs.first)
@ -459,7 +459,7 @@ RSpec.describe FeedManager do
it 'leaves a multiply-reblogged status if another reblog was in feed' do it 'leaves a multiply-reblogged status if another reblog was in feed' do
reblogged = Fabricate(:status) reblogged = Fabricate(:status)
reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) } reblogs = Array.new(3) { Fabricate(:status, reblog: reblogged) }
reblogs.each do |reblog| reblogs.each do |reblog|
FeedManager.instance.push_to_home(receiver, reblog) FeedManager.instance.push_to_home(receiver, reblog)

View File

@ -33,7 +33,7 @@ describe RequestPool do
subject subject
threads = 20.times.map do |_i| threads = Array.new(20) do |_i|
Thread.new do Thread.new do
20.times do 20.times do
subject.with('http://example.com') do |http_client| subject.with('http://example.com') do |http_client|

View File

@ -902,7 +902,7 @@ RSpec.describe Account, type: :model do
describe 'recent' do describe 'recent' do
it 'returns a relation of accounts sorted by recent creation' do it 'returns a relation of accounts sorted by recent creation' do
matches = 2.times.map { Fabricate(:account) } matches = Array.new(2) { Fabricate(:account) }
expect(Account.where('id > 0').recent).to match_array(matches) expect(Account.where('id > 0').recent).to match_array(matches)
end end
end end