Test cascade delete using sql query

When first starting NH mapping, we just want to make sure the data is really being deleted, for cascaded child, some tricky code are needed to work around the strong relationship we created in domain entities. SqlQuery is one of those.


[Test]
 public void Should_delete_email()
 {
 // Retrieve
 Organization org = _repository.FetchById(8172);

 // Ensure org was retrieved.
 Assert.That(org, Is.Not.Null);

 var emailToAdd = new Email("Home Alternate", "x@y");
 org.AddEmail(emailToAdd);

 _repository.Update(org, org.LastModifiedDate);
Assert.That(emailToAdd.Id == 0);
_session.Flush();
Assert.That(emailToAdd.Id > 0);

 // Delete
 org.DeleteEmail(emailToAdd);
 _session.Flush();

 // Ensure email data is physically deleted
 ISQLQuery query = _session.CreateSQLQuery("select * from Stakeholder_Email_CV where stakeholder_email_id = " +
 emailToAdd.Id);
 Assert.That(query.List().Count == 0);

 }

Quite interesting testing code for NHibernate mapping.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s