forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvp_go19_db_test.go
751 lines (684 loc) · 18.4 KB
/
tvp_go19_db_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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// +build go1.9
package mssql
import (
"context"
"database/sql"
"log"
"reflect"
"testing"
"time"
)
const (
crateSchema = `create schema TestTVPSchema;`
dropSchema = `drop schema TestTVPSchema;`
createTVP = `
CREATE TYPE TestTVPSchema.exempleTVP AS TABLE
(
message NVARCHAR(100)
)`
dropTVP = `DROP TYPE TestTVPSchema.exempleTVP;`
procedureWithTVP = `
CREATE PROCEDURE ExecTVP
@param1 TestTVPSchema.exempleTVP READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM @param1;
END;
`
dropProcedure = `drop PROCEDURE ExecTVP`
execTvp = `exec ExecTVP @param1;`
)
type TvptableRow struct {
PBinary []byte `db:"p_binary"`
PVarchar string `db:"p_varchar"`
PVarcharNull *string `db:"p_varcharNull"`
PNvarchar string `db:"p_nvarchar"`
PNvarcharNull *string `db:"p_nvarcharNull"`
PID UniqueIdentifier `db:"p_id"`
PIDNull *UniqueIdentifier `db:"p_idNull"`
PVarbinary []byte `db:"p_varbinary"`
PTinyint int8 `db:"p_tinyint"`
PTinyintNull *int8 `db:"p_tinyintNull"`
PSmallint int16 `db:"p_smallint"`
PSmallintNull *int16 `db:"p_smallintNull"`
PInt int32 `db:"p_int"`
PIntNull *int32 `db:"p_intNull"`
PBigint int64 `db:"p_bigint"`
PBigintNull *int64 `db:"p_bigintNull"`
PBit bool `db:"p_bit"`
PBitNull *bool `db:"p_bitNull"`
PFloat32 float32 `db:"p_float32"`
PFloatNull32 *float32 `db:"p_floatNull32"`
PFloat64 float64 `db:"p_float64"`
PFloatNull64 *float64 `db:"p_floatNull64"`
DTime time.Time `db:"p_timeNull"`
DTimeNull *time.Time `db:"p_time"`
Pint int `db:"pInt"`
PintNull *int `db:"pIntNull"`
}
type TvptableRowWithSkipTag struct {
PBinary []byte `db:"p_binary"`
SkipPBinary []byte `json:"-"`
PVarchar string `db:"p_varchar"`
SkipPVarchar string `tvp:"-"`
PVarcharNull *string `db:"p_varcharNull"`
SkipPVarcharNull *string `json:"-" tvp:"-"`
PNvarchar string `db:"p_nvarchar"`
SkipPNvarchar string `json:"-"`
PNvarcharNull *string `db:"p_nvarcharNull"`
SkipPNvarcharNull *string `json:"-"`
PID UniqueIdentifier `db:"p_id"`
SkipPID UniqueIdentifier `json:"-"`
PIDNull *UniqueIdentifier `db:"p_idNull"`
SkipPIDNull *UniqueIdentifier `tvp:"-"`
PVarbinary []byte `db:"p_varbinary"`
SkipPVarbinary []byte `json:"-" tvp:"-"`
PTinyint int8 `db:"p_tinyint"`
SkipPTinyint int8 `tvp:"-"`
PTinyintNull *int8 `db:"p_tinyintNull"`
SkipPTinyintNull *int8 `tvp:"-" json:"any"`
PSmallint int16 `db:"p_smallint"`
SkipPSmallint int16 `json:"-"`
PSmallintNull *int16 `db:"p_smallintNull"`
SkipPSmallintNull *int16 `tvp:"-"`
PInt int32 `db:"p_int"`
SkipPInt int32 `json:"-"`
PIntNull *int32 `db:"p_intNull"`
SkipPIntNull *int32 `tvp:"-"`
PBigint int64 `db:"p_bigint"`
SkipPBigint int64 `tvp:"-"`
PBigintNull *int64 `db:"p_bigintNull"`
SkipPBigintNull *int64 `json:"any" tvp:"-"`
PBit bool `db:"p_bit"`
SkipPBit bool `json:"-"`
PBitNull *bool `db:"p_bitNull"`
SkipPBitNull *bool `json:"-"`
PFloat32 float32 `db:"p_float32"`
SkipPFloat32 float32 `tvp:"-"`
PFloatNull32 *float32 `db:"p_floatNull32"`
SkipPFloatNull32 *float32 `tvp:"-"`
PFloat64 float64 `db:"p_float64"`
SkipPFloat64 float64 `tvp:"-"`
PFloatNull64 *float64 `db:"p_floatNull64"`
SkipPFloatNull64 *float64 `tvp:"-"`
DTime time.Time `db:"p_timeNull"`
SkipDTime time.Time `tvp:"-"`
DTimeNull *time.Time `db:"p_time"`
SkipDTimeNull *time.Time `tvp:"-"`
Pint int `db:"p_int_null"`
SkipPint int `tvp:"-"`
PintNull *int `db:"p_int_"`
SkipPintNull *int `tvp:"-"`
}
func TestTVP(t *testing.T) {
checkConnStr(t)
SetLogger(testLogger{t})
db, err := sql.Open("sqlserver", makeConnStr(t).String())
if err != nil {
t.Fatalf("failed to open driver sqlserver")
}
defer db.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sqltextcreatetable := `
CREATE TYPE tvptable AS TABLE
(
p_binary BINARY(3),
p_varchar VARCHAR(500),
p_varcharNull VARCHAR(500),
p_nvarchar NVARCHAR(100),
p_nvarcharNull NVARCHAR(100),
p_id UNIQUEIDENTIFIER,
p_idNull UNIQUEIDENTIFIER,
p_varbinary VARBINARY(MAX),
p_tinyint TINYINT,
p_tinyintNull TINYINT,
p_smallint SMALLINT,
p_smallintNull SMALLINT,
p_int INT,
p_intNull INT,
p_bigint BIGINT,
p_bigintNull BIGINT,
p_bit BIT,
p_bitNull BIT,
p_float32 FLOAT,
p_floatNull32 FLOAT,
p_float64 FLOAT,
p_floatNull64 FLOAT,
p_time datetime2,
p_timeNull datetime2,
pInt INT,
pIntNull INT
); `
sqltextdroptable := `DROP TYPE tvptable;`
sqltextcreatesp := `
CREATE PROCEDURE spwithtvp
@param1 tvptable READONLY,
@param2 tvptable READONLY,
@param3 NVARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM @param1;
SELECT * FROM @param2;
SELECT @param3;
END;`
sqltextdropsp := `DROP PROCEDURE spwithtvp;`
db.ExecContext(ctx, sqltextdropsp)
db.ExecContext(ctx, sqltextdroptable)
_, err = db.ExecContext(ctx, sqltextcreatetable)
if err != nil {
t.Fatal(err)
}
defer db.ExecContext(ctx, sqltextdroptable)
_, err = db.ExecContext(ctx, sqltextcreatesp)
if err != nil {
t.Fatal(err)
}
defer db.ExecContext(ctx, sqltextdropsp)
varcharNull := "aaa"
nvarchar := "bbb"
bytesMock := []byte("ddd")
i8 := int8(1)
i16 := int16(2)
i32 := int32(3)
i64 := int64(4)
i := int(5)
bFalse := false
floatValue64 := 0.123
floatValue32 := float32(-10.123)
timeNow := time.Now().UTC()
param1 := []TvptableRow{
{
PBinary: []byte("ccc"),
PVarchar: varcharNull,
PNvarchar: nvarchar,
PID: UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
PVarbinary: bytesMock,
PTinyint: i8,
PSmallint: i16,
PInt: i32,
PBigint: i64,
PBit: bFalse,
PFloat32: floatValue32,
PFloat64: floatValue64,
DTime: timeNow,
Pint: 355,
},
{
PBinary: []byte("www"),
PVarchar: "eee",
PNvarchar: "lll",
PID: UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
PVarbinary: []byte("zzz"),
PTinyint: 5,
PSmallint: 16000,
PInt: 20000000,
PBigint: 2000000020000000,
PBit: true,
PFloat32: -123.45,
PFloat64: -123.45,
DTime: time.Date(2001, 11, 16, 23, 59, 39, 0, time.UTC),
Pint: 455,
},
{
PBinary: nil,
PVarcharNull: &varcharNull,
PNvarcharNull: &nvarchar,
PIDNull: &UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
PTinyintNull: &i8,
PSmallintNull: &i16,
PIntNull: &i32,
PBigintNull: &i64,
PBitNull: &bFalse,
PFloatNull32: &floatValue32,
PFloatNull64: &floatValue64,
DTime: timeNow,
DTimeNull: &timeNow,
PintNull: &i,
},
{
PBinary: []byte("www"),
PVarchar: "eee",
PNvarchar: "lll",
PIDNull: &UniqueIdentifier{},
PVarbinary: []byte("zzz"),
PTinyint: 5,
PSmallint: 16000,
PInt: 20000000,
PBigint: 2000000020000000,
PBit: true,
PFloat64: 123.45,
DTime: time.Date(2001, 11, 16, 23, 59, 39, 0, time.UTC),
PVarcharNull: &varcharNull,
PNvarcharNull: &nvarchar,
PTinyintNull: &i8,
PSmallintNull: &i16,
PIntNull: &i32,
PBigintNull: &i64,
PBitNull: &bFalse,
PFloatNull32: &floatValue32,
PFloatNull64: &floatValue64,
DTimeNull: &timeNow,
PintNull: &i,
},
}
tvpType := TVP{
TypeName: "tvptable",
Value: param1,
}
tvpTypeEmpty := TVP{
TypeName: "tvptable",
Value: []TvptableRow{},
}
rows, err := db.QueryContext(ctx,
"exec spwithtvp @param1, @param2, @param3",
sql.Named("param1", tvpType),
sql.Named("param2", tvpTypeEmpty),
sql.Named("param3", "test"),
)
if err != nil {
t.Fatal(err)
}
var result1 []TvptableRow
for rows.Next() {
var val TvptableRow
err := rows.Scan(
&val.PBinary,
&val.PVarchar,
&val.PVarcharNull,
&val.PNvarchar,
&val.PNvarcharNull,
&val.PID,
&val.PIDNull,
&val.PVarbinary,
&val.PTinyint,
&val.PTinyintNull,
&val.PSmallint,
&val.PSmallintNull,
&val.PInt,
&val.PIntNull,
&val.PBigint,
&val.PBigintNull,
&val.PBit,
&val.PBitNull,
&val.PFloat32,
&val.PFloatNull32,
&val.PFloat64,
&val.PFloatNull64,
&val.DTime,
&val.DTimeNull,
&val.Pint,
&val.PintNull,
)
if err != nil {
t.Fatalf("scan failed with error: %s", err)
}
result1 = append(result1, val)
}
if !reflect.DeepEqual(param1, result1) {
t.Logf("expected: %+v", param1)
t.Logf("actual: %+v", result1)
t.Errorf("first resultset did not match param1")
}
if !rows.NextResultSet() {
t.Errorf("second resultset did not exist")
}
if rows.Next() {
t.Errorf("second resultset was not empty")
}
if !rows.NextResultSet() {
t.Errorf("third resultset did not exist")
}
if !rows.Next() {
t.Errorf("third resultset was empty")
}
var result3 string
if err := rows.Scan(&result3); err != nil {
t.Errorf("error scanning third result set: %s", err)
}
if result3 != "test" {
t.Errorf("third result set had wrong value expected: %s actual: %s", "test", result3)
}
}
func TestTVP_WithTag(t *testing.T) {
checkConnStr(t)
SetLogger(testLogger{t})
db, err := sql.Open("sqlserver", makeConnStr(t).String())
if err != nil {
t.Fatalf("failed to open driver sqlserver")
}
defer db.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sqltextcreatetable := `
CREATE TYPE tvptable AS TABLE
(
p_binary BINARY(3),
p_varchar VARCHAR(500),
p_varcharNull VARCHAR(500),
p_nvarchar NVARCHAR(100),
p_nvarcharNull NVARCHAR(100),
p_id UNIQUEIDENTIFIER,
p_idNull UNIQUEIDENTIFIER,
p_varbinary VARBINARY(MAX),
p_tinyint TINYINT,
p_tinyintNull TINYINT,
p_smallint SMALLINT,
p_smallintNull SMALLINT,
p_int INT,
p_intNull INT,
p_bigint BIGINT,
p_bigintNull BIGINT,
p_bit BIT,
p_bitNull BIT,
p_float32 FLOAT,
p_floatNull32 FLOAT,
p_float64 FLOAT,
p_floatNull64 FLOAT,
p_time datetime2,
p_timeNull datetime2,
pInt INT,
pIntNull INT
); `
sqltextdroptable := `DROP TYPE tvptable;`
sqltextcreatesp := `
CREATE PROCEDURE spwithtvp
@param1 tvptable READONLY,
@param2 tvptable READONLY,
@param3 NVARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM @param1;
SELECT * FROM @param2;
SELECT @param3;
END;`
sqltextdropsp := `DROP PROCEDURE spwithtvp;`
db.ExecContext(ctx, sqltextdropsp)
db.ExecContext(ctx, sqltextdroptable)
_, err = db.ExecContext(ctx, sqltextcreatetable)
if err != nil {
t.Fatal(err)
}
defer db.ExecContext(ctx, sqltextdroptable)
_, err = db.ExecContext(ctx, sqltextcreatesp)
if err != nil {
t.Fatal(err)
}
defer db.ExecContext(ctx, sqltextdropsp)
varcharNull := "aaa"
nvarchar := "bbb"
bytesMock := []byte("ddd")
i8 := int8(1)
i16 := int16(2)
i32 := int32(3)
i64 := int64(4)
i := int(355)
bFalse := false
floatValue64 := 0.123
floatValue32 := float32(-10.123)
timeNow := time.Now().UTC()
param1 := []TvptableRowWithSkipTag{
{
PBinary: []byte("ccc"),
PVarchar: varcharNull,
PNvarchar: nvarchar,
PID: UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
PVarbinary: bytesMock,
PTinyint: i8,
PSmallint: i16,
PInt: i32,
PBigint: i64,
PBit: bFalse,
PFloat32: floatValue32,
PFloat64: floatValue64,
DTime: timeNow,
Pint: i,
PintNull: &i,
},
{
PBinary: []byte("www"),
PVarchar: "eee",
PNvarchar: "lll",
PID: UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
PVarbinary: []byte("zzz"),
PTinyint: 5,
PSmallint: 16000,
PInt: 20000000,
PBigint: 2000000020000000,
PBit: true,
PFloat32: -123.45,
PFloat64: -123.45,
DTime: time.Date(2001, 11, 16, 23, 59, 39, 0, time.UTC),
Pint: 3669,
PintNull: &i,
},
{
PBinary: nil,
PVarcharNull: &varcharNull,
PNvarcharNull: &nvarchar,
PIDNull: &UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
PTinyintNull: &i8,
PSmallintNull: &i16,
PIntNull: &i32,
PBigintNull: &i64,
PBitNull: &bFalse,
PFloatNull32: &floatValue32,
PFloatNull64: &floatValue64,
DTime: timeNow,
DTimeNull: &timeNow,
Pint: 969,
},
{
PBinary: []byte("www"),
PVarchar: "eee",
PNvarchar: "lll",
PIDNull: &UniqueIdentifier{},
PVarbinary: []byte("zzz"),
PTinyint: 5,
PSmallint: 16000,
PInt: 20000000,
PBigint: 2000000020000000,
PBit: true,
PFloat64: 123.45,
DTime: time.Date(2001, 11, 16, 23, 59, 39, 0, time.UTC),
PVarcharNull: &varcharNull,
PNvarcharNull: &nvarchar,
PTinyintNull: &i8,
PSmallintNull: &i16,
PIntNull: &i32,
PBigintNull: &i64,
PBitNull: &bFalse,
PFloatNull32: &floatValue32,
PFloatNull64: &floatValue64,
DTimeNull: &timeNow,
PintNull: &i,
},
}
tvpType := TVP{
TypeName: "tvptable",
Value: param1,
}
tvpTypeEmpty := TVP{
TypeName: "tvptable",
Value: []TvptableRowWithSkipTag{},
}
rows, err := db.QueryContext(ctx,
"exec spwithtvp @param1, @param2, @param3",
sql.Named("param1", tvpType),
sql.Named("param2", tvpTypeEmpty),
sql.Named("param3", "test"),
)
if err != nil {
t.Fatal(err)
}
var result1 []TvptableRowWithSkipTag
for rows.Next() {
var val TvptableRowWithSkipTag
err := rows.Scan(
&val.PBinary,
&val.PVarchar,
&val.PVarcharNull,
&val.PNvarchar,
&val.PNvarcharNull,
&val.PID,
&val.PIDNull,
&val.PVarbinary,
&val.PTinyint,
&val.PTinyintNull,
&val.PSmallint,
&val.PSmallintNull,
&val.PInt,
&val.PIntNull,
&val.PBigint,
&val.PBigintNull,
&val.PBit,
&val.PBitNull,
&val.PFloat32,
&val.PFloatNull32,
&val.PFloat64,
&val.PFloatNull64,
&val.DTime,
&val.DTimeNull,
&val.Pint,
&val.PintNull,
)
if err != nil {
t.Fatalf("scan failed with error: %s", err)
}
result1 = append(result1, val)
}
if !reflect.DeepEqual(param1, result1) {
t.Logf("expected: %+v", param1)
t.Logf("actual: %+v", result1)
t.Errorf("first resultset did not match param1")
}
if !rows.NextResultSet() {
t.Errorf("second resultset did not exist")
}
if rows.Next() {
t.Errorf("second resultset was not empty")
}
if !rows.NextResultSet() {
t.Errorf("third resultset did not exist")
}
if !rows.Next() {
t.Errorf("third resultset was empty")
}
var result3 string
if err := rows.Scan(&result3); err != nil {
t.Errorf("error scanning third result set: %s", err)
}
if result3 != "test" {
t.Errorf("third result set had wrong value expected: %s actual: %s", "test", result3)
}
}
type TvpExample struct {
Message string
}
func TestTVPSchema(t *testing.T) {
checkConnStr(t)
SetLogger(testLogger{t})
conn, err := sql.Open("sqlserver", makeConnStr(t).String())
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
defer conn.Close()
_, err = conn.Exec(crateSchema)
if err != nil {
log.Println(err)
return
}
defer conn.Exec(dropSchema)
_, err = conn.Exec(createTVP)
if err != nil {
log.Println(err)
return
}
defer conn.Exec(dropTVP)
_, err = conn.Exec(procedureWithTVP)
if err != nil {
log.Println(err)
return
}
defer conn.Exec(dropProcedure)
exempleData := []TvpExample{
{
Message: "Hello",
},
{
Message: "World",
},
{
Message: "TVP",
},
}
tvpType := TVP{
TypeName: "TestTVPSchema.exempleTVP",
Value: exempleData,
}
rows, err := conn.Query(execTvp,
sql.Named("param1", tvpType),
)
if err != nil {
log.Println(err)
return
}
tvpResult := make([]TvpExample, 0)
for rows.Next() {
tvpExemple := TvpExample{}
err = rows.Scan(&tvpExemple.Message)
if err != nil {
log.Println(err)
return
}
tvpResult = append(tvpResult, tvpExemple)
}
log.Println(tvpResult)
}
func TestTVPObject(t *testing.T) {
checkConnStr(t)
SetLogger(testLogger{t})
conn, err := sql.Open("sqlserver", makeConnStr(t).String())
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
defer conn.Close()
tests := []struct {
name string
tvp TVP
wantErr bool
}{
{
name: "empty name",
wantErr: true,
tvp: TVP{TypeName: ""},
},
{
name: "value is wrong type",
wantErr: true,
tvp: TVP{TypeName: "type", Value: "wrong type"},
},
{
name: "tvp type is wrong",
wantErr: true,
tvp: TVP{TypeName: "[type", Value: []TvpExample{{}}},
},
{
name: "tvp type is wrong",
wantErr: true,
tvp: TVP{TypeName: "[type", Value: []TestFieldsUnsupportedTypes{{}}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := conn.Exec("somequery", tt.tvp)
if (err != nil) != tt.wantErr {
t.Errorf("TVP.encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}