2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe LfsObjectsProject do
|
2020-02-05 07:09:15 -05:00
|
|
|
let_it_be(:project) { create(:project) }
|
2017-09-05 03:17:28 -04:00
|
|
|
|
|
|
|
subject do
|
|
|
|
create(:lfs_objects_project, project: project)
|
|
|
|
end
|
2016-11-22 11:58:10 -05:00
|
|
|
|
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
it { is_expected.to belong_to(:lfs_object) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validation' do
|
|
|
|
it { is_expected.to validate_presence_of(:lfs_object_id) }
|
|
|
|
it { is_expected.to validate_presence_of(:project_id) }
|
2017-09-05 03:17:28 -04:00
|
|
|
|
|
|
|
it 'validates object id' do
|
|
|
|
is_expected.to validate_uniqueness_of(:lfs_object_id)
|
2019-06-06 02:51:50 -04:00
|
|
|
.scoped_to(:project_id, :repository_type)
|
|
|
|
.with_message("already exists in repository")
|
2017-09-05 03:17:28 -04:00
|
|
|
end
|
2016-11-22 11:58:10 -05:00
|
|
|
end
|
|
|
|
|
2021-12-09 19:13:05 -05:00
|
|
|
describe '#link_to_project!' do
|
|
|
|
it 'does not throw error when duplicate exists' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect do
|
|
|
|
result = described_class.link_to_project!(subject.lfs_object, subject.project)
|
|
|
|
expect(result).to be_a(LfsObjectsProject)
|
|
|
|
end.not_to change { described_class.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'upserts a new entry and updates the project cache' do
|
|
|
|
new_project = create(:project)
|
|
|
|
|
|
|
|
allow(ProjectCacheWorker).to receive(:perform_async).and_call_original
|
|
|
|
expect(ProjectCacheWorker).to receive(:perform_async).with(new_project.id, [], [:lfs_objects_size])
|
|
|
|
expect { described_class.link_to_project!(subject.lfs_object, new_project) }
|
|
|
|
.to change { described_class.count }
|
|
|
|
|
|
|
|
expect(described_class.find_by(lfs_object_id: subject.lfs_object.id, project_id: new_project.id)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
describe '#update_project_statistics' do
|
|
|
|
it 'updates project statistics when the object is added' do
|
|
|
|
expect(ProjectCacheWorker).to receive(:perform_async)
|
|
|
|
.with(project.id, [], [:lfs_objects_size])
|
|
|
|
|
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates project statistics when the object is removed' do
|
|
|
|
subject.save!
|
|
|
|
|
|
|
|
expect(ProjectCacheWorker).to receive(:perform_async)
|
|
|
|
.with(project.id, [], [:lfs_objects_size])
|
|
|
|
|
2021-05-20 23:10:24 -04:00
|
|
|
subject.destroy!
|
2016-11-22 11:58:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|