-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynmgrm_test.go
534 lines (505 loc) · 11 KB
/
dynmgrm_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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package dynmgrm
import (
"database/sql"
"errors"
"github.com/miyamo2/dynmgrm/internal/mocks"
"github.com/miyamo2/godynamo"
"go.uber.org/mock/gomock"
"reflect"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
)
func TestNew(t *testing.T) {
type args struct {
option []DialectorOption
}
type want struct {
dsn string
conn gorm.ConnPool
}
type test struct {
args args
want want
env map[string]string
}
tests := map[string]test{
"happy_path/without_option": {
args: args{
option: []DialectorOption{},
},
want: want{
dsn: "",
},
},
"happy_path/with_region": {
args: args{
option: []DialectorOption{
WithRegion("ap-north-east1"),
},
},
want: want{
dsn: "region=ap-north-east1",
},
},
"happy_path/with_ak_id": {
args: args{
option: []DialectorOption{
WithAccessKeyID("ACCESS_KEY_ID"),
},
},
want: want{
dsn: "akId=ACCESS_KEY_ID",
},
},
"happy_path/with_secret": {
args: args{
option: []DialectorOption{
WithSecretKey("SECRET"),
}},
want: want{
dsn: "secretKey=SECRET",
},
},
"happy_path/with_endpoint": {
args: args{
option: []DialectorOption{
WithEndpoint("http://localhost:8000"),
},
},
want: want{
dsn: "endpoint=http://localhost:8000",
},
},
"happy_path/with_timeout": {
args: args{
option: []DialectorOption{
WithTimeout(1000),
},
},
want: want{
dsn: "timeout=1000",
},
},
"happy_path/with_connection": {
args: args{
option: []DialectorOption{
WithConnection(&sql.DB{}),
},
},
want: want{
dsn: "",
conn: &sql.DB{},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
for k, v := range tt.env {
t.Setenv(k, v)
}
got := New(tt.args.option...)
d, ok := got.(*Dialector)
if !ok {
t.Fatal("dialector is not *Dialector")
}
if diff := cmp.Diff(tt.want.dsn, d.dbOpener.DSN()); diff != "" {
t.Errorf("DSN() mismatch (-want +got): \n%v", diff)
}
if !reflect.DeepEqual(tt.want.conn, d.conn) {
t.Errorf("conn expected: %v, actual: %v", tt.want.conn, d.conn)
}
})
}
}
func TestOpen(t *testing.T) {
type args struct {
dsn string
}
type want struct {
dsn string
}
type test struct {
args args
want want
}
tests := map[string]test{
"happy_path": {
args: args{
dsn: "region=ap-north-east1;akId=ACCESS_KEY_ID;secret",
},
want: want{
dsn: "region=ap-north-east1;akId=ACCESS_KEY_ID;secret",
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := Open(tt.args.dsn)
d, ok := got.(*Dialector)
if !ok {
t.Fatal("dialector is not *Dialector")
}
if diff := cmp.Diff(tt.want.dsn, d.dbOpener.DSN()); diff != "" {
t.Errorf("DSN() mismatch (-want +got): \n%v", diff)
}
})
}
}
type mockClauseWriter struct {
v byte
}
func (m *mockClauseWriter) WriteByte(b byte) error {
m.v = b
return nil
}
func (m *mockClauseWriter) WriteString(s string) (int, error) {
return 0, nil
}
func TestDialector_BindVarTo(t *testing.T) {
type args struct {
writer clause.Writer
stmt *gorm.Statement
v interface{}
}
type test struct {
args args
expect byte
}
tests := map[string]test{
"happy_path": {
args: args{
writer: &mockClauseWriter{},
stmt: nil,
v: nil,
},
expect: '?',
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dialector := Dialector{}
dialector.BindVarTo(tt.args.writer, tt.args.stmt, tt.args.v)
actual := tt.args.writer.(*mockClauseWriter).v
if diff := cmp.Diff(tt.expect, actual); diff != "" {
t.Errorf("BindVarTo() mismatch (-want +got): \n%v", diff)
}
})
}
}
func TestDialector_Name(t *testing.T) {
type test struct {
want string
}
tests := map[string]test{
"happy_path": {
want: DBName,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dialector := Dialector{}
got := dialector.Name()
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("Name() mismatch (-want +got): \n%v", diff)
}
})
}
}
func TestDialector_DefaultValueOf(t *testing.T) {
type args struct {
field *schema.Field
}
type test struct {
args args
want clause.Expression
}
tests := map[string]test{
"happy_path": {
args: args{
field: &schema.Field{},
},
want: clause.Expr{SQL: ""},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dialector := Dialector{}
got := dialector.DefaultValueOf(tt.args.field)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("DefaultValueOf() mismatch (-want +got): \n%v", diff)
}
})
}
}
func Test_writeConnectionParameter(t *testing.T) {
type args struct {
dsnbuf *strings.Builder
key string
value string
}
type test struct {
args args
preAddition map[string]string
expectString string
}
tests := map[string]test{
"happy_path": {
args: args{
dsnbuf: &strings.Builder{},
key: "key",
value: "value",
},
expectString: "key=value",
},
"happy_path/with_pre_addition": {
args: args{
dsnbuf: &strings.Builder{},
key: "key2",
value: "value2",
},
preAddition: map[string]string{
"key1": "value1",
},
expectString: "key1=value1;key2=value2",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
for k, v := range tt.preAddition {
writeConnectionParameter(tt.args.dsnbuf, k, v)
}
writeConnectionParameter(tt.args.dsnbuf, tt.args.key, tt.args.value)
actual := tt.args.dsnbuf.String()
if diff := cmp.Diff(tt.expectString, actual); diff != "" {
t.Errorf("writeConnectionParameter() mismatch (-want +got): \n%v", diff)
}
})
}
}
func TestDialector_QuoteTo(t *testing.T) {
type args struct {
writer clause.Writer
str string
}
type test struct {
args args
expected string
}
tests := map[string]test{
"happy_path": {
args: args{
writer: &gorm.Statement{},
str: "test",
},
expected: `"test"`,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dialector := Dialector{}
dialector.QuoteTo(tt.args.writer, tt.args.str)
actual := tt.args.writer.(*gorm.Statement).SQL.String()
if diff := cmp.Diff(tt.expected, actual); diff != "" {
t.Errorf("QuoteTo() mismatch (-want +got): \n%v", diff)
}
})
}
}
func TestDialector_DataTypeOf(t *testing.T) {
type args struct {
field *schema.Field
}
type test struct {
args args
want string
}
tests := map[string]test{
"happy_path/bool": {
args: args{
field: &schema.Field{
DataType: schema.Bool,
},
},
want: KeySchemaDataTypeString.String(),
},
"happy_path/uint": {
args: args{
field: &schema.Field{
DataType: schema.Uint,
},
},
want: KeySchemaDataTypeNumber.String(),
},
"happy_path/int": {
args: args{
field: &schema.Field{
DataType: schema.Int,
},
},
want: KeySchemaDataTypeNumber.String(),
},
"happy_path/float": {
args: args{
field: &schema.Field{
DataType: schema.Float,
},
},
want: KeySchemaDataTypeNumber.String(),
},
"happy_path/string": {
args: args{
field: &schema.Field{
DataType: schema.String,
},
},
want: KeySchemaDataTypeString.String(),
},
"happy_path/bytes": {
args: args{
field: &schema.Field{
DataType: schema.Bytes,
},
},
want: KeySchemaDataTypeBinary.String(),
},
"happy_path/time": {
args: args{
field: &schema.Field{
DataType: schema.Time,
},
},
want: KeySchemaDataTypeString.String(),
},
"happy_path/default": {
args: args{
field: &schema.Field{
DataType: schema.DataType("jsonb"),
},
},
want: KeySchemaDataTypeString.String(),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dialector := Dialector{}
got := dialector.DataTypeOf(tt.args.field)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("DataTypeOf() mismatch (-want +got): \n%v", diff)
}
})
}
}
func TestDialector_Initialize(t *testing.T) {
type test struct {
want error
conn gorm.ConnPool
setupDBOpener func(*mocks.MockDBOpener)
setupCallbacksRegisterer func(*mocks.MockCallbacksRegisterer)
}
errApply := errors.New("apply error")
tests := map[string]test{
"happy_path": {
setupDBOpener: func(do *mocks.MockDBOpener) {
do.EXPECT().Apply().Times(1).Return(&sql.DB{}, nil)
},
setupCallbacksRegisterer: func(registerer *mocks.MockCallbacksRegisterer) {
registerer.EXPECT().Register(gomock.Any(), gomock.Any()).Times(1)
},
},
"happy_path/with-conn": {
conn: &sql.DB{},
setupDBOpener: func(do *mocks.MockDBOpener) {
do.EXPECT().Apply().Times(0)
},
setupCallbacksRegisterer: func(registerer *mocks.MockCallbacksRegisterer) {
registerer.EXPECT().Register(gomock.Any(), gomock.Any()).Times(1)
},
},
"unhappy_path/apply-error": {
setupDBOpener: func(do *mocks.MockDBOpener) {
do.EXPECT().Apply().Times(1).Return(nil, errApply)
},
setupCallbacksRegisterer: func(registerer *mocks.MockCallbacksRegisterer) {
registerer.EXPECT().Register(gomock.Any(), gomock.Any()).Times(0)
},
want: errApply,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
do := mocks.NewMockDBOpener(ctrl)
if setup := tt.setupDBOpener; setup != nil {
setup(do)
}
cr := mocks.NewMockCallbacksRegisterer(ctrl)
if setup := tt.setupCallbacksRegisterer; setup != nil {
setup(cr)
}
dialector := Dialector{
dbOpener: do,
callbacksRegisterer: cr,
}
if conn := tt.conn; conn != nil {
dialector.conn = conn
}
if err := dialector.Initialize(&gorm.DB{
Config: &gorm.Config{
ClauseBuilders: make(map[string]clause.ClauseBuilder),
},
}); !errors.Is(tt.want, err) {
t.Errorf("Initialize() error = %v, wantErr %v", err, tt.want)
}
})
}
}
func TestDialector_Translate(t *testing.T) {
type test struct {
args error
want error
}
errOther := errors.New("other")
tests := map[string]test{
"happy_path/ErrTxCommitting": {
args: godynamo.ErrInTx,
want: gorm.ErrInvalidTransaction,
},
"happy_path/ErrTxRollingBack": {
args: godynamo.ErrTxRollingBack,
want: gorm.ErrInvalidTransaction,
},
"happy_path/ErrInTx": {
args: godynamo.ErrInTx,
want: gorm.ErrInvalidTransaction,
},
"happy_path/ErrInvalidTxStage": {
args: godynamo.ErrInvalidTxStage,
want: gorm.ErrInvalidTransaction,
},
"happy_path/ErrNoTx": {
args: godynamo.ErrNoTx,
want: gorm.ErrInvalidTransaction,
},
"happy_path/other": {
args: errOther,
want: errOther,
},
"happy_path/nil": {
args: nil,
want: nil,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dialector := Dialector{}
err := dialector.Translate(tt.args)
if !errors.Is(err, tt.want) {
t.Errorf("Translate() error = %v, want %v", err, tt.want)
}
})
}
}