debian-mirror-gitlab/spec/models/concerns/batch_destroy_dependent_associations_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.8 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe BatchDestroyDependentAssociations do
2018-11-08 19:23:39 +05:30
class TestProject < ActiveRecord::Base
self.table_name = 'projects'
2022-05-07 20:08:51 +05:30
has_many :builds
2018-11-18 11:00:15 +05:30
has_many :notification_settings, as: :source, dependent: :delete_all
2018-11-08 19:23:39 +05:30
has_many :pages_domains
2022-05-07 20:08:51 +05:30
has_many :todos, dependent: :destroy
2018-11-08 19:23:39 +05:30
include BatchDestroyDependentAssociations
end
describe '#dependent_associations_to_destroy' do
2020-03-13 15:44:24 +05:30
let_it_be(:project) { TestProject.new }
2018-11-08 19:23:39 +05:30
it 'returns the right associations' do
2022-05-07 20:08:51 +05:30
expect(project.dependent_associations_to_destroy.map(&:name)).to match_array([:todos])
2018-11-08 19:23:39 +05:30
end
end
describe '#destroy_dependent_associations_in_batches' do
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project) }
let_it_be(:build) { create(:ci_build, project: project) }
let_it_be(:notification_setting) { create(:notification_setting, project: project) }
2022-05-07 20:08:51 +05:30
let_it_be(:note) { create(:note, project: project) }
2021-04-29 21:17:54 +05:30
2022-05-07 20:08:51 +05:30
it 'destroys multiple notes' do
create(:note, project: project)
2018-11-08 19:23:39 +05:30
2022-05-07 20:08:51 +05:30
expect(Note.count).to eq(2)
2018-11-08 19:23:39 +05:30
project.destroy_dependent_associations_in_batches
2022-05-07 20:08:51 +05:30
expect(Note.count).to eq(0)
2018-11-08 19:23:39 +05:30
end
2022-05-07 20:08:51 +05:30
it 'destroys note in batches' do
expect(project).to receive_message_chain(:notes, :find_each).and_yield(note)
expect(note).to receive(:destroy).and_call_original
2018-11-08 19:23:39 +05:30
project.destroy_dependent_associations_in_batches
2022-05-07 20:08:51 +05:30
expect(Ci::Build.count).to eq(1)
expect(Note.count).to eq(0)
2018-11-08 19:23:39 +05:30
expect(User.count).to be > 0
expect(NotificationSetting.count).to eq(User.count)
end
it 'excludes associations' do
2022-05-07 20:08:51 +05:30
project.destroy_dependent_associations_in_batches(exclude: [:notes])
2018-11-08 19:23:39 +05:30
2022-05-07 20:08:51 +05:30
expect(Note.count).to eq(1)
2018-11-08 19:23:39 +05:30
expect(Ci::Build.count).to eq(1)
expect(User.count).to be > 0
expect(NotificationSetting.count).to eq(User.count)
end
end
end