@@ -480,37 +480,60 @@ func TestEmptyResultWithError(t *testing.T) {
480
480
}{
481
481
{
482
482
query : `
483
- if OBJECT_ID('dbo.MyUsers ', 'U') is not null drop table MyUsers
484
- create table MyUsers (
483
+ if OBJECT_ID('dbo.MyUser ', 'U') is not null drop table MyUser
484
+ create table MyUser (
485
485
ID INT IDENTITY(1,1) PRIMARY KEY,
486
486
Username NVARCHAR(50) not null,
487
487
Userage int
488
488
);
489
- insert into MyUsers (Userage)
489
+ insert into MyUser (Userage)
490
490
output inserted.ID
491
491
values (42);
492
492
` ,
493
- expected : "mssql: Cannot insert the value NULL into column 'Username', table 'master.dbo.MyUsers'; column does not allow nulls. INSERT fails." ,
493
+ expected : "mssql: Cannot insert the value NULL into column 'Username', table 'master.dbo.MyUser'; column does not allow nulls. INSERT fails." ,
494
+ },
495
+ {
496
+ query : `
497
+ if OBJECT_ID('dbo.Employee', 'U') is not null drop table Employee
498
+ if OBJECT_ID('dbo.Department', 'U') is not null drop table Department
499
+ create table Department (
500
+ ID INT IDENTITY(1,1) PRIMARY KEY,
501
+ Name NVARCHAR(50) not null,
502
+ );
503
+ create table Employee (
504
+ ID INT IDENTITY(1,1) PRIMARY KEY,
505
+ Name NVARCHAR(50) not null,
506
+ Department int
507
+ constraint fk_Department foreign key references Department(ID),
508
+ );
509
+
510
+ insert into Employee(Name, Department) values ('Bob', 1);
511
+ select @@rowcount;
512
+ ` ,
513
+ expected : "mssql: The INSERT statement conflicted with the FOREIGN KEY constraint \" fk_Department\" . The conflict occurred in database \" master\" , table \" dbo.Department\" , column 'ID'." ,
514
+ // TODO(dsf)
494
515
},
495
516
}
496
517
497
- for _ , tc := range testcases {
498
- // ExecContext error
499
- _ , errExec := ExecContext (context .Background (), sqldb , tc .query , "world" )
500
- assert .Error (t , errExec )
501
- assert .Equal (t , tc .expected , errExec .Error ())
502
-
503
- // SingleOf error
504
- rs := New (context .Background (), sqldb , tc .query )
505
- _ = rs .Rows
506
- _ , errSingle := NextResult (rs , SingleOf [int ])
507
- assert .Error (t , errSingle )
508
- // The errSingle has the same underlying error as the errExec
509
- assert .True (t , errors .Is (errSingle , errExec ))
510
- // But the errSingle is not the same error as the errExec because,
511
- // in addition to the underlying error, errSingle also contains
512
- // the information that we called Single and didn't get any value back
513
- assert .False (t , errors .Is (errExec , errSingle ))
518
+ for i , tc := range testcases {
519
+ t .Run (fmt .Sprintf ("%d:%s" , i , tc .name ), func (t * testing.T ) {
520
+ // ExecContext error
521
+ _ , errExec := ExecContext (context .Background (), sqldb , tc .query , "world" )
522
+ require .Error (t , errExec )
523
+ require .Equal (t , tc .expected , errExec .Error ())
524
+
525
+ // SingleOf error
526
+ rs := New (context .Background (), sqldb , tc .query )
527
+ _ = rs .Rows
528
+ _ , errSingle := NextResult (rs , SingleOf [int ])
529
+ assert .Error (t , errSingle )
530
+ // The errSingle has the same underlying error as the errExec
531
+ assert .True (t , errors .Is (errSingle , errExec ))
532
+ // But the errSingle is not the same error as the errExec because,
533
+ // in addition to the underlying error, errSingle also contains
534
+ // the information that we called Single and didn't get any value back
535
+ assert .False (t , errors .Is (errExec , errSingle ))
536
+ })
514
537
}
515
538
}
516
539
@@ -653,12 +676,12 @@ func TestStructScanError(t *testing.T) {
653
676
654
677
func TestExecContext (t * testing.T ) {
655
678
qry := `
656
- if OBJECT_ID('dbo.MyUsers ', 'U') is not null drop table MyUsers
657
- create table MyUsers (
679
+ if OBJECT_ID('dbo.MyUser ', 'U') is not null drop table MyUser
680
+ create table MyUser (
658
681
ID INT IDENTITY(1,1) PRIMARY KEY,
659
682
Username NVARCHAR(50)
660
683
);
661
- insert into MyUsers (Username) values ('JohnDoe');
684
+ insert into MyUser (Username) values ('JohnDoe');
662
685
663
686
-- logging
664
687
select _log='info', Y = 'one';
0 commit comments