problem_updater_cache_spec.rb
4.37 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'spec_helper'
describe ProblemUpdaterCache do
let(:problem) { Fabricate(:problem_with_errs) }
let(:first_errs) { problem.errs }
let!(:notice) { Fabricate(:notice, :err => first_errs.first) }
describe "#update" do
context "without notice pass args" do
before do
problem.update_attribute(:notices_count, 0)
end
it 'update the notice_count' do
expect {
ProblemUpdaterCache.new(problem).update
}.to change{
problem.notices_count
}.from(0).to(1)
end
context "with only one notice" do
before do
problem.update_attributes!(:messages => {})
ProblemUpdaterCache.new(problem).update
end
it 'update information about this notice' do
expect(problem.message).to eq notice.message
expect(problem.where).to eq notice.where
end
it 'update first_notice_at' do
expect(problem.first_notice_at).to eq notice.created_at
end
it 'update last_notice_at' do
expect(problem.last_notice_at).to eq notice.created_at
end
it 'update stats messages' do
expect(problem.messages).to eq({
Digest::MD5.hexdigest(notice.message) => {'value' => notice.message, 'count' => 1}
})
end
it 'update stats hosts' do
expect(problem.hosts).to eq({
Digest::MD5.hexdigest(notice.host) => {'value' => notice.host, 'count' => 1}
})
end
it 'update stats user_agents' do
expect(problem.user_agents).to eq({
Digest::MD5.hexdigest(notice.user_agent_string) => {'value' => notice.user_agent_string, 'count' => 1}
})
end
end
context "with several notices" do
let!(:notice_2) { Fabricate(:notice, :err => first_errs.first) }
let!(:notice_3) { Fabricate(:notice, :err => first_errs.first) }
before do
problem.update_attributes!(:messages => {})
ProblemUpdaterCache.new(problem).update
end
it 'update information about this notice' do
expect(problem.message).to eq notice.message
expect(problem.where).to eq notice.where
end
it 'update first_notice_at' do
expect(problem.first_notice_at.to_i).to be_within(1).of(notice.created_at.to_i)
end
it 'update last_notice_at' do
expect(problem.last_notice_at.to_i).to be_within(1).of(notice.created_at.to_i)
end
it 'update stats messages' do
expect(problem.messages).to eq({
Digest::MD5.hexdigest(notice.message) => {'value' => notice.message, 'count' => 3}
})
end
it 'update stats hosts' do
expect(problem.hosts).to eq({
Digest::MD5.hexdigest(notice.host) => {'value' => notice.host, 'count' => 3}
})
end
it 'update stats user_agents' do
expect(problem.user_agents).to eq({
Digest::MD5.hexdigest(notice.user_agent_string) => {'value' => notice.user_agent_string, 'count' => 3}
})
end
end
end
context "with notice pass in args" do
before do
ProblemUpdaterCache.new(problem, notice).update
end
it 'increase notices_count by 1' do
expect {
ProblemUpdaterCache.new(problem, notice).update
}.to change{
problem.notices_count
}.by(1)
end
it 'update information about this notice' do
expect(problem.message).to eq notice.message
expect(problem.where).to eq notice.where
end
it 'update first_notice_at' do
expect(problem.first_notice_at).to eq notice.created_at
end
it 'update last_notice_at' do
expect(problem.last_notice_at).to eq notice.created_at
end
it 'inc stats messages' do
expect(problem.messages).to eq({
Digest::MD5.hexdigest(notice.message) => {'value' => notice.message, 'count' => 2}
})
end
it 'inc stats hosts' do
expect(problem.hosts).to eq({
Digest::MD5.hexdigest(notice.host) => {'value' => notice.host, 'count' => 2}
})
end
it 'inc stats user_agents' do
expect(problem.user_agents).to eq({
Digest::MD5.hexdigest(notice.user_agent_string) => {'value' => notice.user_agent_string, 'count' => 2}
})
end
end
end
end