-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_test.go
100 lines (94 loc) · 1.82 KB
/
api_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
package bartend
import (
"flag"
"testing"
"github.com/freeconf/yang/fc"
"github.com/freeconf/yang/node"
"github.com/freeconf/yang/nodeutil"
"github.com/freeconf/yang/parser"
"github.com/freeconf/yang/source"
)
var updateGoldFiles = flag.Bool("update", false, "update expected golden file(s)")
func TestApi(t *testing.T) {
data := `
{
"pump": [
{
"id": 0,
"gpioPin": 1,
"liquid": "gin",
"timeToVolumeRatioMs": 10
},
{
"id": 1,
"gpioPin": 2,
"liquid": "juice",
"timeToVolumeRatioMs": 10
}
],
"recipe": [
{
"name": "snoop",
"description": "good for sipping",
"ingredient": [
{
"liquid": "gin",
"amount": 1.5
},
{
"liquid": "juice",
"amount": 1
}
]
},
{
"name": "straight gin",
"ingredient": [
{
"liquid": "gin",
"amount": 1
}
]
},
{
"name": "screwdriver gin",
"ingredient": [
{
"liquid": "vodka",
"amount": 1
},
{
"liquid": "juice",
"amount": 1
}
]
}
]
}`
ypath := source.Dir("./etc/yang")
m := parser.RequireModule(ypath, "bartend")
app := NewBartend()
b := node.NewBrowser(m, Node(app))
root := b.Root()
if err := root.InsertFrom(nodeutil.ReadJSON(data)).LastErr; err != nil {
t.Fatal(err)
}
in := nodeutil.ReadJSON(`{"multiplier":10}`)
update := make(chan bool)
var unsub node.NotifyCloser
var err error
if err := root.Find("available=snoop/make").Action(in).LastErr; err != nil {
t.Fatal(err)
}
unsub, err = root.Find("drink/update").Notifications(func(msg node.Notification) {
update <- true
defer unsub()
})
if err != nil {
t.Fatal(err)
}
<-update
actual, err := nodeutil.WritePrettyJSON(root)
fc.AssertEqual(t, nil, err)
fc.Gold(t, *updateGoldFiles, []byte(actual), "testdata/api.json")
}