tSQLt-org / tSQLt

The official tSQLt repository. (Download at: http://tSQLt.org/downloads )
http://tSQLt.org
413 stars 102 forks source link

tSQLt.ApplyConstraint fails when constraint's name must be quoted #163

Open CodingForFunToo opened 2 years ago

CodingForFunToo commented 2 years ago

When a constraint needs quotes because it contains punctuations or spaces, tSQLt.ApplyConstraint fails because it passes parameters @SchemaName and @OriginalName without quotes to procedure tSQLt.Private_MarkObjectBeforeRename. Within this proc OBJECT_ID(@SchemaName + '.' + @OriginalName) evaluates to NULL.

T-SQL Code to reproduce:

create table dbo.MasterTbl(id int not null identity constraint [PK_Master] primary key clustered);

create table dbo.ChildTbl
(
  id int not null identity constraint [PK_Child] primary key clustered
, masterId int not null constraint [FK.with.dots and spaces] foreign key references dbo.MasterTbl(id)
);

EXEC tSQLt.NewTestClass @ClassName = N'ConstraintTests';

CREATE PROCEDURE ConstraintTests.[test that fails to execute]
as
begin
exec tSQLt.FakeTable @TableName       = N'dbo.MasterTbl';
exec tSQLt.FakeTable @TableName       = N'dbo.ChildTbl';
exec tSQLt.ApplyConstraint @TableName = N'dbo.ChildTbl', @ConstraintName = N'FK.with.dots and spaces';

exec tSQLt.Fail @Message0 = N'Will never be executed, because tSQLt.ApplyConstraint fails';
end
GO

exec tSQLt.Run @TestName = N'ConstraintTests';

exec tSQLt.DropClass @ClassName = N'ConstraintTests';
drop table dbo.ChildTbl;
drop table dbo.MasterTbl;