@@ -6,8 +6,10 @@ import (
6
6
"github.com/stretchr/testify/assert"
7
7
"iot-demo/mocks"
8
8
add_metrics "iot-demo/pkg/metrics/add-metrics"
9
+ "iot-demo/pkg/metrics/alert"
9
10
"iot-demo/pkg/metrics/ingestion"
10
11
"testing"
12
+ "time"
11
13
)
12
14
13
15
//go:generate mockgen -package mocks -destination ../../../mocks/add_metrics.go iot-demo/pkg/metrics/add-metrics Inserter,ConfigGetter
@@ -33,7 +35,7 @@ func TestAdd_Should_Fail_If_Insert_Fails(t *testing.T) {
33
35
34
36
getter .
35
37
EXPECT ().
36
- GetMessage ().
38
+ GetThreshold ().
37
39
Times (0 )
38
40
39
41
// Act
@@ -44,13 +46,16 @@ func TestAdd_Should_Fail_If_Insert_Fails(t *testing.T) {
44
46
assert .Equal (t , err , expectedErr )
45
47
}
46
48
47
- func TestAdd_Should_Succeed_And_Return_Get_Message (t * testing.T ) {
49
+ func TestAdd_Should_Succeed_And_Return_Ok (t * testing.T ) {
48
50
ctrl := gomock .NewController (t )
49
51
defer ctrl .Finish ()
50
52
51
53
// Arrange
52
54
deviceID , metricsToInsert := 52 , []* ingestion.DecimalMetricValue (nil )
53
- expectedMessage := "get-message-result"
55
+ threshold := alert.Threshold {
56
+ Min : 0 ,
57
+ Max : 0 ,
58
+ }
54
59
55
60
inserter := mocks .NewMockInserter (ctrl )
56
61
getter := mocks .NewMockConfigGetter (ctrl )
@@ -65,14 +70,54 @@ func TestAdd_Should_Succeed_And_Return_Get_Message(t *testing.T) {
65
70
66
71
getter .
67
72
EXPECT ().
68
- GetMessage ().
69
- Return (expectedMessage ).
73
+ GetThreshold ().
74
+ Return (threshold ).
70
75
Times (1 )
71
76
72
77
// Act
73
78
res , err := addMetrics .Add (deviceID , metricsToInsert )
74
79
75
80
// Assert
76
81
assert .Nil (t , err )
77
- assert .Equal (t , res , expectedMessage )
82
+ assert .Equal (t , res , add_metrics .Ok )
83
+ }
84
+
85
+
86
+ func TestAdd_Should_Alert_And_Return_Alert (t * testing.T ) {
87
+ ctrl := gomock .NewController (t )
88
+ defer ctrl .Finish ()
89
+
90
+ // Arrange
91
+ deviceID , metricsToInsert := 52 , []* ingestion.DecimalMetricValue {{
92
+ Value : 5 ,
93
+ Time : ingestion .Time (time .Now ()),
94
+ }}
95
+ threshold := alert.Threshold {
96
+ Min : 1 ,
97
+ Max : 10 ,
98
+ }
99
+
100
+ inserter := mocks .NewMockInserter (ctrl )
101
+ getter := mocks .NewMockConfigGetter (ctrl )
102
+
103
+ addMetrics := add_metrics .NewService (inserter , getter )
104
+
105
+ inserter .
106
+ EXPECT ().
107
+ Insert (deviceID , metricsToInsert ).
108
+ Return (nil ).
109
+ Times (1 )
110
+
111
+ getter .
112
+ EXPECT ().
113
+ GetThreshold ().
114
+ Return (threshold ).
115
+ Times (1 )
116
+
117
+ // Act
118
+ res , err := addMetrics .Add (deviceID , metricsToInsert )
119
+
120
+ // Assert
121
+ assert .Nil (t , err )
122
+ assert .Equal (t , res , add_metrics .Alert )
78
123
}
0 commit comments