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

Aggregates with groupby an optionset do not work #482

@filcole

Description

@filcole

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"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions