forked from a8m/rql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearching_test.go
96 lines (92 loc) · 2.05 KB
/
searching_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
package rql
import (
"fmt"
"testing"
"github.com/jinzhu/gorm"
)
func TestSearching(t *testing.T) {
tests := []struct {
name string
conf Config
input []byte
wantErr bool
wantOut *Params
}{
{
name: "simple search of single column",
conf: Config{
Model: new(struct {
Age int `rql:"filter"`
Name string `rql:"filter, search"`
}),
DefaultLimit: 25,
},
input: []byte(`{
"search": {
"query": "foo"
}
}`),
wantOut: &Params{
Limit: 25,
Search: "LOWER(name) LIKE LOWER('%' || ? || '%')",
},
},
{
name: "multi-column search",
conf: Config{
Model: new(struct {
Age int `rql:"filter"`
Name string `rql:"filter, search"`
City string `rql:"filter, search"`
}),
DefaultLimit: 25,
},
input: []byte(`{
"search": {
"query": "foo"
}
}`),
wantOut: &Params{
Limit: 25,
Search: "LOWER(name) LIKE LOWER('%' || ? || '%') OR LOWER(city) LIKE LOWER('%' || ? || '%')",
},
},
{ // todo: the succes of this is undeterministic, since the order of elements in map is not guaranteed
name: "multi-column search with mapping",
conf: Config{
Model: new(struct {
Age int `rql:"filter"`
Name string `rql:"filter, search"`
City string `rql:"filter, search"`
}),
ColumnFn: func(columnName string) string {
return fmt.Sprintf("person.%s", gorm.ToDBName(columnName))
},
DefaultLimit: 25,
},
input: []byte(`{
"search": {
"query": "foo"
}
}`),
wantOut: &Params{
Limit: 25,
Search: "LOWER(person.city) LIKE LOWER('%' || ? || '%') OR LOWER(person.name) LIKE LOWER('%' || ? || '%')",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.conf.Log = t.Logf
p, err := NewParser(tt.conf)
if err != nil {
t.Fatalf("failed to build parser: %v", err)
}
out, err := p.Parse(tt.input)
if tt.wantErr != (err != nil) {
t.Fatalf("want: %v\ngot:%v\nerr: %v", tt.wantErr, err != nil, err)
}
assertParams(t, out, tt.wantOut)
})
}
}