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

feat(#141): add support for link entities using the exists join operator #91

Open
wants to merge 2 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.7.0]

### Added

- Added support for link entities that use the `Exists` join operator - https://github.com/DynamicsValue/fake-xrm-easy/issues/141

## [2.6.0]

### Added
Expand Down
1 change: 1 addition & 0 deletions src/FakeXrmEasy.Core/FakeXrmEasy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<SignAssembly>True</SignAssembly>
<PublicSign Condition="'$(OS)'=='Unix'">true</PublicSign>
<SonarQubeTestProject>False</SonarQubeTestProject>
<Version>2.7.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion src/FakeXrmEasy.Core/Query/LinkEntityQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ internal static IQueryable<Entity> ToQueryable(this LinkEntity le, IXrmFakedCont
, (x, y) => x.outerEl
.JoinAttributes(y, new ColumnSet(true), leAlias, context));


break;
#if FAKE_XRM_EASY_9
case JoinOperator.Exists:
// This is most likely not the most performant implementation, but it is probably the closest match
// to the generated sql query, which will be a correlated subquery.
query = query.Where(outerEl =>
inner.Any(innerEl =>
outerEl.KeySelector(linkFromAlias, context).Equals(innerEl.KeySelector(le.LinkToAttributeName, context))));
break;
#endif
default: //This shouldn't be reached as there are only 3 types of Join...
throw UnsupportedExceptionFactory.New(context.LicenseContext.Value, string.Format("The join operator {0} is currently not supported. ", le.JoinOperator));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,5 +1127,101 @@ public void TestRetriveMultipleWithLinkEntityWithAlternateNullField()
// this fails (entity2Value is "value")
Assert.Null(entity2Value);
}

[Fact]
public void Should_Find_Record_With_Associations_When_Exists_Join_Is_Used()
{
// Arrange
Entity account = new Entity("account", Guid.NewGuid());

Entity contact = new Entity("contact", Guid.NewGuid())
{
["parentcustomerid"] = account.ToEntityReference(),
};
List<Entity> entities = new List<Entity>()
{
account, contact
};

_context.Initialize(entities);

QueryExpression queryExpression = new QueryExpression("account");
queryExpression.ColumnSet = new ColumnSet(true);

LinkEntity contactQuery = queryExpression.AddLink("contact", "accountid", "parentcustomerid");
contactQuery.JoinOperator = JoinOperator.Exists;
contactQuery.EntityAlias = "c";

// Act
EntityCollection results = _service.RetrieveMultiple(queryExpression);

// Assert
Assert.Single(results.Entities);
}


[Fact]
public void Should_Not_Find_Record_Without_Associations_When_Exists_Join_Is_Used()
{
// Arrange
Entity account = new Entity("account", Guid.NewGuid());

Entity contact = new Entity("contact", Guid.NewGuid());
List<Entity> entities = new List<Entity>()
{
account, contact
};

_context.Initialize(entities);

QueryExpression queryExpression = new QueryExpression("account");
queryExpression.ColumnSet = new ColumnSet(true);

LinkEntity contactQuery = queryExpression.AddLink("contact", "accountid", "parentcustomerid");
contactQuery.JoinOperator = JoinOperator.Exists;
contactQuery.EntityAlias = "c";

// Act
EntityCollection result = _service.RetrieveMultiple(queryExpression);

// Assert
Assert.Empty(result.Entities);
}

[Fact]
public void Should_Return_Only_Outer_Record_When_Exists_Join_Is_Used()
{
// Arrange
Entity account = new Entity("account", Guid.NewGuid());

Entity contact = new Entity("contact", Guid.NewGuid())
{
["parentcustomerid"] = account.ToEntityReference(),
["firstname"] = "firstname"
};
List<Entity> entities = new List<Entity>()
{
account, contact
};

_context.Initialize(entities);

QueryExpression queryExpression = new QueryExpression("account");
queryExpression.ColumnSet = new ColumnSet(true);

LinkEntity contactQuery = queryExpression.AddLink("contact", "accountid", "parentcustomerid");
contactQuery.JoinOperator = JoinOperator.Exists;
contactQuery.EntityAlias = "c";
contactQuery.Columns = new ColumnSet(true);

// Act
EntityCollection result = _service.RetrieveMultiple(queryExpression);

// Assert
Assert.Single(result.Entities);

Entity outerRecord = result.Entities.First();
Assert.Null(outerRecord.GetAttributeValue<AliasedValue>("c.firstname"));
}
}
}
Loading