Compare predicates for unit tests

Posts   
 
    
yowl
User
Posts: 271
Joined: 11-Feb-2008
# Posted on: 05-Jan-2015 18:57:29   

Hi,

Using 4.2 Final, I would like to be able to compare predicates against those in a unit test. This is JustMock, but probably any mocking framework would do:

 dataService.Arrange(
                ds => ds.DeleteEntitiesDirectlty<HubexRollbackStateEntity>(Arg.Matches((Predicate p) => p.Equals(HubexRollbackStateFields.Key == 1)))).OccursOnce();

You can see that I expect a call to a function I have, DeleteEntitiesDirectly, which takes a SD.LLBLGen.Pro.ORMSupportClasses.Predicate and that that predicate is an Equals value comparison on the field Key to the int with value 1. This expectation does not work as there is no override of Equals in the Predicate or FieldCompareValuePredicate class so I guess it is just a reference comparison. Is there any short way of doing this, or do I just check for a FieldCompareValuePredicate and then check the Field and Value properties on that?

Thanks,

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 06-Jan-2015 07:09:18   

do I just check for a FieldCompareValuePredicate and then check the Field and Value properties on that?

Either that or compare the string output of the ToQueryText() method of each.

yowl
User
Posts: 271
Joined: 11-Feb-2008
# Posted on: 06-Jan-2015 14:23:32   

Hi,

Using ToQueryText thus:

dataService.Arrange(
                ds => ds.DeleteEntitiesDirectlty<HubexRollbackStateEntity>(Arg.Matches((Predicate p) => p.ToQueryText().Equals((HubexRollbackStateFields.Key == 1).ToQueryText())), Arg.AnyString)).OccursOnce();

Gives

System.ApplicationException : DatabaseSpecificCreator object not set. Cannot create query part. at SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.ToQueryText(Boolean inHavingClause) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.2\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\QueryApiElements\FieldCompareValuePredicate.cs: line 262 at lambda_method(Closure, Predicate)

which no doubt could be worked around with more code in the unit test.

Testing the values explicitly like this:

dataService.Arrange(
                ds => ds.DeleteEntitiesDirectlty<HubexRollbackStateEntity>(Arg.Matches((FieldCompareValuePredicate p) => p.Field == HubexRollbackStateFields.Key  && p.Value == (object)1), Arg.AnyString)).OccursOnce();

Gives

System.InvalidOperationException : This object was constructed using a non-selfservicing constructor. Can't retrieve an IEntityField after that. at SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.get_Field() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.2\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\QueryApiElements\FieldCompareValuePredicate.cs: line 511 at lambda_method(Closure, FieldCompareValuePredicate)

I dont use self-servicing, so the first part of the message is correct, but I don't know why it complains about retrieving IEntityField.

Looks like to get this expectation to work I'll need more code which is a shame as I don't like to clutter up the unit test.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Jan-2015 06:38:07   

yowl wrote:

Hi,

Using ToQueryText thus:

dataService.Arrange(
                ds => ds.DeleteEntitiesDirectlty<HubexRollbackStateEntity>(Arg.Matches((Predicate p) => p.ToQueryText().Equals((HubexRollbackStateFields.Key == 1).ToQueryText())), Arg.AnyString)).OccursOnce();

Gives

System.ApplicationException : DatabaseSpecificCreator object not set. Cannot create query part. at SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.ToQueryText(Boolean inHavingClause) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.2\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\QueryApiElements\FieldCompareValuePredicate.cs: line 262 at lambda_method(Closure, Predicate)

which no doubt could be worked around with more code in the unit test.

The exception is because LLBLGen doesn't have persistence information to produce the SQL Query (i.e. the real field names, the schema, etc). You can workaround it doing this:

In a DataAccessAdapter partial class write this:

public partial class DataAccessAdapter
{
    public string GetQuery(PredicateExpression filter)
    {
        InsertPersistenceInfoObjects(filter);
        filter.DatabaseSpecificCreator = this.GetDbSpecificCreatorInstance();
        return filter.ToQueryText();
    }
}

Then, at your code:

using (var adapter = new DataAccessAdapter())
{
    var queryText = adapter.GetQuery(filter);
}

You can do the same with other LLBLGen objects like SortExpression, for instance.

David Elizondo | LLBLGen Support Team