2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-26 22:45:22 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe BatchDestroyDependentAssociations do
|
2018-04-26 22:45:22 -04:00
|
|
|
class TestProject < ActiveRecord::Base
|
|
|
|
self.table_name = 'projects'
|
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
has_many :builds
|
2018-07-02 06:43:06 -04:00
|
|
|
has_many :notification_settings, as: :source, dependent: :delete_all
|
2018-04-26 22:45:22 -04:00
|
|
|
has_many :pages_domains
|
2022-02-23 07:13:44 -05:00
|
|
|
has_many :todos, dependent: :destroy
|
2018-04-26 22:45:22 -04:00
|
|
|
|
|
|
|
include BatchDestroyDependentAssociations
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#dependent_associations_to_destroy' do
|
2020-02-05 07:09:15 -05:00
|
|
|
let_it_be(:project) { TestProject.new }
|
2018-04-26 22:45:22 -04:00
|
|
|
|
|
|
|
it 'returns the right associations' do
|
2022-02-23 07:13:44 -05:00
|
|
|
expect(project.dependent_associations_to_destroy.map(&:name)).to match_array([:todos])
|
2018-04-26 22:45:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy_dependent_associations_in_batches' do
|
2020-02-05 07:09:15 -05:00
|
|
|
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-02-23 07:13:44 -05:00
|
|
|
let_it_be(:note) { create(:note, project: project) }
|
2021-04-05 14:09:15 -04:00
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
it 'destroys multiple notes' do
|
|
|
|
create(:note, project: project)
|
2018-04-26 22:45:22 -04:00
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
expect(Note.count).to eq(2)
|
2018-04-26 22:45:22 -04:00
|
|
|
|
|
|
|
project.destroy_dependent_associations_in_batches
|
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
expect(Note.count).to eq(0)
|
2018-04-26 22:45:22 -04:00
|
|
|
end
|
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
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-04-26 22:45:22 -04:00
|
|
|
|
|
|
|
project.destroy_dependent_associations_in_batches
|
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
expect(Ci::Build.count).to eq(1)
|
|
|
|
expect(Note.count).to eq(0)
|
2018-04-26 22:45:22 -04:00
|
|
|
expect(User.count).to be > 0
|
|
|
|
expect(NotificationSetting.count).to eq(User.count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes associations' do
|
2022-02-23 07:13:44 -05:00
|
|
|
project.destroy_dependent_associations_in_batches(exclude: [:notes])
|
2018-04-26 22:45:22 -04:00
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
expect(Note.count).to eq(1)
|
2018-04-26 22:45:22 -04:00
|
|
|
expect(Ci::Build.count).to eq(1)
|
|
|
|
expect(User.count).to be > 0
|
|
|
|
expect(NotificationSetting.count).to eq(User.count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|