-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecorder_test.go
90 lines (82 loc) · 1.97 KB
/
recorder_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
package gogadgets_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"github.com/cswank/gogadgets"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Recorder", func() {
var (
r *gogadgets.Recorder
ts *httptest.Server
posts []string
urls []string
)
BeforeEach(func() {
posts = []string{}
urls = []string{}
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := &bytes.Buffer{}
io.Copy(buf, r.Body)
posts = append(posts, strings.TrimSpace(buf.String()))
urls = append(urls, r.URL.Path)
r.Body.Close()
}))
cfg := fmt.Sprintf(`{
"args": {
"host": "%s/api/fakeid/locations/%%s/devices/%%s/datapoints",
"token": "xyz"
}
}`, ts.URL)
p := &gogadgets.Pin{}
err := json.Unmarshal([]byte(cfg), p)
Expect(err).To(BeNil())
x, err := gogadgets.NewRecorder(p)
Expect(err).To(BeNil())
r = x.(*gogadgets.Recorder)
r.On(nil)
})
AfterEach(func() {
ts.Close()
})
Describe("when all's good", func() {
It("saves output values", func() {
msg := &gogadgets.Message{
Type: gogadgets.UPDATE,
Location: "lab",
Name: "thermometer",
Value: gogadgets.Value{
Value: 411.0,
},
}
r.Update(msg)
Expect(posts).To(ConsistOf(`{"value":411}`))
Expect(urls).To(ConsistOf("/api/fakeid/locations/lab/devices/thermometer/datapoints"))
})
It("saves input values", func() {
msg := &gogadgets.Message{
Type: gogadgets.UPDATE,
Location: "lab",
Name: "thermostat",
Value: gogadgets.Value{
Output: map[string]bool{
"heat": true,
"cool": false,
},
},
Info: gogadgets.Info{
Direction: "output",
},
}
r.Update(msg)
Expect(posts).To(ConsistOf(`{"value":1}`, `{"value":0}`))
Expect(urls).To(ConsistOf("/api/fakeid/locations/lab/devices/thermostat heat/datapoints", "/api/fakeid/locations/lab/devices/thermostat cool/datapoints"))
})
})
})