debian-mirror-gitlab/spec/lib/gitlab/usage_data_queries_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
4.6 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::UsageDataQueries do
2021-10-27 15:23:28 +05:30
describe '#add_metric' do
let(:metric) { 'CountBoardsMetric' }
it 'builds the query for given metric' do
expect(described_class.add_metric(metric)).to eq('SELECT COUNT("boards"."id") FROM "boards"')
end
end
2023-03-17 16:20:25 +05:30
describe '.with_metadata' do
2022-07-16 23:28:13 +05:30
it 'yields passed block' do
2023-03-17 16:20:25 +05:30
expect { |block| described_class.with_metadata(&block) }.to yield_with_no_args
2022-07-16 23:28:13 +05:30
end
end
2020-11-24 15:15:51 +05:30
describe '.count' do
it 'returns the raw SQL' do
expect(described_class.count(User)).to start_with('SELECT COUNT("users"."id") FROM "users"')
end
2021-04-29 21:17:54 +05:30
it 'does not mix a nil column with keyword arguments' do
2021-09-04 01:27:46 +05:30
expect(described_class.count(User, nil)).to eq('SELECT COUNT("users"."id") FROM "users"')
2021-04-29 21:17:54 +05:30
end
2020-11-24 15:15:51 +05:30
end
describe '.distinct_count' do
it 'returns the raw SQL' do
expect(described_class.distinct_count(Issue, :author_id)).to eq('SELECT COUNT(DISTINCT "issues"."author_id") FROM "issues"')
end
2021-04-29 21:17:54 +05:30
it 'does not mix a nil column with keyword arguments' do
2021-09-04 01:27:46 +05:30
expect(described_class.distinct_count(Issue, nil, start: 1, finish: 2)).to eq('SELECT COUNT(DISTINCT "issues"."id") FROM "issues"')
2021-04-29 21:17:54 +05:30
end
2020-11-24 15:15:51 +05:30
end
describe '.redis_usage_data' do
subject(:redis_usage_data) { described_class.redis_usage_data { 42 } }
2022-06-21 17:19:12 +05:30
it 'returns a stringified class for redis_usage_data with a counter call' do
2020-11-24 15:15:51 +05:30
expect(described_class.redis_usage_data(Gitlab::UsageDataCounters::WikiPageCounter))
2022-06-21 17:19:12 +05:30
.to eq(redis_usage_data_counter: "Gitlab::UsageDataCounters::WikiPageCounter")
2020-11-24 15:15:51 +05:30
end
2022-06-21 17:19:12 +05:30
it 'returns a placeholder string for redis_usage_data with a block' do
2020-11-24 15:15:51 +05:30
is_expected.to include(:redis_usage_data_block)
2022-06-21 17:19:12 +05:30
expect(redis_usage_data[:redis_usage_data_block]).to eq('non-SQL usage data block')
2020-11-24 15:15:51 +05:30
end
end
2022-05-07 20:08:51 +05:30
describe '.alt_usage_data' do
subject(:alt_usage_data) { described_class.alt_usage_data { 42 } }
it 'returns value when used with value' do
expect(described_class.alt_usage_data(1))
.to eq(alt_usage_data_value: 1)
end
2022-06-21 17:19:12 +05:30
it 'returns a placeholder string for alt_usage_data with a block' do
expect(alt_usage_data[:alt_usage_data_block]).to eq('non-SQL usage data block')
2022-05-07 20:08:51 +05:30
end
end
2020-11-24 15:15:51 +05:30
describe '.sum' do
it 'returns the raw SQL' do
expect(described_class.sum(Issue, :weight)).to eq('SELECT SUM("issues"."weight") FROM "issues"')
end
end
2021-04-17 20:07:23 +05:30
describe '.add' do
it 'returns the combined raw SQL with an inner query' do
expect(described_class.add('SELECT COUNT("users"."id") FROM "users"',
'SELECT COUNT("issues"."id") FROM "issues"'))
.to eq('SELECT (SELECT COUNT("users"."id") FROM "users") + (SELECT COUNT("issues"."id") FROM "issues")')
end
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
describe '.histogram' do
it 'returns the histogram sql' do
expect(described_class.histogram(AlertManagement::HttpIntegration.active,
:project_id, buckets: 1..2, bucket_size: 101))
.to match(/^WITH "count_cte" AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported}/)
end
end
2021-04-29 21:17:54 +05:30
describe 'min/max methods' do
it 'returns nil' do
# user min/max
expect(described_class.minimum_id(User)).to eq(nil)
expect(described_class.maximum_id(User)).to eq(nil)
# issue min/max
expect(described_class.minimum_id(Issue)).to eq(nil)
expect(described_class.maximum_id(Issue)).to eq(nil)
# deployment min/max
expect(described_class.minimum_id(Deployment)).to eq(nil)
expect(described_class.maximum_id(Deployment)).to eq(nil)
# project min/max
expect(described_class.minimum_id(Project)).to eq(nil)
expect(described_class.maximum_id(Project)).to eq(nil)
end
end
2022-07-23 23:45:48 +05:30
describe 'sent_in_product_marketing_email_count' do
it 'returns sql query that returns correct value' do
expect(described_class.sent_in_product_marketing_email_count(nil, 0, 0)).to eq(
'SELECT COUNT("in_product_marketing_emails"."id") ' \
'FROM "in_product_marketing_emails" ' \
'WHERE "in_product_marketing_emails"."track" = 0 AND "in_product_marketing_emails"."series" = 0'
)
end
end
describe 'clicked_in_product_marketing_email_count' do
it 'returns sql query that returns correct value' do
expect(described_class.clicked_in_product_marketing_email_count(nil, 0, 0)).to eq(
'SELECT COUNT("in_product_marketing_emails"."id") ' \
'FROM "in_product_marketing_emails" ' \
'WHERE "in_product_marketing_emails"."track" = 0 AND "in_product_marketing_emails"."series" = 0 ' \
'AND "in_product_marketing_emails"."cta_clicked_at" IS NOT NULL'
)
end
end
2020-11-24 15:15:51 +05:30
end