-
Notifications
You must be signed in to change notification settings - Fork 46
/
handler_test.go
72 lines (62 loc) · 1.26 KB
/
handler_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 weave
import (
"encoding/json"
"testing"
"github.com/iov-one/weave/errors"
"github.com/iov-one/weave/weavetest/assert"
)
func TestOptionsStream(t *testing.T) {
cases := map[string]struct {
json string
wantErr error
exp []struct{ Key int }
empty bool
}{
"happy path": {
json: `{"list": [{"key": 1}, {"key": 2}]}`,
exp: []struct{ Key int }{
{Key: 1},
{Key: 2},
},
wantErr: errors.ErrEmpty,
},
"empty list": {
json: `{}`,
wantErr: errors.ErrEmpty,
empty: true,
},
"wrong value": {
json: `{"list": [{"key": "dasdasas"}]}`,
exp: []struct{ Key int }{
{},
},
wantErr: errors.ErrInput,
},
"wrong body": {
json: `{"list": "adasda"}`,
wantErr: errors.ErrInput,
},
}
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
var o Options
var s struct{ Key int }
assert.Nil(t, json.Unmarshal([]byte(tc.json), &o))
f := o.Stream("list")
if tc.empty {
assert.IsErr(t, tc.wantErr, f(&s))
return
}
for _, e := range tc.exp {
err := f(&s)
if err != nil {
assert.IsErr(t, tc.wantErr, err)
return
}
assert.Equal(t, e, s)
}
assert.IsErr(t, tc.wantErr, f(&s))
assert.IsErr(t, errors.ErrState, f(&s))
})
}
}