-
Notifications
You must be signed in to change notification settings - Fork 1
/
route_test.go
50 lines (48 loc) · 1.15 KB
/
route_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
package main
import (
"github.com/xeipuuv/gojsonschema"
"testing"
)
func TestRouteSql(t *testing.T) {
schemaContent := `
{
"title": "Get user",
"description": "Get user",
"type": "object",
"properties": {
"id": {
"description": "User id",
"type": "integer"
}
},
"required": ["id"]
}
`
schema, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(string(schemaContent)))
if err != nil {
t.Errorf("Expected not to get error, but got: %v", err)
}
tmpl, err := makeTemplate(`select * from users where id={{.id}}`)
if err != nil {
t.Errorf("Expected not to get error, but got: %v", err)
}
route := &Route{
Versions: map[int]*RouteVersion{
0: {
Version: 0,
SqlTemplate: tmpl,
Schema: schema,
},
},
}
params := make(map[string]interface{})
params["id"] = 23
sql, err := route.Sql(params, 0)
if err != nil {
t.Errorf("Expected not to get error, but got: %v", err)
}
expected := "with response_table as (select * from users where id=23) select row_to_json(t) as value from (select * from response_table) t"
if sql != expected {
t.Errorf("Expected sql:\n%v, but got:\n%v\n", expected, sql)
}
}