我正在尝试回顾性地为我继承的代码编写一些单元测试。
一种特殊的方法是在LinkedEntity
FilterCondition
QueryExpression
的Dynamics CRM 2011插件方法中导致我出现问题
实体结构
联络 - (N:1 [查询]) - > CustomEntity1 - (N:1 [Lookup]) - > CustomEntity2
方法的目标
我尝试测试的方法会创建一个QueryExpression
,根据Contacts
的属性过滤CustomEntity1
,包括LookupField
到CustomEntity2
的值
工作查询代码
为了测试我对QueryExpression的理解,我已经重写了下面的代码
QueryExpression query = new QueryExpression();
query.EntityName = "contact";
query.ColumnSet = new ColumnSet(true);
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
FilterExpression filter = new FilterExpression(LogicalOperator.And);
FilterExpression filter1 = new FilterExpression(LogicalOperator.Or);
filter1.Conditions.Add(new ConditionExpression("new_datefield1", ConditionOperator.Null));
filter1.Conditions.Add(new ConditionExpression("new_datefield1", ConditionOperator.LessEqual, offset));
FilterExpression filter2 = new FilterExpression(LogicalOperator.Or);
filter2.Conditions.Add(new ConditionExpression("new_datefield2", ConditionOperator.Null));
filter2.Conditions.Add(new ConditionExpression("new_datefield2", ConditionOperator.LessEqual, offset));
filter.AddFilter(filter1);
filter.AddFilter(filter2);
query.Criteria.Filters.Add(filter);
// Create the link from the contact to the CustomEntity1 entity
LinkEntity linkHistory = new LinkEntity(Contact.EntityLogicalName, new_CustomEntity1.EntityLogicalName, Contact.AttributeNames.new_CustomEntity1Lookup, new_CustomEntity1.AttributeNames.Id, JoinOperator.Inner);
linkHistory.Columns = new ColumnSet(true);
linkHistory.EntityAlias = "custEnt1";
linkHistory.LinkCriteria = new FilterExpression();
linkHistory.LinkCriteria.FilterOperator = LogicalOperator.And;
#region code added to make the test work
// Create the CustomEntity2 condition
LinkEntity linkStatus = new LinkEntity(new_CustomEntity1.EntityLogicalName, new_CustomEntity2.EntityLogicalName, new_CustomEntity1.AttributeNames.new_CustomEntity2Lookup, new_CustomEntity2.AttributeNames.Id, JoinOperator.Inner);
linkStatus.Columns = new ColumnSet(true);
linkStatus.EntityAlias = "custEnt2";
linkStatus.LinkCriteria = new FilterExpression();
linkStatus.LinkCriteria.FilterOperator = LogicalOperator.And;
linkStatus.LinkCriteria.Conditions.Add(new ConditionExpression(new_CustomEntity2.AttributeNames.Id, ConditionOperator.Equal, indStatus.Id));
linkHistory.LinkEntities.Add(linkStatus);
#endregion
//some code removed for brevity
query.LinkEntities.Add(linkHistory);
此代码返回一个QueryExpression,然后用于运行测试,测试在运行时传递,并在FakeXrmEasy中设置一些测试数据。但是,我需要确保我的更改不会对当前代码和逻辑产生负面影响,因此在进行任何更改之前,需要根据当前方法检查我的结果。
非工作代码
QueryExpression query = new QueryExpression();
query.PageInfo = new PagingInfo();
query.PageInfo.Count = fetchCount;
query.PageInfo.PageNumber = pageNumber;
query.PageInfo.PagingCookie = null;
// Setup the query for the contact entity
query.EntityName = Contact.EntityLogicalName;
// Specify the columns to retrieve
query.ColumnSet = new ColumnSet(true);
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
FilterExpression filter1 = new FilterExpression();
filter1.FilterOperator = LogicalOperator.Or;
// Create the e2sds_lastcontactdate condition
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = Contact.AttributeNames.new_DateField1;
condition1.Operator = ConditionOperator.Null;
// Create the e2sds_lastcontactdate condition
ConditionExpression condition2 = new ConditionExpression();
condition2.AttributeName = Contact.AttributeNames.new_DateField1;
condition2.Operator = ConditionOperator.LessEqual;
condition2.Values.Add(offset);
FilterExpression filter2 = new FilterExpression();
filter2.FilterOperator = LogicalOperator.Or;
// Create the Last Third Party Contact condition
ConditionExpression ltpcCond1 = new ConditionExpression();
condition1.AttributeName = Contact.AttributeNames.new_DateField2;
condition1.Operator = ConditionOperator.Null;
// Create the Last Third Party Contactcondition
ConditionExpression ltpcCond2 = new ConditionExpression();
condition2.AttributeName = Contact.AttributeNames.new_DateField2;
condition2.Operator = ConditionOperator.LessEqual;
condition2.Values.Add(offset);
filter2.Conditions.AddRange(ltpcCond1, ltpcCond2);
query.Criteria.Filters.Add(filter1);
query.Criteria.Filters.Add(filter2);
// Create the link from the contact to the CustomEntity1 entity
LinkEntity linkHistory = new LinkEntity();
linkHistory.JoinOperator = JoinOperator.Natural;
linkHistory.LinkFromEntityName = Contact.EntityLogicalName;
linkHistory.LinkFromAttributeName = Contact.AttributeNames.new_CustomEntity1Lookup;
linkHistory.LinkToEntityName = new_CustomEntity1.EntityLogicalName;
linkHistory.LinkToAttributeName = new_CustomEntity1.AttributeNames.Id;
linkHistory.LinkCriteria = new FilterExpression();
linkHistory.LinkCriteria.FilterOperator = LogicalOperator.And;
#region this code throws a specified cast not valid exception
// Create the e2sds_statusid condition
ConditionExpression condition3 = new ConditionExpression();
condition3.AttributeName = new_CustomEntity1.AttributeNames.new_CustomEntity2LookupField;
condition3.Operator = ConditionOperator.Equal;
condition3.Values.Add(status.Id);
#endregion
linkHistory.LinkCriteria.Conditions.Add(condition3);
//removed code for brevity
query.LinkEntities.Add(linkHistory);
我尝试了什么
不幸的是,由于QueryExpression
在RetrieveMultiple
请求中执行,我无法进入代码以找出导致无效转换的数据,但是,注释掉在非工作中标记的区域代码意味着异常消失。
重写代码是一个选项 - 这就是我如何知道TestData的工作方式,因为我编写的工作QueryExpression代码返回了一组数据。
我不知道此问题是否与QueryExpression或FakeXrmEasy有关,因此如果无法提供实际解决方案,即使提供跟踪问题根本原因的方法也会有所帮助。
注意:出于保密原因,自定义实体,字段和查找的名称已更改。如果名称不匹配,则可能是手动转置错误,而不是代码问题。