blob: 1b09c3dd6364160dbb94221ec4d24b8726f9afed (
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
|
# frozen_string_literal: true
require 'spec_helper'
shared_examples_for 'UpdateProjectStatistics' do
let(:project) { subject.project }
let(:project_statistics_name) { described_class.project_statistics_name }
let(:statistic_attribute) { described_class.statistic_attribute }
def reload_stat
project.statistics.reload.send(project_statistics_name).to_i
end
def read_attribute
subject.read_attribute(statistic_attribute).to_i
end
it { is_expected.to be_new_record }
context 'when creating' do
it 'updates the project statistics' do
delta = read_attribute
expect { subject.save! }
.to change { reload_stat }
.by(delta)
end
end
context 'when updating' do
before do
subject.save!
end
it 'updates project statistics' do
delta = 42
expect(ProjectStatistics)
.to receive(:increment_statistic)
.and_call_original
subject.write_attribute(statistic_attribute, read_attribute + delta)
expect { subject.save! }
.to change { reload_stat }
.by(delta)
end
end
context 'when destroying' do
before do
subject.save!
end
it 'updates the project statistics' do
delta = -read_attribute
expect(ProjectStatistics)
.to receive(:increment_statistic)
.and_call_original
expect { subject.destroy }
.to change { reload_stat }
.by(delta)
end
context 'when it is destroyed from the project level' do
it 'does not update the project statistics' do
expect(ProjectStatistics)
.not_to receive(:increment_statistic)
project.update(pending_delete: true)
project.destroy!
end
end
end
end
|