Audit for deletion of new entities?

Posts   
 
    
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 22-Jan-2008 20:24:55   

I know in the real world it wouldn't make sense to audit for the deletion of an entity that hasn't been saved but...

I'm mocking out the Auditor and wanted to make sure the method "AuditDeleteOfEntity" got called when an entity was deleted.

This works if the entity already exists in the database. So no problem there. But what I really wanted to test is if the method got called without having to really delete something from the database.

Thanks, -Luke

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 23-Jan-2008 11:22:55   

This works if the entity already exists in the database. So no problem there. But what I really wanted to test is if the method got called without having to really delete something from the database.

So when exactly do you want it to be audited? At which event?

LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 23-Jan-2008 19:35:53   

Walaa wrote:

This works if the entity already exists in the database. So no problem there. But what I really wanted to test is if the method got called without having to really delete something from the database.

So when exactly do you want it to be audited? At which event?

Ok... this works for me when testing to make sure a field change gets audited.

    [Test]
    public void TestFieldSetAuditOfEntity()
    {
        // Intercept calls to the RAMAuditor object
        Mock entityListMock = MockManager.Mock(typeof(RAMAuditor), Constructor.NotMocked);

        // This auditor will now be mocked
        RAMAuditor ramAuditor = new RAMAuditor();

        RIAEntity entityToTest = new RIAEntity();
        entityToTest.AuditorToUse = ramAuditor;

        // When we rename the entity the method AddAuditEntryToList should be called
        entityListMock.ExpectCall("AddAuditEntryToList").Args(entityToTest, RAMAuditor.AuditType.EntityFieldSet, Check.IsAny());

        entityToTest.Name = "*" + entityToTest.Name;

        MockManager.Verify();
    }

Now when I try to do the same for deleting an entity with this

    [Test]
    public void TestDeletionAuditOfEntity()
    {
        // Intercept calls to the RAMAuditor object
        Mock entityListMock = MockManager.Mock(typeof(RAMAuditor),Constructor.NotMocked);

        // This auditor will now be mocked
        RAMAuditor ramAuditor = new RAMAuditor();

        RIAEntity entityToTest = new RIAEntity();
        entityToTest.AuditorToUse = ramAuditor;

        // When we delete an entity the method AddAuditEntryToList with "DeleteOfEntity" should be called.
        entityListMock.ExpectCall("AddAuditEntryToList").Args(entityToTest, RAMAuditor.AuditType.DeleteOfEntity, Check.IsAny());

        _adapter.DeleteEntity(entityToTest);
        MockManager.Verify();
    }

It won't trigger the DeleteOfEntity method UNLESS I replace

RIAEntity entityToTest = new RIAEntity();

with this

RIAEntity entityToTest = new RIAEntity(xx);

Where xx represents a real record inside the database.

Basically I want to know is if the AuditDeleteOfEntity gets called IF the entity is NEW and un-saved.

Thanks, -Luke

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 24-Jan-2008 16:15:51   

Basically I want to know is if the AuditDeleteOfEntity gets called IF the entity is NEW and un-saved.

No, it's called when the entity is removed from the db. A new entity which is deleted is ignored by the framework anyway as the entity doesn't exist so no call to the DB is made. If an entity which is new and which is marked for deletion is still saved, it won't be saved.

Frans Bouma | Lead developer LLBLGen Pro
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 24-Jan-2008 17:55:46   

Thanks. What I'll do is create a new test on a dummy table (entity) which basically creates a new record and then deletes it. The test will make sure both calls are made with the appropriate parameters.

Thanks.

Otis wrote:

Basically I want to know is if the AuditDeleteOfEntity gets called IF the entity is NEW and un-saved.

No, it's called when the entity is removed from the db. A new entity which is deleted is ignored by the framework anyway as the entity doesn't exist so no call to the DB is made. If an entity which is new and which is marked for deletion is still saved, it won't be saved.