debian-mirror-gitlab/spec/lib/gitlab/usage/metric_spec.rb

82 lines
2.5 KiB
Ruby
Raw Normal View History

2021-03-08 18:12:59 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Usage::Metric do
2021-10-27 15:23:28 +05:30
let!(:issue) { create(:issue) }
let(:attributes) do
{
data_category: "Operational",
key_path: "counts.issues",
description: "Count of Issues created",
product_section: "dev",
product_stage: "plan",
2022-08-13 15:12:31 +05:30
product_group: "plan",
2021-10-27 15:23:28 +05:30
product_category: "issue_tracking",
value_type: "number",
2021-11-11 11:23:49 +05:30
status: "active",
2021-10-27 15:23:28 +05:30
time_frame: "all",
data_source: "database",
instrumentation_class: "CountIssuesMetric",
distribution: %w(ce ee),
tier: %w(free premium ultimate)
}
2021-03-08 18:12:59 +05:30
end
2021-10-27 15:23:28 +05:30
let(:issue_count_metric_definiton) do
double(:issue_count_metric_definiton,
attributes.merge({ attributes: attributes })
)
end
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
before do
allow(ApplicationRecord.connection).to receive(:transaction_open?).and_return(false)
end
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
describe '#with_value' do
it 'returns key_path metric with the corresponding value' do
expect(described_class.new(issue_count_metric_definiton).with_value).to eq({ counts: { issues: 1 } })
end
end
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
describe '#with_instrumentation' do
it 'returns key_path metric with the corresponding generated query' do
expect(described_class.new(issue_count_metric_definiton).with_instrumentation).to eq({ counts: { issues: "SELECT COUNT(\"issues\".\"id\") FROM \"issues\"" } })
2021-03-08 18:12:59 +05:30
end
end
2021-12-11 22:18:48 +05:30
describe '#with_suggested_name' do
it 'returns key_path metric with the corresponding generated query' do
expect(described_class.new(issue_count_metric_definiton).with_suggested_name).to eq({ counts: { issues: 'count_issues' } })
end
end
2022-07-16 23:28:13 +05:30
context 'unavailable metric' do
let(:instrumentation_class) { "UnavailableMetric" }
let(:issue_count_metric_definiton) do
double(:issue_count_metric_definiton,
attributes.merge({ attributes: attributes, instrumentation_class: instrumentation_class })
)
end
before do
unavailable_metric_class = Class.new(Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric) do
def available?
false
end
end
stub_const("Gitlab::Usage::Metrics::Instrumentations::#{instrumentation_class}", unavailable_metric_class)
end
[:with_value, :with_instrumentation, :with_suggested_name].each do |method_name|
describe "##{method_name}" do
it 'returns an empty hash' do
expect(described_class.new(issue_count_metric_definiton).public_send(method_name)).to eq({})
end
end
end
end
2021-03-08 18:12:59 +05:30
end