-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_test.go
72 lines (64 loc) · 1.96 KB
/
get_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
package yeelight
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testCtx = context.Background()
testHost = "im_test_home"
testResultOk = []byte(`{"id":123, "result":["ok"]}`) // deprecated
testResultOkStr = `{"id":123, "result":["ok"]}`
)
func TestClient_Get(t *testing.T) {
tests := map[string]struct {
properties []string
tr transportFn
exp map[string]string
expErr error
}{
"correct": {
properties: []string{PropertyPower, PropertySat},
tr: func(ctx context.Context, host string, raw string) ([]byte, error) {
assert.Equal(t, testCtx, ctx)
assert.Equal(t, testHost, host)
assert.Equal(t, `{"id":123,"method":"get_prop","params":["power","sat"]}`, raw)
return []byte(`{"id":1, "result":["on", "100"]}`), nil
},
exp: map[string]string{PropertyPower: "on", PropertySat: "100"},
},
"wrong_number_of_result_items": {
properties: []string{PropertyPower, PropertySat},
tr: func(ctx context.Context, host string, raw string) ([]byte, error) {
return []byte(`{"id":1, "result":["on"]}`), nil
},
expErr: ErrWrongNumberOfResultItems,
},
"bad_json": {
properties: []string{PropertyPower, PropertySat},
tr: func(ctx context.Context, host string, raw string) ([]byte, error) {
return []byte(`{"id":1, "result":"bad_value"}`), nil
},
expErr: ErrResponseJSONSyntax,
},
"empty_properties_list": {
properties: []string{},
exp: map[string]string{},
},
"err_connection": {
properties: []string{PropertyPower, PropertySat},
tr: func(ctx context.Context, host string, raw string) ([]byte, error) {
return nil, ErrConnect
},
expErr: ErrConnect,
},
}
for testCase, tt := range tests {
t.Run(testCase, func(t *testing.T) {
got, err := Client{host: testHost, transport: tt.tr}.GetProperties(testCtx, tt.properties)
require.Equal(t, tt.expErr, err)
require.Equal(t, tt.exp, got)
})
}
}