blob: fee6cbbfcbaf16559bc9f4664aa4a72468cbaa8b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
require 'spec_helper'
describe Gitlab::Database::Count do
before do
create_list(:project, 3)
create(:identity)
end
let(:models) { [Project, Identity] }
describe '.approximate_counts' do
context 'with MySQL' do
context 'when reltuples have not been updated' do
it 'counts all models the normal way' do
expect(Gitlab::Database).to receive(:postgresql?).and_return(false)
expect(Project).to receive(:count).and_call_original
expect(Identity).to receive(:count).and_call_original
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
end
end
end
context 'with PostgreSQL', :postgresql do
let(:reltuples_strategy) { double('reltuples_strategy', count: {}) }
before do
allow(Gitlab::Database::Count::ReltuplesCountStrategy).to receive(:new).with(models).and_return(reltuples_strategy)
end
describe 'when reltuples have not been updated' do
it 'counts all models the normal way' do
expect(Project).to receive(:count).and_call_original
expect(Identity).to receive(:count).and_call_original
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
end
end
describe 'no permission' do
it 'falls back to standard query' do
allow(ActiveRecord::Base).to receive(:transaction).and_raise(PG::InsufficientPrivilege)
expect(Project).to receive(:count).and_call_original
expect(Identity).to receive(:count).and_call_original
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
end
end
describe 'when some reltuples have been updated' do
it 'counts projects in the fast way' do
expect(reltuples_strategy).to receive(:count).and_return({ Project => 3 })
expect(Project).not_to receive(:count).and_call_original
expect(Identity).to receive(:count).and_call_original
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
end
end
# TODO: This covers two parts: reltuple strategy itself and the fallback
# TODO: Add spec that covers strategy details for reltuple strategy
describe 'when all reltuples have been updated' do
#before do
#ActiveRecord::Base.connection.execute('ANALYZE projects')
#ActiveRecord::Base.connection.execute('ANALYZE identities')
#end
it 'counts models with the standard way' do
allow(reltuples_strategy).to receive(:count).and_return({ Project => 3, Identity => 1 })
expect(Project).not_to receive(:count)
expect(Identity).not_to receive(:count)
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
end
end
describe Gitlab::Database::Count::ExactCountStrategy do
subject { described_class.new(models).count }
it 'counts all models' do
models.each { |model| expect(model).to receive(:count).and_call_original }
expect(subject).to eq({ Project => 3, Identity => 1 })
end
end
end
end
end
|