diff options
Diffstat (limited to 'spec/lib/gitlab/usage')
4 files changed, 41 insertions, 5 deletions
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb index 8e7bd7b84e6..f73155642d6 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb @@ -160,6 +160,38 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do end end end + + context 'with custom timestamp column' do + subject do + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation :count + metric_class.timestamp_column :last_edited_at + end.new(time_frame: '28d') + end + + it 'calculates a correct result' do + create(:issue, last_edited_at: 5.days.ago) + + expect(subject.value).to eq(1) + end + end + + context 'with default timestamp column' do + subject do + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation :count + end.new(time_frame: '28d') + end + + it 'calculates a correct result' do + create(:issue, last_edited_at: 5.days.ago) + create(:issue, created_at: 5.days.ago) + + expect(subject.value).to eq(1) + end + end end context 'with unimplemented operation method used' do diff --git a/spec/lib/gitlab/usage/metrics/name_suggestion_spec.rb b/spec/lib/gitlab/usage/metrics/name_suggestion_spec.rb index 9ee8bc6b568..f9cd6e88e0a 100644 --- a/spec/lib/gitlab/usage/metrics/name_suggestion_spec.rb +++ b/spec/lib/gitlab/usage/metrics/name_suggestion_spec.rb @@ -66,7 +66,7 @@ RSpec.describe Gitlab::Usage::Metrics::NameSuggestion do let(:key_path) { 'counts.jira_imports_total_imported_issues_count' } let(:operation) { :sum } let(:relation) { JiraImportState.finished } - let(:column) { :imported_issues_count} + let(:column) { :imported_issues_count } let(:name_suggestion) { /sum_imported_issues_count_from_<adjective describing\: '\(jira_imports\.status = \d+\)'>_jira_imports/ } end end @@ -77,7 +77,7 @@ RSpec.describe Gitlab::Usage::Metrics::NameSuggestion do let(:key_path) { 'counts.ci_pipeline_duration' } let(:operation) { :average } let(:relation) { Ci::Pipeline } - let(:column) { :duration} + let(:column) { :duration } let(:name_suggestion) { /average_duration_from_ci_pipelines/ } end end diff --git a/spec/lib/gitlab/usage/metrics/names_suggestions/generator_spec.rb b/spec/lib/gitlab/usage/metrics/names_suggestions/generator_spec.rb index 167dba9b57d..7e8b15d23db 100644 --- a/spec/lib/gitlab/usage/metrics/names_suggestions/generator_spec.rb +++ b/spec/lib/gitlab/usage/metrics/names_suggestions/generator_spec.rb @@ -17,7 +17,7 @@ RSpec.describe Gitlab::Usage::Metrics::NamesSuggestions::Generator do end describe '#add_metric' do - let(:metric) {'CountIssuesMetric' } + let(:metric) { 'CountIssuesMetric' } it 'computes the suggested name for given metric' do expect(described_class.add_metric(metric)).to eq('count_issues') diff --git a/spec/lib/gitlab/usage/service_ping_report_spec.rb b/spec/lib/gitlab/usage/service_ping_report_spec.rb index 1e8f9db4dea..7a37a31b195 100644 --- a/spec/lib/gitlab/usage/service_ping_report_spec.rb +++ b/spec/lib/gitlab/usage/service_ping_report_spec.rb @@ -111,8 +111,12 @@ RSpec.describe Gitlab::Usage::ServicePingReport, :use_clean_rails_memory_store_c # Because test cases are run inside a transaction, if any query raise and error all queries that follows # it are automatically canceled by PostgreSQL, to avoid that problem, and to provide exhaustive information # about every metric, queries are wrapped explicitly in sub transactions. - ApplicationRecord.transaction do - ApplicationRecord.connection.execute(query)&.first&.values&.first + table = PgQuery.parse(query).tables.first + gitlab_schema = Gitlab::Database::GitlabSchema.tables_to_schema[table] + base_model = gitlab_schema == :gitlab_main ? ApplicationRecord : Ci::ApplicationRecord + + base_model.transaction do + base_model.connection.execute(query)&.first&.values&.first end rescue ActiveRecord::StatementInvalid => e e.message |