-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarshal_test.go
424 lines (371 loc) · 11.6 KB
/
marshal_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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2016 Horacio Duran.
// Licenced under the MIT licence, see LICENCE for details.
package sqlmarshal
import (
"fmt"
"reflect"
"testing"
goyaml "gopkg.in/yaml.v2"
)
type dumbFK struct {
aField int `sql:"primary"`
anotherField string
}
type dumbStruct struct {
testInt int `sql:"primary"`
testString string
testFloat float32
testPtr *dumbFK
testStruct dumbFK
}
type dumbFKMulti struct {
aField int `sql:"primary"`
aField2 int `sql:"primary"`
anotherField string
}
type dumbStructMulti struct {
testInt int `sql:"primary"`
testString string
testFloat float32
testPtr *dumbFKMulti
testStruct dumbFKMulti
}
type untaggedDumbFK struct {
aField int
anotherField string
}
type untaggedDumbStruct struct {
testInt int
testString string
testFloat float32
testPtr *untaggedDumbFK
testStruct untaggedDumbFK
}
func (*dumbStruct) methodsAreIgnored() {
}
func TestTagged(t *testing.T) {
d := dumbStruct{
testInt: 1,
testString: "some velvet string",
testFloat: 2.0,
testPtr: &dumbFK{
aField: 3,
anotherField: "another string",
},
testStruct: dumbFK{
aField: 4,
anotherField: "another non struct field",
},
}
m, err := NewTypeSQLMarshaller(d, "")
if err != nil {
t.Errorf("cannot create marshaler: %v", err)
}
dr := &ANSISQLDriver{}
c, err := m.Create(dr)
if err != nil {
t.Errorf("cannot marshall to CREATE statement: %v", err)
}
t.Log(c)
expectedSQL := `CREATE TABLE dumbStruct (testInt SMALLINT, testString VARCHAR, testFloat FLOAT, testPtr_aField_fk SMALLINT, testStruct_aField_fk SMALLINT, FOREIGN KEY (testPtr_aField_fk) REFERENCES dumbFK (aField) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (testStruct_aField_fk) REFERENCES dumbFK (aField) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (testInt));`
if c != expectedSQL {
t.Errorf("unexpected CREATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
c, err = m.Insert(d)
if err != nil {
t.Errorf("cannot marshall to INSERT statement: %v", err)
}
t.Log(c)
expectedSQL = `INSERT INTO dumbStruct (testInt, testString, testFloat, testPtr_aField_fk, testStruct_aField_fk) VALUES (1, "some velvet string", 2.000000, 3, 4);`
if c != expectedSQL {
t.Errorf("unexpected INSERT statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
c, err = m.UpdatePK(d)
if err != nil {
t.Errorf("cannot marshall to UPDATE statement: %v", err)
}
t.Log(c)
expectedSQL = `UPDATE dumbStruct SET testString="some velvet string", testFloat=2.000000, testPtr_aField_fk=3, testStruct_aField_fk=4 WHERE testInt=1;`
if c != expectedSQL {
t.Errorf("unexpected UPDATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
}
func TestTaggedMultiPK(t *testing.T) {
dm := dumbFKMulti{
aField: 3,
aField2: 5,
anotherField: "another string",
}
d := dumbStructMulti{
testInt: 1,
testString: "some velvet string",
testFloat: 2.0,
testPtr: &dm,
testStruct: dumbFKMulti{
aField: 4,
aField2: 6,
anotherField: "another non struct field",
},
}
m, err := NewTypeSQLMarshaller(d, "")
if err != nil {
t.Errorf("cannot create marshaler: %v", err)
}
dr := &ANSISQLDriver{}
c, err := m.Create(dr)
if err != nil {
t.Errorf("cannot marshall to CREATE statement: %v", err)
}
t.Log(c)
expectedSQL := `CREATE TABLE dumbStructMulti (testInt SMALLINT, testString VARCHAR, testFloat FLOAT, testPtr_aField_fk SMALLINT, testPtr_aField2_fk SMALLINT, testStruct_aField_fk SMALLINT, testStruct_aField2_fk SMALLINT, FOREIGN KEY (testPtr_aField_fk, testPtr_aField2_fk) REFERENCES dumbFKMulti (aField, aField2) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (testStruct_aField_fk, testStruct_aField2_fk) REFERENCES dumbFKMulti (aField, aField2) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (testInt));`
if c != expectedSQL {
t.Errorf("unexpected CREATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
c, err = m.Insert(d)
if err != nil {
t.Errorf("cannot marshall to INSERT statement: %v", err)
}
t.Log(c)
expectedSQL = `INSERT INTO dumbStructMulti (testInt, testString, testFloat, testPtr_aField_fk, testPtr_aField2_fk, testStruct_aField_fk, testStruct_aField2_fk) VALUES (1, "some velvet string", 2.000000, 3, 5, 4, 6);`
if c != expectedSQL {
t.Errorf("unexpected INSERT statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
c, err = m.UpdatePK(d)
if err != nil {
t.Errorf("cannot marshall to UPDATE statement: %v", err)
}
t.Log(c)
expectedSQL = `UPDATE dumbStructMulti SET testString="some velvet string", testFloat=2.000000, testPtr_aField_fk=3, testPtr_aField2_fk=5, testStruct_aField_fk=4, testStruct_aField2_fk=6 WHERE testInt=1;`
if c != expectedSQL {
t.Errorf("unexpected UPDATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
m, err = NewTypeSQLMarshaller(dm, "")
if err != nil {
t.Errorf("cannot create marshaler: %v", err)
}
c, err = m.Create(dr)
if err != nil {
t.Errorf("cannot marshall to CREATE statement: %v", err)
}
t.Log(c)
expectedSQL = `CREATE TABLE dumbFKMulti (aField SMALLINT, aField2 SMALLINT, anotherField VARCHAR, PRIMARY KEY (aField ,aField2));`
if c != expectedSQL {
t.Errorf("unexpected CREATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
c, err = m.UpdatePK(dm)
if err != nil {
t.Errorf("cannot marshall to UPDATE statement: %v", err)
}
t.Log(c)
expectedSQL = `UPDATE dumbFKMulti SET anotherField="another string" WHERE aField=3 AND aField2=5;`
if c != expectedSQL {
t.Errorf("unexpected UPDATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
}
func TestUnTagged(t *testing.T) {
d := untaggedDumbStruct{
testInt: 1,
testString: "some velvet string",
testFloat: 2.0,
testPtr: &untaggedDumbFK{
aField: 3,
anotherField: "another string",
},
testStruct: untaggedDumbFK{
aField: 4,
anotherField: "another non struct field",
},
}
m, err := NewTypeSQLMarshaller(d, "")
if err != nil {
t.Errorf("cannot create marshaler: %v", err)
}
dr := &ANSISQLDriver{}
c, err := m.Create(dr)
if err != nil {
t.Errorf("cannot marshall to CREATE statement: %v", err)
}
t.Log(c)
expectedSQL := `CREATE TABLE untaggedDumbStruct (testInt SMALLINT, testString VARCHAR, testFloat FLOAT, testPtr INT, testStruct INT, FOREIGN KEY (testPtr) REFERENCES untaggedDumbFK (_ID) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (testStruct) REFERENCES untaggedDumbFK (_ID) ON DELETE CASCADE ON UPDATE CASCADE);`
if c != expectedSQL {
t.Errorf("unexpected CREATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
}
// DOC Sample
type Sample struct {
ID int `sql:"primary"`
Name string
Reference *Reference
ConcreteReference Reference
}
type Reference struct {
DifferentNameID int `sql:"primary"`
Name string
}
func doSQLCreate() (string, error) {
sample := Sample{
ID: 1,
Name: "a sample name",
Reference: &Reference{
DifferentNameID: 1,
Name: "a reference name",
},
ConcreteReference: Reference{
DifferentNameID: 2,
Name: "another reference name",
},
}
m, err := NewTypeSQLMarshaller(sample, "")
if err != nil {
return "", fmt.Errorf("cannot create marshaler: %v", err)
}
dr := &ANSISQLDriver{}
c, err := m.Create(dr)
if err != nil {
return "", fmt.Errorf("cannot marshall to CREATE statement: %v", err)
}
return c, nil
}
func TestDocSampleCreate(t *testing.T) {
c, err := doSQLCreate()
if err != nil {
t.Errorf("could not run documentation sample for CREATE: %v", err)
}
t.Log(c)
expectedSQL := `CREATE TABLE Sample (ID SMALLINT, Name VARCHAR, Reference_DifferentNameID_fk SMALLINT, ConcreteReference_DifferentNameID_fk SMALLINT, FOREIGN KEY (Reference_DifferentNameID_fk) REFERENCES Reference (DifferentNameID) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (ConcreteReference_DifferentNameID_fk) REFERENCES Reference (DifferentNameID) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (ID));`
if c != expectedSQL {
t.Errorf("unexpected CREATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
}
type SampleInsert struct {
ID int `sql:"primary"`
Name string
Reference *ReferenceInsert
ConcreteReference Reference
}
type ReferenceInsert struct {
DifferentNameID int `sql:"primary"`
AnExtraID int `sql:"primary"`
Name string
}
func doSQLInsert() (string, error) {
sample := SampleInsert{
ID: 1,
Name: "a sample name",
Reference: &ReferenceInsert{
DifferentNameID: 1,
AnExtraID: 3,
Name: "a reference name",
},
ConcreteReference: Reference{
DifferentNameID: 2,
Name: "another reference name",
},
}
m, err := NewTypeSQLMarshaller(sample, "")
if err != nil {
return "", fmt.Errorf("cannot create marshaler: %v", err)
}
c, err := m.Insert(sample)
if err != nil {
return "", fmt.Errorf("cannot marshall to INSERT statement: %v", err)
}
return c, nil
}
func TestDocSampleInsert(t *testing.T) {
c, err := doSQLInsert()
if err != nil {
t.Errorf("could not run documentation sample for INSERT: %v", err)
}
t.Log(c)
expectedSQL := `INSERT INTO SampleInsert (ID, Name, Reference_DifferentNameID_fk, Reference_AnExtraID_fk, ConcreteReference_DifferentNameID_fk) VALUES (1, "a sample name", 1, 3, 2);`
if c != expectedSQL {
t.Errorf("unexpected INSERT statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
}
type ReferenceUpdate struct {
DifferentNameID int `sql:"primary"`
AnExtraID int `sql:"primary"`
Name string
AnotherField string
}
func doSQLUpdate() (string, error) {
sample := ReferenceUpdate{
DifferentNameID: 1,
AnExtraID: 3,
Name: "a reference name",
AnotherField: "just to show off",
}
m, err := NewTypeSQLMarshaller(sample, "")
if err != nil {
return "", fmt.Errorf("cannot create marshaler: %v", err)
}
c, err := m.UpdatePK(sample)
if err != nil {
return "", fmt.Errorf("cannot marshall to UPDATE statement: %v", err)
}
return c, nil
}
func TestDocSampleUpdate(t *testing.T) {
c, err := doSQLUpdate()
if err != nil {
t.Errorf("could not run documentation sample for UPDATE: %v", err)
}
t.Log(c)
expectedSQL := `UPDATE ReferenceUpdate SET Name="a reference name", AnotherField="just to show off" WHERE DifferentNameID=1 AND AnExtraID=3;`
if c != expectedSQL {
t.Errorf("unexpected CREATE statement: \nexpected: %q\nobtained: %q", expectedSQL, c)
}
}
const (
ValidYAMLSchema string = `
tables:
humans:
fields:
id:
type: int
unique: true
primary: true
created:
type: int
mamals:
fields:
id:
type: int
created:
type: string
unique: true`
)
type DatabaseSchema struct {
Tables map[string]map[string]interface{}
}
func TestCreateFromYAML(t *testing.T) {
var schema DatabaseSchema
err := goyaml.Unmarshal([]byte(ValidYAMLSchema), &schema)
if err != nil {
t.Errorf("%v", err)
}
var obtained []string
for table_name, table := range schema.Tables {
if fields, ok := table["fields"]; ok {
m, err := NewTypeSQLMarshaller(fields, table_name)
if err != nil {
t.Errorf("Cannot create marshaller: %v", err)
}
dr := &ANSISQLDriver{}
c, err := m.Create(dr)
if err != nil {
t.Errorf("cannot marshall to CREATE statement: %v", err)
}
obtained = append(obtained, c)
}
}
expected := []string{
"CREATE TABLE humans (id SMALLINT, created SMALLINT, PRIMARY KEY (id));",
"CREATE TABLE mamals (id SMALLINT, created VARCHAR);",
}
if !reflect.DeepEqual(obtained, expected) {
t.Errorf("unexpected CREATE statement: \nexpected: %q\nobtained: %q", expected, obtained)
}
t.Log(obtained)
}