1
|
|
|
package cli |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
|
6
|
|
|
"github.com/stretchr/testify/assert" |
7
|
|
|
"github.com/stretchr/testify/mock" |
8
|
|
|
|
9
|
|
|
"github.com/fraenky8/tables-to-go/pkg/database" |
10
|
|
|
"github.com/fraenky8/tables-to-go/pkg/settings" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
type mockDb struct { |
14
|
|
|
mock.Mock |
15
|
|
|
database.Database |
16
|
|
|
|
17
|
|
|
tables []*database.Table |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
func newMockDb(db database.Database) *mockDb { |
21
|
|
|
return &mockDb{Database: db} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
func (db *mockDb) Connect() (err error) { |
25
|
|
|
db.Called() |
26
|
|
|
return nil |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
func (db *mockDb) Close() (err error) { |
30
|
|
|
db.Called() |
31
|
|
|
return nil |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func (db *mockDb) GetTables() (tables []*database.Table, err error) { |
35
|
|
|
db.Called() |
36
|
|
|
return db.tables, nil |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
func (db *mockDb) PrepareGetColumnsOfTableStmt() (err error) { |
40
|
|
|
db.Called() |
41
|
|
|
return nil |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func (db *mockDb) GetColumnsOfTable(table *database.Table) (err error) { |
45
|
|
|
db.Called(table) |
46
|
|
|
return nil |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
type mockWriter struct { |
50
|
|
|
mock.Mock |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
func newMockWriter() *mockWriter { |
54
|
|
|
return &mockWriter{} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func (w *mockWriter) Write(tableName string, content string) error { |
58
|
|
|
w.Called(tableName, content) |
59
|
|
|
return nil |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
func Test_toInitialisms(t *testing.T) { |
63
|
|
|
tests := []struct { |
64
|
|
|
desc string |
65
|
|
|
intput string |
66
|
|
|
expected string |
67
|
|
|
}{ |
68
|
|
|
{ |
69
|
|
|
desc: "id should be upper case", |
70
|
|
|
intput: "Id", |
71
|
|
|
expected: "ID", |
72
|
|
|
}, |
73
|
|
|
{ |
74
|
|
|
desc: "id at the end of string should be upper case", |
75
|
|
|
intput: "userId", |
76
|
|
|
expected: "userID", |
77
|
|
|
}, |
78
|
|
|
{ |
79
|
|
|
desc: "id at the beginning of string should be upper case", |
80
|
|
|
intput: "Iduser", |
81
|
|
|
expected: "IDuser", |
82
|
|
|
}, |
83
|
|
|
{ |
84
|
|
|
desc: "id in the middle of string should be upper case", |
85
|
|
|
intput: "userIdprim", |
86
|
|
|
expected: "userIDprim", |
87
|
|
|
}, |
88
|
|
|
{ |
89
|
|
|
desc: "multiple occurrences should be upper case", |
90
|
|
|
intput: "userIdasJsonWithUrl", |
91
|
|
|
expected: "userIDasJSONWithURL", |
92
|
|
|
}, |
93
|
|
|
{ |
94
|
|
|
desc: "multiple id in the string should be upper case", |
95
|
|
|
intput: "IduserId", |
96
|
|
|
expected: "IDuserID", |
97
|
|
|
}, |
98
|
|
|
{ |
99
|
|
|
desc: "non replacement in the string should be return original string", |
100
|
|
|
intput: "name", |
101
|
|
|
expected: "name", |
102
|
|
|
}, |
103
|
|
|
{ |
104
|
|
|
desc: "replacements only in the string should be return original string", |
105
|
|
|
intput: "IdjsonuRlHtTp", |
106
|
|
|
expected: "IDJSONURLHTTP", |
107
|
|
|
}, |
108
|
|
|
} |
109
|
|
|
for _, tt := range tests { |
110
|
|
|
t.Run(tt.desc, func(t *testing.T) { |
111
|
|
|
actual := toInitialisms(tt.intput) |
112
|
|
|
assert.Equal(t, tt.expected, actual, "test case input: "+tt.intput) |
113
|
|
|
}) |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
func TestRun_StringTextColumns(t *testing.T) { |
118
|
|
|
for dbType := range settings.SupportedDbTypes { |
119
|
|
|
t.Run(dbType.String(), func(t *testing.T) { |
120
|
|
|
|
121
|
|
|
s := settings.New() |
122
|
|
|
s.DbType = dbType |
123
|
|
|
db := database.New(s) |
124
|
|
|
|
125
|
|
|
columnTypes := db.GetStringDatatypes() |
126
|
|
|
|
127
|
|
|
for _, columnType := range columnTypes { |
128
|
|
|
t.Run(columnType, func(t *testing.T) { |
129
|
|
|
|
130
|
|
|
t.Run("single table with NOT NULL column", func(t *testing.T) { |
131
|
|
|
s := settings.New() |
132
|
|
|
s.DbType = dbType |
133
|
|
|
|
134
|
|
|
mdb := newMockDb(db) |
135
|
|
|
|
136
|
|
|
table := &database.Table{ |
137
|
|
|
Name: "test_table", |
138
|
|
|
Columns: []database.Column{ |
139
|
|
|
{ |
140
|
|
|
OrdinalPosition: 1, |
141
|
|
|
Name: "column_name", |
142
|
|
|
DataType: columnType, |
143
|
|
|
}, |
144
|
|
|
}, |
145
|
|
|
} |
146
|
|
|
mdb.tables = append(mdb.tables, table) |
147
|
|
|
|
148
|
|
|
mdb. |
149
|
|
|
On("GetTables"). |
150
|
|
|
Return(mdb.tables, nil) |
151
|
|
|
mdb. |
152
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
153
|
|
|
Return(nil) |
154
|
|
|
mdb. |
155
|
|
|
On("GetColumnsOfTable", table) |
156
|
|
|
|
157
|
|
|
w := newMockWriter() |
158
|
|
|
w. |
159
|
|
|
On( |
160
|
|
|
"Write", |
161
|
|
|
"TestTable", |
162
|
|
|
"package dto\n\ntype TestTable struct {\nColumnName string `db:\"column_name\"`\n}", |
163
|
|
|
) |
164
|
|
|
|
165
|
|
|
err := Run(s, mdb, w) |
166
|
|
|
assert.NoError(t, err) |
167
|
|
|
}) |
168
|
|
|
|
169
|
|
|
t.Run("single table with NULL column", func(t *testing.T) { |
170
|
|
|
s := settings.New() |
171
|
|
|
s.DbType = dbType |
172
|
|
|
|
173
|
|
|
mdb := newMockDb(db) |
174
|
|
|
|
175
|
|
|
table := &database.Table{ |
176
|
|
|
Name: "test_table", |
177
|
|
|
Columns: []database.Column{ |
178
|
|
|
{ |
179
|
|
|
OrdinalPosition: 1, |
180
|
|
|
Name: "column_name", |
181
|
|
|
DataType: columnType, |
182
|
|
|
IsNullable: "YES", |
183
|
|
|
}, |
184
|
|
|
}, |
185
|
|
|
} |
186
|
|
|
mdb.tables = append(mdb.tables, table) |
187
|
|
|
|
188
|
|
|
mdb. |
189
|
|
|
On("GetTables"). |
190
|
|
|
Return(mdb.tables, nil) |
191
|
|
|
mdb. |
192
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
193
|
|
|
Return(nil) |
194
|
|
|
mdb. |
195
|
|
|
On("GetColumnsOfTable", table) |
196
|
|
|
|
197
|
|
|
w := newMockWriter() |
198
|
|
|
w. |
199
|
|
|
On( |
200
|
|
|
"Write", |
201
|
|
|
"TestTable", |
202
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName sql.NullString `db:\"column_name\"`\n}", |
203
|
|
|
) |
204
|
|
|
|
205
|
|
|
err := Run(s, mdb, w) |
206
|
|
|
assert.NoError(t, err) |
207
|
|
|
}) |
208
|
|
|
|
209
|
|
|
t.Run("single table with NULL column and native data type", func(t *testing.T) { |
210
|
|
|
s := settings.New() |
211
|
|
|
s.DbType = dbType |
212
|
|
|
s.Null = settings.NullTypeNative |
213
|
|
|
|
214
|
|
|
mdb := newMockDb(db) |
215
|
|
|
|
216
|
|
|
table := &database.Table{ |
217
|
|
|
Name: "test_table", |
218
|
|
|
Columns: []database.Column{ |
219
|
|
|
{ |
220
|
|
|
OrdinalPosition: 1, |
221
|
|
|
Name: "column_name", |
222
|
|
|
DataType: columnType, |
223
|
|
|
IsNullable: "YES", |
224
|
|
|
}, |
225
|
|
|
}, |
226
|
|
|
} |
227
|
|
|
mdb.tables = append(mdb.tables, table) |
228
|
|
|
|
229
|
|
|
mdb. |
230
|
|
|
On("GetTables"). |
231
|
|
|
Return(mdb.tables, nil) |
232
|
|
|
mdb. |
233
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
234
|
|
|
Return(nil) |
235
|
|
|
mdb. |
236
|
|
|
On("GetColumnsOfTable", table) |
237
|
|
|
|
238
|
|
|
w := newMockWriter() |
239
|
|
|
w. |
240
|
|
|
On( |
241
|
|
|
"Write", |
242
|
|
|
"TestTable", |
243
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName *string `db:\"column_name\"`\n}", |
244
|
|
|
) |
245
|
|
|
|
246
|
|
|
err := Run(s, mdb, w) |
247
|
|
|
assert.NoError(t, err) |
248
|
|
|
}) |
249
|
|
|
|
250
|
|
|
t.Run("single table with two mixed columns", func(t *testing.T) { |
251
|
|
|
s := settings.New() |
252
|
|
|
s.DbType = dbType |
253
|
|
|
|
254
|
|
|
mdb := newMockDb(db) |
255
|
|
|
|
256
|
|
|
table := &database.Table{ |
257
|
|
|
Name: "test_table", |
258
|
|
|
Columns: []database.Column{ |
259
|
|
|
{ |
260
|
|
|
OrdinalPosition: 1, |
261
|
|
|
Name: "column_name_1", |
262
|
|
|
DataType: columnType, |
263
|
|
|
IsNullable: "YES", |
264
|
|
|
}, |
265
|
|
|
{ |
266
|
|
|
OrdinalPosition: 2, |
267
|
|
|
Name: "column_name_2", |
268
|
|
|
DataType: columnType, |
269
|
|
|
}, |
270
|
|
|
}, |
271
|
|
|
} |
272
|
|
|
mdb.tables = append(mdb.tables, table) |
273
|
|
|
|
274
|
|
|
mdb. |
275
|
|
|
On("GetTables"). |
276
|
|
|
Return(mdb.tables, nil) |
277
|
|
|
mdb. |
278
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
279
|
|
|
Return(nil) |
280
|
|
|
mdb. |
281
|
|
|
On("GetColumnsOfTable", table) |
282
|
|
|
|
283
|
|
|
w := newMockWriter() |
284
|
|
|
w. |
285
|
|
|
On( |
286
|
|
|
"Write", |
287
|
|
|
"TestTable", |
288
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName1 sql.NullString `db:\"column_name_1\"`\nColumnName2 string `db:\"column_name_2\"`\n}", |
289
|
|
|
) |
290
|
|
|
|
291
|
|
|
err := Run(s, mdb, w) |
292
|
|
|
assert.NoError(t, err) |
293
|
|
|
}) |
294
|
|
|
|
295
|
|
|
t.Run("single table with two mixed columns and native data type", func(t *testing.T) { |
296
|
|
|
s := settings.New() |
297
|
|
|
s.DbType = dbType |
298
|
|
|
s.Null = settings.NullTypeNative |
299
|
|
|
|
300
|
|
|
mdb := newMockDb(db) |
301
|
|
|
|
302
|
|
|
table := &database.Table{ |
303
|
|
|
Name: "test_table", |
304
|
|
|
Columns: []database.Column{ |
305
|
|
|
{ |
306
|
|
|
OrdinalPosition: 1, |
307
|
|
|
Name: "column_name_1", |
308
|
|
|
DataType: columnType, |
309
|
|
|
IsNullable: "YES", |
310
|
|
|
}, |
311
|
|
|
{ |
312
|
|
|
OrdinalPosition: 2, |
313
|
|
|
Name: "column_name_2", |
314
|
|
|
DataType: columnType, |
315
|
|
|
}, |
316
|
|
|
}, |
317
|
|
|
} |
318
|
|
|
mdb.tables = append(mdb.tables, table) |
319
|
|
|
|
320
|
|
|
mdb. |
321
|
|
|
On("GetTables"). |
322
|
|
|
Return(mdb.tables, nil) |
323
|
|
|
mdb. |
324
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
325
|
|
|
Return(nil) |
326
|
|
|
mdb. |
327
|
|
|
On("GetColumnsOfTable", table) |
328
|
|
|
|
329
|
|
|
w := newMockWriter() |
330
|
|
|
w. |
331
|
|
|
On( |
332
|
|
|
"Write", |
333
|
|
|
"TestTable", |
334
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName1 *string `db:\"column_name_1\"`\nColumnName2 string `db:\"column_name_2\"`\n}", |
335
|
|
|
) |
336
|
|
|
|
337
|
|
|
err := Run(s, mdb, w) |
338
|
|
|
assert.NoError(t, err) |
339
|
|
|
}) |
340
|
|
|
|
341
|
|
|
t.Run("multi table with multi columns", func(t *testing.T) { |
342
|
|
|
s := settings.New() |
343
|
|
|
s.DbType = dbType |
344
|
|
|
|
345
|
|
|
mdb := newMockDb(db) |
346
|
|
|
|
347
|
|
|
table1 := &database.Table{ |
348
|
|
|
Name: "test_table_1", |
349
|
|
|
Columns: []database.Column{ |
350
|
|
|
{ |
351
|
|
|
OrdinalPosition: 1, |
352
|
|
|
Name: "column_name_1", |
353
|
|
|
DataType: columnType, |
354
|
|
|
IsNullable: "YES", |
355
|
|
|
}, |
356
|
|
|
{ |
357
|
|
|
OrdinalPosition: 2, |
358
|
|
|
Name: "column_name_2", |
359
|
|
|
DataType: columnType, |
360
|
|
|
}, |
361
|
|
|
}, |
362
|
|
|
} |
363
|
|
|
table2 := &database.Table{ |
364
|
|
|
Name: "test_table_2", |
365
|
|
|
Columns: []database.Column{ |
366
|
|
|
{ |
367
|
|
|
OrdinalPosition: 1, |
368
|
|
|
Name: "column_name_1", |
369
|
|
|
DataType: columnType, |
370
|
|
|
}, |
371
|
|
|
{ |
372
|
|
|
OrdinalPosition: 2, |
373
|
|
|
Name: "column_name_2", |
374
|
|
|
DataType: columnType, |
375
|
|
|
IsNullable: "YES", |
376
|
|
|
}, |
377
|
|
|
}, |
378
|
|
|
} |
379
|
|
|
mdb.tables = append(mdb.tables, table1, table2) |
380
|
|
|
|
381
|
|
|
mdb. |
382
|
|
|
On("GetTables"). |
383
|
|
|
Return(mdb.tables, nil) |
384
|
|
|
mdb. |
385
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
386
|
|
|
Return(nil) |
387
|
|
|
mdb. |
388
|
|
|
On("GetColumnsOfTable", table1). |
389
|
|
|
On("GetColumnsOfTable", table2) |
390
|
|
|
|
391
|
|
|
w := newMockWriter() |
392
|
|
|
w. |
393
|
|
|
On( |
394
|
|
|
"Write", |
395
|
|
|
"TestTable1", |
396
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable1 struct {\nColumnName1 sql.NullString `db:\"column_name_1\"`\nColumnName2 string `db:\"column_name_2\"`\n}", |
397
|
|
|
). |
398
|
|
|
On( |
399
|
|
|
"Write", |
400
|
|
|
"TestTable2", |
401
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable2 struct {\nColumnName1 string `db:\"column_name_1\"`\nColumnName2 sql.NullString `db:\"column_name_2\"`\n}", |
402
|
|
|
) |
403
|
|
|
|
404
|
|
|
err := Run(s, mdb, w) |
405
|
|
|
assert.NoError(t, err) |
406
|
|
|
}) |
407
|
|
|
}) |
408
|
|
|
} |
409
|
|
|
}) |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
func TestRun_IntegerColumns(t *testing.T) { |
414
|
|
|
for dbType := range settings.SupportedDbTypes { |
415
|
|
|
t.Run(dbType.String(), func(t *testing.T) { |
416
|
|
|
|
417
|
|
|
s := settings.New() |
418
|
|
|
s.DbType = dbType |
419
|
|
|
db := database.New(s) |
420
|
|
|
|
421
|
|
|
columnTypes := db.GetIntegerDatatypes() |
422
|
|
|
|
423
|
|
|
for _, columnType := range columnTypes { |
424
|
|
|
t.Run(columnType, func(t *testing.T) { |
425
|
|
|
|
426
|
|
|
t.Run("single table with NOT NULL column", func(t *testing.T) { |
427
|
|
|
s := settings.New() |
428
|
|
|
s.DbType = dbType |
429
|
|
|
|
430
|
|
|
mdb := newMockDb(db) |
431
|
|
|
|
432
|
|
|
table := &database.Table{ |
433
|
|
|
Name: "test_table", |
434
|
|
|
Columns: []database.Column{ |
435
|
|
|
{ |
436
|
|
|
OrdinalPosition: 1, |
437
|
|
|
Name: "column_name", |
438
|
|
|
DataType: columnType, |
439
|
|
|
}, |
440
|
|
|
}, |
441
|
|
|
} |
442
|
|
|
mdb.tables = append(mdb.tables, table) |
443
|
|
|
|
444
|
|
|
mdb. |
445
|
|
|
On("GetTables"). |
446
|
|
|
Return(mdb.tables, nil) |
447
|
|
|
mdb. |
448
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
449
|
|
|
Return(nil) |
450
|
|
|
mdb. |
451
|
|
|
On("GetColumnsOfTable", table) |
452
|
|
|
|
453
|
|
|
w := newMockWriter() |
454
|
|
|
w. |
455
|
|
|
On( |
456
|
|
|
"Write", |
457
|
|
|
"TestTable", |
458
|
|
|
"package dto\n\ntype TestTable struct {\nColumnName int `db:\"column_name\"`\n}", |
459
|
|
|
) |
460
|
|
|
|
461
|
|
|
err := Run(s, mdb, w) |
462
|
|
|
assert.NoError(t, err) |
463
|
|
|
}) |
464
|
|
|
|
465
|
|
|
t.Run("single table with NULL column", func(t *testing.T) { |
466
|
|
|
s := settings.New() |
467
|
|
|
s.DbType = dbType |
468
|
|
|
|
469
|
|
|
mdb := newMockDb(db) |
470
|
|
|
|
471
|
|
|
table := &database.Table{ |
472
|
|
|
Name: "test_table", |
473
|
|
|
Columns: []database.Column{ |
474
|
|
|
{ |
475
|
|
|
OrdinalPosition: 1, |
476
|
|
|
Name: "column_name", |
477
|
|
|
DataType: columnType, |
478
|
|
|
IsNullable: "YES", |
479
|
|
|
}, |
480
|
|
|
}, |
481
|
|
|
} |
482
|
|
|
mdb.tables = append(mdb.tables, table) |
483
|
|
|
|
484
|
|
|
mdb. |
485
|
|
|
On("GetTables"). |
486
|
|
|
Return(mdb.tables, nil) |
487
|
|
|
mdb. |
488
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
489
|
|
|
Return(nil) |
490
|
|
|
mdb. |
491
|
|
|
On("GetColumnsOfTable", table) |
492
|
|
|
|
493
|
|
|
w := newMockWriter() |
494
|
|
|
w. |
495
|
|
|
On( |
496
|
|
|
"Write", |
497
|
|
|
"TestTable", |
498
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName sql.NullInt64 `db:\"column_name\"`\n}", |
499
|
|
|
) |
500
|
|
|
|
501
|
|
|
err := Run(s, mdb, w) |
502
|
|
|
assert.NoError(t, err) |
503
|
|
|
}) |
504
|
|
|
|
505
|
|
|
t.Run("single table with NULL column and native data type", func(t *testing.T) { |
506
|
|
|
s := settings.New() |
507
|
|
|
s.DbType = dbType |
508
|
|
|
s.Null = settings.NullTypeNative |
509
|
|
|
|
510
|
|
|
mdb := newMockDb(db) |
511
|
|
|
|
512
|
|
|
table := &database.Table{ |
513
|
|
|
Name: "test_table", |
514
|
|
|
Columns: []database.Column{ |
515
|
|
|
{ |
516
|
|
|
OrdinalPosition: 1, |
517
|
|
|
Name: "column_name", |
518
|
|
|
DataType: columnType, |
519
|
|
|
IsNullable: "YES", |
520
|
|
|
}, |
521
|
|
|
}, |
522
|
|
|
} |
523
|
|
|
mdb.tables = append(mdb.tables, table) |
524
|
|
|
|
525
|
|
|
mdb. |
526
|
|
|
On("GetTables"). |
527
|
|
|
Return(mdb.tables, nil) |
528
|
|
|
mdb. |
529
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
530
|
|
|
Return(nil) |
531
|
|
|
mdb. |
532
|
|
|
On("GetColumnsOfTable", table) |
533
|
|
|
|
534
|
|
|
w := newMockWriter() |
535
|
|
|
w. |
536
|
|
|
On( |
537
|
|
|
"Write", |
538
|
|
|
"TestTable", |
539
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName *int `db:\"column_name\"`\n}", |
540
|
|
|
) |
541
|
|
|
|
542
|
|
|
err := Run(s, mdb, w) |
543
|
|
|
assert.NoError(t, err) |
544
|
|
|
}) |
545
|
|
|
|
546
|
|
|
t.Run("single table with two mixed columns", func(t *testing.T) { |
547
|
|
|
s := settings.New() |
548
|
|
|
s.DbType = dbType |
549
|
|
|
|
550
|
|
|
mdb := newMockDb(db) |
551
|
|
|
|
552
|
|
|
table := &database.Table{ |
553
|
|
|
Name: "test_table", |
554
|
|
|
Columns: []database.Column{ |
555
|
|
|
{ |
556
|
|
|
OrdinalPosition: 1, |
557
|
|
|
Name: "column_name_1", |
558
|
|
|
DataType: columnType, |
559
|
|
|
IsNullable: "YES", |
560
|
|
|
}, |
561
|
|
|
{ |
562
|
|
|
OrdinalPosition: 2, |
563
|
|
|
Name: "column_name_2", |
564
|
|
|
DataType: columnType, |
565
|
|
|
}, |
566
|
|
|
}, |
567
|
|
|
} |
568
|
|
|
mdb.tables = append(mdb.tables, table) |
569
|
|
|
|
570
|
|
|
mdb. |
571
|
|
|
On("GetTables"). |
572
|
|
|
Return(mdb.tables, nil) |
573
|
|
|
mdb. |
574
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
575
|
|
|
Return(nil) |
576
|
|
|
mdb. |
577
|
|
|
On("GetColumnsOfTable", table) |
578
|
|
|
|
579
|
|
|
w := newMockWriter() |
580
|
|
|
w. |
581
|
|
|
On( |
582
|
|
|
"Write", |
583
|
|
|
"TestTable", |
584
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName1 sql.NullInt64 `db:\"column_name_1\"`\nColumnName2 int `db:\"column_name_2\"`\n}", |
585
|
|
|
) |
586
|
|
|
|
587
|
|
|
err := Run(s, mdb, w) |
588
|
|
|
assert.NoError(t, err) |
589
|
|
|
}) |
590
|
|
|
|
591
|
|
|
t.Run("single table with two mixed columns and native data type", func(t *testing.T) { |
592
|
|
|
s := settings.New() |
593
|
|
|
s.DbType = dbType |
594
|
|
|
s.Null = settings.NullTypeNative |
595
|
|
|
|
596
|
|
|
mdb := newMockDb(db) |
597
|
|
|
|
598
|
|
|
table := &database.Table{ |
599
|
|
|
Name: "test_table", |
600
|
|
|
Columns: []database.Column{ |
601
|
|
|
{ |
602
|
|
|
OrdinalPosition: 1, |
603
|
|
|
Name: "column_name_1", |
604
|
|
|
DataType: columnType, |
605
|
|
|
IsNullable: "YES", |
606
|
|
|
}, |
607
|
|
|
{ |
608
|
|
|
OrdinalPosition: 2, |
609
|
|
|
Name: "column_name_2", |
610
|
|
|
DataType: columnType, |
611
|
|
|
}, |
612
|
|
|
}, |
613
|
|
|
} |
614
|
|
|
mdb.tables = append(mdb.tables, table) |
615
|
|
|
|
616
|
|
|
mdb. |
617
|
|
|
On("GetTables"). |
618
|
|
|
Return(mdb.tables, nil) |
619
|
|
|
mdb. |
620
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
621
|
|
|
Return(nil) |
622
|
|
|
mdb. |
623
|
|
|
On("GetColumnsOfTable", table) |
624
|
|
|
|
625
|
|
|
w := newMockWriter() |
626
|
|
|
w. |
627
|
|
|
On( |
628
|
|
|
"Write", |
629
|
|
|
"TestTable", |
630
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName1 *int `db:\"column_name_1\"`\nColumnName2 int `db:\"column_name_2\"`\n}", |
631
|
|
|
) |
632
|
|
|
|
633
|
|
|
err := Run(s, mdb, w) |
634
|
|
|
assert.NoError(t, err) |
635
|
|
|
}) |
636
|
|
|
|
637
|
|
|
t.Run("multi table with multi columns", func(t *testing.T) { |
638
|
|
|
s := settings.New() |
639
|
|
|
s.DbType = dbType |
640
|
|
|
|
641
|
|
|
mdb := newMockDb(db) |
642
|
|
|
|
643
|
|
|
table1 := &database.Table{ |
644
|
|
|
Name: "test_table_1", |
645
|
|
|
Columns: []database.Column{ |
646
|
|
|
{ |
647
|
|
|
OrdinalPosition: 1, |
648
|
|
|
Name: "column_name_1", |
649
|
|
|
DataType: columnType, |
650
|
|
|
IsNullable: "YES", |
651
|
|
|
}, |
652
|
|
|
{ |
653
|
|
|
OrdinalPosition: 2, |
654
|
|
|
Name: "column_name_2", |
655
|
|
|
DataType: columnType, |
656
|
|
|
}, |
657
|
|
|
}, |
658
|
|
|
} |
659
|
|
|
table2 := &database.Table{ |
660
|
|
|
Name: "test_table_2", |
661
|
|
|
Columns: []database.Column{ |
662
|
|
|
{ |
663
|
|
|
OrdinalPosition: 1, |
664
|
|
|
Name: "column_name_1", |
665
|
|
|
DataType: columnType, |
666
|
|
|
}, |
667
|
|
|
{ |
668
|
|
|
OrdinalPosition: 2, |
669
|
|
|
Name: "column_name_2", |
670
|
|
|
DataType: columnType, |
671
|
|
|
IsNullable: "YES", |
672
|
|
|
}, |
673
|
|
|
}, |
674
|
|
|
} |
675
|
|
|
mdb.tables = append(mdb.tables, table1, table2) |
676
|
|
|
|
677
|
|
|
mdb. |
678
|
|
|
On("GetTables"). |
679
|
|
|
Return(mdb.tables, nil) |
680
|
|
|
mdb. |
681
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
682
|
|
|
Return(nil) |
683
|
|
|
mdb. |
684
|
|
|
On("GetColumnsOfTable", table1). |
685
|
|
|
On("GetColumnsOfTable", table2) |
686
|
|
|
|
687
|
|
|
w := newMockWriter() |
688
|
|
|
w. |
689
|
|
|
On( |
690
|
|
|
"Write", |
691
|
|
|
"TestTable1", |
692
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable1 struct {\nColumnName1 sql.NullInt64 `db:\"column_name_1\"`\nColumnName2 int `db:\"column_name_2\"`\n}", |
693
|
|
|
). |
694
|
|
|
On( |
695
|
|
|
"Write", |
696
|
|
|
"TestTable2", |
697
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable2 struct {\nColumnName1 int `db:\"column_name_1\"`\nColumnName2 sql.NullInt64 `db:\"column_name_2\"`\n}", |
698
|
|
|
) |
699
|
|
|
|
700
|
|
|
err := Run(s, mdb, w) |
701
|
|
|
assert.NoError(t, err) |
702
|
|
|
}) |
703
|
|
|
}) |
704
|
|
|
} |
705
|
|
|
}) |
706
|
|
|
} |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
func TestRun_FloatColumns(t *testing.T) { |
710
|
|
|
for dbType := range settings.SupportedDbTypes { |
711
|
|
|
t.Run(dbType.String(), func(t *testing.T) { |
712
|
|
|
|
713
|
|
|
s := settings.New() |
714
|
|
|
s.DbType = dbType |
715
|
|
|
db := database.New(s) |
716
|
|
|
|
717
|
|
|
columnTypes := db.GetFloatDatatypes() |
718
|
|
|
|
719
|
|
|
for _, columnType := range columnTypes { |
720
|
|
|
t.Run(columnType, func(t *testing.T) { |
721
|
|
|
|
722
|
|
|
t.Run("single table with NOT NULL column", func(t *testing.T) { |
723
|
|
|
s := settings.New() |
724
|
|
|
s.DbType = dbType |
725
|
|
|
|
726
|
|
|
mdb := newMockDb(db) |
727
|
|
|
|
728
|
|
|
table := &database.Table{ |
729
|
|
|
Name: "test_table", |
730
|
|
|
Columns: []database.Column{ |
731
|
|
|
{ |
732
|
|
|
OrdinalPosition: 1, |
733
|
|
|
Name: "column_name", |
734
|
|
|
DataType: columnType, |
735
|
|
|
}, |
736
|
|
|
}, |
737
|
|
|
} |
738
|
|
|
mdb.tables = append(mdb.tables, table) |
739
|
|
|
|
740
|
|
|
mdb. |
741
|
|
|
On("GetTables"). |
742
|
|
|
Return(mdb.tables, nil) |
743
|
|
|
mdb. |
744
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
745
|
|
|
Return(nil) |
746
|
|
|
mdb. |
747
|
|
|
On("GetColumnsOfTable", table) |
748
|
|
|
|
749
|
|
|
w := newMockWriter() |
750
|
|
|
w. |
751
|
|
|
On( |
752
|
|
|
"Write", |
753
|
|
|
"TestTable", |
754
|
|
|
"package dto\n\ntype TestTable struct {\nColumnName float64 `db:\"column_name\"`\n}", |
755
|
|
|
) |
756
|
|
|
|
757
|
|
|
err := Run(s, mdb, w) |
758
|
|
|
assert.NoError(t, err) |
759
|
|
|
}) |
760
|
|
|
|
761
|
|
|
t.Run("single table with NULL column", func(t *testing.T) { |
762
|
|
|
s := settings.New() |
763
|
|
|
s.DbType = dbType |
764
|
|
|
|
765
|
|
|
mdb := newMockDb(db) |
766
|
|
|
|
767
|
|
|
table := &database.Table{ |
768
|
|
|
Name: "test_table", |
769
|
|
|
Columns: []database.Column{ |
770
|
|
|
{ |
771
|
|
|
OrdinalPosition: 1, |
772
|
|
|
Name: "column_name", |
773
|
|
|
DataType: columnType, |
774
|
|
|
IsNullable: "YES", |
775
|
|
|
}, |
776
|
|
|
}, |
777
|
|
|
} |
778
|
|
|
mdb.tables = append(mdb.tables, table) |
779
|
|
|
|
780
|
|
|
mdb. |
781
|
|
|
On("GetTables"). |
782
|
|
|
Return(mdb.tables, nil) |
783
|
|
|
mdb. |
784
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
785
|
|
|
Return(nil) |
786
|
|
|
mdb. |
787
|
|
|
On("GetColumnsOfTable", table) |
788
|
|
|
|
789
|
|
|
w := newMockWriter() |
790
|
|
|
w. |
791
|
|
|
On( |
792
|
|
|
"Write", |
793
|
|
|
"TestTable", |
794
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName sql.NullFloat64 `db:\"column_name\"`\n}", |
795
|
|
|
) |
796
|
|
|
|
797
|
|
|
err := Run(s, mdb, w) |
798
|
|
|
assert.NoError(t, err) |
799
|
|
|
}) |
800
|
|
|
|
801
|
|
|
t.Run("single table with NULL column and native data type", func(t *testing.T) { |
802
|
|
|
s := settings.New() |
803
|
|
|
s.DbType = dbType |
804
|
|
|
s.Null = settings.NullTypeNative |
805
|
|
|
|
806
|
|
|
mdb := newMockDb(db) |
807
|
|
|
|
808
|
|
|
table := &database.Table{ |
809
|
|
|
Name: "test_table", |
810
|
|
|
Columns: []database.Column{ |
811
|
|
|
{ |
812
|
|
|
OrdinalPosition: 1, |
813
|
|
|
Name: "column_name", |
814
|
|
|
DataType: columnType, |
815
|
|
|
IsNullable: "YES", |
816
|
|
|
}, |
817
|
|
|
}, |
818
|
|
|
} |
819
|
|
|
mdb.tables = append(mdb.tables, table) |
820
|
|
|
|
821
|
|
|
mdb. |
822
|
|
|
On("GetTables"). |
823
|
|
|
Return(mdb.tables, nil) |
824
|
|
|
mdb. |
825
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
826
|
|
|
Return(nil) |
827
|
|
|
mdb. |
828
|
|
|
On("GetColumnsOfTable", table) |
829
|
|
|
|
830
|
|
|
w := newMockWriter() |
831
|
|
|
w. |
832
|
|
|
On( |
833
|
|
|
"Write", |
834
|
|
|
"TestTable", |
835
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName *float64 `db:\"column_name\"`\n}", |
836
|
|
|
) |
837
|
|
|
|
838
|
|
|
err := Run(s, mdb, w) |
839
|
|
|
assert.NoError(t, err) |
840
|
|
|
}) |
841
|
|
|
|
842
|
|
|
t.Run("single table with two mixed columns", func(t *testing.T) { |
843
|
|
|
s := settings.New() |
844
|
|
|
s.DbType = dbType |
845
|
|
|
|
846
|
|
|
mdb := newMockDb(db) |
847
|
|
|
|
848
|
|
|
table := &database.Table{ |
849
|
|
|
Name: "test_table", |
850
|
|
|
Columns: []database.Column{ |
851
|
|
|
{ |
852
|
|
|
OrdinalPosition: 1, |
853
|
|
|
Name: "column_name_1", |
854
|
|
|
DataType: columnType, |
855
|
|
|
IsNullable: "YES", |
856
|
|
|
}, |
857
|
|
|
{ |
858
|
|
|
OrdinalPosition: 2, |
859
|
|
|
Name: "column_name_2", |
860
|
|
|
DataType: columnType, |
861
|
|
|
}, |
862
|
|
|
}, |
863
|
|
|
} |
864
|
|
|
mdb.tables = append(mdb.tables, table) |
865
|
|
|
|
866
|
|
|
mdb. |
867
|
|
|
On("GetTables"). |
868
|
|
|
Return(mdb.tables, nil) |
869
|
|
|
mdb. |
870
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
871
|
|
|
Return(nil) |
872
|
|
|
mdb. |
873
|
|
|
On("GetColumnsOfTable", table) |
874
|
|
|
|
875
|
|
|
w := newMockWriter() |
876
|
|
|
w. |
877
|
|
|
On( |
878
|
|
|
"Write", |
879
|
|
|
"TestTable", |
880
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName1 sql.NullFloat64 `db:\"column_name_1\"`\nColumnName2 float64 `db:\"column_name_2\"`\n}", |
881
|
|
|
) |
882
|
|
|
|
883
|
|
|
err := Run(s, mdb, w) |
884
|
|
|
assert.NoError(t, err) |
885
|
|
|
}) |
886
|
|
|
|
887
|
|
|
t.Run("single table with two mixed columns and native data type", func(t *testing.T) { |
888
|
|
|
s := settings.New() |
889
|
|
|
s.DbType = dbType |
890
|
|
|
s.Null = settings.NullTypeNative |
891
|
|
|
|
892
|
|
|
mdb := newMockDb(db) |
893
|
|
|
|
894
|
|
|
table := &database.Table{ |
895
|
|
|
Name: "test_table", |
896
|
|
|
Columns: []database.Column{ |
897
|
|
|
{ |
898
|
|
|
OrdinalPosition: 1, |
899
|
|
|
Name: "column_name_1", |
900
|
|
|
DataType: columnType, |
901
|
|
|
IsNullable: "YES", |
902
|
|
|
}, |
903
|
|
|
{ |
904
|
|
|
OrdinalPosition: 2, |
905
|
|
|
Name: "column_name_2", |
906
|
|
|
DataType: columnType, |
907
|
|
|
}, |
908
|
|
|
}, |
909
|
|
|
} |
910
|
|
|
mdb.tables = append(mdb.tables, table) |
911
|
|
|
|
912
|
|
|
mdb. |
913
|
|
|
On("GetTables"). |
914
|
|
|
Return(mdb.tables, nil) |
915
|
|
|
mdb. |
916
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
917
|
|
|
Return(nil) |
918
|
|
|
mdb. |
919
|
|
|
On("GetColumnsOfTable", table) |
920
|
|
|
|
921
|
|
|
w := newMockWriter() |
922
|
|
|
w. |
923
|
|
|
On( |
924
|
|
|
"Write", |
925
|
|
|
"TestTable", |
926
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName1 *float64 `db:\"column_name_1\"`\nColumnName2 float64 `db:\"column_name_2\"`\n}", |
927
|
|
|
) |
928
|
|
|
|
929
|
|
|
err := Run(s, mdb, w) |
930
|
|
|
assert.NoError(t, err) |
931
|
|
|
}) |
932
|
|
|
|
933
|
|
|
t.Run("multi table with multi columns", func(t *testing.T) { |
934
|
|
|
s := settings.New() |
935
|
|
|
s.DbType = dbType |
936
|
|
|
|
937
|
|
|
mdb := newMockDb(db) |
938
|
|
|
|
939
|
|
|
table1 := &database.Table{ |
940
|
|
|
Name: "test_table_1", |
941
|
|
|
Columns: []database.Column{ |
942
|
|
|
{ |
943
|
|
|
OrdinalPosition: 1, |
944
|
|
|
Name: "column_name_1", |
945
|
|
|
DataType: columnType, |
946
|
|
|
IsNullable: "YES", |
947
|
|
|
}, |
948
|
|
|
{ |
949
|
|
|
OrdinalPosition: 2, |
950
|
|
|
Name: "column_name_2", |
951
|
|
|
DataType: columnType, |
952
|
|
|
}, |
953
|
|
|
}, |
954
|
|
|
} |
955
|
|
|
table2 := &database.Table{ |
956
|
|
|
Name: "test_table_2", |
957
|
|
|
Columns: []database.Column{ |
958
|
|
|
{ |
959
|
|
|
OrdinalPosition: 1, |
960
|
|
|
Name: "column_name_1", |
961
|
|
|
DataType: columnType, |
962
|
|
|
}, |
963
|
|
|
{ |
964
|
|
|
OrdinalPosition: 2, |
965
|
|
|
Name: "column_name_2", |
966
|
|
|
DataType: columnType, |
967
|
|
|
IsNullable: "YES", |
968
|
|
|
}, |
969
|
|
|
}, |
970
|
|
|
} |
971
|
|
|
mdb.tables = append(mdb.tables, table1, table2) |
972
|
|
|
|
973
|
|
|
mdb. |
974
|
|
|
On("GetTables"). |
975
|
|
|
Return(mdb.tables, nil) |
976
|
|
|
mdb. |
977
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
978
|
|
|
Return(nil) |
979
|
|
|
mdb. |
980
|
|
|
On("GetColumnsOfTable", table1). |
981
|
|
|
On("GetColumnsOfTable", table2) |
982
|
|
|
|
983
|
|
|
w := newMockWriter() |
984
|
|
|
w. |
985
|
|
|
On( |
986
|
|
|
"Write", |
987
|
|
|
"TestTable1", |
988
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable1 struct {\nColumnName1 sql.NullFloat64 `db:\"column_name_1\"`\nColumnName2 float64 `db:\"column_name_2\"`\n}", |
989
|
|
|
). |
990
|
|
|
On( |
991
|
|
|
"Write", |
992
|
|
|
"TestTable2", |
993
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable2 struct {\nColumnName1 float64 `db:\"column_name_1\"`\nColumnName2 sql.NullFloat64 `db:\"column_name_2\"`\n}", |
994
|
|
|
) |
995
|
|
|
|
996
|
|
|
err := Run(s, mdb, w) |
997
|
|
|
assert.NoError(t, err) |
998
|
|
|
}) |
999
|
|
|
}) |
1000
|
|
|
} |
1001
|
|
|
}) |
1002
|
|
|
} |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
func TestRun_TemporalColumns(t *testing.T) { |
1006
|
|
|
for dbType := range settings.SupportedDbTypes { |
1007
|
|
|
t.Run(dbType.String(), func(t *testing.T) { |
1008
|
|
|
|
1009
|
|
|
s := settings.New() |
1010
|
|
|
s.DbType = dbType |
1011
|
|
|
db := database.New(s) |
1012
|
|
|
|
1013
|
|
|
columnTypes := db.GetTemporalDatatypes() |
1014
|
|
|
|
1015
|
|
|
for _, columnType := range columnTypes { |
1016
|
|
|
t.Run(columnType, func(t *testing.T) { |
1017
|
|
|
|
1018
|
|
|
t.Run("single table with NOT NULL column", func(t *testing.T) { |
1019
|
|
|
s := settings.New() |
1020
|
|
|
s.DbType = dbType |
1021
|
|
|
|
1022
|
|
|
mdb := newMockDb(db) |
1023
|
|
|
|
1024
|
|
|
table := &database.Table{ |
1025
|
|
|
Name: "test_table", |
1026
|
|
|
Columns: []database.Column{ |
1027
|
|
|
{ |
1028
|
|
|
OrdinalPosition: 1, |
1029
|
|
|
Name: "column_name", |
1030
|
|
|
DataType: columnType, |
1031
|
|
|
}, |
1032
|
|
|
}, |
1033
|
|
|
} |
1034
|
|
|
mdb.tables = append(mdb.tables, table) |
1035
|
|
|
|
1036
|
|
|
mdb. |
1037
|
|
|
On("GetTables"). |
1038
|
|
|
Return(mdb.tables, nil) |
1039
|
|
|
mdb. |
1040
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1041
|
|
|
Return(nil) |
1042
|
|
|
mdb. |
1043
|
|
|
On("GetColumnsOfTable", table) |
1044
|
|
|
|
1045
|
|
|
w := newMockWriter() |
1046
|
|
|
w. |
1047
|
|
|
On( |
1048
|
|
|
"Write", |
1049
|
|
|
"TestTable", |
1050
|
|
|
"package dto\n\nimport (\n\t\"time\"\n)\n\ntype TestTable struct {\nColumnName time.Time `db:\"column_name\"`\n}", |
1051
|
|
|
) |
1052
|
|
|
|
1053
|
|
|
err := Run(s, mdb, w) |
1054
|
|
|
assert.NoError(t, err) |
1055
|
|
|
}) |
1056
|
|
|
|
1057
|
|
|
t.Run("single table with NULL column", func(t *testing.T) { |
1058
|
|
|
s := settings.New() |
1059
|
|
|
s.DbType = dbType |
1060
|
|
|
|
1061
|
|
|
mdb := newMockDb(db) |
1062
|
|
|
|
1063
|
|
|
table := &database.Table{ |
1064
|
|
|
Name: "test_table", |
1065
|
|
|
Columns: []database.Column{ |
1066
|
|
|
{ |
1067
|
|
|
OrdinalPosition: 1, |
1068
|
|
|
Name: "column_name", |
1069
|
|
|
DataType: columnType, |
1070
|
|
|
IsNullable: "YES", |
1071
|
|
|
}, |
1072
|
|
|
}, |
1073
|
|
|
} |
1074
|
|
|
mdb.tables = append(mdb.tables, table) |
1075
|
|
|
|
1076
|
|
|
mdb. |
1077
|
|
|
On("GetTables"). |
1078
|
|
|
Return(mdb.tables, nil) |
1079
|
|
|
mdb. |
1080
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1081
|
|
|
Return(nil) |
1082
|
|
|
mdb. |
1083
|
|
|
On("GetColumnsOfTable", table) |
1084
|
|
|
|
1085
|
|
|
w := newMockWriter() |
1086
|
|
|
w. |
1087
|
|
|
On( |
1088
|
|
|
"Write", |
1089
|
|
|
"TestTable", |
1090
|
|
|
"package dto\n\nimport (\n\t\n"+db.GetDriverImportLibrary()+"\n)\n\ntype TestTable struct {\nColumnName "+dbType.String()+".NullTime `db:\"column_name\"`\n}", |
1091
|
|
|
) |
1092
|
|
|
|
1093
|
|
|
err := Run(s, mdb, w) |
1094
|
|
|
assert.NoError(t, err) |
1095
|
|
|
}) |
1096
|
|
|
|
1097
|
|
|
t.Run("single table with NULL column and native data type", func(t *testing.T) { |
1098
|
|
|
s := settings.New() |
1099
|
|
|
s.DbType = dbType |
1100
|
|
|
s.Null = settings.NullTypeNative |
1101
|
|
|
|
1102
|
|
|
mdb := newMockDb(db) |
1103
|
|
|
|
1104
|
|
|
table := &database.Table{ |
1105
|
|
|
Name: "test_table", |
1106
|
|
|
Columns: []database.Column{ |
1107
|
|
|
{ |
1108
|
|
|
OrdinalPosition: 1, |
1109
|
|
|
Name: "column_name", |
1110
|
|
|
DataType: columnType, |
1111
|
|
|
IsNullable: "YES", |
1112
|
|
|
}, |
1113
|
|
|
}, |
1114
|
|
|
} |
1115
|
|
|
mdb.tables = append(mdb.tables, table) |
1116
|
|
|
|
1117
|
|
|
mdb. |
1118
|
|
|
On("GetTables"). |
1119
|
|
|
Return(mdb.tables, nil) |
1120
|
|
|
mdb. |
1121
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1122
|
|
|
Return(nil) |
1123
|
|
|
mdb. |
1124
|
|
|
On("GetColumnsOfTable", table) |
1125
|
|
|
|
1126
|
|
|
w := newMockWriter() |
1127
|
|
|
w. |
1128
|
|
|
On( |
1129
|
|
|
"Write", |
1130
|
|
|
"TestTable", |
1131
|
|
|
"package dto\n\nimport (\n\t\"time\"\n)\n\ntype TestTable struct {\nColumnName *time.Time `db:\"column_name\"`\n}", |
1132
|
|
|
) |
1133
|
|
|
|
1134
|
|
|
err := Run(s, mdb, w) |
1135
|
|
|
assert.NoError(t, err) |
1136
|
|
|
}) |
1137
|
|
|
|
1138
|
|
|
t.Run("single table with two mixed columns", func(t *testing.T) { |
1139
|
|
|
s := settings.New() |
1140
|
|
|
s.DbType = dbType |
1141
|
|
|
|
1142
|
|
|
mdb := newMockDb(db) |
1143
|
|
|
|
1144
|
|
|
table := &database.Table{ |
1145
|
|
|
Name: "test_table", |
1146
|
|
|
Columns: []database.Column{ |
1147
|
|
|
{ |
1148
|
|
|
OrdinalPosition: 1, |
1149
|
|
|
Name: "column_name_1", |
1150
|
|
|
DataType: columnType, |
1151
|
|
|
IsNullable: "YES", |
1152
|
|
|
}, |
1153
|
|
|
{ |
1154
|
|
|
OrdinalPosition: 2, |
1155
|
|
|
Name: "column_name_2", |
1156
|
|
|
DataType: columnType, |
1157
|
|
|
}, |
1158
|
|
|
}, |
1159
|
|
|
} |
1160
|
|
|
mdb.tables = append(mdb.tables, table) |
1161
|
|
|
|
1162
|
|
|
mdb. |
1163
|
|
|
On("GetTables"). |
1164
|
|
|
Return(mdb.tables, nil) |
1165
|
|
|
mdb. |
1166
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1167
|
|
|
Return(nil) |
1168
|
|
|
mdb. |
1169
|
|
|
On("GetColumnsOfTable", table) |
1170
|
|
|
|
1171
|
|
|
w := newMockWriter() |
1172
|
|
|
w. |
1173
|
|
|
On( |
1174
|
|
|
"Write", |
1175
|
|
|
"TestTable", |
1176
|
|
|
"package dto\n\nimport (\n\t\"time\"\n\t\n"+db.GetDriverImportLibrary()+"\n)\n\ntype TestTable struct {\nColumnName1 "+dbType.String()+".NullTime `db:\"column_name_1\"`\nColumnName2 time.Time `db:\"column_name_2\"`\n}", |
1177
|
|
|
) |
1178
|
|
|
|
1179
|
|
|
err := Run(s, mdb, w) |
1180
|
|
|
assert.NoError(t, err) |
1181
|
|
|
}) |
1182
|
|
|
|
1183
|
|
|
t.Run("single table with two mixed columns and native data type", func(t *testing.T) { |
1184
|
|
|
s := settings.New() |
1185
|
|
|
s.DbType = dbType |
1186
|
|
|
s.Null = settings.NullTypeNative |
1187
|
|
|
|
1188
|
|
|
mdb := newMockDb(db) |
1189
|
|
|
|
1190
|
|
|
table := &database.Table{ |
1191
|
|
|
Name: "test_table", |
1192
|
|
|
Columns: []database.Column{ |
1193
|
|
|
{ |
1194
|
|
|
OrdinalPosition: 1, |
1195
|
|
|
Name: "column_name_1", |
1196
|
|
|
DataType: columnType, |
1197
|
|
|
IsNullable: "YES", |
1198
|
|
|
}, |
1199
|
|
|
{ |
1200
|
|
|
OrdinalPosition: 2, |
1201
|
|
|
Name: "column_name_2", |
1202
|
|
|
DataType: columnType, |
1203
|
|
|
}, |
1204
|
|
|
}, |
1205
|
|
|
} |
1206
|
|
|
mdb.tables = append(mdb.tables, table) |
1207
|
|
|
|
1208
|
|
|
mdb. |
1209
|
|
|
On("GetTables"). |
1210
|
|
|
Return(mdb.tables, nil) |
1211
|
|
|
mdb. |
1212
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1213
|
|
|
Return(nil) |
1214
|
|
|
mdb. |
1215
|
|
|
On("GetColumnsOfTable", table) |
1216
|
|
|
|
1217
|
|
|
w := newMockWriter() |
1218
|
|
|
w. |
1219
|
|
|
On( |
1220
|
|
|
"Write", |
1221
|
|
|
"TestTable", |
1222
|
|
|
"package dto\n\nimport (\n\t\"time\"\n)\n\ntype TestTable struct {\nColumnName1 *time.Time `db:\"column_name_1\"`\nColumnName2 time.Time `db:\"column_name_2\"`\n}", |
1223
|
|
|
) |
1224
|
|
|
|
1225
|
|
|
err := Run(s, mdb, w) |
1226
|
|
|
assert.NoError(t, err) |
1227
|
|
|
}) |
1228
|
|
|
|
1229
|
|
|
t.Run("multi table with multi columns", func(t *testing.T) { |
1230
|
|
|
s := settings.New() |
1231
|
|
|
s.DbType = dbType |
1232
|
|
|
|
1233
|
|
|
mdb := newMockDb(db) |
1234
|
|
|
|
1235
|
|
|
table1 := &database.Table{ |
1236
|
|
|
Name: "test_table_1", |
1237
|
|
|
Columns: []database.Column{ |
1238
|
|
|
{ |
1239
|
|
|
OrdinalPosition: 1, |
1240
|
|
|
Name: "column_name_1", |
1241
|
|
|
DataType: columnType, |
1242
|
|
|
IsNullable: "YES", |
1243
|
|
|
}, |
1244
|
|
|
{ |
1245
|
|
|
OrdinalPosition: 2, |
1246
|
|
|
Name: "column_name_2", |
1247
|
|
|
DataType: columnType, |
1248
|
|
|
}, |
1249
|
|
|
}, |
1250
|
|
|
} |
1251
|
|
|
table2 := &database.Table{ |
1252
|
|
|
Name: "test_table_2", |
1253
|
|
|
Columns: []database.Column{ |
1254
|
|
|
{ |
1255
|
|
|
OrdinalPosition: 1, |
1256
|
|
|
Name: "column_name_1", |
1257
|
|
|
DataType: columnType, |
1258
|
|
|
}, |
1259
|
|
|
{ |
1260
|
|
|
OrdinalPosition: 2, |
1261
|
|
|
Name: "column_name_2", |
1262
|
|
|
DataType: columnType, |
1263
|
|
|
IsNullable: "YES", |
1264
|
|
|
}, |
1265
|
|
|
}, |
1266
|
|
|
} |
1267
|
|
|
mdb.tables = append(mdb.tables, table1, table2) |
1268
|
|
|
|
1269
|
|
|
mdb. |
1270
|
|
|
On("GetTables"). |
1271
|
|
|
Return(mdb.tables, nil) |
1272
|
|
|
mdb. |
1273
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1274
|
|
|
Return(nil) |
1275
|
|
|
mdb. |
1276
|
|
|
On("GetColumnsOfTable", table1). |
1277
|
|
|
On("GetColumnsOfTable", table2) |
1278
|
|
|
|
1279
|
|
|
w := newMockWriter() |
1280
|
|
|
w. |
1281
|
|
|
On( |
1282
|
|
|
"Write", |
1283
|
|
|
"TestTable1", |
1284
|
|
|
"package dto\n\nimport (\n\t\"time\"\n\t\n"+db.GetDriverImportLibrary()+"\n)\n\ntype TestTable1 struct {\nColumnName1 "+dbType.String()+".NullTime `db:\"column_name_1\"`\nColumnName2 time.Time `db:\"column_name_2\"`\n}", |
1285
|
|
|
). |
1286
|
|
|
On( |
1287
|
|
|
"Write", |
1288
|
|
|
"TestTable2", |
1289
|
|
|
"package dto\n\nimport (\n\t\"time\"\n\t\n"+db.GetDriverImportLibrary()+"\n)\n\ntype TestTable2 struct {\nColumnName1 time.Time `db:\"column_name_1\"`\nColumnName2 "+dbType.String()+".NullTime `db:\"column_name_2\"`\n}", |
1290
|
|
|
) |
1291
|
|
|
|
1292
|
|
|
err := Run(s, mdb, w) |
1293
|
|
|
assert.NoError(t, err) |
1294
|
|
|
}) |
1295
|
|
|
}) |
1296
|
|
|
} |
1297
|
|
|
}) |
1298
|
|
|
} |
1299
|
|
|
} |
1300
|
|
|
|
1301
|
|
|
func TestRun_BooleanColumns(t *testing.T) { |
1302
|
|
|
for dbType := range settings.SupportedDbTypes { |
1303
|
|
|
t.Run(dbType.String(), func(t *testing.T) { |
1304
|
|
|
|
1305
|
|
|
s := settings.New() |
1306
|
|
|
s.DbType = dbType |
1307
|
|
|
db := database.New(s) |
1308
|
|
|
|
1309
|
|
|
columnTypes := []string{"boolean"} |
1310
|
|
|
|
1311
|
|
|
for _, columnType := range columnTypes { |
1312
|
|
|
t.Run(columnType, func(t *testing.T) { |
1313
|
|
|
|
1314
|
|
|
t.Run("single table with NOT NULL column", func(t *testing.T) { |
1315
|
|
|
s := settings.New() |
1316
|
|
|
s.DbType = dbType |
1317
|
|
|
|
1318
|
|
|
mdb := newMockDb(db) |
1319
|
|
|
|
1320
|
|
|
table := &database.Table{ |
1321
|
|
|
Name: "test_table", |
1322
|
|
|
Columns: []database.Column{ |
1323
|
|
|
{ |
1324
|
|
|
OrdinalPosition: 1, |
1325
|
|
|
Name: "column_name", |
1326
|
|
|
DataType: columnType, |
1327
|
|
|
}, |
1328
|
|
|
}, |
1329
|
|
|
} |
1330
|
|
|
mdb.tables = append(mdb.tables, table) |
1331
|
|
|
|
1332
|
|
|
mdb. |
1333
|
|
|
On("GetTables"). |
1334
|
|
|
Return(mdb.tables, nil) |
1335
|
|
|
mdb. |
1336
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1337
|
|
|
Return(nil) |
1338
|
|
|
mdb. |
1339
|
|
|
On("GetColumnsOfTable", table) |
1340
|
|
|
|
1341
|
|
|
w := newMockWriter() |
1342
|
|
|
w. |
1343
|
|
|
On( |
1344
|
|
|
"Write", |
1345
|
|
|
"TestTable", |
1346
|
|
|
"package dto\n\ntype TestTable struct {\nColumnName bool `db:\"column_name\"`\n}", |
1347
|
|
|
) |
1348
|
|
|
|
1349
|
|
|
err := Run(s, mdb, w) |
1350
|
|
|
assert.NoError(t, err) |
1351
|
|
|
}) |
1352
|
|
|
|
1353
|
|
|
t.Run("single table with NULL column", func(t *testing.T) { |
1354
|
|
|
s := settings.New() |
1355
|
|
|
s.DbType = dbType |
1356
|
|
|
|
1357
|
|
|
mdb := newMockDb(db) |
1358
|
|
|
|
1359
|
|
|
table := &database.Table{ |
1360
|
|
|
Name: "test_table", |
1361
|
|
|
Columns: []database.Column{ |
1362
|
|
|
{ |
1363
|
|
|
OrdinalPosition: 1, |
1364
|
|
|
Name: "column_name", |
1365
|
|
|
DataType: columnType, |
1366
|
|
|
IsNullable: "YES", |
1367
|
|
|
}, |
1368
|
|
|
}, |
1369
|
|
|
} |
1370
|
|
|
mdb.tables = append(mdb.tables, table) |
1371
|
|
|
|
1372
|
|
|
mdb. |
1373
|
|
|
On("GetTables"). |
1374
|
|
|
Return(mdb.tables, nil) |
1375
|
|
|
mdb. |
1376
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1377
|
|
|
Return(nil) |
1378
|
|
|
mdb. |
1379
|
|
|
On("GetColumnsOfTable", table) |
1380
|
|
|
|
1381
|
|
|
w := newMockWriter() |
1382
|
|
|
w. |
1383
|
|
|
On( |
1384
|
|
|
"Write", |
1385
|
|
|
"TestTable", |
1386
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName sql.NullBool `db:\"column_name\"`\n}", |
1387
|
|
|
) |
1388
|
|
|
|
1389
|
|
|
err := Run(s, mdb, w) |
1390
|
|
|
assert.NoError(t, err) |
1391
|
|
|
}) |
1392
|
|
|
|
1393
|
|
|
t.Run("single table with NULL column and native data type", func(t *testing.T) { |
1394
|
|
|
s := settings.New() |
1395
|
|
|
s.DbType = dbType |
1396
|
|
|
s.Null = settings.NullTypeNative |
1397
|
|
|
|
1398
|
|
|
mdb := newMockDb(db) |
1399
|
|
|
|
1400
|
|
|
table := &database.Table{ |
1401
|
|
|
Name: "test_table", |
1402
|
|
|
Columns: []database.Column{ |
1403
|
|
|
{ |
1404
|
|
|
OrdinalPosition: 1, |
1405
|
|
|
Name: "column_name", |
1406
|
|
|
DataType: columnType, |
1407
|
|
|
IsNullable: "YES", |
1408
|
|
|
}, |
1409
|
|
|
}, |
1410
|
|
|
} |
1411
|
|
|
mdb.tables = append(mdb.tables, table) |
1412
|
|
|
|
1413
|
|
|
mdb. |
1414
|
|
|
On("GetTables"). |
1415
|
|
|
Return(mdb.tables, nil) |
1416
|
|
|
mdb. |
1417
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1418
|
|
|
Return(nil) |
1419
|
|
|
mdb. |
1420
|
|
|
On("GetColumnsOfTable", table) |
1421
|
|
|
|
1422
|
|
|
w := newMockWriter() |
1423
|
|
|
w. |
1424
|
|
|
On( |
1425
|
|
|
"Write", |
1426
|
|
|
"TestTable", |
1427
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName *bool `db:\"column_name\"`\n}", |
1428
|
|
|
) |
1429
|
|
|
|
1430
|
|
|
err := Run(s, mdb, w) |
1431
|
|
|
assert.NoError(t, err) |
1432
|
|
|
}) |
1433
|
|
|
|
1434
|
|
|
t.Run("single table with two mixed columns", func(t *testing.T) { |
1435
|
|
|
s := settings.New() |
1436
|
|
|
s.DbType = dbType |
1437
|
|
|
|
1438
|
|
|
mdb := newMockDb(db) |
1439
|
|
|
|
1440
|
|
|
table := &database.Table{ |
1441
|
|
|
Name: "test_table", |
1442
|
|
|
Columns: []database.Column{ |
1443
|
|
|
{ |
1444
|
|
|
OrdinalPosition: 1, |
1445
|
|
|
Name: "column_name_1", |
1446
|
|
|
DataType: columnType, |
1447
|
|
|
IsNullable: "YES", |
1448
|
|
|
}, |
1449
|
|
|
{ |
1450
|
|
|
OrdinalPosition: 2, |
1451
|
|
|
Name: "column_name_2", |
1452
|
|
|
DataType: columnType, |
1453
|
|
|
}, |
1454
|
|
|
}, |
1455
|
|
|
} |
1456
|
|
|
mdb.tables = append(mdb.tables, table) |
1457
|
|
|
|
1458
|
|
|
mdb. |
1459
|
|
|
On("GetTables"). |
1460
|
|
|
Return(mdb.tables, nil) |
1461
|
|
|
mdb. |
1462
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1463
|
|
|
Return(nil) |
1464
|
|
|
mdb. |
1465
|
|
|
On("GetColumnsOfTable", table) |
1466
|
|
|
|
1467
|
|
|
w := newMockWriter() |
1468
|
|
|
w. |
1469
|
|
|
On( |
1470
|
|
|
"Write", |
1471
|
|
|
"TestTable", |
1472
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable struct {\nColumnName1 sql.NullBool `db:\"column_name_1\"`\nColumnName2 bool `db:\"column_name_2\"`\n}", |
1473
|
|
|
) |
1474
|
|
|
|
1475
|
|
|
err := Run(s, mdb, w) |
1476
|
|
|
assert.NoError(t, err) |
1477
|
|
|
}) |
1478
|
|
|
|
1479
|
|
|
t.Run("single table with two mixed columns and native data type", func(t *testing.T) { |
1480
|
|
|
s := settings.New() |
1481
|
|
|
s.DbType = dbType |
1482
|
|
|
s.Null = settings.NullTypeNative |
1483
|
|
|
|
1484
|
|
|
mdb := newMockDb(db) |
1485
|
|
|
|
1486
|
|
|
table := &database.Table{ |
1487
|
|
|
Name: "test_table", |
1488
|
|
|
Columns: []database.Column{ |
1489
|
|
|
{ |
1490
|
|
|
OrdinalPosition: 1, |
1491
|
|
|
Name: "column_name_1", |
1492
|
|
|
DataType: columnType, |
1493
|
|
|
IsNullable: "YES", |
1494
|
|
|
}, |
1495
|
|
|
{ |
1496
|
|
|
OrdinalPosition: 2, |
1497
|
|
|
Name: "column_name_2", |
1498
|
|
|
DataType: columnType, |
1499
|
|
|
}, |
1500
|
|
|
}, |
1501
|
|
|
} |
1502
|
|
|
mdb.tables = append(mdb.tables, table) |
1503
|
|
|
|
1504
|
|
|
mdb. |
1505
|
|
|
On("GetTables"). |
1506
|
|
|
Return(mdb.tables, nil) |
1507
|
|
|
mdb. |
1508
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1509
|
|
|
Return(nil) |
1510
|
|
|
mdb. |
1511
|
|
|
On("GetColumnsOfTable", table) |
1512
|
|
|
|
1513
|
|
|
w := newMockWriter() |
1514
|
|
|
w. |
1515
|
|
|
On( |
1516
|
|
|
"Write", |
1517
|
|
|
"TestTable", |
1518
|
|
|
"package dto\n\nimport (\n)\n\ntype TestTable struct {\nColumnName1 *bool `db:\"column_name_1\"`\nColumnName2 bool `db:\"column_name_2\"`\n}", |
1519
|
|
|
) |
1520
|
|
|
|
1521
|
|
|
err := Run(s, mdb, w) |
1522
|
|
|
assert.NoError(t, err) |
1523
|
|
|
}) |
1524
|
|
|
|
1525
|
|
|
t.Run("multi table with multi columns", func(t *testing.T) { |
1526
|
|
|
s := settings.New() |
1527
|
|
|
s.DbType = dbType |
1528
|
|
|
|
1529
|
|
|
mdb := newMockDb(db) |
1530
|
|
|
|
1531
|
|
|
table1 := &database.Table{ |
1532
|
|
|
Name: "test_table_1", |
1533
|
|
|
Columns: []database.Column{ |
1534
|
|
|
{ |
1535
|
|
|
OrdinalPosition: 1, |
1536
|
|
|
Name: "column_name_1", |
1537
|
|
|
DataType: columnType, |
1538
|
|
|
IsNullable: "YES", |
1539
|
|
|
}, |
1540
|
|
|
{ |
1541
|
|
|
OrdinalPosition: 2, |
1542
|
|
|
Name: "column_name_2", |
1543
|
|
|
DataType: columnType, |
1544
|
|
|
}, |
1545
|
|
|
}, |
1546
|
|
|
} |
1547
|
|
|
table2 := &database.Table{ |
1548
|
|
|
Name: "test_table_2", |
1549
|
|
|
Columns: []database.Column{ |
1550
|
|
|
{ |
1551
|
|
|
OrdinalPosition: 1, |
1552
|
|
|
Name: "column_name_1", |
1553
|
|
|
DataType: columnType, |
1554
|
|
|
}, |
1555
|
|
|
{ |
1556
|
|
|
OrdinalPosition: 2, |
1557
|
|
|
Name: "column_name_2", |
1558
|
|
|
DataType: columnType, |
1559
|
|
|
IsNullable: "YES", |
1560
|
|
|
}, |
1561
|
|
|
}, |
1562
|
|
|
} |
1563
|
|
|
mdb.tables = append(mdb.tables, table1, table2) |
1564
|
|
|
|
1565
|
|
|
mdb. |
1566
|
|
|
On("GetTables"). |
1567
|
|
|
Return(mdb.tables, nil) |
1568
|
|
|
mdb. |
1569
|
|
|
On("PrepareGetColumnsOfTableStmt"). |
1570
|
|
|
Return(nil) |
1571
|
|
|
mdb. |
1572
|
|
|
On("GetColumnsOfTable", table1). |
1573
|
|
|
On("GetColumnsOfTable", table2) |
1574
|
|
|
|
1575
|
|
|
w := newMockWriter() |
1576
|
|
|
w. |
1577
|
|
|
On( |
1578
|
|
|
"Write", |
1579
|
|
|
"TestTable1", |
1580
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable1 struct {\nColumnName1 sql.NullBool `db:\"column_name_1\"`\nColumnName2 bool `db:\"column_name_2\"`\n}", |
1581
|
|
|
). |
1582
|
|
|
On( |
1583
|
|
|
"Write", |
1584
|
|
|
"TestTable2", |
1585
|
|
|
"package dto\n\nimport (\n\t\"database/sql\"\n)\n\ntype TestTable2 struct {\nColumnName1 bool `db:\"column_name_1\"`\nColumnName2 sql.NullBool `db:\"column_name_2\"`\n}", |
1586
|
|
|
) |
1587
|
|
|
|
1588
|
|
|
err := Run(s, mdb, w) |
1589
|
|
|
assert.NoError(t, err) |
1590
|
|
|
}) |
1591
|
|
|
}) |
1592
|
|
|
} |
1593
|
|
|
}) |
1594
|
|
|
} |
1595
|
|
|
} |
1596
|
|
|
|