forked from pganalyze/pg_query_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_test.go
115 lines (108 loc) · 2.47 KB
/
split_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//go:build cgo
// +build cgo
package pg_query_test
import (
"testing"
pg_query "github.com/cossacklabs/pg_query_go/v5"
)
var splitTests = []struct {
name string
splitFunc func(string, bool) ([]string, error)
input string
trimSpace bool
expected []string
}{
{
name: "splitWithParser - basic split",
splitFunc: pg_query.SplitWithParser,
input: "select * from a;select * from b;",
trimSpace: true,
expected: []string{
"select * from a",
"select * from b",
},
},
{
name: "splitWithParser - procedure",
splitFunc: pg_query.SplitWithParser,
input: splitTestInput,
trimSpace: true,
expected: []string{
splitExpected1,
splitExpected2,
},
},
{
name: "splitWithParser - basic split, no trim",
splitFunc: pg_query.SplitWithParser,
input: " select * from a;select * from b;",
trimSpace: false,
expected: []string{
" select * from a",
"select * from b",
},
},
{
name: "splitWithScanner - basic split",
splitFunc: pg_query.SplitWithScanner,
input: "select * from a;select * from b;",
trimSpace: true,
expected: []string{
"select * from a",
"select * from b",
},
},
{
name: "splitWithScanner - procedure",
splitFunc: pg_query.SplitWithScanner,
input: splitTestInput,
trimSpace: true,
expected: []string{
splitExpected1,
splitExpected2,
},
},
{
name: "splitWithScanner - basic split, no trim",
splitFunc: pg_query.SplitWithScanner,
input: " select * from a;select * from b;",
trimSpace: false,
expected: []string{
" select * from a",
"select * from b",
},
},
}
var (
splitTestInput = `UPDATE client SET name = "John Doe" WHERE id = 1;
CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
BEGIN
RETURN i + 1;
END;
$$ LANGUAGE plpgsql;
`
splitExpected1 = `UPDATE client SET name = "John Doe" WHERE id = 1`
splitExpected2 = `CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
BEGIN
RETURN i + 1;
END;
$$ LANGUAGE plpgsql`
)
func TestSplit(t *testing.T) {
for _, test := range splitTests {
t.Run(test.name, func(t *testing.T) {
actuals, err := test.splitFunc(test.input, test.trimSpace)
if err != nil {
t.Error(err)
}
if len(actuals) != len(test.expected) {
t.Error("unexpected number of results")
}
for i, actual := range actuals {
if actual != test.expected[i] {
t.Errorf("expected: [%s], actual: [%s]", test.expected[i], actual)
}
}
})
}
}