-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfi9800p_test.go
178 lines (165 loc) · 4.09 KB
/
fi9800p_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
package foscam
import (
"encoding/xml"
"io"
"net/http"
"reflect"
"strings"
"testing"
"github.com/mdmfernandes/go-foscam/mocks"
"github.com/mdmfernandes/go-foscam/testutil"
)
func TestFI9800p_GetMotionDetect(t *testing.T) {
tests := []struct {
name string
Client *mocks.MockHTTPClient
want fi9800pMotion
wantErr error
wantGetCalled int
}{
{
name: "Motion config ok",
Client: &mocks.MockHTTPClient{
GetFunc: func(_ string) (*http.Response, error) {
xml := `<CGI_Result>
<result>0</result>
<isEnable>1</isEnable>
<area4>69</area4>
</CGI_Result>`
b := io.NopCloser(strings.NewReader(xml))
return &http.Response{
StatusCode: http.StatusOK,
Body: b,
}, nil
},
},
want: fi9800pMotion{
XMLName: xml.Name{Local: "CGI_Result"},
IsEnable: 1,
Area4: 69,
},
wantErr: nil,
wantGetCalled: 1,
},
{
name: "Invalid XML response", // Didn't close CGI_Result
Client: &mocks.MockHTTPClient{
GetFunc: func(_ string) (*http.Response, error) {
xml := `<CGI_Result>
<result>0</result>
<isEnable>1</isEnable>
<area4>69</area4>`
b := io.NopCloser(strings.NewReader(xml))
return &http.Response{
StatusCode: http.StatusOK,
Body: b,
}, nil
},
},
want: fi9800pMotion{
XMLName: xml.Name{Local: "CGI_Result"},
IsEnable: 1,
Area4: 69,
},
wantErr: &xml.SyntaxError{Line: 4, Msg: "unexpected EOF"},
wantGetCalled: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cam := &fi9800p{
Client: tt.Client,
}
got, err := cam.GetMotionDetect()
if !testutil.EqualError(t, err, tt.wantErr) {
t.Errorf("Error: got = %v; want = %v", err, tt.wantErr)
}
if !reflect.DeepEqual(tt.want, got) {
t.Errorf("got = %q; want = %q", got, tt.want)
}
if tt.wantGetCalled != tt.Client.GetCount {
t.Errorf("Number of requests: got = %d; want = %d", tt.Client.GetCount, tt.wantGetCalled)
}
})
}
}
func TestFI9800p_updateMotionDetect(t *testing.T) {
// updateMotionDetect() arguments
type args struct {
mc fi9800pMotion
}
tests := []struct {
name string
args args
Client *mocks.MockHTTPClient
wantErr error
wantGetCalled int
}{
{
name: "update ok",
Client: &mocks.MockHTTPClient{
GetFunc: func(_ string) (*http.Response, error) {
xml := `<CGI_Result>
<result>0</result>
</CGI_Result>`
b := io.NopCloser(strings.NewReader(xml))
return &http.Response{
StatusCode: http.StatusOK,
Body: b,
}, nil
},
},
wantErr: nil,
wantGetCalled: 1,
},
{
name: "update fails (result not 0)", // result != 0
Client: &mocks.MockHTTPClient{
GetFunc: func(_ string) (*http.Response, error) {
xml := `<CGI_Result>
<result>1</result>
</CGI_Result>`
b := io.NopCloser(strings.NewReader(xml))
return &http.Response{
StatusCode: http.StatusOK,
Body: b,
}, nil
},
},
wantErr: &BadResponseError{0, 1},
wantGetCalled: 1,
},
{
name: "Invalid XML response", // Didn't close CGI_Result
Client: &mocks.MockHTTPClient{
GetFunc: func(_ string) (*http.Response, error) {
xml := `<CGI_Result>
<result>0</result>`
b := io.NopCloser(strings.NewReader(xml))
return &http.Response{
StatusCode: http.StatusOK,
Body: b,
}, nil
},
},
wantErr: &xml.SyntaxError{Line: 2, Msg: "unexpected EOF"},
wantGetCalled: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cam := &fi9800p{
Client: tt.Client,
}
if err := cam.updateMotionDetect(tt.args.mc); !testutil.EqualError(t, err, tt.wantErr) {
t.Errorf("Error: got = %v; want = %v", err, tt.wantErr)
}
if tt.wantGetCalled != tt.Client.GetCount {
t.Errorf("Number of requests: got = %d; want = %d", tt.Client.GetCount, tt.wantGetCalled)
}
})
}
}
func TestFI9800p_ChangeMotionStatus(t *testing.T) {
t.SkipNow()
}