-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathlist_test.go
145 lines (127 loc) · 3.78 KB
/
list_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package params
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestListParamsFromRequest(t *testing.T) {
// should work for nil list params & request with no list information
r, err := http.NewRequest("POST", "/test", nil)
if err != nil {
t.Fatal(err)
}
// should not error if there is an already populated params.List and no list
// information in the request
expect := List{Offset: 10}
got := List{Offset: 10}
err = got.ListParamsFromRequest(r)
if err != nil {
t.Fatalf("ListParamsFromRequest should not error if there is no list information in the request: %s", err)
}
if diff := cmp.Diff(expect, got); diff != "" {
t.Errorf("params.List mismatch (-want,+got):\n%s", diff)
}
// add list queries to the request
q := r.URL.Query()
q.Add("offset", "10")
q.Add("limit", "10")
q.Add("filter", "username:peer")
q.Add("orderby", "+name,-updated")
r.URL.RawQuery = q.Encode()
got = List{}
expect = List{
Offset: 10,
Limit: 10,
Filter: []string{"username:peer"},
OrderBy: OrderBy{{Key: "name", Direction: OrderASC}, {Key: "updated", Direction: OrderDESC}},
}
err = got.ListParamsFromRequest(r)
if err != nil {
t.Fatalf("ListParamsFromRequest unexpected error: %s", err)
}
if diff := cmp.Diff(expect, got); diff != "" {
t.Errorf("params.List mismatch (-want,+got):\n%s", diff)
}
// should error if list params are not empty & there is list info in the request
got = List{Filter: []string{"name:test"}}
err = got.ListParamsFromRequest(r)
if !errors.Is(err, ErrListParamsNotEmpty) {
t.Errorf("expected error to be %q, got %s", ErrListParamsNotEmpty, err)
}
}
func TestIsEmpty(t *testing.T) {
cases := []struct {
description string
lp List
expect bool
}{
{"empty list params", List{}, true},
{"each field empty", List{Offset: 0, Limit: 0, Filter: []string{}, OrderBy: OrderBy{}}, true},
{"has offset", List{Offset: 1}, false},
{"has limit", List{Limit: 1}, false},
{"has filter", List{Filter: []string{"public:true"}}, false},
{"has orderby", List{OrderBy: OrderBy{{Key: "", Direction: ""}}}, false},
}
for _, c := range cases {
got := c.lp.IsEmpty()
if got != c.expect {
t.Errorf("error in case %q, expected %t, got %t", c.description, c.expect, got)
}
}
}
func TestParamsWith(t *testing.T) {
expect := List{
OrderBy: OrderBy{{Key: "1", Direction: OrderASC}, {Key: "2", Direction: OrderASC}, {Key: "3", Direction: OrderASC}},
Filter: []string{"a", "b", "c"},
Offset: 200,
Limit: 100,
}
got := ListAll.WithFilters("a", "b", "c").WithOrderBy("1,2,3").WithOffsetLimit(200, 100)
if diff := cmp.Diff(expect, got); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
}
func TestParamsValidate(t *testing.T) {
good := []List{
{Limit: -1, Offset: 0},
}
for i, c := range good {
t.Run(fmt.Sprintf("good_case_%d", i), func(t *testing.T) {
if err := c.Validate(); err != nil {
t.Errorf("unexpected error: %s", err)
}
})
}
bad := []List{
{Offset: -1},
{Limit: -2},
}
for i, c := range bad {
t.Run(fmt.Sprintf("bad_case_%d", i), func(t *testing.T) {
if err := c.Validate(); err == nil {
t.Errorf("expected error, got none")
}
})
}
}
func TestListParamsAll(t *testing.T) {
cases := []struct {
p List
expect bool
}{
{List{Limit: -1, Offset: 0}, true},
{List{OrderBy: OrderBy{{Key: "date", Direction: OrderDESC}}, Limit: -1, Offset: 0}, true},
{List{Filter: []string{"user:noodles"}, Limit: -1, Offset: 0}, true},
{List{Limit: 0, Offset: 0}, false},
{List{Limit: -1, Offset: 100000}, false},
{List{OrderBy: OrderBy{{Key: "time", Direction: OrderASC}}, Limit: 0, Offset: 0}, false},
}
for i, c := range cases {
got := c.p.All()
if c.expect != got {
t.Errorf("case %d result mismatch. want: %t got: %t", i, c.expect, got)
}
}
}