Skip to content

Commit

Permalink
Reduce factory creation in spec/models/account_statuses_cleanup_polocy
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski committed Dec 21, 2023
1 parent cd64a5b commit faceb71
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 117 deletions.
1 change: 0 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ RSpec/LetSetup:
- 'spec/lib/activitypub/activity/delete_spec.rb'
- 'spec/lib/vacuum/applications_vacuum_spec.rb'
- 'spec/lib/vacuum/preview_cards_vacuum_spec.rb'
- 'spec/models/account_statuses_cleanup_policy_spec.rb'
- 'spec/models/status_spec.rb'
- 'spec/services/account_statuses_cleanup_service_spec.rb'
- 'spec/services/activitypub/fetch_featured_collection_service_spec.rb'
Expand Down
189 changes: 73 additions & 116 deletions spec/models/account_statuses_cleanup_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,17 @@
describe '#compute_cutoff_id' do
subject { account_statuses_cleanup_policy.compute_cutoff_id }

let!(:unrelated_status) { Fabricate(:status, created_at: 3.years.ago) }
before { Fabricate(:status, created_at: 3.years.ago) }

let(:account_statuses_cleanup_policy) { Fabricate(:account_statuses_cleanup_policy, account: account) }

context 'when the account has posted multiple toots' do
let!(:very_old_status) { Fabricate(:status, created_at: 3.years.ago, account: account) }
let!(:old_status) { Fabricate(:status, created_at: 3.weeks.ago, account: account) }
let!(:recent_status) { Fabricate(:status, created_at: 2.days.ago, account: account) }
before do
Fabricate(:status, created_at: 3.years.ago, account: account)
Fabricate(:status, created_at: 2.days.ago, account: account)
end

let!(:old_status) { Fabricate(:status, created_at: 3.weeks.ago, account: account) }

it 'returns the most recent id that is still below policy age' do
expect(subject).to eq old_status.id
Expand Down Expand Up @@ -270,16 +274,16 @@
let!(:faved_secondary) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:reblogged_primary) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:reblogged_secondary) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:recent_status) { Fabricate(:status, created_at: 2.days.ago, account: account) }

let!(:media_attachment) { Fabricate(:media_attachment, account: account, status: status_with_media) }
let!(:status_pin) { Fabricate(:status_pin, account: account, status: pinned_status) }
let!(:favourite) { Fabricate(:favourite, account: account, status: self_faved) }
let!(:bookmark) { Fabricate(:bookmark, account: account, status: self_bookmarked) }
let!(:recent_status) { Fabricate(:status, created_at: 2.days.ago, account: account) }

let(:account_statuses_cleanup_policy) { Fabricate(:account_statuses_cleanup_policy, account: account) }

before do
_media_attachment = Fabricate(:media_attachment, account: account, status: status_with_media)
_status_pin = Fabricate(:status_pin, account: account, status: pinned_status)
_favourite = Fabricate(:favourite, account: account, status: self_faved)
_bookmark = Fabricate(:bookmark, account: account, status: self_bookmarked)

faved_primary.status_stat.update(favourites_count: 4)
faved_secondary.status_stat.update(favourites_count: 5)
reblogged_primary.status_stat.update(reblogs_count: 4)
Expand All @@ -292,16 +296,11 @@
let!(:old_status) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:slightly_less_old_status) { Fabricate(:status, created_at: 6.months.ago, account: account) }

it 'returns statuses including max_id' do
expect(subject).to include(old_status.id)
end

it 'returns statuses including older than max_id' do
expect(subject).to include(very_old_status.id)
end

it 'does not return statuses newer than max_id' do
expect(subject).to_not include(slightly_less_old_status.id)
it 'returns statuses included the max_id and older than the max_id but not newer than max_id' do
expect(subject)
.to include(old_status.id)
.and include(very_old_status.id)
.and not_include(slightly_less_old_status.id)
end
end

Expand All @@ -311,16 +310,11 @@
let!(:old_status) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:slightly_less_old_status) { Fabricate(:status, created_at: 6.months.ago, account: account) }

it 'returns statuses including min_id' do
expect(subject).to include(old_status.id)
end

it 'returns statuses including newer than max_id' do
expect(subject).to include(slightly_less_old_status.id)
end

it 'does not return statuses older than min_id' do
expect(subject).to_not include(very_old_status.id)
it 'returns statuses including min_id and newer than min_id, but not older than min_id' do
expect(subject)
.to include(old_status.id)
.and include(slightly_less_old_status.id)
.and not_include(very_old_status.id)
end
end

Expand All @@ -335,12 +329,10 @@
account_statuses_cleanup_policy.min_status_age = 2.years.seconds
end

it 'does not return unrelated old status' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end

it 'returns only oldest status for deletion' do
expect(subject.pluck(:id)).to eq [very_old_status.id]
it 'does not return unrelated old status and does return oldest status' do
expect(subject.pluck(:id))
.to not_include(unrelated_status.id)
.and eq [very_old_status.id]
end
end

Expand All @@ -354,12 +346,10 @@
account_statuses_cleanup_policy.keep_self_bookmark = false
end

it 'does not return the old direct message for deletion' do
expect(subject.pluck(:id)).to_not include(direct_message.id)
end

it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns every old status except does not return the old direct message for deletion' do
expect(subject.pluck(:id))
.to not_include(direct_message.id)
.and include(very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -373,12 +363,10 @@
account_statuses_cleanup_policy.keep_self_bookmark = true
end

it 'does not return the old self-bookmarked message for deletion' do
expect(subject.pluck(:id)).to_not include(self_bookmarked.id)
end

it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns every old status but does not return the old self-bookmarked message for deletion' do
expect(subject.pluck(:id))
.to not_include(self_bookmarked.id)
.and include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -392,12 +380,10 @@
account_statuses_cleanup_policy.keep_self_bookmark = false
end

it 'does not return the old self-bookmarked message for deletion' do
expect(subject.pluck(:id)).to_not include(self_faved.id)
end

it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns every old status but does not return the old self-faved message for deletion' do
expect(subject.pluck(:id))
.to not_include(self_faved.id)
.and include(direct_message.id, very_old_status.id, pinned_status.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -411,12 +397,10 @@
account_statuses_cleanup_policy.keep_self_bookmark = false
end

it 'does not return the old message with media for deletion' do
expect(subject.pluck(:id)).to_not include(status_with_media.id)
end

it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns every old status but does not return the old message with media for deletion' do
expect(subject.pluck(:id))
.to not_include(status_with_media.id)
.and include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -430,12 +414,10 @@
account_statuses_cleanup_policy.keep_self_bookmark = false
end

it 'does not return the old poll message for deletion' do
expect(subject.pluck(:id)).to_not include(status_with_poll.id)
end

it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns every old status but does not return the old poll message for deletion' do
expect(subject.pluck(:id))
.to not_include(status_with_poll.id)
.and include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -449,12 +431,10 @@
account_statuses_cleanup_policy.keep_self_bookmark = false
end

it 'does not return the old pinned message for deletion' do
expect(subject.pluck(:id)).to_not include(pinned_status.id)
end

it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns every old status but does not return the old pinned message for deletion' do
expect(subject.pluck(:id))
.to not_include(pinned_status.id)
.and include(direct_message.id, very_old_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -468,16 +448,11 @@
account_statuses_cleanup_policy.keep_self_bookmark = false
end

it 'does not return the recent toot' do
expect(subject.pluck(:id)).to_not include(recent_status.id)
end

it 'does not return the unrelated toot' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end

it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns every old status but does not return the recent or unrelated statuses' do
expect(subject.pluck(:id))
.to not_include(recent_status.id)
.and not_include(unrelated_status.id)
.and include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -491,12 +466,10 @@
account_statuses_cleanup_policy.keep_self_bookmark = true
end

it 'does not return unrelated old status' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end

it 'returns only normal statuses for deletion' do
expect(subject.pluck(:id)).to contain_exactly(very_old_status.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns normal statuses and does not return unrelated old status' do
expect(subject.pluck(:id))
.to not_include(unrelated_status.id)
.and contain_exactly(very_old_status.id, faved_primary.id, faved_secondary.id, reblogged_primary.id, reblogged_secondary.id)
end
end

Expand All @@ -505,20 +478,12 @@
account_statuses_cleanup_policy.min_reblogs = 5
end

it 'does not return the recent toot' do
expect(subject.pluck(:id)).to_not include(recent_status.id)
end

it 'does not return the toot reblogged 5 times' do
expect(subject.pluck(:id)).to_not include(reblogged_secondary.id)
end

it 'does not return the unrelated toot' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end

it 'returns old statuses not reblogged as much' do
expect(subject.pluck(:id)).to include(very_old_status.id, faved_primary.id, faved_secondary.id, reblogged_primary.id)
it 'returns old not-reblogged statuses but does not return the recent, 5-times reblogged, or unrelated statuses' do
expect(subject.pluck(:id))
.to not_include(recent_status.id)
.and not_include(reblogged_secondary.id)
.and not_include(unrelated_status.id)
.and include(very_old_status.id, faved_primary.id, faved_secondary.id, reblogged_primary.id)
end
end

Expand All @@ -527,20 +492,12 @@
account_statuses_cleanup_policy.min_favs = 5
end

it 'does not return the recent toot' do
expect(subject.pluck(:id)).to_not include(recent_status.id)
end

it 'does not return the toot faved 5 times' do
expect(subject.pluck(:id)).to_not include(faved_secondary.id)
end

it 'does not return the unrelated toot' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end

it 'returns old statuses not faved as much' do
expect(subject.pluck(:id)).to include(very_old_status.id, faved_primary.id, reblogged_primary.id, reblogged_secondary.id)
it 'returns old not-faved statuses but does not return the recent, 5-times faved, or unrelated statuses' do
expect(subject.pluck(:id))
.to not_include(recent_status.id)
.and not_include(faved_secondary.id)
.and not_include(unrelated_status.id)
.and include(very_old_status.id, faved_primary.id, reblogged_primary.id, reblogged_secondary.id)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def sign_in(resource, _deprecated = nil, scope: nil)
end

RSpec::Matchers.define_negated_matcher :not_change, :change
RSpec::Matchers.define_negated_matcher :not_include, :include

def request_fixture(name)
Rails.root.join('spec', 'fixtures', 'requests', name).read
Expand Down

0 comments on commit faceb71

Please sign in to comment.