-
Notifications
You must be signed in to change notification settings - Fork 44
/
options_test.go
110 lines (102 loc) · 2.59 KB
/
options_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
101
102
103
104
105
106
107
108
109
110
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package kivik
import (
"net/url"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestDuration(t *testing.T) {
o := Duration("heartbeat", 15*time.Second)
query := url.Values{}
o.Apply(&query)
want := "15000"
if got := query.Get("heartbeat"); got != want {
t.Errorf("Unexpected url query value: %s", got)
}
opts := map[string]interface{}{}
o.Apply(opts)
if got := opts["heartbeat"]; got != want {
t.Errorf("Unexpected map value: %s", got)
}
}
func Test_params_Apply(t *testing.T) {
tests := []struct {
name string
target interface{}
want interface{}
option params
}{
{
name: "no options",
target: map[string]interface{}{},
want: map[string]interface{}{},
},
{
name: "map option",
target: map[string]interface{}{},
want: map[string]interface{}{"foo": "bar"},
option: params{"foo": "bar"},
},
{
name: "unsupported target type",
target: "",
want: "",
option: params{"foo": "bar"},
},
{
name: "query, string",
target: &url.Values{},
want: &url.Values{"foo": []string{"bar"}},
option: params{"foo": "bar"},
},
{
name: "query, bool",
target: &url.Values{},
want: &url.Values{"foo": []string{"true"}},
option: params{"foo": true},
},
{
name: "query, int",
target: &url.Values{},
want: &url.Values{"foo": []string{"42"}},
option: params{"foo": 42},
},
{
name: "query, float64",
target: &url.Values{},
want: &url.Values{"foo": []string{"42.5"}},
option: params{"foo": 42.5},
},
{
name: "query, float32",
target: &url.Values{},
want: &url.Values{"foo": []string{"42.5"}},
option: params{"foo": float32(42.5)},
},
{
name: "query, []string",
target: &url.Values{},
want: &url.Values{"foo": []string{"bar", "baz"}},
option: params{"foo": []string{"bar", "baz"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.option.Apply(tt.target)
if d := cmp.Diff(tt.want, tt.target); d != "" {
t.Errorf("Unexpected result (-want, +got):\n%s", d)
}
})
}
}