diff options
Diffstat (limited to 'spec/models/analytics')
3 files changed, 131 insertions, 17 deletions
diff --git a/spec/models/analytics/cycle_analytics/project_stage_spec.rb b/spec/models/analytics/cycle_analytics/project_stage_spec.rb index 4675f037957..fce31af619c 100644 --- a/spec/models/analytics/cycle_analytics/project_stage_spec.rb +++ b/spec/models/analytics/cycle_analytics/project_stage_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Analytics::CycleAnalytics::ProjectStage do end end - it_behaves_like 'cycle analytics stage' do + it_behaves_like 'value stream analytics stage' do let(:parent) { build(:project) } let(:parent_name) { :project } end diff --git a/spec/models/analytics/devops_adoption/segment_selection_spec.rb b/spec/models/analytics/devops_adoption/segment_selection_spec.rb new file mode 100644 index 00000000000..5866cbaa48e --- /dev/null +++ b/spec/models/analytics/devops_adoption/segment_selection_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Analytics::DevopsAdoption::SegmentSelection, type: :model do + subject { build(:devops_adoption_segment_selection, :project) } + + describe 'validation' do + let_it_be(:group) { create(:group) } + let_it_be(:project) { create(:project) } + + it { is_expected.to validate_presence_of(:segment) } + + context do + subject { create(:devops_adoption_segment_selection, :project, project: project) } + + it { is_expected.to validate_uniqueness_of(:project_id).scoped_to(:segment_id) } + end + + context do + subject { create(:devops_adoption_segment_selection, :group, group: group) } + + it { is_expected.to validate_uniqueness_of(:group_id).scoped_to(:segment_id) } + end + + it 'project is required' do + selection = build(:devops_adoption_segment_selection, project: nil, group: nil) + + selection.validate + + expect(selection.errors).to have_key(:project) + end + + it 'project is not required when a group is given' do + selection = build(:devops_adoption_segment_selection, :group, group: group) + + expect(selection).to be_valid + end + + it 'does not allow group to be set when project is present' do + selection = build(:devops_adoption_segment_selection) + + selection.group = group + selection.project = project + + selection.validate + + expect(selection.errors[:group]).to eq([s_('DevopsAdoptionSegmentSelection|The selection cannot be configured for a project and for a group at the same time')]) + end + + context 'limit the number of segment selections' do + let_it_be(:segment) { create(:devops_adoption_segment) } + + subject { build(:devops_adoption_segment_selection, segment: segment, project: project) } + + before do + create(:devops_adoption_segment_selection, :project, segment: segment) + + stub_const("#{described_class}::ALLOWED_SELECTIONS_PER_SEGMENT", 1) + end + + it 'shows validation error' do + subject.validate + + expect(subject.errors[:segment]).to eq([s_('DevopsAdoptionSegment|The maximum number of selections has been reached')]) + end + end + end +end diff --git a/spec/models/analytics/instance_statistics/measurement_spec.rb b/spec/models/analytics/instance_statistics/measurement_spec.rb index 379272cfcb9..dbb16c5ffbe 100644 --- a/spec/models/analytics/instance_statistics/measurement_spec.rb +++ b/spec/models/analytics/instance_statistics/measurement_spec.rb @@ -14,7 +14,7 @@ RSpec.describe Analytics::InstanceStatistics::Measurement, type: :model do describe 'identifiers enum' do it 'maps to the correct values' do - expect(described_class.identifiers).to eq({ + identifiers = { projects: 1, users: 2, issues: 3, @@ -24,8 +24,11 @@ RSpec.describe Analytics::InstanceStatistics::Measurement, type: :model do pipelines_succeeded: 7, pipelines_failed: 8, pipelines_canceled: 9, - pipelines_skipped: 10 - }.with_indifferent_access) + pipelines_skipped: 10, + billable_users: 11 + } + + expect(described_class.identifiers).to eq(identifiers.with_indifferent_access) end end @@ -45,29 +48,71 @@ RSpec.describe Analytics::InstanceStatistics::Measurement, type: :model do it { is_expected.to match_array([measurement_1, measurement_2]) } end - end - describe '#measurement_identifier_values' do - subject { described_class.measurement_identifier_values.count } + describe '.recorded_after' do + subject { described_class.recorded_after(8.days.ago) } - context 'when the `store_ci_pipeline_counts_by_status` feature flag is off' do - let(:expected_count) { Analytics::InstanceStatistics::Measurement.identifiers.size - Analytics::InstanceStatistics::Measurement::EXPERIMENTAL_IDENTIFIERS.size } + it { is_expected.to match_array([measurement_2, measurement_3]) } - before do - stub_feature_flags(store_ci_pipeline_counts_by_status: false) + context 'when nil is given' do + subject { described_class.recorded_after(nil) } + + it 'does not apply filtering' do + expect(subject).to match_array([measurement_1, measurement_2, measurement_3]) + end end + end + + describe '.recorded_before' do + subject { described_class.recorded_before(4.days.ago) } - it { is_expected.to eq(expected_count) } + it { is_expected.to match_array([measurement_1, measurement_3]) } + + context 'when nil is given' do + subject { described_class.recorded_after(nil) } + + it 'does not apply filtering' do + expect(subject).to match_array([measurement_1, measurement_2, measurement_3]) + end + end end + end + + describe '.identifier_query_mapping' do + subject { described_class.identifier_query_mapping } + + it { is_expected.to be_a Hash } + end + + describe '.identifier_min_max_queries' do + subject { described_class.identifier_min_max_queries } + + it { is_expected.to be_a Hash } + end + + describe '.measurement_identifier_values' do + let(:expected_count) { described_class.identifiers.size } + + subject { described_class.measurement_identifier_values.count } + + it { is_expected.to eq(expected_count) } + end - context 'when the `store_ci_pipeline_counts_by_status` feature flag is on' do - let(:expected_count) { Analytics::InstanceStatistics::Measurement.identifiers.size } + describe '.find_latest_or_fallback' do + subject(:count) { described_class.find_latest_or_fallback(:pipelines_skipped).count } - before do - stub_feature_flags(store_ci_pipeline_counts_by_status: true) + context 'with instance statistics' do + let!(:measurement) { create(:instance_statistics_measurement, :pipelines_skipped_count) } + + it 'returns the latest stored measurement' do + expect(count).to eq measurement.count end + end - it { is_expected.to eq(expected_count) } + context 'without instance statistics' do + it 'returns the realtime query of the measurement' do + expect(count).to eq 0 + end end end end |