Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added support for ConditionOperator AboveOrEqual in a Condition Expression #85

Open
wants to merge 4 commits into
base: 2x-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [2.5.2]

### Added

- **Alpha**: Initial support for AboveOrEqual in ConditionExpressions

## [2.5.1]

### Changed
Expand Down
14 changes: 7 additions & 7 deletions src/FakeXrmEasy.Core/FakeXrmEasy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY_2013'">net452</TargetFrameworks>
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY'">net452</TargetFrameworks>
<PackageId>FakeXrmEasy.Core</PackageId>
<VersionPrefix>2.5.1</VersionPrefix>
<VersionPrefix>2.5.2</VersionPrefix>
<Authors>Jordi Montaña</Authors>
<Company>Dynamics Value</Company>
<Title>FakeXrmEasy Core</Title>
Expand Down Expand Up @@ -102,22 +102,22 @@
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2011" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2011" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2013'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2013" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2013" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2015'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2015" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2015" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2016'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2016" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2016" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_365'">
<PackageReference Include="FakeXrmEasy.Abstractions.v365" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v365" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_9'">
<PackageReference Include="FakeXrmEasy.Abstractions.v9" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v9" Version="[2.5.1-*,3.0)" />
</ItemGroup>

<Target Name="PreparePackageReleaseNotesFromFile" BeforeTargets="GenerateNuspec">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#if FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9
using FakeXrmEasy.Abstractions;
using FakeXrmEasy.Query;
using Microsoft.Xrm.Sdk;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace FakeXrmEasy.Query
{
internal static partial class ConditionExpressionExtensions
{
internal static Expression ToAboveOrEqualExpression(this TypedConditionExpression tc, Expression getAttributeValueExpr, Expression containsAttributeExpr, IXrmFakedContext context)
{
var c = tc.CondExpression;
var entityLogicalName = !string.IsNullOrEmpty(c.EntityName) ? c.EntityName : tc.QueryExpression.EntityName;

if(!context.Relationships.Any(r => r.IsHierarchical && r.Entity1LogicalName.Equals(entityLogicalName) && r.Entity1Attribute.Equals(c.AttributeName)))
{
return tc.ToEqualExpression(context, getAttributeValueExpr, containsAttributeExpr);
}

//Retrieve the hierarchical relationship to determine the parent attribute
var hierarchicalRelationship = context.Relationships.FirstOrDefault(r => r.IsHierarchical && r.Entity1LogicalName.Equals(entityLogicalName) && r.Entity1Attribute.Equals(c.AttributeName));

//Retrieve initial record from the hierarchical tree
var currentRecord = context.CreateQuery(entityLogicalName).FirstOrDefault(e => ((Guid)e.Attributes[c.AttributeName]) == ((Guid)c.Values[0]));
if(currentRecord == null)
{
return tc.ToEqualExpression(context, getAttributeValueExpr, containsAttributeExpr);
}

//Iterrate through parents via the relationship attributes to walk through the entire hierarchy and build a list of identifiers of the nodes in the hierarchy.
RetrieveParentEntity(context, hierarchicalRelationship, currentRecord, c.Values);

return tc.ToInExpression(getAttributeValueExpr, containsAttributeExpr);
}

private static void RetrieveParentEntity(IXrmFakedContext context, XrmFakedRelationship hierarchicalRelationship, Entity currentRecord, DataCollection<object> values)
{
if (!currentRecord.Attributes.ContainsKey(hierarchicalRelationship.Entity2Attribute) || currentRecord.Attributes[hierarchicalRelationship.Entity2Attribute] == null)
{
return;
}

var parentRecord = context.CreateQuery(hierarchicalRelationship.Entity1LogicalName).FirstOrDefault(e => ((Guid)e.Attributes[hierarchicalRelationship.Entity1Attribute]) == ((EntityReference)currentRecord.Attributes[hierarchicalRelationship.Entity2Attribute]).Id);
if (parentRecord != null)
{
values.Add(parentRecord.Id);
RetrieveParentEntity(context, hierarchicalRelationship, parentRecord, values);
}
}
}
}
#endif
6 changes: 6 additions & 0 deletions src/FakeXrmEasy.Core/Query/ConditionExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ internal static Expression ToExpression(this TypedConditionExpression c, QueryEx
break;
#endif

#if FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9
case ConditionOperator.AboveOrEqual:
operatorExpression = c.ToAboveOrEqualExpression(getNonBasicValueExpr, containsAttributeExpression, context);
break;
#endif

default:
throw UnsupportedExceptionFactory.New(context.LicenseContext.Value, string.Format("Operator {0} not yet implemented for condition expression", c.CondExpression.Operator.ToString()));

Expand Down
26 changes: 13 additions & 13 deletions tests/FakeXrmEasy.Core.Tests/FakeXrmEasy.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<IsPackable>true</IsPackable>

<PackageId>FakeXrmEasy.CoreTests</PackageId>
<VersionPrefix>2.5.1</VersionPrefix>
<VersionPrefix>2.5.2</VersionPrefix>
<Authors>Jordi Montaña</Authors>
<Company>Dynamics Value S.L.</Company>
<Title>Internal Unit test suite for FakeXrmEasy.Core package</Title>
Expand Down Expand Up @@ -114,45 +114,45 @@
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2011" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2011" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2013'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2013" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2013" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2015'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2015" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2015" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2016'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2016" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2016" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_365'">
<PackageReference Include="FakeXrmEasy.Abstractions.v365" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v365" Version="[2.5.1-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_9'">
<PackageReference Include="FakeXrmEasy.Abstractions.v9" Version="[2.5.0-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v9" Version="[2.5.1-*,3.0)" />
</ItemGroup>

<ItemGroup Condition="'$(PackTests)' == ''">
<ProjectReference Include="..\..\src\FakeXrmEasy.Core\FakeXrmEasy.Core.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY'">
<PackageReference Include="FakeXrmEasy.Core.v2011" Version="[2.5.1-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2011" Version="[2.5.2-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_2013'">
<PackageReference Include="FakeXrmEasy.Core.v2013" Version="[2.5.1-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2013" Version="[2.5.2-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_2015'">
<PackageReference Include="FakeXrmEasy.Core.v2015" Version="[2.5.1-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2015" Version="[2.5.2-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_2016'">
<PackageReference Include="FakeXrmEasy.Core.v2016" Version="[2.5.1-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v2016" Version="[2.5.2-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_365'">
<PackageReference Include="FakeXrmEasy.Core.v365" Version="[2.5.1-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v365" Version="[2.5.2-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(PackTests)' == 'true' And '$(Configuration)'=='FAKE_XRM_EASY_9'">
<PackageReference Include="FakeXrmEasy.Core.v9" Version="[2.5.1-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Core.v9" Version="[2.5.2-*,3.0)" />
</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,5 +875,54 @@ public void Should_Not_Fail_On_Conditions_In_Link_Entities_Multiple()
Assert.Single(invoiceDetails.Entities);
Assert.Equal(invoicedetail02.Id, invoiceDetails.Entities[0].Id);
}

#if FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9
[Fact]
public void When_executing_a_queryexpression_with_aboveorequal_all_records_in_hierarchy_are_returned()
{
Entity parentParentAccount = new Entity("account");
parentParentAccount.Id = Guid.NewGuid();
parentParentAccount.Attributes.Add("name", "Parent Parent Account");

Entity parentAccount = new Entity("account");
parentAccount.Id = Guid.NewGuid();
parentAccount.Attributes.Add("name", "Parent Account");
parentAccount.Attributes.Add("parentaccountid", new EntityReference("account", parentParentAccount.Id));

Entity childAccount = new Entity("account");
childAccount.Id = Guid.NewGuid();
childAccount.Attributes.Add("name", "Child Account");
childAccount.Attributes.Add("parentaccountid", new EntityReference("account", parentAccount.Id));

//Important step to specify the hierarchical relation ship.
_context.AddRelationship("account_parent_account", new XrmFakedRelationship
{
RelationshipType = XrmFakedRelationship.FakeRelationshipType.OneToMany,
IsHierarchical = true,
Entity1LogicalName = "account",
Entity1Attribute = "accountid",
Entity2LogicalName = "account",
Entity2Attribute = "parentaccountid"
});

_service.Create(parentParentAccount);
_service.Create(parentAccount);
_service.Create(childAccount);

QueryExpression query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(true) };
query.Criteria.AddCondition("accountid", ConditionOperator.AboveOrEqual, childAccount.Id);

var result = _service.RetrieveMultiple(query);

Assert.NotNull(result);
Assert.NotNull(result.Entities);
Assert.True(result.Entities.Count == 3);

foreach (var item in result.Entities)
{
Assert.True(item.Id == childAccount.Id || item.Id == parentAccount.Id || item.Id == parentParentAccount.Id);
}
}
#endif
}
}
Loading