forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsht3x_driver_test.go
272 lines (204 loc) · 6.69 KB
/
sht3x_driver_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package i2c
import (
"errors"
"strings"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*SHT3xDriver)(nil)
// --------- HELPERS
func initTestSHT3xDriver() (driver *SHT3xDriver) {
driver, _ = initTestSHT3xDriverWithStubbedAdaptor()
return
}
func initTestSHT3xDriverWithStubbedAdaptor() (*SHT3xDriver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewSHT3xDriver(adaptor), adaptor
}
// --------- TESTS
func TestNewSHT3xDriver(t *testing.T) {
// Does it return a pointer to an instance of SHT3xDriver?
var bm interface{} = NewSHT3xDriver(newI2cTestAdaptor())
_, ok := bm.(*SHT3xDriver)
if !ok {
t.Errorf("NewSHT3xDriver() should have returned a *SHT3xDriver")
}
b := NewSHT3xDriver(newI2cTestAdaptor())
gobottest.Refute(t, b.Connection(), nil)
}
// Methods
func TestSHT3xDriverStart(t *testing.T) {
sht3x, _ := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
}
func TestSHT3xStartConnectError(t *testing.T) {
d, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestSHT3xDriverHalt(t *testing.T) {
sht3x := initTestSHT3xDriver()
gobottest.Assert(t, sht3x.Halt(), nil)
}
// Test Name & SetName
func TestSHT3xDriverName(t *testing.T) {
sht3x := initTestSHT3xDriver()
gobottest.Assert(t, strings.HasPrefix(sht3x.Name(), "SHT3x"), true)
sht3x.SetName("Sensor")
gobottest.Assert(t, sht3x.Name(), "Sensor")
}
func TestSHT3xDriverOptions(t *testing.T) {
d := NewSHT3xDriver(newI2cTestAdaptor(), WithBus(2))
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
}
// Test Accuracy & SetAccuracy
func TestSHT3xDriverSetAccuracy(t *testing.T) {
sht3x := initTestSHT3xDriver()
gobottest.Assert(t, sht3x.Accuracy(), byte(SHT3xAccuracyHigh))
err := sht3x.SetAccuracy(SHT3xAccuracyMedium)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, sht3x.Accuracy(), byte(SHT3xAccuracyMedium))
err = sht3x.SetAccuracy(SHT3xAccuracyLow)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, sht3x.Accuracy(), byte(SHT3xAccuracyLow))
err = sht3x.SetAccuracy(0xff)
gobottest.Assert(t, err, ErrInvalidAccuracy)
}
// Test Sample
func TestSHT3xDriverSampleNormal(t *testing.T) {
sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x92})
return 6, nil
}
temp, rh, _ := sht3x.Sample()
gobottest.Assert(t, temp, float32(85.523003))
gobottest.Assert(t, rh, float32(74.5845))
// check the temp with the units in F
sht3x.Units = "F"
temp, _, _ = sht3x.Sample()
gobottest.Assert(t, temp, float32(185.9414))
}
func TestSHT3xDriverSampleBadCrc(t *testing.T) {
sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
// Check that the 1st crc failure is caught
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0xbe, 0xef, 0x00, 0xbe, 0xef, 0x92})
return 6, nil
}
_, _, err := sht3x.Sample()
gobottest.Assert(t, err, ErrInvalidCrc)
// Check that the 2nd crc failure is caught
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x00})
return 6, nil
}
_, _, err = sht3x.Sample()
gobottest.Assert(t, err, ErrInvalidCrc)
}
func TestSHT3xDriverSampleBadRead(t *testing.T) {
sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
// Check that the 1st crc failure is caught
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0xbe, 0xef, 0x00, 0xbe, 0xef})
return 5, nil
}
_, _, err := sht3x.Sample()
gobottest.Assert(t, err, ErrNotEnoughBytes)
}
func TestSHT3xDriverSampleUnits(t *testing.T) {
sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
// Check that the 1st crc failure is caught
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef, 0x92})
return 6, nil
}
sht3x.Units = "K"
_, _, err := sht3x.Sample()
gobottest.Assert(t, err, ErrInvalidTemp)
}
// Test internal sendCommandDelayGetResponse
func TestSHT3xDriverSCDGRIoFailures(t *testing.T) {
sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
invalidRead := errors.New("Read error")
invalidWrite := errors.New("Write error")
// Only send 5 bytes
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0xbe, 0xef, 0x92, 0xbe, 0xef})
return 5, nil
}
_, err := sht3x.sendCommandDelayGetResponse(nil, nil, 2)
gobottest.Assert(t, err, ErrNotEnoughBytes)
// Don't read any bytes and return an error
adaptor.i2cReadImpl = func([]byte) (int, error) {
return 0, invalidRead
}
_, err = sht3x.sendCommandDelayGetResponse(nil, nil, 1)
gobottest.Assert(t, err, invalidRead)
// Don't write any bytes and return an error
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 42, invalidWrite
}
_, err = sht3x.sendCommandDelayGetResponse(nil, nil, 1)
gobottest.Assert(t, err, invalidWrite)
}
// Test Heater and getStatusRegister
func TestSHT3xDriverHeater(t *testing.T) {
sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
// heater enabled
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0x20, 0x00, 0x5d})
return 3, nil
}
status, err := sht3x.Heater()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, status, true)
// heater disabled
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0x00, 0x00, 0x81})
return 3, nil
}
status, err = sht3x.Heater()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, status, false)
// heater crc failed
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0x00, 0x00, 0x00})
return 3, nil
}
status, err = sht3x.Heater()
gobottest.Assert(t, err, ErrInvalidCrc)
// heater read failed
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0x00, 0x00})
return 2, nil
}
status, err = sht3x.Heater()
gobottest.Refute(t, err, nil)
}
// Test SetHeater
func TestSHT3xDriverSetHeater(t *testing.T) {
sht3x, _ := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
sht3x.SetHeater(false)
sht3x.SetHeater(true)
}
// Test SerialNumber
func TestSHT3xDriverSerialNumber(t *testing.T) {
sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{0x20, 0x00, 0x5d, 0xbe, 0xef, 0x92})
return 6, nil
}
sn, err := sht3x.SerialNumber()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, sn, uint32(0x2000beef))
}