generated from michaelpeterswa/go-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenogymodbus_internal_test.go
137 lines (128 loc) · 3.73 KB
/
renogymodbus_internal_test.go
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
package gorenogymodbus
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetControllerFaults(t *testing.T) {
tests := []struct {
Name string
InputBytes []byte
ExpectedErrors []string
}{
{
Name: "no error flags set",
InputBytes: []byte{0b00000000, 0b00000000, 0b00000000, 0b00000000},
ExpectedErrors: nil,
},
{
Name: "single error flag set",
InputBytes: []byte{0b00001000, 0b00000000, 0b0000000, 0b00000000},
ExpectedErrors: []string{
"solar panel working point overvoltage",
},
},
{
Name: "dual error flag set",
InputBytes: []byte{0b0011000, 0b00000000, 0b0000000, 0b00000000},
ExpectedErrors: []string{"solar panel working point overvoltage", "solar panel reversely connected"},
},
{
Name: "all error flag set",
InputBytes: []byte{0b01111111, 0b11111111, 0b10000000, 0b00000000},
ExpectedErrors: []string{
"battery over discharge", "battery over voltage", "battery under voltage", "load short circuit",
"load over power or load over current", "controller temperature too high", "ambient temperature too high",
"photovoltaic input overpower", "photovoltaic input side short circuit", "photovoltaic input side over voltage",
"solar panel counter current", "solar panel working point overvoltage", "solar panel reversely connected",
"anti reverse mos short", "charge mos short circuit",
},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
result, err := getControllerFaults(tc.InputBytes)
if err != nil {
assert.Fail(t, err.Error())
}
assert.ElementsMatch(t, tc.ExpectedErrors, result)
})
}
}
func TestBuildStreetLightStateAndBrightnessByte(t *testing.T) {
tests := []struct {
Name string
StreetLightStatus bool
StreetLightBrightness int
ResultByte byte
ShouldError bool
}{
{
Name: "load off and minimum brightness",
StreetLightStatus: false,
StreetLightBrightness: 0,
ResultByte: 0b00000000,
ShouldError: false,
},
{
Name: "load on and max brightness",
StreetLightStatus: true,
StreetLightBrightness: 100,
ResultByte: 0b11100100,
ShouldError: false,
},
{
Name: "load on and 50% brightness",
StreetLightStatus: true,
StreetLightBrightness: 50,
ResultByte: 0b10110010,
ShouldError: false,
},
{
Name: "load on and 150% brightness, should error",
StreetLightStatus: true,
StreetLightBrightness: 150,
ResultByte: 0b10010110,
ShouldError: true,
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
result, err := buildStreetLightStatusAndBrightnessByte(tc.StreetLightStatus, tc.StreetLightBrightness)
if !tc.ShouldError {
assert.NoError(t, err)
assert.Equal(t, tc.ResultByte, result)
} else {
assert.Error(t, err)
}
})
}
}
func TestGetChargingState(t *testing.T) {
tests := []struct {
Name string
Byte byte
ExpectedChargingState ChargingState
}{
{
Name: "minimum charging state",
Byte: 0x0,
ExpectedChargingState: ChargingDeactivated,
},
{
Name: "maximum charging state",
Byte: 0x6,
ExpectedChargingState: CurrentLimitingOverPower,
},
{
Name: "invalid charging state",
Byte: 0x7,
ExpectedChargingState: 0x7,
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
result := getChargingState(tc.Byte)
assert.Equal(t, tc.ExpectedChargingState, result)
})
}
}