2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-16 02:06:55 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Database::Count do
|
|
|
|
before do
|
|
|
|
create_list(:project, 3)
|
2018-05-25 17:28:16 -04:00
|
|
|
create(:identity)
|
2018-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
let(:models) { [Project, Identity] }
|
2018-05-16 02:06:55 -04:00
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
describe '.approximate_counts' do
|
2018-10-28 12:50:04 -04:00
|
|
|
context 'fallbacks' do
|
2018-10-28 13:01:17 -04:00
|
|
|
subject { described_class.approximate_counts(models, strategies: strategies) }
|
|
|
|
|
|
|
|
let(:strategies) do
|
|
|
|
[
|
2019-07-24 13:00:34 -04:00
|
|
|
double('s1', new: first_strategy),
|
|
|
|
double('s2', new: second_strategy)
|
2018-10-28 13:01:17 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:first_strategy) { double('first strategy', count: {}) }
|
|
|
|
let(:second_strategy) { double('second strategy', count: {}) }
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
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 })
|
|
|
|
|
|
|
|
expect(subject).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2018-10-28 12:50:04 -04:00
|
|
|
end
|
2018-10-28 13:23:18 -04:00
|
|
|
end
|
2018-05-16 02:06:55 -04:00
|
|
|
end
|