-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers_test.go
64 lines (59 loc) · 1.46 KB
/
helpers_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
package kivikmock
import (
"encoding/json"
"testing"
"gitlab.com/flimzy/testy"
"github.com/go-kivik/kivik/v4/driver"
)
func TestDocument(t *testing.T) {
type tst struct {
i interface{}
expected *driver.Document
content interface{}
err string
}
tests := testy.NewTable()
tests.Add("simple doc", tst{
i: map[string]string{"foo": "bar"},
expected: &driver.Document{},
content: []byte(`{"foo":"bar"}`),
})
tests.Add("Unmarshalable", tst{
i: func() {},
err: "json: unsupported type: func()",
})
tests.Add("raw string", tst{
i: `{"foo":"bar"}`,
expected: &driver.Document{},
content: []byte(`{"foo":"bar"}`),
})
tests.Add("raw bytes", tst{
i: []byte(`{"foo":"bar"}`),
expected: &driver.Document{},
content: []byte(`{"foo":"bar"}`),
})
tests.Add("json.RawMessage", tst{
i: json.RawMessage(`{"foo":"bar"}`),
expected: &driver.Document{},
content: []byte(`{"foo":"bar"}`),
})
tests.Add("rev", tst{
i: `{"_rev":"1-xxx"}`,
expected: &driver.Document{
Rev: "1-xxx",
},
content: []byte(`{"_rev":"1-xxx"}`),
})
tests.Run(t, func(t *testing.T, test tst) {
result, err := Document(test.i)
testy.Error(t, test.err, err)
if d := testy.DiffAsJSON(test.content, result.Body); d != nil {
t.Errorf("Unexpected content:\n%s\n", d)
}
result.Body.Close() // nolint: errcheck
result.Body = nil
if d := testy.DiffInterface(test.expected, result); d != nil {
t.Error(d)
}
})
}