diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-09-12 15:54:25 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-09-14 13:20:05 +0200 |
commit | 3e999684f9c3b7306dea7a7feee90536ee100ccf (patch) | |
tree | 224eabd4599d31a70e799b8ff7a94b759f826865 /spec/models | |
parent | 1140fcce4f8b5463f451356b76fea125826478b2 (diff) | |
download | gitlab-ce-3e999684f9c3b7306dea7a7feee90536ee100ccf.tar.gz |
Memoize pipelines for project download buttons
This adds Project#latest_successful_pipeline_for and
Project#latest_successful_pipeline_for_default_branch. The 2nd method
memoizes the result (taking nil values into account) to ensure the
underlying query isn't executed multiple times when viewing a project's
homepage.
See https://gitlab.com/gitlab-org/gitlab-ce/issues/36878#note_40073607
for more information.
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/project_spec.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 48fc77423ff..78226c6c3fa 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2682,4 +2682,60 @@ describe Project do end end end + + describe '#latest_successful_builds_for' do + let(:project) { build(:project) } + + before do + allow(project).to receive(:default_branch).and_return('master') + end + + context 'without a ref' do + it 'returns a pipeline for the default branch' do + expect(project) + .to receive(:latest_successful_pipeline_for_default_branch) + + project.latest_successful_pipeline_for + end + end + + context 'with the ref set to the default branch' do + it 'returns a pipeline for the default branch' do + expect(project) + .to receive(:latest_successful_pipeline_for_default_branch) + + project.latest_successful_pipeline_for(project.default_branch) + end + end + + context 'with a ref that is not the default branch' do + it 'returns the latest successful pipeline for the given ref' do + expect(project.pipelines).to receive(:latest_successful_for).with('foo') + + project.latest_successful_pipeline_for('foo') + end + end + end + + describe '#latest_successful_pipeline_for_default_branch' do + let(:project) { build(:project) } + + before do + allow(project).to receive(:default_branch).and_return('master') + end + + it 'memoizes and returns the latest successful pipeline for the default branch' do + pipeline = double(:pipeline) + + expect(project.pipelines).to receive(:latest_successful_for) + .with(project.default_branch) + .and_return(pipeline) + .once + + 2.times do + expect(project.latest_successful_pipeline_for_default_branch) + .to eq(pipeline) + end + end + end end |