2018-10-15 14:42:47 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Database::Count do
|
|
|
|
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
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
context '.approximate_counts' do
|
|
|
|
context 'selecting strategies' do
|
|
|
|
let(:strategies) { [double('s1', enabled?: true), double('s2', enabled?: false)] }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
it 'uses only enabled strategies' do
|
|
|
|
expect(strategies[0]).to receive(:new).and_return(double('strategy1', count: {}))
|
|
|
|
expect(strategies[1]).not_to receive(:new)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
described_class.approximate_counts(models, strategies: strategies)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
context 'fallbacks' do
|
|
|
|
subject { described_class.approximate_counts(models, strategies: strategies) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
let(:strategies) do
|
|
|
|
[
|
|
|
|
double('s1', enabled?: true, new: first_strategy),
|
|
|
|
double('s2', enabled?: true, new: second_strategy)
|
|
|
|
]
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
let(:first_strategy) { double('first strategy', count: {}) }
|
|
|
|
let(:second_strategy) { double('second strategy', count: {}) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-23 12:14:25 +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
|
|
|
|
|
2018-12-23 12:14:25 +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
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
expect(subject).to eq({ Project => 3, Identity => 1 })
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-12-23 12:14:25 +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
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
subject
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|