This repository was archived by the owner on Mar 23, 2026. It is now read-only.
forked from a8m/rql
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapping_test.go
More file actions
106 lines (103 loc) · 2.49 KB
/
mapping_test.go
File metadata and controls
106 lines (103 loc) · 2.49 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
package rql
import (
"fmt"
"testing"
"github.com/jinzhu/gorm"
)
func TestMapping(t *testing.T) {
tests := []struct {
name string
conf Config
input []byte
wantErr bool
wantOut *Params
}{
{
name: "test simple mapping of column names",
conf: Config{
ColumnFn: func(columnName string) string {
return fmt.Sprintf("person.%s", gorm.ToDBName(columnName))
},
Model: new(struct {
Age int `rql:"filter, sort"`
Name string `rql:"filter"`
Address string `rql:"filter"`
}),
DefaultSort: []string{"+age"},
DefaultLimit: 25,
},
input: []byte(`{
"filter": {
"name": "foo",
"age": 12,
"$or": [
{ "name": { "$in": ["foo", "bar"] } },
{ "address": "DC" },
{ "address": "Marvel" }
],
"$and": [
{ "age": { "$neq": 10} },
{ "age": { "$neq": 20} },
{ "$or": [{ "age": 11 }, {"age": 10}] }
]
}
}`),
wantOut: &Params{
Limit: 25,
FilterExp: "person.name = ? AND person.age = ? AND (person.name IN (?,?) OR person.address = ? OR person.address = ?) AND (person.age <> ? AND person.age <> ? AND (person.age = ? OR person.age = ?))",
FilterArgs: []interface{}{"foo", 12, "foo", "bar", "DC", "Marvel", 10, 20, 11, 10},
Sort: "person.age asc",
},
},
{
name: "test simple mapping of column values",
conf: Config{
ValueFn: func(columnName string) func(interface{}) interface{} {
return func(val interface{}) interface{} {
if columnName == "age" {
return val.(float64) + 20
}
return val
}
},
Model: new(struct {
Age int `rql:"filter, sort"`
Name string `rql:"filter"`
Address string `rql:"filter"`
}),
DefaultSort: []string{"+age"},
DefaultLimit: 25,
},
input: []byte(`{
"filter": {
"name": "foo",
"age": 12,
"$or": [
{ "age": 22 },
{ "age": 32 }
]
}
}`),
wantOut: &Params{
Limit: 25,
FilterExp: "name = ? AND age = ? AND (age = ? OR age = ?)",
FilterArgs: []interface{}{"foo", 32, 42, 52},
Sort: "age asc",
},
},
}
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)
})
}
}