@@ -480,37 +480,59 @@ func TestEmptyResultWithError(t *testing.T) {
480480 }{
481481 {
482482 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 (
485485 ID INT IDENTITY(1,1) PRIMARY KEY,
486486 Username NVARCHAR(50) not null,
487487 Userage int
488488);
489- insert into MyUsers (Userage)
489+ insert into MyUser (Userage)
490490output inserted.ID
491491values (42);
492492` ,
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.Department', 'U') is not null drop table Department
498+ create table Department (
499+ ID INT IDENTITY(1,1) PRIMARY KEY,
500+ Name NVARCHAR(50) not null,
501+ );
502+ if OBJECT_ID('dbo.Employee', 'U') is not null drop table Employee
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 Employee(ID),
508+ );
509+
510+ insert into Employee(Name, Department) values ('Bob', 1);
511+ select @@rowcount;
512+ ` ,
513+ expected : "" , // TODO(dsf)
494514 },
495515 }
496516
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 ))
517+ for i , tc := range testcases {
518+ t .Run (fmt .Sprintf ("%d:%s" , i , tc .name ), func (t * testing.T ) {
519+ // ExecContext error
520+ _ , errExec := ExecContext (context .Background (), sqldb , tc .query , "world" )
521+ require .Error (t , errExec )
522+ require .Equal (t , tc .expected , errExec .Error ())
523+
524+ // SingleOf error
525+ rs := New (context .Background (), sqldb , tc .query )
526+ _ = rs .Rows
527+ _ , errSingle := NextResult (rs , SingleOf [int ])
528+ assert .Error (t , errSingle )
529+ // The errSingle has the same underlying error as the errExec
530+ assert .True (t , errors .Is (errSingle , errExec ))
531+ // But the errSingle is not the same error as the errExec because,
532+ // in addition to the underlying error, errSingle also contains
533+ // the information that we called Single and didn't get any value back
534+ assert .False (t , errors .Is (errExec , errSingle ))
535+ })
514536 }
515537}
516538
@@ -653,12 +675,12 @@ func TestStructScanError(t *testing.T) {
653675
654676func TestExecContext (t * testing.T ) {
655677 qry := `
656- if OBJECT_ID('dbo.MyUsers ', 'U') is not null drop table MyUsers
657- create table MyUsers (
678+ if OBJECT_ID('dbo.MyUser ', 'U') is not null drop table MyUser
679+ create table MyUser (
658680 ID INT IDENTITY(1,1) PRIMARY KEY,
659681 Username NVARCHAR(50)
660682);
661- insert into MyUsers (Username) values ('JohnDoe');
683+ insert into MyUser (Username) values ('JohnDoe');
662684
663685-- logging
664686select _log='info', Y = 'one';
0 commit comments