chad
chad

Reputation: 564

db4o delete isn't working after unit test execution sequence

I have a small set of unit tests that are working fine until I add a test that is verifying whether my unique constraint is working properly. These aren't really unit tests, they're integration tests.

At any rate, if I omit the test validating the unique constraint, all is well with the delete. However, if I add the SerialNumberUniqueConstraint_Test method, it fails and my item class is not null as the delete never occurs. If I move the SerialNumberUniqueConstraint_Test ahead of other tests, the subsequent tests fail with the same UniqueFieldValueConstraintValidationException as well. What am I doing incorrectly?

    [TestMethod]
    [ExpectedException( typeof( UniqueFieldValueConstraintViolationException ) )]
    public void SerialNumberUniqueConstraint_Test()
    {
        using( var logic = new ItemLogic() )
        {
            logic.Save( CreateItem() );
        }
    }

    [TestMethod]
    public void DeleteItem_Test()
    {
        Item item = null;

        using( var logic = new ItemLogic() )
        {
            logic.Delete( SerialNumber );
        }

        using( var logic = new ItemLogic() )
        {
            item = logic.Retrieve( SerialNumber );
        }

        Assert.IsNull( item );
    }

    private Item CreateItem()
    {
        return new Item { Name = "My item", Make = "make", Model = "model", SerialNumber = "1234" };
    }

    public Item Save( Item item )
    {
        Db4oDatabase.Database.Store( item );
        Db4oDatabase.Database.Commit();

        return this.Retrieve( item.SerialNumber );
    }

    public Item Retrieve( string serialNumber )
    {
        Item item = (from   i in Db4oDatabase.Database.AsQueryable<Item>()
                     where  i.SerialNumber == serialNumber
                     select i).FirstOrDefault();            

        return item;
    }

    public void Delete( string serialNumber )
    {
        Db4oDatabase.Database.Delete( this.Retrieve( serialNumber ) );
    }

Upvotes: 1

Views: 162

Answers (1)

chad
chad

Reputation: 564

The data class' Save method now utilizes a try/catch on the Commit() operation and performs a Rollback should the UniqueFieldValueConstraintViolationException occur. Additionally, I have made the DeleteItem_Test independent as recommended by Bob Horn.

    public Item Save( Item item )
    {
        Db4oDatabase.Database.Store( item );

        try
        {
            Db4oDatabase.Database.Commit();
        }
        catch( UniqueFieldValueConstraintViolationException )
        {
            Db4oDatabase.Database.Rollback();
            throw;
        }

        return this.Retrieve( item.SerialNumber );
    }

    [TestMethod]
    public void DeleteItem_Test()
    {
        string serialNumber = "DeleteItem_Test";

        Item item = new Item
        {
            Name          = "Washer",
            Make          = "Samsung",
            Model         = "Model No",
            SerialNumber  = serialNumber,
            PurchasePrice = 2500m
        };

        using( var logic = new ItemLogic() )
        {
            item = logic.Save( item );

            Assert.IsNotNull( item, TestResources.DevMessage_IntermediateOperationFailed, "Save", serialNumber );

            logic.Delete( item );                

            item = logic.Retrieve( serialNumber );
        }

        Assert.IsNull( item );
    }

Upvotes: 1

Related Questions