-
Notifications
You must be signed in to change notification settings - Fork 0
/
qparser_test.go
128 lines (115 loc) · 4 KB
/
qparser_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
package qparser
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLimitPage(t *testing.T) {
url := "http://some-api.com/api/endpoint?limit=1&page=2"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
assert.Equal(t, 1, res.Pagination.Limit)
assert.Equal(t, 2, res.Pagination.Page)
}
}
func TestLimitPageDefault(t *testing.T) {
url := "http://some-api.com/api/endpoint"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
assert.Equal(t, 25, res.Pagination.Limit)
assert.Equal(t, 1, res.Pagination.Page)
}
}
func TestLimitPageCustom(t *testing.T) {
opts := &ParserOptions {
LimitString: "l",
PageString: "pg",
}
url := "http://some-api.com/api/endpoint?l=1&pg=2"
parser := NewParser(opts)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
assert.Equal(t, 1, res.Pagination.Limit)
assert.Equal(t, 2, res.Pagination.Page)
}
}
func TestQueryDefault(t *testing.T) {
url := "http://some-api.com/api/endpoint?q=somename&p=name,description"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
if assert.Equal(t, 2, len(res.Values.Search.Keys)) {
assert.Equal(t, "name", res.Values.Search.Keys[0])
assert.Equal(t, "description", res.Values.Search.Keys[1])
}
assert.Equal(t, "somename", res.Values.Search.Value)
}
}
func TestQueryDefault2(t *testing.T) {
url := "http://some-api.com/api/endpoint?q=somename&p=name&p=description"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
if assert.Equal(t, 2, len(res.Values.Search.Keys)) {
assert.Equal(t, "name", res.Values.Search.Keys[0])
assert.Equal(t, "description", res.Values.Search.Keys[1])
}
assert.Equal(t, "somename", res.Values.Search.Value)
}
}
func TestQueryAllParams(t *testing.T) {
url := "http://some-api.com/api/endpoint?q=somename"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
if assert.Equal(t, 0, len(res.Values.Search.Keys)) {
assert.Equal(t, "somename", res.Values.Search.Value)
}
}
}
func TestQueryParticular(t *testing.T) {
url := "http://some-api.com/api/endpoint?fruit=apple&color=red"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
if assert.Equal(t, 2, len(res.Values.Filter)) {
assert.Equal(t, "apple", res.Values.Filter["fruit"][0])
assert.Equal(t, "red", res.Values.Filter["color"][0])
}
}
}
func TestExpandItem(t *testing.T) {
url := "http://some-api.com/api/endpoint?expand=relation"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
assert.NotNil(t, res.Expand.Get("relation"))
}
}
func TestExpandList(t *testing.T) {
url := "http://some-api.com/api/endpoint?expand=relation(limit:6,page:8)"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
if assert.NotNil(t, res.Expand.Get("relation")) {
assert.Equal(t, 6, res.Expand.Get("relation").Limit)
assert.Equal(t, 8, res.Expand.Get("relation").Page)
}
}
}
func TestOrder(t *testing.T) {
url := "http://some-api.com/api/endpoint?order=field1(asc),field2(desc)"
parser := NewParser(nil)
res, err := parser.ParseString(url)
if assert.Nil(t, err) {
if assert.NotNil(t, res.Values.Order) {
v1 := (*res.Values.Order)[0]
v2 := (*res.Values.Order)[1]
assert.Equal(t, "field1", v1.Field)
assert.Equal(t, ASC, v1.Order)
assert.Equal(t, "field2", v2.Field)
assert.Equal(t, DESC, v2.Order)
}
}
}