Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Use OptionSetValue.Value for comparison with optionset group by #484

Open
wants to merge 3 commits into
base: master
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
4 changes: 4 additions & 0 deletions FakeXrmEasy.Shared/XrmFakedContext.Aggregations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ public override IComparable FindGroupValue(object attributeValue)
{
return new ComparableEntityReference(attributeValue as EntityReference) as IComparable;
}
else if (attributeValue is OptionSetValue)
{
return ((OptionSetValue)attributeValue).Value as IComparable;
}
else
{
return attributeValue as IComparable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ public void FetchXml_Aggregate_Group_Count()
Assert.Equal(1, grantGroup.GetAttributeValue<AliasedValue>("count.contacts").Value);
}

[Fact]
public void FetchXml_Aggregate_Group_OptionSet_Count()
{
var fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>
<entity name='contact'>
<attribute name='contactid' alias='count.contacts' aggregate='count' />
<attribute name='gendercode' alias='group.gendercode' groupby='true' />
</entity>
</fetch>";

var male = new OptionSetValue(1);
var female = new OptionSetValue(2);

var ctx = new XrmFakedContext();
ctx.Initialize(new[] {
new Contact() { Id = Guid.NewGuid(), GenderCode = male, FirstName = "John" },
new Contact() { Id = Guid.NewGuid(), GenderCode = female, FirstName = "Jane" },
new Contact() { Id = Guid.NewGuid(), GenderCode = male, FirstName = "Sam" },
new Contact() { Id = Guid.NewGuid(), GenderCode = male, FirstName = "John" },
});

var collection = ctx.GetFakedOrganizationService().RetrieveMultiple(new FetchExpression(fetchXml));

// Make sure we only have the expected properties
foreach (var e in collection.Entities)
{
Assert.Equal(new[] { "count.contacts", "group.gendercode" }, e.Attributes.Keys.OrderBy(x => x));
}

Assert.Equal(2, collection.Entities.Count);

var maleGroup = collection.Entities.SingleOrDefault(x => (male.Value).Equals(x.GetAttributeValue<AliasedValue>("group.gendercode").Value));
Assert.Equal(3, maleGroup.GetAttributeValue<AliasedValue>("count.contacts").Value);

var femaleGroup = collection.Entities.SingleOrDefault(x => (female.Value).Equals(x.GetAttributeValue<AliasedValue>("group.gendercode").Value));
Assert.Equal(1, femaleGroup.GetAttributeValue<AliasedValue>("count.contacts").Value);
}

[Fact]
public void FetchXml_Aggregate_Group_EntityReference_Count()
{
Expand Down