fake-xrm-easy icon indicating copy to clipboard operation
fake-xrm-easy copied to clipboard

Aggregates with groupby an optionset do not work

Open filcole opened this issue 4 years ago • 1 comments

When a FetchXml aggregate includes a groupby clause on an optionset value then FakeXrmEasy will return incorrect results. The following unit tests is similar to other tests within AggregateTests.cs, except it is altered to group by an optionset. The test fails.

        [Fact]
        public void FetchXml_Aggregate_GroupByOptionset_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);
        }

Output of unit test:

Message: Assert.Equal() Failure
Expected: String[] ["count.contacts", "group.gendercode"]
Actual:   OrderedEnumerable<String, String> ["count.contacts"]

filcole avatar Mar 03 '20 08:03 filcole

Branch including unit test (without fix) is here: https://github.com/filcole/fake-xrm-easy/tree/OptionSetGroupBy

filcole avatar Mar 03 '20 08:03 filcole