This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
postgres_test.go
405 lines (320 loc) · 11.1 KB
/
postgres_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
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
package gondolier
import (
"database/sql"
"testing"
)
type testUser struct {
Id uint64 `gondolier:"type:bigint;pk;seq:1,1,-,-,1;default:nextval(seq);notnull"`
Name string `gondolier:"type:varchar(255);notnull;unique"`
Age uint `gondolier:"type:integer;notnull"`
Picture uint64 `gondolier:"type:bigint;fk:testPicture.Id;null"`
}
type testPicture struct {
Id uint64 `gondolier:"type:bigint;id"`
FileName string `gondolier:"type:varchar(255);notnull"`
}
type testPost struct {
Id uint64 `gondolier:"type:bigint;id"`
Post string `gondolier:"type:varchar(255);notnull"`
User uint64 `gondolier:"type:bigint;fk:testUser.Id;notnull"`
Picture uint64 `gondolier:"type:bigint;fk:testPicture.Id;null"`
}
type testArticle struct {
Id uint64 `gondolier:"type:bigint;id"`
Filename string `gondolier:"type:varchar(255);notnull"`
Tags []string `gondolier:"type:varchar(255)[]"`
Views uint `gondolier:"type:integer;notnull"`
WIP bool `gondolier:"type:boolean;notnull"`
ReadEveryone bool `gondolier:"type:boolean;notnull"`
WriteEveryone bool `gondolier:"type:boolean;notnull"`
SomeSlice []int `gondolier:"-"`
}
type testDropColumn struct {
Id uint64 `gondolier:"type:bigint;id"`
}
type testAddColumn struct {
Id uint64 `gondolier:"type:bigint;id"`
NewColumn string `gondolier:"type:varchar(255)"`
}
type testUpdateColumn struct {
Column string `gondolier:"type:varchar(255);default:'default';notnull;unique;pk"`
}
type testUpdateColumnReduce struct {
Column string `gondolier:"type:text"`
}
type testUpdateColumnSeq struct {
Column int `gondolier:"type:integer;seq:1,1,-,-,1;default:nextval(seq)"`
}
type testUpdateColumnSeqReduce struct {
Column int `gondolier:"type:integer"`
}
type testUpdateColumnFk struct {
Fk uint64 `gondolier:"type:bigint;fk:testOther.Id"`
}
type testUpdateColumnFkReduce struct {
Fk uint64 `gondolier:"type:bigint"`
}
type testNullableFields struct {
NullBool sql.NullBool `gondolier:"type:boolean"`
NullFloat sql.NullFloat64 `gondolier:"type:real"`
NullInt sql.NullInt64 `gondolier:"type:integer"`
NullString sql.NullString `gondolier:"type:text"`
}
func TestPostgresCreateTable(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresCreateTable ---")
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUser{}, testPost{}, testPicture{}, testArticle{})
Migrate()
if !postgres.tableExists("test_post") {
t.Fatal("Table must have been created: test_post")
}
if !postgres.tableExists("test_user") {
t.Fatal("Table must have been created: test_user")
}
if !postgres.tableExists("test_picture") {
t.Fatal("Table must have been created: test_picture")
}
if !postgres.tableExists("test_article") {
t.Fatal("Table must have been created: test_article")
}
if !postgres.sequenceExists("test_post_id_seq") {
t.Fatal("Sequence must have been created: test_post_id_seq")
}
if !postgres.sequenceExists("test_user_id_seq") {
t.Fatal("Sequence must have been created: test_user_id_seq")
}
if !postgres.sequenceExists("test_picture_id_seq") {
t.Fatal("Sequence must have been created: test_picture_id_seq")
}
if !postgres.sequenceExists("test_article_id_seq") {
t.Fatal("Sequence must have been created: test_article_id_seq")
}
if !postgres.foreignKeyExists("test_user", "test_user_picture_test_picture_id_fk") {
t.Fatal("Foreign key must have been created: test_user_test_picture_fk")
}
if !postgres.foreignKeyExists("test_post", "test_post_user_test_user_id_fk") {
t.Fatal("Foreign key must have been created: test_post_test_user_fk")
}
if !postgres.foreignKeyExists("test_post", "test_post_picture_test_picture_id_fk") {
t.Fatal("Foreign key must have been created: test_post_test_picture_fk")
}
}
func TestPostgresDropTable(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresDropTable ---")
if _, err := testdb.Exec(`CREATE TABLE "test_user" ("id" bigint not null)`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Drop(testUser{})
if postgres.tableExists("test_user") {
t.Fatal("Table must have been dropped")
}
}
func TestPostgresDropTableNotExists(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresDropTableNotExists ---")
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Drop(testUser{})
if postgres.tableExists("test_user") {
t.Fatal("Table must have been dropped")
}
}
func TestPostgresDropColumn(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresDropColumn ---")
if _, err := testdb.Exec(`CREATE TABLE "test_drop_column"
("id" bigint not null, "drop_me" text not null)`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", DropColumns: true, Log: true}
Use(testdb, postgres)
Model(testDropColumn{})
Migrate()
if postgres.columnExists("test_drop_column", "drop_me") {
t.Fatal("Column 'drop_me' should not exist anymore")
}
if !postgres.columnExists("test_drop_column", "id") {
t.Fatal("Column 'id' must still exist")
}
}
func TestPostgresAddColumn(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresAddColumn ---")
if _, err := testdb.Exec(`CREATE TABLE "test_add_column" ("id" bigint not null)`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testAddColumn{})
Migrate()
if !postgres.columnExists("test_add_column", "new_column") {
t.Fatal("Column 'new_column' must exist")
}
}
func TestPostgresUpdateColumn(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresUpdateColumn ---")
if _, err := testdb.Exec(`CREATE TABLE "test_update_column"
("column" text)`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUpdateColumn{})
Migrate()
istype := postgres.getColumnType("test_update_column", "column")
if istype != "character varying" {
t.Fatalf("Type must be character varying, but was %v", istype)
}
if postgres.isNullable("test_update_column", "column") {
t.Fatal("Column must not be nullable")
}
if !postgres.constraintExists("test_update_column_pkey") {
t.Fatal("Primary key constraint must exist")
}
if !postgres.constraintExists("test_update_column_column_key") {
t.Fatal("Unique constraint must exist")
}
}
func TestPostgresUpdateColumnReduce(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresUpdateColumnReduce ---")
if _, err := testdb.Exec(`CREATE TABLE "test_update_column_reduce"
("column" text NOT NULL PRIMARY KEY UNIQUE)`); err != nil {
t.Fatal(err)
}
if _, err := testdb.Exec(`ALTER TABLE "test_update_column_reduce"
RENAME CONSTRAINT "test_update_column_reduce_pkey"
TO "test_update_column_reduce_column_pkey"`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUpdateColumnReduce{})
Migrate()
if !postgres.isNullable("test_update_column_reduce", "column") {
t.Fatal("Column must be nullable")
}
if postgres.constraintExists("test_update_column_reduce_pkey") {
t.Fatal("Primary key constraint must not exist")
}
if postgres.constraintExists("test_update_column_column_reduce_unique") {
t.Fatal("Unique constraint must not exist")
}
}
func TestPostgresUpdateColumnSeq(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresUpdateColumnSeq ---")
if _, err := testdb.Exec(`CREATE TABLE "test_update_column_seq"
("column" integer)`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUpdateColumnSeq{})
Migrate()
if !postgres.sequenceExists("test_update_column_seq_column_seq") {
t.Fatal("Sequence must exist")
}
}
func TestPostgresUpdateColumnSeqReduce(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresUpdateColumnSeqReduce ---")
if _, err := testdb.Exec(`CREATE SEQUENCE "test_update_column_seq_reduce_column_seq"
START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1`); err != nil {
t.Fatal(err)
}
if _, err := testdb.Exec(`CREATE TABLE "test_update_column_seq_reduce"
("column" integer DEFAULT nextval('test_update_column_seq_reduce_column_seq'::regclass))`); err != nil {
t.Fatal(err)
}
if _, err := testdb.Exec(`ALTER SEQUENCE "test_update_column_seq_reduce_column_seq"
OWNED BY "test_update_column_seq_reduce"."column"`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUpdateColumnSeqReduce{})
Migrate()
if postgres.sequenceExists("test_update_column_seq_reduce_column_seq") {
t.Fatal("Sequence must not exist")
}
}
func TestPostgresUpdateColumnFk(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresUpdateColumnFk ---")
if _, err := testdb.Exec(`CREATE TABLE "test_other"
("id" bigint unique)`); err != nil {
t.Fatal(err)
}
if _, err := testdb.Exec(`CREATE TABLE "test_update_column_fk"
("fk" bigint)`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUpdateColumnFk{})
Migrate()
if !postgres.constraintExists("test_update_column_fk_fk_test_other_id_fk") {
t.Fatal("Foreign key must exist")
}
}
func TestPostgresUpdateColumnFkReduce(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresUpdateColumnFkReduce ---")
if _, err := testdb.Exec(`CREATE TABLE "test_other"
("id" bigint unique)`); err != nil {
t.Fatal(err)
}
if _, err := testdb.Exec(`CREATE TABLE "test_update_column_fk_reduce"
("fk" bigint REFERENCES "test_other"("id"))`); err != nil {
t.Fatal(err)
}
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUpdateColumnFkReduce{})
Migrate()
if postgres.constraintExists("test_update_column_fk_reduce_fk_test_other_id_fk") {
t.Fatal("Foreign key must not exist")
}
}
func TestPostgresDontDropPk(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresDontDropPk ---")
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testUser{}, testPost{}, testPicture{}, testArticle{})
Migrate()
t.Log("------------------------------")
Model(testUser{}, testPost{}, testPicture{}, testArticle{})
Migrate()
}
func TestPostgresNullableFields(t *testing.T) {
testCleanDb()
t.Log("--- TestPostgresNullableFields ---")
postgres := &Postgres{Schema: "public", Log: true}
Use(testdb, postgres)
Model(testNullableFields{})
Migrate()
}
func testCleanDb() {
testdb.Exec(`DROP TABLE IF EXISTS "test_post"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_user"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_picture"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_article"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_drop_column"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_add_column"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_update_column"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_update_column_reduce"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_update_column_seq"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_update_column_seq_reduce"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_update_column_fk"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_update_column_fk_reduce"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_other"`)
testdb.Exec(`DROP TABLE IF EXISTS "test_nullable_fields"`)
}