-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcameras_test.go
103 lines (80 loc) · 3.18 KB
/
cameras_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
package securityspy_test
import (
"encoding/xml"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"golift.io/securityspy"
"golift.io/securityspy/mocks"
"golift.io/securityspy/server"
)
func TestUnmarshalXMLCameraSchedule(t *testing.T) {
t.Parallel()
assert := assert.New(t)
var s securityspy.CameraSchedule
err := xml.Unmarshal([]byte("<tag>3</tag>"), &s)
assert.Nil(err, "valid data must not produce an error")
assert.Equal(3, s.ID, "the data was not unmarshalled properly")
}
func TestAll(t *testing.T) {
t.Parallel()
assert := assert.New(t)
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
server := securityspy.NewMust(
&server.Config{Username: "user", Password: "pass", URL: "http://127.0.0.1:5678", VerifySSL: true})
fake := mocks.NewMockAPI(mockCtrl)
server.API = fake
fake.EXPECT().GetXML(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Do(
func(_, _, v interface{}) {
_ = xml.Unmarshal([]byte(testSystemInfo), &v)
},
)
assert.Nil(server.Refresh(), "there must no error when loading fake data") // load the fake testSystemInfo data.
cams := server.Cameras.All()
assert.EqualValues(2, len(cams), "the data contains two cameras, two cameras must be returned")
}
func TestByNum(t *testing.T) {
t.Parallel()
assert := assert.New(t)
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
server := securityspy.NewMust(
&server.Config{Username: "user", Password: "pass", URL: "http://127.0.0.1:5678", VerifySSL: true})
fake := mocks.NewMockAPI(mockCtrl)
server.API = fake
fake.EXPECT().GetXML(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Do(
func(_, _, v interface{}) {
_ = xml.Unmarshal([]byte(testSystemInfo), &v)
},
)
assert.Nil(server.Refresh(), "there must no error when loading fake data") // load the fake testSystemInfo data.
cam := server.Cameras.ByNum(1)
assert.EqualValues("Porch", cam.Name, "camera 1 is Porch in the test data")
assert.Nil(server.Cameras.ByNum(99), "a non-existent camera must return nil")
}
func TestByName(t *testing.T) {
t.Parallel()
assert := assert.New(t)
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
server := securityspy.NewMust(
&server.Config{Username: "user", Password: "pass", URL: "http://127.0.0.1:5678", VerifySSL: true})
fake := mocks.NewMockAPI(mockCtrl)
server.API = fake
fake.EXPECT().GetXML(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Do(
func(_, _, v interface{}) {
_ = xml.Unmarshal([]byte(testSystemInfo), &v)
},
)
assert.Nil(server.Refresh(), "there must no error when loading fake data") // load the fake testSystemInfo data.
cam := server.Cameras.ByName("Porch")
assert.EqualValues(1, cam.Number, "camera 1 is Porch in the test data")
assert.Nil(server.Cameras.ByName("not here"), "a non-existent camera must return nil")
cam = server.Cameras.ByName("porch2")
assert.Nil(cam, "there is no camera named porch2")
cam = server.Cameras.ByName("porch")
assert.EqualValues(1, cam.Number, "camera 1 is Porch in the test data")
assert.Nil(server.Cameras.ByName("not here"), "a non-existent camera must return nil")
}
/* Having a comment at the end of the file like this allows commenting the whole file easily. */