Unit Test for Concurrency Check...?

Unit Test for Concurrency Check...?

Old forum URL: forums.lhotka.net/forums/t/4519.aspx


reagan123 posted on Wednesday, March 19, 2008

Hello everyone.

I'm using a datetime field for my concurrency checks.  In my save stored procedure I use RAISERROR to throw an error if the record has been changed by someone else.

This works great, but i'm trying to figure out what the best way to write a test to check this behavior.  Currently I do the following which works, but I think the way I'm handling the catch is very poor.  I basically want to catch the error I throw and handle it so the test passes, but I want any other failures to cause the test to fail.

Any suggestions?
Thanks!

<Test()> _
Public Sub ConcurrencyCheck_Person()

Try

' create and save person
Dim myGuid as guid
Dim myPerson As Person
myPerson = Person.NewPerson
myGuid = Person.Guid
myPerson.FName = "John"
myPerson.LName = "Doe"
myPerson.Save()

' get the person... this will have the dateTimeStamp that was set in the database on the save
myPerson= Person.GetPerson(myGuid )

' print the original DateTimeStamp from the database
Debug.WriteLine("Old DTS = " & myPerson.DateTimeStamp)

' make change and save.. This save should work fine
myPerson.FName = "Jane"
myPerson.Save()

' print the updated DateTimeStamp after the object has been saved
Debug.WriteLine("New DTS = " & myPerson.DateTimeStamp)

' change the datetimestamp so it doesn't match the Database
myPerson.DateTimeStamp = DateTime.Now.ToString

' this save should throw an exception because the DateTimeStamps will not match
wuc.Save()

Catch ex As Foundation.Data.DataPortalException
   
If ex.InnerException.InnerException.Message.ToString = "Concurrency check failed" Then
      
Err.Clear()
   
End If

End Try

End Sub

ajj3085 replied on Wednesday, March 19, 2008

Are you using Nunit or the MS testing?

nermin replied on Wednesday, March 19, 2008

Based on attribute above test method being <Test()> and not <TestMethod()> I presume that it is NUnit.

 

Reagan123 – couldn’t you just use ExpectedException attribute and then set it to the exception that you want to ignore?

 

Nermin

 

From: ajj3085 [mailto:cslanet@lhotka.net]
Sent: Wednesday, March 19, 2008 2:26 PM
To: Nermin Dibek
Subject: Re: [CSLA .NET] Unit Test for Concurrency Check...?

 

Are you using Nunit or the MS testing?


reagan123 replied on Wednesday, March 19, 2008

Thanks for the replies... Yes, I am using Nunit.

"couldn’t you just use ExpectedException"
Never heard of this before so I'll go do some researching... Thanks so far!

reagan123 replied on Wednesday, March 19, 2008

Hmm... ExpectedException looks like exactly what I need, but now I have another issue.  I believe the exception being raised now is a "DataPortalException"... The problem with this is that it may not be my concurrency issue that caused it... maybe something else happened.

Any ideas on an approach to get around this?

Thanks for the suggestion... I never knew about that attribute.

nermin replied on Wednesday, March 19, 2008

Let me re-phrase the question to make sure I understand exactly what you are asking:

 

I believe that your problem is that there are 2 different types of exceptions being thrown: One for the concurrency issue, and another unexpected one. 

 

However booth of these are wrapped into DataPortalException (as InnerException), and therefore it is hard for you to determine which one is it? And if you cannot determine that you cannot determine whether test passes or fails, right?

 

This seems like a question for Rocky, perhaps.  Maybe what we need is a “configurable policy” in Csla itself that determines how exceptions are handled and passed to the client – in other words whether Original exception wrapped in DataPortalException is passed or the original exception is re-thrown on the client.

 

Another solution is to use this type of catch:

 

Catch(DataPortalException ex)

{

    Assert.AreEqual (typeof (ExceptionTypeILookFor), ex.GetType());

}

 

Hope that helps,

 

Nermin

From: reagan123 [mailto:cslanet@lhotka.net]
Sent: Wednesday, March 19, 2008 3:46 PM
To: Nermin Dibek
Subject: Re: [CSLA .NET] RE: Unit Test for Concurrency Check...?

 

Hmm... ExpectedException looks like exactly what I need, but now I have another issue.  I believe the exception being raised now is a "DataPortalException"... The problem with this is that it may not be my concurrency issue that caused it... maybe something else happened.

Any ideas on an approach to get around this?

Thanks for the suggestion... I never knew about that attribute.



stanc replied on Wednesday, March 19, 2008

Your actual exception will be wrapped up in the DataPortalException. You should be able to drill down to get the actual exception that occured within the dataportal.

Copyright (c) Marimer LLC