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

Aggregates with groupby an optionset do not work #482

Open
filcole opened this issue Mar 3, 2020 · 1 comment · May be fixed by #484
Open

Aggregates with groupby an optionset do not work #482

filcole opened this issue Mar 3, 2020 · 1 comment · May be fixed by #484

Comments

@filcole
Copy link

filcole commented Mar 3, 2020

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"]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant