2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|