@@ -480,37 +480,62 @@ 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.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)
494515 },
495516 }
496517
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+ })
514539 }
515540}
516541
@@ -653,12 +678,12 @@ func TestStructScanError(t *testing.T) {
653678
654679func TestExecContext (t * testing.T ) {
655680 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 (
658683 ID INT IDENTITY(1,1) PRIMARY KEY,
659684 Username NVARCHAR(50)
660685);
661- insert into MyUsers (Username) values ('JohnDoe');
686+ insert into MyUser (Username) values ('JohnDoe');
662687
663688-- logging
664689select _log='info', Y = 'one';
0 commit comments