diff options
Diffstat (limited to 'spec/models/concerns')
-rw-r--r-- | spec/models/concerns/cache_markdown_field_spec.rb | 31 | ||||
-rw-r--r-- | spec/models/concerns/ci/artifactable_spec.rb | 21 | ||||
-rw-r--r-- | spec/models/concerns/ci/has_status_spec.rb | 36 | ||||
-rw-r--r-- | spec/models/concerns/counter_attribute_spec.rb | 52 | ||||
-rw-r--r-- | spec/models/concerns/featurable_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/concerns/issuable_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/concerns/manual_inverse_association_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/concerns/milestoneable_spec.rb | 18 | ||||
-rw-r--r-- | spec/models/concerns/sha_attribute_spec.rb | 8 |
9 files changed, 133 insertions, 38 deletions
diff --git a/spec/models/concerns/cache_markdown_field_spec.rb b/spec/models/concerns/cache_markdown_field_spec.rb index 5f8c65e429e..440943171c3 100644 --- a/spec/models/concerns/cache_markdown_field_spec.rb +++ b/spec/models/concerns/cache_markdown_field_spec.rb @@ -20,6 +20,7 @@ RSpec.describe CacheMarkdownField, :clean_gitlab_redis_cache do @title, @description, @cached_markdown_version = args[:title], args[:description], args[:cached_markdown_version] @title_html, @description_html = args[:title_html], args[:description_html] @author, @project = args[:author], args[:project] + @parent_user = args[:parent_user] end attr_accessor :title, :description, :cached_markdown_version @@ -41,8 +42,8 @@ RSpec.describe CacheMarkdownField, :clean_gitlab_redis_cache do let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 } - def thing_subclass(klass, extra_attribute) - Class.new(klass) { attr_accessor(extra_attribute) } + def thing_subclass(klass, *extra_attributes) + Class.new(klass) { attr_accessor(*extra_attributes) } end shared_examples 'a class with cached markdown fields' do @@ -192,11 +193,33 @@ RSpec.describe CacheMarkdownField, :clean_gitlab_redis_cache do end context 'with an author' do - let(:thing) { thing_subclass(klass, :author).new(title: markdown, title_html: html, author: :author_value) } + let(:user) { build(:user) } + let(:thing) { thing_subclass(klass, :author).new(title: markdown, title_html: html, author: user) } it 'sets the author in the context' do is_expected.to have_key(:author) - expect(context[:author]).to eq(:author_value) + expect(context[:author]).to eq(user) + end + end + + context 'with a parent_user' do + let(:user) { build(:user) } + let(:thing) { thing_subclass(klass, :author, :parent_user).new(title: markdown, title_html: html, parent_user: user, author: user) } + + it 'sets the user in the context' do + is_expected.to have_key(:user) + expect(context[:user]).to eq(user) + end + + context 'when the personal_snippet_reference_filters flag is disabled' do + before do + stub_feature_flags(personal_snippet_reference_filters: false) + end + + it 'does not set the user in the context' do + is_expected.not_to have_key(:user) + expect(context[:user]).to be_nil + end end end end diff --git a/spec/models/concerns/ci/artifactable_spec.rb b/spec/models/concerns/ci/artifactable_spec.rb new file mode 100644 index 00000000000..13c2ff5efe5 --- /dev/null +++ b/spec/models/concerns/ci/artifactable_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::Artifactable do + let(:ci_job_artifact) { build(:ci_job_artifact) } + + describe 'artifact properties are included' do + context 'when enum is defined' do + subject { ci_job_artifact } + + it { is_expected.to define_enum_for(:file_format).with_values(raw: 1, zip: 2, gzip: 3).with_suffix } + end + + context 'when const is defined' do + subject { ci_job_artifact.class } + + it { is_expected.to be_const_defined(:FILE_FORMAT_ADAPTERS) } + end + end +end diff --git a/spec/models/concerns/ci/has_status_spec.rb b/spec/models/concerns/ci/has_status_spec.rb index fe46b63781d..b16420bc658 100644 --- a/spec/models/concerns/ci/has_status_spec.rb +++ b/spec/models/concerns/ci/has_status_spec.rb @@ -3,10 +3,10 @@ require 'spec_helper' RSpec.describe Ci::HasStatus do - describe '.slow_composite_status' do + describe '.composite_status' do using RSpec::Parameterized::TableSyntax - subject { CommitStatus.slow_composite_status(project: nil) } + subject { CommitStatus.composite_status } shared_examples 'build status summary' do context 'all successful' do @@ -184,26 +184,16 @@ RSpec.describe Ci::HasStatus do end end - where(:ci_composite_status) do - [false, true] - end - - with_them do - before do - stub_feature_flags(ci_composite_status: ci_composite_status) - end + context 'ci build statuses' do + let(:type) { :ci_build } - context 'ci build statuses' do - let(:type) { :ci_build } - - it_behaves_like 'build status summary' - end + it_behaves_like 'build status summary' + end - context 'generic commit statuses' do - let(:type) { :generic_commit_status } + context 'generic commit statuses' do + let(:type) { :generic_commit_status } - it_behaves_like 'build status summary' - end + it_behaves_like 'build status summary' end end @@ -400,12 +390,4 @@ RSpec.describe Ci::HasStatus do end end end - - describe '.legacy_status_sql' do - subject { Ci::Build.legacy_status_sql } - - it 'returns SQL' do - puts subject - end - end end diff --git a/spec/models/concerns/counter_attribute_spec.rb b/spec/models/concerns/counter_attribute_spec.rb new file mode 100644 index 00000000000..f23865a5dbb --- /dev/null +++ b/spec/models/concerns/counter_attribute_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe CounterAttribute, :counter_attribute, :clean_gitlab_redis_shared_state do + using RSpec::Parameterized::TableSyntax + + let(:project_statistics) { create(:project_statistics) } + let(:model) { CounterAttributeModel.find(project_statistics.id) } + + it_behaves_like CounterAttribute, [:build_artifacts_size, :commit_count] do + let(:model) { CounterAttributeModel.find(project_statistics.id) } + end + + describe '.steal_increments' do + let(:increment_key) { 'counters:Model:123:attribute' } + let(:flushed_key) { 'counter:Model:123:attribute:flushed' } + + subject { model.send(:steal_increments, increment_key, flushed_key) } + + where(:increment, :flushed, :result, :flushed_key_present) do + nil | nil | 0 | false + nil | 0 | 0 | false + 0 | 0 | 0 | false + 1 | 0 | 1 | true + 1 | nil | 1 | true + 1 | 1 | 2 | true + 1 | -2 | -1 | true + -1 | 1 | 0 | false + end + + with_them do + before do + Gitlab::Redis::SharedState.with do |redis| + redis.set(increment_key, increment) if increment + redis.set(flushed_key, flushed) if flushed + end + end + + it { is_expected.to eq(result) } + + it 'drops the increment key and creates the flushed key if it does not exist' do + subject + + Gitlab::Redis::SharedState.with do |redis| + expect(redis.exists(increment_key)).to be_falsey + expect(redis.exists(flushed_key)).to eq(flushed_key_present) + end + end + end + end +end diff --git a/spec/models/concerns/featurable_spec.rb b/spec/models/concerns/featurable_spec.rb index cc01820cc97..31186b5fc77 100644 --- a/spec/models/concerns/featurable_spec.rb +++ b/spec/models/concerns/featurable_spec.rb @@ -42,6 +42,7 @@ RSpec.describe Featurable do end end end + let!(:instance) { klass.new } it { expect(klass.available_features).to eq [:feature1, :feature2] } diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index 96d3e2b7b1b..0824b5c7834 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -17,6 +17,7 @@ RSpec.describe Issuable do it { is_expected.to have_many(:notes).dependent(:destroy) } it { is_expected.to have_many(:todos).dependent(:destroy) } it { is_expected.to have_many(:labels) } + it { is_expected.to have_many(:note_authors).through(:notes) } context 'Notes' do let!(:note) { create(:note, noteable: issue, project: issue.project) } @@ -149,6 +150,7 @@ RSpec.describe Issuable do let!(:searchable_issue) do create(:issue, title: "Searchable awesome issue", description: 'Many cute kittens') end + let!(:searchable_issue2) { create(:issue, title: "Aw", description: "Cu") } it 'returns issues with a matching title' do diff --git a/spec/models/concerns/manual_inverse_association_spec.rb b/spec/models/concerns/manual_inverse_association_spec.rb index 1349d2cc680..0d56d06c624 100644 --- a/spec/models/concerns/manual_inverse_association_spec.rb +++ b/spec/models/concerns/manual_inverse_association_spec.rb @@ -14,7 +14,7 @@ RSpec.describe ManualInverseAssociation do stub_const("#{described_class}::Model", model) end - let(:instance) { create(:merge_request).becomes(model) } + let(:instance) { create(:merge_request).becomes(model) } # rubocop: disable Cop/AvoidBecomes describe '.manual_inverse_association' do context 'when the relation exists' do diff --git a/spec/models/concerns/milestoneable_spec.rb b/spec/models/concerns/milestoneable_spec.rb index 15352a1453c..3dd6f1450c7 100644 --- a/spec/models/concerns/milestoneable_spec.rb +++ b/spec/models/concerns/milestoneable_spec.rb @@ -103,7 +103,7 @@ RSpec.describe Milestoneable do end describe 'release scopes' do - let_it_be(:project) { create(:project) } + let_it_be(:project) { create(:project, :repository) } let_it_be(:release_1) { create(:release, tag: 'v1.0', project: project) } let_it_be(:release_2) { create(:release, tag: 'v2.0', project: project) } @@ -126,6 +126,22 @@ RSpec.describe Milestoneable do let_it_be(:items) { Issue.all } + describe '#any_milestone' do + context 'when milestone filter is present and related closing issues are joined' do + let_it_be(:merge_request_1) { create(:merge_request, source_project: project, source_branch: 'feature-1') } + let_it_be(:merge_request_2) { create(:merge_request, source_project: project, source_branch: 'feature-2') } + + let_it_be(:mrc_issue_1) { create(:merge_requests_closing_issues, issue: issue_1, merge_request: merge_request_1) } + let_it_be(:mrc_issue_2) { create(:merge_requests_closing_issues, issue: issue_2, merge_request: merge_request_2) } + + it 'returns merge request closing issues of any milestone' do + relation = items.joins(merge_requests_closing_issues: :issue).any_milestone + + expect(relation).to contain_exactly(issue_1, issue_2) + end + end + end + describe '#without_release' do it 'returns the issues not tied to any milestone and the ones tied to milestone with no release' do expect(items.without_release).to contain_exactly(issue_5, issue_6) diff --git a/spec/models/concerns/sha_attribute_spec.rb b/spec/models/concerns/sha_attribute_spec.rb index 50748efcda4..3846dd9c231 100644 --- a/spec/models/concerns/sha_attribute_spec.rb +++ b/spec/models/concerns/sha_attribute_spec.rb @@ -15,7 +15,7 @@ RSpec.describe ShaAttribute do end describe '#sha_attribute' do - context 'when in non-production' do + context 'when in development' do before do stub_rails_env('development') end @@ -38,24 +38,22 @@ RSpec.describe ShaAttribute do end context 'when the table does not exist' do - it 'allows the attribute to be added and issues a warning' do + it 'allows the attribute to be added' do allow(model).to receive(:table_exists?).and_return(false) expect(model).not_to receive(:columns) expect(model).to receive(:attribute) - expect(model).to receive(:warn) model.sha_attribute(:name) end end context 'when the column does not exist' do - it 'allows the attribute to be added and issues a warning' do + it 'allows the attribute to be added' do allow(model).to receive(:table_exists?).and_return(true) expect(model).to receive(:columns) expect(model).to receive(:attribute) - expect(model).to receive(:warn) model.sha_attribute(:no_name) end |