-
Notifications
You must be signed in to change notification settings - Fork 59
/
util_internal_test.go
101 lines (94 loc) · 2.12 KB
/
util_internal_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
package luno
import (
"testing"
"time"
"github.com/luno/luno-go/decimal"
)
func TestMakeURLValues(t *testing.T) {
type S string
type Req struct {
S string `url:"s"`
I int `url:"i"`
I64 int64 `url:"i64"`
F32 float32 `url:"f32"`
F64 float64 `url:"f64"`
B bool `url:"b"`
ASt []string `url:"ast"`
TS S `url:"ts"`
Amt decimal.Decimal `url:"amt"`
T Time `url:"t"` // implements QueryValuer
}
tests := []struct {
name string
r *Req
expected string
}{
{
name: "nil",
r: nil,
},
{
name: "valid time",
r: &Req{
S: "foo",
I: 42,
I64: 42,
F32: 42.42,
F64: 42.42,
B: true,
ASt: []string{"foo", "bar"},
TS: S("foo"),
Amt: decimal.NewFromFloat64(2.1, 1),
T: Time(time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)),
},
expected: "amt=2.1&ast=foo&ast=bar&b=true&f32=42.4200&f64=42.4200&i=42&i64=42&s=foo&t=1514764800000&ts=foo",
},
{
name: "zero time",
r: &Req{
S: "foo",
I: 42,
I64: 42,
F32: 42.42,
F64: 42.42,
B: true,
ASt: []string{"foo", "bar"},
TS: S("foo"),
Amt: decimal.NewFromFloat64(0.1, 1),
T: Time(time.Time{}),
},
expected: "amt=0.1&ast=foo&ast=bar&b=true&f32=42.4200&f64=42.4200&i=42&i64=42&s=foo&t=&ts=foo",
},
{
name: "valid amount",
r: &Req{
S: "foo",
I: 42,
I64: 42,
F32: 42.42,
F64: 42.42,
B: true,
ASt: []string{"foo", "bar"},
TS: S("foo"),
Amt: decimal.NewFromFloat64(0.0001, 10),
T: Time(time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)),
},
expected: "amt=0.0001000000&ast=foo&ast=bar&b=true&f32=42.4200&f64=42.4200&i=42&i64=42&s=foo&t=1514764800000&ts=foo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
act := makeURLValues(tt.r).Encode()
if act != tt.expected {
t.Errorf("Expected %q, got %q", tt.expected, act)
return
}
})
}
}
func TestNilURLValues(t *testing.T) {
act := makeURLValues(nil).Encode()
if act != "" {
t.Errorf("Expected empty url, got %s", act)
}
}