1
2
3
4
5
6
7
8
9
10
11
12
13 package couchdb
14
15 import (
16 "encoding/json"
17 "net/http"
18 "testing"
19
20 "gitlab.com/flimzy/testy"
21
22 internal "github.com/go-kivik/kivik/v4/int/errors"
23 )
24
25 func TestEncodeKey(t *testing.T) {
26 type tst struct {
27 input interface{}
28 expected string
29 status int
30 err string
31 }
32 tests := testy.NewTable()
33 tests.Add("string", tst{
34 input: "foo",
35 expected: `"foo"`,
36 })
37 tests.Add("chan", tst{
38 input: make(chan int),
39 status: http.StatusBadRequest,
40 err: "json: unsupported type: chan int",
41 })
42 tests.Add("[]byte", tst{
43 input: []byte("foo"),
44 expected: `"Zm9v"`,
45 })
46 tests.Add("json.RawMessage", tst{
47 input: json.RawMessage(`"foo"`),
48 expected: `"foo"`,
49 })
50
51 tests.Run(t, func(t *testing.T, test tst) {
52 result, err := encodeKey(test.input)
53 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
54 t.Error(d)
55 }
56 if d := testy.DiffJSON([]byte(test.expected), []byte(result)); d != nil {
57 t.Error(d)
58 }
59 })
60 }
61
62 func TestEncodeKeys(t *testing.T) {
63 type tst struct {
64 input map[string]interface{}
65 expected map[string]interface{}
66 status int
67 err string
68 }
69 type keyStruct struct {
70 Foo string
71 Bar interface{} `json:",omitempty"`
72 }
73 tests := testy.NewTable()
74 tests.Add("nil", tst{
75 input: nil,
76 expected: nil,
77 })
78 tests.Add("unmarshalable", tst{
79 input: map[string]interface{}{
80 "key": make(chan int),
81 },
82 status: http.StatusBadRequest,
83 err: "json: unsupported type: chan int",
84 })
85 tests.Add("unaltered", tst{
86 input: map[string]interface{}{
87 "foo": 123,
88 },
89 expected: map[string]interface{}{
90 "foo": 123,
91 },
92 })
93 tests.Add("key", tst{
94 input: map[string]interface{}{
95 "key": 123,
96 },
97 expected: map[string]interface{}{
98 "key": "123",
99 },
100 })
101 tests.Add("keys []interface{}", tst{
102 input: map[string]interface{}{
103 "foo": 123,
104 "keys": []interface{}{"foo", 123},
105 },
106 expected: map[string]interface{}{
107 "foo": 123,
108 "keys": `["foo",123]`,
109 },
110 })
111 tests.Add("keys []interface{} invalid", tst{
112 input: map[string]interface{}{
113 "foo": 123,
114 "keys": []interface{}{"foo", 123, make(chan int)},
115 },
116 status: http.StatusBadRequest,
117 err: "json: unsupported type: chan int",
118 })
119 tests.Add("keys string", tst{
120 input: map[string]interface{}{
121 "foo": 123,
122 "keys": []string{"foo", "123"},
123 },
124 expected: map[string]interface{}{
125 "foo": 123,
126 "keys": `["foo","123"]`,
127 },
128 })
129 tests.Add("keys structs", tst{
130 input: map[string]interface{}{
131 "keys": []keyStruct{
132 {Foo: "abc"},
133 {Foo: "xyz"},
134 },
135 },
136 expected: map[string]interface{}{
137 "keys": `[{"Foo":"abc"},{"Foo":"xyz"}]`,
138 },
139 })
140 tests.Add("keys structs invalid", tst{
141 input: map[string]interface{}{
142 "keys": []keyStruct{
143 {Foo: "abc", Bar: make(chan int)},
144 {Foo: "xyz"},
145 },
146 },
147 status: http.StatusBadRequest,
148 err: "json: unsupported type: chan int",
149 })
150
151 tests.Run(t, func(t *testing.T, test tst) {
152 err := encodeKeys(test.input)
153 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
154 t.Error(d)
155 }
156 if err != nil {
157 return
158 }
159 if d := testy.DiffInterface(test.expected, test.input); d != nil {
160 t.Error(d)
161 }
162 })
163 }
164
View as plain text