Search code examples
c#sql-servervisual-studiounit-testingsql-server-ce

C# nUnit test failing saying SQL Server CE constraint doesn't exist when it actually does


I've been trying to figure this out for days now, nobody in my company seems to be able to help me out so, here's the interesting problem I am facing.

We have a test suite of about 6500 unit tests, 650 of which are relating to how we interact with both SQL Server and SQL Server CE 3.5 databases. The SQL Server CE database is just a local mode / flat file .sdf. Tests are being run with Resharper / CodeRush plugins.

Now, all the unit tests are passing except for one with SQL Server CE- I am trying to alter a table to drop 2 columns from the following table:

CREATE TABLE Entity
(
    Id UNIQUEIDENTIFIER PRIMARY KEY,
    Name NVARCHAR(256) NOT NULL,
    SerialNumber NVARCHAR(64) NOT NULL,
    EquipmentType NVARCHAR(24) NULL,
    EntityInformationDate DATETIME NULL,
    EntityFamily INT NOT NULL 
        CONSTRAINT DF_Entity_EntityFamily DEFAULT(0)
)

Now, the latest script I am running which crashes in testing but works in production:

ALTER TABLE Entity DROP CONSTRAINT DF_Entity_EntityFamily
ALTER TABLE Entity DROP COLUMN EntityFamily
ALTER TABLE Entity DROP COLUMN EquipmentType

The error message thrown by the test is

Common.Utils.DatabaseUpdateException : Failing script is: ALTER TABLE Entity > DROP CONSTRAINT DF_Entity_EntityFamily

System.Data.SqlServerCe.SqlCeException
The foreign key constraint does not exist. [ DF_Entity_EntityFamily ]

The reason for this change is I have created a new table of possible EntityFamily using Guids so am in the process of swapping it over.

What have I tried so far:

  • I have debugged and stepped through what scripts are running during this particular unit test, and at no time is anything else called on this table, so the constraint must be there.

  • I have tried to console print debug messages or printf's but nUnit seems to eat them

  • I have tried to intercept and inspect the .sdf file during the execution but it always is empty if I open it in CompactView or Linqpad - which leads me to think maybe there is a mock running this instead of the actual file. Even though the file access date is changing in windows explorer.

Is there some sort of test sync or order issue, threading, or otherwise that I should look for? This really has me beaten... :(


Solution

  • It turned out to be a problem with the inherited factory that was creating the database connection object.

    Depending on test order, it was possible to enter the start of the test suites with an already created Session Singleton, which would point to SQL Server. Then, the tests being run were only to SQL Server CE which would not have the constraints that SQL Server had. When running the test fixture on its own, the singleton was null, so it would create the right connection.

    Now I just make it create the connection I specifically ask for every time.

    Edit: Just for info, this problem turned out to be caused when we migrated unit tests that were spread across 5 projects, into one single test project.