debian-mirror-gitlab/spec/lib/gitlab/database/count_spec.rb

95 lines
2.9 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Database::Count do
2018-10-15 14:42:47 +05:30
before do
create_list(:project, 3)
2018-11-08 19:23:39 +05:30
create(:identity)
2018-10-15 14:42:47 +05:30
end
2018-11-08 19:23:39 +05:30
let(:models) { [Project, Identity] }
2018-10-15 14:42:47 +05:30
2020-03-13 15:44:24 +05:30
describe '.approximate_counts' do
2019-02-15 15:39:39 +05:30
context 'fallbacks' do
subject { described_class.approximate_counts(models, strategies: strategies) }
2018-10-15 14:42:47 +05:30
2019-02-15 15:39:39 +05:30
let(:strategies) do
[
2019-10-12 21:52:04 +05:30
double('s1', new: first_strategy),
double('s2', new: second_strategy)
2019-02-15 15:39:39 +05:30
]
2018-10-15 14:42:47 +05:30
end
2019-02-15 15:39:39 +05:30
let(:first_strategy) { double('first strategy', count: {}) }
let(:second_strategy) { double('second strategy', count: {}) }
2018-10-15 14:42:47 +05:30
2019-02-15 15:39:39 +05:30
it 'gets results from first strategy' do
expect(strategies[0]).to receive(:new).with(models).and_return(first_strategy)
expect(first_strategy).to receive(:count)
subject
2018-10-15 14:42:47 +05:30
end
2019-02-15 15:39:39 +05:30
it 'gets more results from second strategy if some counts are missing' do
expect(first_strategy).to receive(:count).and_return({ Project => 3 })
expect(strategies[1]).to receive(:new).with([Identity]).and_return(second_strategy)
expect(second_strategy).to receive(:count).and_return({ Identity => 1 })
2018-11-08 19:23:39 +05:30
2019-02-15 15:39:39 +05:30
expect(subject).to eq({ Project => 3, Identity => 1 })
2018-10-15 14:42:47 +05:30
end
2019-02-15 15:39:39 +05:30
it 'does not get more results as soon as all counts are present' do
expect(first_strategy).to receive(:count).and_return({ Project => 3, Identity => 1 })
expect(strategies[1]).not_to receive(:new)
2018-10-15 14:42:47 +05:30
2019-02-15 15:39:39 +05:30
subject
2018-10-15 14:42:47 +05:30
end
end
2021-11-18 22:05:49 +05:30
context 'default strategies' do
subject { described_class.approximate_counts(models) }
context 'with a read-only database' do
before do
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
end
it 'only uses the ExactCountStrategy' do
allow_next_instance_of(Gitlab::Database::Count::TablesampleCountStrategy) do |instance|
expect(instance).not_to receive(:count)
end
allow_next_instance_of(Gitlab::Database::Count::ReltuplesCountStrategy) do |instance|
expect(instance).not_to receive(:count)
end
expect_next_instance_of(Gitlab::Database::Count::ExactCountStrategy) do |instance|
expect(instance).to receive(:count).and_return({})
end
subject
end
end
context 'with a read-write database' do
before do
allow(Gitlab::Database).to receive(:read_only?).and_return(false)
end
it 'uses the available strategies' do
[
Gitlab::Database::Count::TablesampleCountStrategy,
Gitlab::Database::Count::ReltuplesCountStrategy,
Gitlab::Database::Count::ExactCountStrategy
].each do |strategy_klass|
expect_next_instance_of(strategy_klass) do |instance|
expect(instance).to receive(:count).and_return({})
end
end
subject
end
end
end
2018-10-15 14:42:47 +05:30
end
end