-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgddl_test.go
More file actions
176 lines (157 loc) · 4.18 KB
/
pgddl_test.go
File metadata and controls
176 lines (157 loc) · 4.18 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package pgddl
import (
"strings"
"testing"
"github.com/pgplex/pgddl/catalog"
)
func TestLoadSQL_Basic(t *testing.T) {
sql := `
CREATE SCHEMA s1;
CREATE TABLE s1.users (
id integer PRIMARY KEY,
name text NOT NULL
);
CREATE INDEX users_name_idx ON s1.users (name);
`
c, err := LoadSQL(sql)
if err != nil {
t.Fatalf("LoadSQL: %v", err)
}
if c.GetRelation("s1", "users") == nil {
t.Error("expected table s1.users to exist")
}
}
func TestLoadSQL_ParseError(t *testing.T) {
sql := `CREATE TABLE t1 (id int; INVALID SYNTAX`
c, err := LoadSQL(sql)
if err == nil {
t.Fatal("expected parse error")
}
if c != nil {
t.Error("expected nil catalog on parse error")
}
}
func TestLoadSQL_DDLError(t *testing.T) {
sql := `
CREATE TABLE t1 (id integer);
CREATE TABLE t1 (id integer);
CREATE TABLE t2 (id integer);
`
c, err := LoadSQL(sql)
if err == nil {
t.Fatal("expected DDL error for duplicate table")
}
// Catalog should reflect the first successful statement.
if c == nil {
t.Fatal("expected non-nil catalog on DDL error")
}
if c.GetRelation("", "t1") == nil {
t.Error("expected t1 to exist (created before error)")
}
if c.GetRelation("", "t2") != nil {
t.Error("expected t2 to NOT exist (after error)")
}
}
func TestLoadSQL_PgDumpStyle(t *testing.T) {
sql := `
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
BEGIN;
CREATE TABLE users (id integer NOT NULL);
COMMIT;
SELECT pg_catalog.set_config('search_path', '', false);
`
c, err := LoadSQL(sql)
if err != nil {
t.Fatalf("LoadSQL: %v", err)
}
if c.GetRelation("", "users") == nil {
t.Error("expected table users to exist")
}
}
func TestExec_LineNumbers(t *testing.T) {
sql := "CREATE SCHEMA s1;\nCREATE TABLE s1.t1 (id int);\n\nCREATE INDEX idx ON s1.t1 (id);"
c := catalog.New()
results, err := c.Exec(sql, nil)
if err != nil {
t.Fatalf("Exec: %v", err)
}
if len(results) != 3 {
t.Fatalf("expected 3 results, got %d", len(results))
}
// Line numbers: "CREATE SCHEMA s1;" starts at line 1,
// "CREATE TABLE ..." at line 2, "CREATE INDEX ..." at line 4.
wantLines := []int{1, 2, 4}
for i, r := range results {
if r.Line != wantLines[i] {
t.Errorf("stmt %d: Line=%d, want %d", i, r.Line, wantLines[i])
}
}
// SQL text should be extracted.
if !strings.HasPrefix(results[0].SQL, "CREATE SCHEMA") {
t.Errorf("stmt 0: SQL=%q, want prefix 'CREATE SCHEMA'", results[0].SQL)
}
if !strings.HasPrefix(results[1].SQL, "CREATE TABLE") {
t.Errorf("stmt 1: SQL=%q, want prefix 'CREATE TABLE'", results[1].SQL)
}
if !strings.HasPrefix(results[2].SQL, "CREATE INDEX") {
t.Errorf("stmt 2: SQL=%q, want prefix 'CREATE INDEX'", results[2].SQL)
}
}
func TestExec_WithWarnings(t *testing.T) {
sql := `
CREATE TABLE t1 (id integer);
CREATE TABLE IF NOT EXISTS t1 (id integer);
CREATE TABLE t2 (id integer);
`
c := catalog.New()
results, err := c.Exec(sql, nil)
if err != nil {
t.Fatalf("Exec: %v", err)
}
if len(results) != 3 {
t.Fatalf("expected 3 results, got %d", len(results))
}
// Statement 0: success, no warnings.
if results[0].Error != nil {
t.Errorf("stmt 0: %v", results[0].Error)
}
if len(results[0].Warnings) != 0 {
t.Errorf("stmt 0: expected 0 warnings, got %d", len(results[0].Warnings))
}
// Statement 1: success with warning.
if results[1].Error != nil {
t.Errorf("stmt 1: %v", results[1].Error)
}
if len(results[1].Warnings) != 1 {
t.Fatalf("stmt 1: expected 1 warning, got %d", len(results[1].Warnings))
}
w := results[1].Warnings[0]
if w.Code != catalog.CodeWarningSkip {
t.Errorf("warning code=%q, want %q", w.Code, catalog.CodeWarningSkip)
}
if !strings.Contains(w.Message, "already exists") {
t.Errorf("warning message=%q, want substring 'already exists'", w.Message)
}
// Statement 1 should have SQL and line number.
if results[1].SQL == "" {
t.Error("stmt 1: SQL should not be empty")
}
if results[1].Line == 0 {
t.Error("stmt 1: Line should not be 0")
}
// Statement 2: success, no warnings.
if results[2].Error != nil {
t.Errorf("stmt 2: %v", results[2].Error)
}
}
func TestLoadSQL_Empty(t *testing.T) {
c, err := LoadSQL("")
if err != nil {
t.Fatalf("LoadSQL: %v", err)
}
if c == nil {
t.Error("expected non-nil catalog")
}
}