...
1
2
3
4
5
6
7
8
9
10
11
12
13 package mockdb
14
15 import (
16 "encoding/json"
17 "testing"
18
19 "gitlab.com/flimzy/testy"
20
21 "github.com/go-kivik/kivik/v4/driver"
22 )
23
24 func TestDocument(t *testing.T) {
25 type tst struct {
26 i interface{}
27 expected *driver.Document
28 content interface{}
29 err string
30 }
31 tests := testy.NewTable()
32 tests.Add("simple doc", tst{
33 i: map[string]string{"foo": "bar"},
34 expected: &driver.Document{},
35 content: []byte(`{"foo":"bar"}`),
36 })
37 tests.Add("Unmarshalable", tst{
38 i: func() {},
39 err: "json: unsupported type: func()",
40 })
41 tests.Add("raw string", tst{
42 i: `{"foo":"bar"}`,
43 expected: &driver.Document{},
44 content: []byte(`{"foo":"bar"}`),
45 })
46 tests.Add("raw bytes", tst{
47 i: []byte(`{"foo":"bar"}`),
48 expected: &driver.Document{},
49 content: []byte(`{"foo":"bar"}`),
50 })
51 tests.Add("json.RawMessage", tst{
52 i: json.RawMessage(`{"foo":"bar"}`),
53 expected: &driver.Document{},
54 content: []byte(`{"foo":"bar"}`),
55 })
56 tests.Add("rev", tst{
57 i: `{"_rev":"1-xxx"}`,
58 expected: &driver.Document{
59 Rev: "1-xxx",
60 },
61 content: []byte(`{"_rev":"1-xxx"}`),
62 })
63
64 tests.Run(t, func(t *testing.T, test tst) {
65 result, err := Document(test.i)
66 if !testy.ErrorMatches(test.err, err) {
67 t.Errorf("Unexpected error: %s", err)
68 }
69 if err != nil {
70 return
71 }
72 if d := testy.DiffAsJSON(test.content, result.Body); d != nil {
73 t.Errorf("Unexpected content:\n%s\n", d)
74 }
75 result.Body.Close()
76 result.Body = nil
77 if d := testy.DiffInterface(test.expected, result); d != nil {
78 t.Error(d)
79 }
80 })
81 }
82
View as plain text