-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.go
121 lines (115 loc) · 2.4 KB
/
query_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
116
117
118
119
120
121
package psqlfront_test
import (
"io"
"log"
"testing"
psqlfront "github.com/mashiike/psql-front"
"github.com/stretchr/testify/require"
)
func TestAnalyzeQuery(t *testing.T) {
cases := []struct {
casename string
query string
tables []*psqlfront.Table
}{
{
casename: "basic select",
query: LoadFile(t, "testdata/sql/basic_select.sql"),
tables: []*psqlfront.Table{
{
SchemaName: "public",
RelName: "calender",
},
},
},
{
casename: "insert into select",
query: LoadFile(t, "testdata/sql/insert_into_select.sql"),
},
{
casename: "with cte",
query: LoadFile(t, "testdata/sql/with_cte.sql"),
tables: []*psqlfront.Table{
{
SchemaName: "public",
RelName: "calender",
},
{
SchemaName: "access",
RelName: "log",
},
},
},
{
casename: "fetch",
query: LoadFile(t, "testdata/sql/fetch.sql"),
tables: []*psqlfront.Table{},
},
{
casename: "declare_cursor",
query: LoadFile(t, "testdata/sql/declare_cursor.sql"),
tables: []*psqlfront.Table{
{
SchemaName: "example",
RelName: "fuga",
},
},
},
{
casename: "pg_catalog_extra_check",
query: LoadFile(t, "testdata/sql/pg_catalog.sql"),
tables: []*psqlfront.Table{
{
SchemaName: "pg_catalog",
RelName: "pg_namespace",
},
{
SchemaName: "pg_catalog",
RelName: "pg_class",
},
{
SchemaName: "pg_catalog",
RelName: "pg_attribute",
},
{
SchemaName: "example",
RelName: "fuga",
},
},
},
{
casename: "pg_catalog_list_tables",
query: LoadFile(t, "testdata/sql/pg_catalog_list_tables.sql"),
tables: []*psqlfront.Table{
{
SchemaName: "pg_catalog",
RelName: "pg_namespace",
},
{
SchemaName: "pg_catalog",
RelName: "pg_class",
},
},
},
}
for _, c := range cases {
t.Run(c.casename, func(t *testing.T) {
tables, err := psqlfront.AnalyzeQuery(c.query)
require.NoError(t, err)
require.ElementsMatch(t, c.tables, tables)
})
}
}
func BenchmarkAnalyzeQuery(b *testing.B) {
original := log.Default().Writer()
log.SetOutput(io.Discard)
defer log.SetOutput(original)
query := LoadFile(b, "testdata/sql/declare_cursor.sql")
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := psqlfront.AnalyzeQuery(query)
require.NoError(b, err)
}
})
}