-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_test.go
More file actions
132 lines (125 loc) · 3.07 KB
/
query_test.go
File metadata and controls
132 lines (125 loc) · 3.07 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package routeit
import "testing"
func TestQueryParams(t *testing.T) {
tests := []struct {
name string
rawQuery string
key string
wantFound bool
wantOnly string
wantOnlyOk bool
wantOnlyErr bool
wantFirst string
wantFirstOk bool
wantLast string
wantLastOk bool
}{
{
name: "no param",
key: "x",
wantFound: false,
wantOnlyOk: false,
wantFirstOk: false,
wantLastOk: false,
},
{
name: "single param",
rawQuery: "x=hello",
key: "x",
wantFound: true,
wantOnly: "hello",
wantOnlyOk: true,
wantFirst: "hello",
wantFirstOk: true,
wantLast: "hello",
wantLastOk: true,
},
{
name: "multiple param values",
rawQuery: "x=a&x=b&x=c",
key: "x",
wantFound: true,
wantOnlyErr: true,
wantOnlyOk: true,
wantFirst: "a",
wantFirstOk: true,
wantLast: "c",
wantLastOk: true,
},
{
name: "empty value",
rawQuery: "x=",
key: "x",
wantFound: true,
wantOnly: "",
wantOnlyOk: true,
wantFirst: "",
wantFirstOk: true,
wantLast: "",
wantLastOk: true,
},
{
name: "empty and non-empty values",
rawQuery: "x=&x=val",
key: "x",
wantFound: true,
wantOnlyErr: true,
wantOnlyOk: true,
wantFirst: "",
wantFirstOk: true,
wantLast: "val",
wantLastOk: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
queries := newQueryParams()
err := parseQueryParams(tc.rawQuery, queries)
if err != nil {
t.Fatalf("parseUri(%q) failed: %v", tc.rawQuery, err)
}
t.Run("All", func(t *testing.T) {
vals, found := queries.All(tc.key)
if found != tc.wantFound {
t.Errorf("All(%q): found = %v, want %v", tc.key, found, tc.wantFound)
}
if found && len(vals) == 0 {
t.Errorf("All(%q): returned empty slice unexpectedly", tc.key)
}
})
t.Run("Only", func(t *testing.T) {
got, found, err := queries.Only(tc.key)
if tc.wantOnlyErr && err == nil {
t.Errorf("Only(%q): expected error, got nil", tc.key)
}
if !tc.wantOnlyErr && err != nil {
t.Errorf("Only(%q): unexpected error: %v", tc.key, err)
}
if found != tc.wantOnlyOk {
t.Errorf("Only(%q): found = %v, want %v", tc.key, found, tc.wantOnlyOk)
}
if got != tc.wantOnly {
t.Errorf("Only(%q): got = %q, want %q", tc.key, got, tc.wantOnly)
}
})
t.Run("First", func(t *testing.T) {
got, found := queries.First(tc.key)
if found != tc.wantFirstOk {
t.Errorf("Fist(%q): found = %v, want %v", tc.key, found, tc.wantFirstOk)
}
if got != tc.wantFirst {
t.Errorf("First(%q): got = %q, want %q", tc.key, got, tc.wantFirst)
}
})
t.Run("Last", func(t *testing.T) {
got, found := queries.Last(tc.key)
if found != tc.wantLastOk {
t.Errorf("Last(%q): found = %v, want %v", tc.key, found, tc.wantLastOk)
}
if got != tc.wantLast {
t.Errorf("Last(%q): got = %q, want %q", tc.key, got, tc.wantLast)
}
})
})
}
}