diff options
author | Alessio Caiazza <acaiazza@gitlab.com> | 2019-04-19 12:37:14 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2019-04-19 12:37:14 +0300 |
commit | fa2968256caf2a3e9eebfe0ceca0c71a60f06b06 (patch) | |
tree | c33d58ded9151624e8ceddb46a8a2cdb5350eb4c /spec/models | |
parent | 1aa0ceae41c444fd18e37237497ecb8306a21307 (diff) |
Extract ProjectStatistics updates into a concern
Refactor existing tests as a shared example
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/ci/build_spec.rb | 52 | ||||
-rw-r--r-- | spec/models/ci/job_artifact_spec.rb | 44 |
2 files changed, 8 insertions, 88 deletions
diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 66be192ab21..95b9bd4a4d9 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -31,6 +31,10 @@ describe Ci::Build do it { is_expected.to be_a(ArtifactMigratable) } + it_behaves_like 'UpdateProjectStatistics' do + subject { FactoryBot.build(:ci_build, pipeline: pipeline, artifacts_size: 23) } + end + describe 'associations' do it 'has a bidirectional relationship with projects' do expect(described_class.reflect_on_association(:project).has_inverse?).to eq(:builds) @@ -2119,54 +2123,6 @@ describe Ci::Build do end end - context 'when updating the build' do - let(:build) { create(:ci_build, artifacts_size: 23) } - - it 'updates project statistics' do - build.artifacts_size = 42 - - expect(build).to receive(:update_project_statistics_after_save).and_call_original - - expect { build.save! } - .to change { build.project.statistics.reload.build_artifacts_size } - .by(19) - end - - context 'when the artifact size stays the same' do - it 'does not update project statistics' do - build.name = 'changed' - - expect(build).not_to receive(:update_project_statistics_after_save) - - build.save! - end - end - end - - context 'when destroying the build' do - let!(:build) { create(:ci_build, artifacts_size: 23) } - - it 'updates project statistics' do - expect(ProjectStatistics) - .to receive(:increment_statistic) - .and_call_original - - expect { build.destroy! } - .to change { build.project.statistics.reload.build_artifacts_size } - .by(-23) - end - - context 'when the build is destroyed due to the project being destroyed' do - it 'does not update the project statistics' do - expect(ProjectStatistics) - .not_to receive(:increment_statistic) - - build.project.update(pending_delete: true) - build.project.destroy! - end - end - end - describe '#variables' do let(:container_registry_enabled) { false } diff --git a/spec/models/ci/job_artifact_spec.rb b/spec/models/ci/job_artifact_spec.rb index d7abd54eec1..5964f66c398 100644 --- a/spec/models/ci/job_artifact_spec.rb +++ b/spec/models/ci/job_artifact_spec.rb @@ -5,6 +5,10 @@ require 'spec_helper' describe Ci::JobArtifact do let(:artifact) { create(:ci_job_artifact, :archive) } + it_behaves_like 'UpdateProjectStatistics' do + subject { build(:ci_job_artifact, :archive, size: 106365) } + end + describe "Associations" do it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:job) } @@ -102,12 +106,6 @@ describe Ci::JobArtifact do it 'sets the size from the file size' do expect(artifact.size).to eq(106365) end - - it 'updates the project statistics' do - expect { artifact } - .to change { project.statistics.reload.build_artifacts_size } - .by(106365) - end end context 'updating the artifact file' do @@ -115,12 +113,6 @@ describe Ci::JobArtifact do artifact.update!(file: fixture_file_upload('spec/fixtures/dk.png')) expect(artifact.size).to eq(1062) end - - it 'updates the project statistics' do - expect { artifact.update!(file: fixture_file_upload('spec/fixtures/dk.png')) } - .to change { artifact.project.statistics.reload.build_artifacts_size } - .by(1062 - 106365) - end end describe 'validates file format' do @@ -259,34 +251,6 @@ describe Ci::JobArtifact do end end - context 'when destroying the artifact' do - let(:project) { create(:project, :repository) } - let(:pipeline) { create(:ci_pipeline, project: project) } - let!(:build) { create(:ci_build, :artifacts, pipeline: pipeline) } - - it 'updates the project statistics' do - artifact = build.job_artifacts.first - - expect(ProjectStatistics) - .to receive(:increment_statistic) - .and_call_original - - expect { artifact.destroy } - .to change { project.statistics.reload.build_artifacts_size } - .by(-106365) - end - - context 'when it is destroyed from the project level' do - it 'does not update the project statistics' do - expect(ProjectStatistics) - .not_to receive(:increment_statistic) - - project.update(pending_delete: true) - project.destroy! - end - end - end - describe 'file is being stored' do subject { create(:ci_job_artifact, :archive) } |