-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilder_test.go
More file actions
232 lines (225 loc) · 7 KB
/
builder_test.go
File metadata and controls
232 lines (225 loc) · 7 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package q2sql
import (
"context"
"fmt"
"reflect"
"testing"
"github.com/velmie/qparser"
)
type resourceBuilderTest struct {
title string
query string
sql string
args []interface{}
expectedErr bool
sb func() *SelectBuilder
additionalOptions []ResourceSelectBuilderOption
}
const (
resourceName = "articles"
resourceFieldID = "id"
resourceFieldTitle = "title"
resourceFieldBody = "body"
resourceFieldCreatedAt = "created_at"
resourceFieldUpdatedAt = "updated_at"
filterEq = "eq"
filterAny = "any"
filterContains = "contains"
)
var resourceBuilderTests = []resourceBuilderTest{
{
title: "Default fields, no conditions",
query: "",
sql: fmt.Sprintf("SELECT * FROM %s", resourceName),
args: []interface{}{},
},
{
title: "Specific fields, no conditions",
query: fmt.Sprintf("fields[%s]=%s,%s", resourceName, resourceFieldID, resourceFieldTitle),
sql: fmt.Sprintf("SELECT %s, %s FROM %s", resourceFieldID, resourceFieldTitle, resourceName),
args: []interface{}{},
},
{
title: "Filter by id 42",
query: fmt.Sprintf("filter[%s]=%s:42", resourceFieldID, filterEq),
sql: fmt.Sprintf("SELECT * FROM %s WHERE %s = ?", resourceName, resourceFieldID),
args: []interface{}{"42"},
},
{
title: "Filter by any id 1,2,3,4,5 and body must contain word 'bitcoin'",
query: fmt.Sprintf("filter[%s]=%s:1,2,3,4,5&filter[%s]=%s:bitcoin", resourceFieldID, filterAny, resourceFieldBody, filterContains),
sql: fmt.Sprintf("SELECT * FROM %s WHERE %s IN (?,?,?,?,?) AND %s LIKE ?", resourceName, resourceFieldID, resourceFieldBody),
args: []interface{}{"1", "2", "3", "4", "5", "%bitcoin%"},
},
{
title: "Apply sorting to the result set where title is 'NewYear'",
query: fmt.Sprintf("sort=-createdAt&filter[%s]=%s:NewYear", resourceFieldTitle, filterEq),
sql: fmt.Sprintf("SELECT * FROM %s WHERE %s = ? ORDER BY %s DESC", resourceName, resourceFieldTitle, resourceFieldCreatedAt),
args: []interface{}{"NewYear"},
},
{
title: "Undefined field",
query: fmt.Sprintf("fields[%s]=unknown", resourceName),
expectedErr: true,
},
{
title: "Undefined filter",
query: fmt.Sprintf("filter[%s]=unknown:value", resourceFieldID),
expectedErr: true,
},
{
title: "Not allowed filter",
query: fmt.Sprintf("filter[%s]=%s:value", resourceFieldCreatedAt, filterEq),
expectedErr: true,
},
{
title: "Not allowed sorting",
query: "sort=id",
expectedErr: true,
},
{
title: "Field updated_at is not allowed",
query: fmt.Sprintf("fields[%s]=updatedAt", resourceName),
expectedErr: true,
},
{
title: "With extra params",
query: fmt.Sprintf("fields[%s]=%s,%s", resourceName, resourceFieldID, resourceFieldTitle),
sql: fmt.Sprintf("SELECT %s, %s FROM %s WHERE id = ?", resourceFieldID, resourceFieldTitle, resourceName),
args: []interface{}{"1"},
sb: func() *SelectBuilder {
sb := new(SelectBuilder)
return sb.Where(&RawSQLWithArgs{"id = ?", []interface{}{"1"}})
},
},
{
title: "Specific fields, specific always select field",
query: fmt.Sprintf("fields[%s]=%s,%s", resourceName, resourceFieldID, resourceFieldTitle),
sql: fmt.Sprintf("SELECT %s, %s, %s FROM %s", resourceFieldID, resourceFieldTitle, resourceFieldBody, resourceName),
args: []interface{}{},
additionalOptions: []ResourceSelectBuilderOption{
AlwaysSelectFields([]string{resourceFieldBody}),
},
},
{
title: "Specific fields, always select all fields",
query: fmt.Sprintf("fields[%s]=%s,%s", resourceName, resourceFieldID, resourceFieldTitle),
sql: fmt.Sprintf("SELECT *, %s, %s, %s, %s FROM %s", resourceFieldID, resourceFieldTitle, resourceFieldBody, resourceFieldCreatedAt, resourceName),
args: []interface{}{},
additionalOptions: []ResourceSelectBuilderOption{
AlwaysSelectAllFields(true),
},
},
{
title: "Second AllowSelectFields specification with duplicate *, always select all fields",
query: fmt.Sprintf("fields[%s]=%s,%s", resourceName, resourceFieldID, resourceFieldTitle),
sql: fmt.Sprintf("SELECT *, %s, %s, %s, %s, %s FROM %s", resourceFieldID, resourceFieldTitle, resourceFieldBody, resourceFieldCreatedAt, resourceFieldUpdatedAt, resourceName),
args: []interface{}{},
additionalOptions: []ResourceSelectBuilderOption{
AllowSelectFields(
[]string{
"*",
resourceFieldUpdatedAt,
},
),
AlwaysSelectAllFields(true),
},
},
}
func TestNewResourceSelectBuilder(t *testing.T) {
conditionFactory := ConditionMap{
filterEq: func(field string, args ...interface{}) (Sqlizer, error) {
return &Eq{
Field: field,
Value: args[0],
}, nil
},
filterAny: func(field string, args ...interface{}) (Sqlizer, error) {
return &In{
Field: field,
Values: args,
}, nil
},
filterContains: func(field string, args ...interface{}) (Sqlizer, error) {
if len(args) == 0 {
return RawSQL(""), nil
}
val := "%" + args[0].(string) + "%"
return &Like{
Field: field,
Value: val,
}, nil
},
}
translator := MapTranslator(
map[string]string{
"id": resourceFieldID,
"title": resourceFieldTitle,
"body": resourceFieldBody,
"createdAt": resourceFieldCreatedAt,
"updatedAt": resourceFieldUpdatedAt,
},
)
allowedConditions := AllowedConditions{
resourceFieldID: []string{filterEq, filterAny},
resourceFieldTitle: []string{filterEq, filterContains},
resourceFieldBody: []string{filterContains},
}
allowedSorting := []string{resourceFieldCreatedAt}
parser := NewDelimitedArgsParser(':', ',')
ctx := context.Background()
for i, tt := range resourceBuilderTests {
builder := NewResourceSelectBuilder(
resourceName,
translator,
append(
[]ResourceSelectBuilderOption{
AllowFiltering(allowedConditions, conditionFactory, parser),
AllowSortingByFields(allowedSorting),
WithDefaultFields([]string{"*"}),
AllowSelectFields(
[]string{
"*",
resourceFieldID,
resourceFieldTitle,
resourceFieldBody,
resourceFieldCreatedAt,
},
),
},
tt.additionalOptions...,
)...,
)
meta := fmt.Sprintf("test %d\n%s:\n\n\t", i, tt.title)
query, err := qparser.ParseQuery(tt.query)
if err != nil {
t.Fatalf("%sqparser.ParseQuery(%q) returned unexpected error: %s", meta, tt.query, err)
}
var sb []*SelectBuilder
if tt.sb != nil {
sb = []*SelectBuilder{tt.sb()}
}
sqlizer, err := builder.Build(ctx, query, sb...)
if !tt.expectedErr && err != nil {
t.Errorf("%sunexpected error %s", meta, err)
continue
} else if err != nil {
continue
}
sql, args, err := sqlizer.ToSQL()
if !tt.expectedErr && err != nil {
t.Errorf("%sunexpected error %s", meta, err)
continue
} else if err != nil {
continue
}
if sql != tt.sql {
t.Errorf("%sexpected sql %q\n\tgot %q", meta, tt.sql, sql)
continue
}
if !reflect.DeepEqual(args, tt.args) {
t.Errorf("%sexpected args %+v\n\tgot %+v", meta, tt.args, args)
continue
}
}
}