Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 555fb08

Browse files
committed
Add test for the snapshot formatters.
1 parent c9f9f88 commit 555fb08

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ require 'rake/testtask'
33

44
Rake::TestTask.new do |t|
55
t.libs.push "lib"
6-
t.test_files = FileList['test/*_test.rb']
6+
t.libs.push "test"
7+
t.test_files = FileList['test/**/*_test.rb']
78
t.verbose = true
89
end
910

lib/cc/formatters/snapshot_formatter.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ class Base
4444
def initialize(repo, payload)
4545
@repo = repo
4646

47-
new_constants = Array(payload["new_constants"])
48-
changed_constants = Array(payload["changed_constants"])
47+
new_constants = Array(payload[:new_constants])
48+
changed_constants = Array(payload[:changed_constants])
4949

50-
alert_constants = new_constants.select(new_constants_selector)
51-
alert_constants += changed_constants.select(decreased_constants_selector)
50+
alert_constants = new_constants.select(&new_constants_selector)
51+
alert_constants += changed_constants.select(&decreased_constants_selector)
5252

53-
improved_constants = changed_constants.select(improved_constants_selector)
53+
improved_constants = changed_constants.select(&improved_constants_selector)
5454

5555
data = {
56-
from: { commit_sha: payload["previous_commit_sha"] },
57-
to: { commit_sha: payload["commit_sha"] }
56+
from: { commit_sha: payload[:previous_commit_sha] },
57+
to: { commit_sha: payload[:commit_sha] }
5858
}
5959

6060
@alert_constants_payload = data.merge(constants: alert_constants) if alert_constants.any?
@@ -80,11 +80,11 @@ def repo_identifier
8080
end
8181

8282
def to_rating(constant)
83-
Rating.new(constant["to"]["rating"])
83+
Rating.new(constant[:to][:rating])
8484
end
8585

8686
def from_rating(constant)
87-
Rating.new(constant["from"]["rating"])
87+
Rating.new(constant[:from][:rating])
8888
end
8989
end
9090

lib/cc/services/slack.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def receive_test
1616
speak(formatter.format_test)
1717

1818
# payloads for test receivers include the weekly quality report.
19-
send_snapshot_to_slack(CC::Formatters::SnapshotFormatter::Test.new(repo, payload))
19+
send_snapshot_to_slack(CC::Formatters::SnapshotFormatter::Sample.new(repo, payload))
2020

2121
{ ok: true, message: "Test message sent" }
2222
rescue => ex
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require "helper"
2+
3+
class TestSnapshotFormatter < Test::Unit::TestCase
4+
def described_class
5+
CC::Formatters::SnapshotFormatter::Base
6+
end
7+
8+
def test_quality_alert_with_new_constants
9+
f = described_class.new(nil, {new_constants: [{to: {rating: "D"}}], changed_constants: []})
10+
refute_nil f.alert_constants_payload
11+
end
12+
13+
def test_quality_alert_with_decreased_constants
14+
f = described_class.new(nil, {new_constants: [],
15+
changed_constants: [{to: {rating: "D"}, from: {rating: "A"}}]
16+
})
17+
refute_nil f.alert_constants_payload
18+
end
19+
20+
def test_quality_improvements_with_better_ratings
21+
f = described_class.new(nil, {new_constants: [],
22+
changed_constants: [{to: {rating: "A"}, from: {rating: "D"}}]
23+
})
24+
refute_nil f.improved_constants_payload
25+
end
26+
27+
def test_nothing_set_without_changes
28+
f = described_class.new(nil, {new_constants: [], changed_constants: []})
29+
assert_nil f.alert_constants_payload
30+
assert_nil f.improved_constants_payload
31+
end
32+
33+
def test_snapshot_formatter_test_with_relaxed_constraints
34+
f = CC::Formatters::SnapshotFormatter::Sample.new(nil, {
35+
new_constants: [{name: "foo", to: {rating: "A"}}, {name: "bar", to: {rating: "A"}}],
36+
changed_constants: [
37+
{from: {rating: "B"}, to: {rating: "C"}},
38+
{from: {rating: "D"}, to: {rating: "D"}},
39+
{from: {rating: "D"}, to: {rating: "D"}},
40+
{from: {rating: "A"}, to: {rating: "B"}},
41+
{from: {rating: "C"}, to: {rating: "B"}}
42+
]})
43+
44+
refute_nil f.alert_constants_payload
45+
refute_nil f.improved_constants_payload
46+
end
47+
end

0 commit comments

Comments
 (0)