-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqparser_test.go
More file actions
440 lines (358 loc) Β· 11.5 KB
/
qparser_test.go
File metadata and controls
440 lines (358 loc) Β· 11.5 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package qparser
import (
"errors"
"testing"
"time"
)
// TestIsQuery tests the IsQuery function
func TestIsQuery(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"title:hello", true},
{"title:hello world", true},
{"plain text", false},
{"", false},
{"title:", true},
{"just text without colon", false},
}
for _, test := range tests {
result := IsQuery(test.input)
if result != test.expected {
t.Errorf("IsQuery(%q) = %v, expected %v", test.input, result, test.expected)
}
}
}
// TestUnmarshal_BasicExample tests the basic example from README
func TestUnmarshal_BasicExample(t *testing.T) {
type SearchFilter struct {
Keyword string `query:"keyword,default"`
Title string `query:"title"`
Limit int `query:"limit"`
Tags []string `query:"tags"`
Before time.Time `query:"before,format=2006-01-02"`
}
q := `hello world title:foobar limit:42 tags:admin,editor,guest before:2024-12-31`
var filter SearchFilter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if filter.Keyword != "hello world" {
t.Errorf("Keyword = %q, expected %q", filter.Keyword, "hello world")
}
if filter.Title != "foobar" {
t.Errorf("Title = %q, expected %q", filter.Title, "foobar")
}
if filter.Limit != 42 {
t.Errorf("Limit = %d, expected %d", filter.Limit, 42)
}
expectedTags := []string{"admin", "editor", "guest"}
if len(filter.Tags) != len(expectedTags) {
t.Errorf("Tags length = %d, expected %d", len(filter.Tags), len(expectedTags))
}
for i, tag := range filter.Tags {
if i >= len(expectedTags) || tag != expectedTags[i] {
t.Errorf("Tags[%d] = %q, expected %q", i, tag, expectedTags[i])
}
}
expectedTime := time.Date(2024, 12, 31, 0, 0, 0, 0, time.UTC)
if !filter.Before.Equal(expectedTime) {
t.Errorf("Before = %v, expected %v", filter.Before, expectedTime)
}
}
// TestUnmarshal_BasicParsing tests basic parsing
func TestUnmarshal_BasicParsing(t *testing.T) {
type Filter struct {
Title string `query:"title"`
Author string `query:"author"`
Category string `query:"category"`
}
q := `title:hello author:world category:"science fiction"`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if filter.Title != "hello" {
t.Errorf("Title = %q, expected %q", filter.Title, "hello")
}
if filter.Author != "world" {
t.Errorf("Author = %q, expected %q", filter.Author, "world")
}
if filter.Category != "science fiction" {
t.Errorf("Category = %q, expected %q", filter.Category, "science fiction")
}
}
// TestUnmarshal_FallbackParsing tests fallback parsing
func TestUnmarshal_FallbackParsing(t *testing.T) {
type Filter struct {
Query string `query:"query,default"`
Type string `query:"type"`
}
q := `search for golang tutorials`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if filter.Query != "search for golang tutorials" {
t.Errorf("Query = %q, expected %q", filter.Query, "search for golang tutorials")
}
if filter.Type != "" {
t.Errorf("Type = %q, expected empty string", filter.Type)
}
}
// TestUnmarshal_EnhancedFallbackParsing tests enhanced fallback parsing
func TestUnmarshal_EnhancedFallbackParsing(t *testing.T) {
type Filter struct {
Keyword string `query:"keyword,default"`
Category string `query:"category"`
}
q := `hello world category:books foo bar emotion:happy`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
expectedKeyword := "hello world foo bar emotion:happy"
if filter.Keyword != expectedKeyword {
t.Errorf("Keyword = %q, expected %q", filter.Keyword, expectedKeyword)
}
if filter.Category != "books" {
t.Errorf("Category = %q, expected %q", filter.Category, "books")
}
}
// TestUnmarshal_NumericParsing tests numeric parsing
func TestUnmarshal_NumericParsing(t *testing.T) {
type Filter struct {
Price float64 `query:"price"`
Discount float32 `query:"discount"`
Quantity uint32 `query:"qty"`
MinAge int8 `query:"min_age"`
MaxSize uint64 `query:"max_size"`
}
q := `price:10.10 discount:0.10 qty:100 min_age:10 max_size:100000`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if filter.Price != 10.10 {
t.Errorf("Price = %f, expected %f", filter.Price, 10.10)
}
if filter.Discount != 0.10 {
t.Errorf("Discount = %f, expected %f", filter.Discount, 0.10)
}
if filter.Quantity != 100 {
t.Errorf("Quantity = %d, expected %d", filter.Quantity, 100)
}
if filter.MinAge != 10 {
t.Errorf("MinAge = %d, expected %d", filter.MinAge, 10)
}
if filter.MaxSize != 100000 {
t.Errorf("MaxSize = %d, expected %d", filter.MaxSize, 100000)
}
}
// TestUnmarshal_SliceParsing tests slice parsing
func TestUnmarshal_SliceParsing(t *testing.T) {
type Filter struct {
Tags []string `query:"tags"`
Scores []int `query:"scores"`
Weights []float64 `query:"weights"`
}
q := `tags:foo,bar scores:10,20,30 weights:0.1,0.2,0.3`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
expectedTags := []string{"foo", "bar"}
if len(filter.Tags) != len(expectedTags) {
t.Errorf("Tags length = %d, expected %d", len(filter.Tags), len(expectedTags))
}
for i, tag := range filter.Tags {
if i >= len(expectedTags) || tag != expectedTags[i] {
t.Errorf("Tags[%d] = %q, expected %q", i, tag, expectedTags[i])
}
}
expectedScores := []int{10, 20, 30}
if len(filter.Scores) != len(expectedScores) {
t.Errorf("Scores length = %d, expected %d", len(filter.Scores), len(expectedScores))
}
for i, score := range filter.Scores {
if i >= len(expectedScores) || score != expectedScores[i] {
t.Errorf("Scores[%d] = %d, expected %d", i, score, expectedScores[i])
}
}
expectedWeights := []float64{0.1, 0.2, 0.3}
if len(filter.Weights) != len(expectedWeights) {
t.Errorf("Weights length = %d, expected %d", len(filter.Weights), len(expectedWeights))
}
for i, weight := range filter.Weights {
if i >= len(expectedWeights) || weight != expectedWeights[i] {
t.Errorf("Weights[%d] = %f, expected %f", i, weight, expectedWeights[i])
}
}
}
// TestUnmarshal_SliceWithSpaces tests slice parsing with spaces
func TestUnmarshal_SliceWithSpaces(t *testing.T) {
type Filter struct {
Tags []string `query:"tags"`
}
q := `tags:"hello world,golang,web dev"`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
expectedTags := []string{"hello world", "golang", "web dev"}
if len(filter.Tags) != len(expectedTags) {
t.Errorf("Tags length = %d, expected %d", len(filter.Tags), len(expectedTags))
}
for i, tag := range filter.Tags {
if i >= len(expectedTags) || tag != expectedTags[i] {
t.Errorf("Tags[%d] = %q, expected %q", i, tag, expectedTags[i])
}
}
}
// TestUnmarshal_TimeParsing tests time parsing
func TestUnmarshal_TimeParsing(t *testing.T) {
type Filter struct {
StartedAt time.Time `query:"started_at"`
EndedAt time.Time `query:"ended_at,format=01/02/2006"`
PublishDate time.Time `query:"publish_date,format=2006 2006-01 2006-01-02"`
}
q := `started_at:2025-12-30 ended_at:12/30/2025 publish_date:2025`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
expectedStartedAt := time.Date(2025, 12, 30, 0, 0, 0, 0, time.UTC)
if !filter.StartedAt.Equal(expectedStartedAt) {
t.Errorf("StartedAt = %v, expected %v", filter.StartedAt, expectedStartedAt)
}
expectedEndedAt := time.Date(2025, 12, 30, 0, 0, 0, 0, time.UTC)
if !filter.EndedAt.Equal(expectedEndedAt) {
t.Errorf("EndedAt = %v, expected %v", filter.EndedAt, expectedEndedAt)
}
expectedPublishDate := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
if !filter.PublishDate.Equal(expectedPublishDate) {
t.Errorf("PublishDate = %v, expected %v", filter.PublishDate, expectedPublishDate)
}
}
// TestUnmarshal_BooleanParsing tests boolean parsing
func TestUnmarshal_BooleanParsing(t *testing.T) {
type Filter struct {
IsActive bool `query:"is_active"`
IsRequired *bool `query:"is_required"`
}
q := `is_active:true`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if !filter.IsActive {
t.Errorf("IsActive = %v, expected %v", filter.IsActive, true)
}
if filter.IsRequired != nil {
t.Errorf("IsRequired = %v, expected nil", *filter.IsRequired)
}
}
// TestUnmarshal_PointerTypes tests pointer types
func TestUnmarshal_PointerTypes(t *testing.T) {
type Filter struct {
Title *string `query:"title"`
Count *int `query:"count"`
Active *bool `query:"active"`
}
q := `title:hello count:42 active:true`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if filter.Title == nil || *filter.Title != "hello" {
t.Errorf("Title = %v, expected %q", filter.Title, "hello")
}
if filter.Count == nil || *filter.Count != 42 {
t.Errorf("Count = %v, expected %d", filter.Count, 42)
}
if filter.Active == nil || !*filter.Active {
t.Errorf("Active = %v, expected %v", filter.Active, true)
}
}
// TestUnmarshalStrict_InvalidValue tests UnmarshalStrict handling of invalid values
func TestUnmarshalStrict_InvalidValue(t *testing.T) {
type Filter struct {
Count int `query:"count"`
}
q := `count:invalid`
var filter Filter
err := UnmarshalStrict(q, &filter)
if err == nil {
t.Fatal("UnmarshalStrict should return error for invalid value")
}
if !errors.Is(err, ErrInvalidValue) {
t.Errorf("Expected ErrInvalidValue, got %v", err)
}
// Ensure field is set to zero value
if filter.Count != 0 {
t.Errorf("Count = %d, expected 0 (zero value)", filter.Count)
}
}
// TestUnmarshalStrict_UnknownField tests UnmarshalStrict handling of unknown fields
func TestUnmarshalStrict_UnknownField(t *testing.T) {
type Filter struct {
Title string `query:"title"`
Keyword string `query:"keyword,default"`
}
q := `title:hello unknown:value extra text`
var filter Filter
err := UnmarshalStrict(q, &filter)
if err == nil {
t.Fatal("UnmarshalStrict should return error for unknown field")
}
if !errors.Is(err, ErrUnknownField) {
t.Errorf("Expected ErrUnknownField, got %v", err)
}
// Ensure known fields parse correctly
if filter.Title != "hello" {
t.Errorf("Title = %q, expected %q", filter.Title, "hello")
}
// Ensure unknown fields and extra text are added to default field
expectedKeyword := "unknown:value extra text"
if filter.Keyword != expectedKeyword {
t.Errorf("Keyword = %q, expected %q", filter.Keyword, expectedKeyword)
}
}
// TestUnmarshal_NormalBehavior tests Unmarshal normal behavior (no errors returned)
func TestUnmarshal_NormalBehavior(t *testing.T) {
type Filter struct {
Count int `query:"count"`
Title string `query:"title"`
Keyword string `query:"keyword,default"`
}
q := `title:hello count:invalid unknown:value extra text`
var filter Filter
err := Unmarshal(q, &filter)
if err != nil {
t.Fatalf("Unmarshal should not return error: %v", err)
}
// Ensure known fields parse correctly
if filter.Title != "hello" {
t.Errorf("Title = %q, expected %q", filter.Title, "hello")
}
// Ensure invalid values are set to zero value
if filter.Count != 0 {
t.Errorf("Count = %d, expected 0 (zero value)", filter.Count)
}
// Ensure unknown fields and extra text are added to default field
expectedKeyword := "unknown:value extra text"
if filter.Keyword != expectedKeyword {
t.Errorf("Keyword = %q, expected %q", filter.Keyword, expectedKeyword)
}
}