-
Notifications
You must be signed in to change notification settings - Fork 79
/
normalize_test.go
96 lines (84 loc) · 2.22 KB
/
normalize_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
//go:build cgo
// +build cgo
package pg_query_test
import (
"reflect"
"testing"
pg_query "github.com/pganalyze/pg_query_go/v5"
"github.com/pganalyze/pg_query_go/v5/parser"
)
var normalizeTests = []struct {
input string
expected string
}{
{
"SELECT 1",
"SELECT $1",
},
}
func TestNormalize(t *testing.T) {
for _, test := range normalizeTests {
actual, err := pg_query.Normalize(test.input)
if err != nil {
t.Errorf("Normalize(%s)\nerror %s\n\n", test.input, err)
} else if !reflect.DeepEqual(actual, test.expected) {
t.Errorf("Normalize(%s)\nexpected %s\nactual %s\n\n", test.input, test.expected, actual)
}
}
}
var normalizeErrorTests = []struct {
input string
expectedErr error
}{
{
"SELECT $",
&parser.Error{
Message: "syntax error at or near \"$\"",
Cursorpos: 8,
Filename: "scan.l",
Funcname: "scanner_yyerror",
},
},
}
func TestNormalizeError(t *testing.T) {
for _, test := range normalizeErrorTests {
_, actualErr := pg_query.Normalize(test.input)
if actualErr == nil {
t.Errorf("Normalize(%s)\nexpected error but none returned\n\n", test.input)
} else {
exp := test.expectedErr.(*parser.Error)
act := actualErr.(*parser.Error)
act.Lineno = 0 // Line number is architecture dependent, so we ignore it
if !reflect.DeepEqual(act, exp) {
t.Errorf(
"Normalize(%s)\nexpected error %s at %d (%s:%d), func: %s, context: %s\nactual error %+v at %d (%s:%d), func: %s, context: %s\n\n",
test.input,
exp.Message, exp.Cursorpos, exp.Filename, exp.Lineno, exp.Funcname, exp.Context,
act.Message, act.Cursorpos, act.Filename, act.Lineno, act.Funcname, act.Context)
}
}
}
}
var normalizeUtilityTests = []struct {
input string
expected string
}{
{
"SELECT 1",
"SELECT 1",
},
{
"CREATE ROLE postgres PASSWORD 'xyz'",
"CREATE ROLE postgres PASSWORD $1",
},
}
func TestNormalizeUtility(t *testing.T) {
for _, test := range normalizeUtilityTests {
actual, err := pg_query.NormalizeUtility(test.input)
if err != nil {
t.Errorf("Normalize(%s)\nerror %s\n\n", test.input, err)
} else if !reflect.DeepEqual(actual, test.expected) {
t.Errorf("Normalize(%s)\nexpected %s\nactual %s\n\n", test.input, test.expected, actual)
}
}
}