2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-27 02:45:22 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe BatchDestroyDependentAssociations do
|
2018-04-27 02:45:22 +00:00
|
|
|
class TestProject < ActiveRecord::Base
|
|
|
|
self.table_name = 'projects'
|
|
|
|
|
2018-07-02 10:43:06 +00:00
|
|
|
has_many :builds, dependent: :destroy
|
|
|
|
has_many :notification_settings, as: :source, dependent: :delete_all
|
2018-04-27 02:45:22 +00:00
|
|
|
has_many :pages_domains
|
|
|
|
has_many :todos
|
|
|
|
|
|
|
|
include BatchDestroyDependentAssociations
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#dependent_associations_to_destroy' do
|
2020-02-05 12:09:15 +00:00
|
|
|
let_it_be(:project) { TestProject.new }
|
2018-04-27 02:45:22 +00:00
|
|
|
|
|
|
|
it 'returns the right associations' do
|
|
|
|
expect(project.dependent_associations_to_destroy.map(&:name)).to match_array([:builds])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy_dependent_associations_in_batches' do
|
2020-02-05 12:09:15 +00: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) }
|
2018-04-27 02:45:22 +00:00
|
|
|
let!(:todos) { create(:todo, project: project) }
|
|
|
|
|
|
|
|
it 'destroys multiple builds' do
|
|
|
|
create(:ci_build, project: project)
|
|
|
|
|
|
|
|
expect(Ci::Build.count).to eq(2)
|
|
|
|
|
|
|
|
project.destroy_dependent_associations_in_batches
|
|
|
|
|
|
|
|
expect(Ci::Build.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys builds in batches' do
|
|
|
|
expect(project).to receive_message_chain(:builds, :find_each).and_yield(build)
|
|
|
|
expect(build).to receive(:destroy).and_call_original
|
|
|
|
|
|
|
|
project.destroy_dependent_associations_in_batches
|
|
|
|
|
|
|
|
expect(Ci::Build.count).to eq(0)
|
|
|
|
expect(Todo.count).to eq(1)
|
|
|
|
expect(User.count).to be > 0
|
|
|
|
expect(NotificationSetting.count).to eq(User.count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes associations' do
|
|
|
|
project.destroy_dependent_associations_in_batches(exclude: [:builds])
|
|
|
|
|
|
|
|
expect(Ci::Build.count).to eq(1)
|
|
|
|
expect(Todo.count).to eq(1)
|
|
|
|
expect(User.count).to be > 0
|
|
|
|
expect(NotificationSetting.count).to eq(User.count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|