2020-09-09 21:08:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::UsageDataQueries do
|
|
|
|
before do
|
|
|
|
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
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-03-26 09:09:24 +00:00
|
|
|
|
|
|
|
it 'does not mix a nil column with keyword arguments' do
|
|
|
|
expect(described_class).to receive(:raw_sql).with(User, nil)
|
|
|
|
|
|
|
|
described_class.count(User, start: 1, finish: 2)
|
|
|
|
end
|
2020-09-09 21:08:33 +00:00
|
|
|
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-03-26 09:09:24 +00:00
|
|
|
|
|
|
|
it 'does not mix a nil column with keyword arguments' do
|
|
|
|
expect(described_class).to receive(:raw_sql).with(Issue, nil, :distinct)
|
|
|
|
|
|
|
|
described_class.distinct_count(Issue, nil, start: 1, finish: 2)
|
|
|
|
end
|
2020-09-09 21:08:33 +00:00
|
|
|
end
|
2020-09-15 21:09:35 +00:00
|
|
|
|
|
|
|
describe '.redis_usage_data' do
|
|
|
|
subject(:redis_usage_data) { described_class.redis_usage_data { 42 } }
|
|
|
|
|
|
|
|
it 'returns a class for redis_usage_data with a counter call' do
|
|
|
|
expect(described_class.redis_usage_data(Gitlab::UsageDataCounters::WikiPageCounter))
|
|
|
|
.to eq(redis_usage_data_counter: Gitlab::UsageDataCounters::WikiPageCounter)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a stringified block for redis_usage_data with a block' do
|
|
|
|
is_expected.to include(:redis_usage_data_block)
|
|
|
|
expect(redis_usage_data[:redis_usage_data_block]).to start_with('#<Proc:')
|
|
|
|
end
|
|
|
|
end
|
2020-09-16 06:09:24 +00:00
|
|
|
|
|
|
|
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-02-25 09:10:45 +00:00
|
|
|
|
|
|
|
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-09 06:09:30 +00:00
|
|
|
|
2021-04-21 15:09:35 +00:00
|
|
|
describe '.histogram' do
|
|
|
|
it 'returns the histogram sql' do
|
|
|
|
expect(described_class.histogram(AlertManagement::HttpIntegration.active,
|
|
|
|
:project_id, buckets: 1..2, bucket_size: 101))
|
2021-04-22 18:10:13 +00:00
|
|
|
.to match(/^WITH "count_cte" AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported}/)
|
2021-04-21 15:09:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-09 06:09:30 +00:00
|
|
|
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
|
2020-09-09 21:08:33 +00:00
|
|
|
end
|