Repository navigation
Expand file tree
/
Copy pathpostgres_test.go
More file actions
449 lines (429 loc) · 12.3 KB
/
Copy pathpostgres_test.go
File metadata and controls
449 lines (429 loc) · 12.3 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package postgres
import (
"strings"
"testing"
"github.com/mstgnz/sqlmapper"
"github.com/stretchr/testify/assert"
)
func TestPostgreSQL_Parse(t *testing.T) {
tests := []struct {
name string
content string
wantErr bool
validate func(*testing.T, *sqlmapper.Schema)
}{
{
name: "Empty content",
content: "",
wantErr: true,
},
{
name: "SET and Configuration Commands",
content: `
SET search_path TO myschema;
SET client_encoding = 'UTF8';
SET timezone = 'Europe/Istanbul';
ALTER SYSTEM SET work_mem = '16MB';
ALTER DATABASE mydb SET timezone = 'UTC';`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.NotNil(t, schema)
// SET komutları şu an için parse edilmiyor
},
},
{
name: "CREATE DATABASE",
content: `
CREATE DATABASE testdb
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Equal(t, "testdb", schema.Name)
},
},
{
name: "CREATE SCHEMA with Variations",
content: `
CREATE SCHEMA IF NOT EXISTS myschema;
CREATE SCHEMA myschema2 AUTHORIZATION postgres;
CREATE SCHEMA myschema3
CREATE TABLE mytable (id int);`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Contains(t, []string{"myschema", "myschema2", "myschema3"}, schema.Name)
},
},
{
name: "CREATE TABLE with All Features",
content: `
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INTEGER NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(20) CHECK (status IN ('new', 'processing', 'completed')),
total_amount DECIMAL(10,2) DEFAULT 0.00,
notes TEXT,
CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,
CONSTRAINT uq_order UNIQUE (order_id, customer_id)
) TABLESPACE mytablespace;
COMMENT ON TABLE orders IS 'Store orders table';
COMMENT ON COLUMN orders.status IS 'Order current status';`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Tables, 1)
table := schema.Tables[0]
assert.Equal(t, "orders", table.Name)
assert.Len(t, table.Columns, 6)
assert.Len(t, table.Constraints, 4) // PK, FK, CHECK, UNIQUE
},
},
{
name: "ALTER TABLE Operations",
content: `
ALTER TABLE employees ADD COLUMN email VARCHAR(100);
ALTER TABLE employees ALTER COLUMN salary SET DEFAULT 5000;
ALTER TABLE employees ALTER COLUMN active SET NOT NULL;
ALTER TABLE employees DROP COLUMN notes;
ALTER TABLE employees ADD CONSTRAINT chk_salary CHECK (salary > 0);
ALTER TABLE employees RENAME TO staff;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
// ALTER komutları şu an için parse edilmiyor
},
},
{
name: "DROP TABLE",
content: `
DROP TABLE IF EXISTS old_employees;
DROP TABLE employees CASCADE;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
// DROP komutları şu an için parse edilmiyor
},
},
{
name: "CREATE INDEX with Variations",
content: `
CREATE INDEX idx_employee_name ON employees(last_name, first_name);
CREATE UNIQUE INDEX idx_employee_email ON employees(email) WHERE active = true;
CREATE INDEX idx_employee_salary ON employees USING btree (salary DESC NULLS LAST);
CREATE INDEX idx_employee_document ON employees USING gin (document jsonb_path_ops);`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
// Index'ler tabloya bağlı olduğu için önce tablo oluşturulmalı
},
},
{
name: "CREATE SEQUENCE",
content: `
CREATE SEQUENCE order_seq
INCREMENT BY 1
MINVALUE 1
MAXVALUE 999999
START WITH 1
CACHE 20
CYCLE;
ALTER SEQUENCE order_seq RESTART WITH 1000;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Sequences, 1)
seq := schema.Sequences[0]
assert.Equal(t, "order_seq", seq.Name)
},
},
{
name: "CREATE VIEW and MATERIALIZED VIEW",
content: `
CREATE OR REPLACE VIEW employee_summary AS
SELECT
department_id,
COUNT(*) as employee_count,
AVG(salary) as avg_salary
FROM employees
GROUP BY department_id;
CREATE MATERIALIZED VIEW monthly_sales WITH (fillfactor=80) AS
SELECT
date_trunc('month', order_date) as month,
SUM(total_amount) as total_sales
FROM orders
GROUP BY date_trunc('month', order_date)
WITH DATA;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Views, 2)
assert.Equal(t, "employee_summary", schema.Views[0].Name)
assert.Equal(t, "monthly_sales", schema.Views[1].Name)
},
},
{
name: "CREATE FUNCTION and PROCEDURE",
content: `
CREATE OR REPLACE FUNCTION update_employee_status()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE PROCEDURE process_payroll(
month INTEGER,
year INTEGER
)
LANGUAGE plpgsql
AS $$
BEGIN
-- Process payroll logic
COMMIT;
END;
$$;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Functions, 2)
assert.Equal(t, "update_employee_status", schema.Functions[0].Name)
assert.Equal(t, "process_payroll", schema.Functions[1].Name)
},
},
{
name: "CREATE TRIGGER",
content: `
CREATE TRIGGER update_employee_timestamp
BEFORE UPDATE ON employees
FOR EACH ROW
EXECUTE FUNCTION update_employee_status();
CREATE TRIGGER check_salary_changes
AFTER UPDATE OF salary ON employees
FOR EACH ROW
WHEN (OLD.salary IS DISTINCT FROM NEW.salary)
EXECUTE FUNCTION audit_salary_changes();`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Triggers, 2)
assert.Equal(t, "update_employee_timestamp", schema.Triggers[0].Name)
assert.Equal(t, "check_salary_changes", schema.Triggers[1].Name)
},
},
{
name: "CREATE TYPE",
content: `
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TYPE complex AS (
r double precision,
t double precision
);
CREATE TYPE inventory_item AS (
name text,
supplier_id integer,
price numeric
);`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Types, 3)
assert.Equal(t, "mood", schema.Types[0].Name)
assert.Equal(t, "complex", schema.Types[1].Name)
assert.Equal(t, "inventory_item", schema.Types[2].Name)
},
},
{
name: "CREATE EXTENSION",
content: `
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION postgis;
CREATE EXTENSION pg_trgm WITH SCHEMA public;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Extensions, 3)
assert.Equal(t, "uuid-ossp", schema.Extensions[0].Name)
assert.Equal(t, "postgis", schema.Extensions[1].Name)
assert.Equal(t, "pg_trgm", schema.Extensions[2].Name)
},
},
{
name: "GRANT and REVOKE",
content: `
GRANT SELECT, INSERT ON employees TO hr_group;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO admin_role;
REVOKE UPDATE ON employees FROM intern_group;
GRANT EXECUTE ON FUNCTION calculate_salary(integer) TO payroll_group;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Permissions, 4)
},
},
{
name: "COMMENT",
content: `
COMMENT ON TABLE employees IS 'Company employees';
COMMENT ON COLUMN employees.salary IS 'Monthly salary in USD';
COMMENT ON FUNCTION calculate_salary(integer) IS 'Calculates employee salary with bonuses';`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
// Comment'ler ilgili nesnelerin comment field'larında saklanmalı
},
},
{
name: "Foreign Key Constraints",
content: `
CREATE TABLE departments (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
department_id INTEGER,
manager_id INTEGER,
CONSTRAINT fk_department
FOREIGN KEY (department_id)
REFERENCES departments(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_manager
FOREIGN KEY (manager_id)
REFERENCES employees(id)
ON DELETE SET NULL
);`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Len(t, schema.Tables, 2)
empTable := schema.Tables[1]
assert.Len(t, empTable.Constraints, 3) // PK + 2 FK
var fkCount int
for _, c := range empTable.Constraints {
if c.Type == "FOREIGN KEY" {
fkCount++
if c.Columns[0] == "department_id" {
assert.Equal(t, "CASCADE", c.DeleteRule)
} else if c.Columns[0] == "manager_id" {
assert.Equal(t, "SET NULL", c.DeleteRule)
}
}
}
assert.Equal(t, 2, fkCount)
},
},
{
name: "SET CONSTRAINTS",
content: `
SET CONSTRAINTS ALL DEFERRED;
SET CONSTRAINTS fk_department IMMEDIATE;`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
// SET CONSTRAINTS komutları şu an için parse edilmiyor
},
},
{
name: "INSERT INTO",
content: `
INSERT INTO departments (name) VALUES ('IT'), ('HR'), ('Sales');
INSERT INTO employees (name, department_id, salary)
VALUES
('John Doe', 1, 5000),
('Jane Smith', 2, 6000)
ON CONFLICT (id) DO UPDATE
SET salary = EXCLUDED.salary;
INSERT INTO employees
SELECT * FROM temp_employees
WHERE hire_date > '2023-01-01';`,
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
// INSERT komutları şu an için parse edilmiyor
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewPostgreSQL()
schema, err := p.Parse(tt.content)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.NotNil(t, schema)
if tt.validate != nil {
tt.validate(t, schema)
}
})
}
}
func TestPostgreSQL_Generate(t *testing.T) {
tests := []struct {
name string
schema *sqlmapper.Schema
want string
wantErr bool
}{
{
name: "Nil schema",
schema: nil,
wantErr: true,
},
{
name: "Basic schema with one table",
schema: &sqlmapper.Schema{
Tables: []sqlmapper.Table{
{
Name: "users",
Columns: []sqlmapper.Column{
{Name: "id", DataType: "SERIAL", IsPrimaryKey: true},
{Name: "name", DataType: "VARCHAR", Length: 100, IsNullable: false},
{Name: "email", DataType: "VARCHAR", Length: 255, IsNullable: false, IsUnique: true},
},
},
},
},
want: strings.TrimSpace(`
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);`),
wantErr: false,
},
{
name: "Schema with table and indexes",
schema: &sqlmapper.Schema{
Tables: []sqlmapper.Table{
{
Name: "products",
Columns: []sqlmapper.Column{
{Name: "id", DataType: "SERIAL", IsPrimaryKey: true},
{Name: "name", DataType: "VARCHAR", Length: 100, IsNullable: false},
{Name: "price", DataType: "NUMERIC", Length: 10, Scale: 2, IsNullable: true},
},
Indexes: []sqlmapper.Index{
{Name: "idx_name", Columns: []string{"name"}},
{Name: "idx_price", Columns: []string{"price"}, IsUnique: true},
},
},
},
},
want: strings.TrimSpace(`
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price NUMERIC(10,2)
);
CREATE INDEX idx_name ON products(name);
CREATE UNIQUE INDEX idx_price ON products(price);`),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewPostgreSQL()
result, err := s.Generate(tt.schema)
if (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.want != "" {
assert.Equal(t, tt.want, strings.TrimSpace(result))
}
})
}
}