Skip to content

Add ApplyIndex support #77

Open
Open
@NightWulfe

Description

@NightWulfe

Hello,

I have a unique filtered index for which I would like to write a tSQLt test to verify the logic is correct. However, since there is no tSQLt.ApplyIndex method, I have to manually re-create the unique filtered index on the table after calling tSQLt.FakeTable.

I'm requesting the addition of an ApplyIndex procedure for this use.

Example use case:

-- The values of `name` and `value` may be duplicated, but only one of those records
-- may have a `parentIsDefault` value of 'Y' (all other records must be 'N')
create table ParentIdentifiers (
    id int identity(1, 1) not null,
    parentId int not null,             -- References some other table
    name varchar(128) not null,
    value varchar(128) not null,
    parentIsDefault char(1) not null
)

-- Filtered unique index
create unique index UX_ParentIdentifiers_parentIsDefault
on ParentIdentifiers (
    name,
    value
)
where parentIsDefault = 'Y'
GO

exec tSQLt.FakeTable 'dbo.ParentIdentifiers'
-- This doesn't exist, so I have to duplicate the create unique index code
exec tSQLt.ApplyIndex 'dbo.ParentIdentifiers', 'UX_ParentIdentifiers_parentIsDefault'

exec tSQLt.ExpectNoException
insert into ParentIdentifiers (name, value, parentIsDefault)
values('One', '1', 'Y'),
      ('One', '1', 'N'),
      ('Two', '2', 'Y')

exec tSQLt.ExpectException @ExpectedMessagePattern = 'Cannot insert duplicate key row in object%',
                           @ExpectedSeverity = 14,
                           @ExpectedState =1

insert into ParentIdentifiers (name, value, parentIsDefault)
values('Two', '2', 'Y'),

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions