@@ -480,37 +480,62 @@ 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 .EnsureDoneAfterNext (), SingleOf [int ])
529
+ require .Error (t , errSingle )
530
+
531
+ fmt .Printf ("single(%s)\n exec(%s)" , errSingle .Error (), errExec .Error ())
532
+ // The errSingle has the same underlying error as the errExec
533
+ require .True (t , errors .Is (errSingle , errExec ), fmt .Sprintf ("single(%s) exec(%s)" , errSingle .Error (), errExec .Error ()))
534
+ // But the errSingle is not the same error as the errExec because,
535
+ // in addition to the underlying error, errSingle also contains
536
+ // the information that we called Single and didn't get any value back
537
+ require .False (t , errors .Is (errExec , errSingle ))
538
+ })
514
539
}
515
540
}
516
541
@@ -653,12 +678,12 @@ func TestStructScanError(t *testing.T) {
653
678
654
679
func TestExecContext (t * testing.T ) {
655
680
qry := `
656
- if OBJECT_ID('dbo.MyUsers ', 'U') is not null drop table MyUsers
657
- create table MyUsers (
681
+ if OBJECT_ID('dbo.MyUser ', 'U') is not null drop table MyUser
682
+ create table MyUser (
658
683
ID INT IDENTITY(1,1) PRIMARY KEY,
659
684
Username NVARCHAR(50)
660
685
);
661
- insert into MyUsers (Username) values ('JohnDoe');
686
+ insert into MyUser (Username) values ('JohnDoe');
662
687
663
688
-- logging
664
689
select _log='info', Y = 'one';
0 commit comments