|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace DevExtreme.AspNet.Data.Tests { |
| 7 | + |
| 8 | + public class IsSummaryQueryTests { |
| 9 | + |
| 10 | + [Theory] |
| 11 | + [MemberData(nameof(CombinatorialBool), 4)] |
| 12 | + public void Case(bool remoteGrouping, bool requireTotalCount, bool hasGroups, bool hasSummary) { |
| 13 | + var loadOptions = new SampleLoadOptions { |
| 14 | + IsSummaryQuery = true, |
| 15 | + RemoteGrouping = remoteGrouping, |
| 16 | + RequireTotalCount = requireTotalCount, |
| 17 | + Filter = new[] { "this", "<", "100" } |
| 18 | + }; |
| 19 | + if(hasGroups) { |
| 20 | + loadOptions.Group = new[] { |
| 21 | + new GroupingInfo { Selector = "any", IsExpanded = !remoteGrouping } |
| 22 | + }; |
| 23 | + loadOptions.GroupSummary = new[] { |
| 24 | + new SummaryInfo { Selector = "any", SummaryType = "any" } |
| 25 | + }; |
| 26 | + } |
| 27 | + if(hasSummary) { |
| 28 | + loadOptions.TotalSummary = new[] { |
| 29 | + new SummaryInfo { Selector = "this", SummaryType = "sum" } |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + var loadResult = DataSourceLoader.Load(new[] { 1, 1, 2, 2, 100 }, loadOptions); |
| 34 | + |
| 35 | + Assert.Null(loadResult.data); |
| 36 | + |
| 37 | + if(hasSummary) |
| 38 | + Assert.Equal(6m, loadResult.summary[0]); |
| 39 | + else |
| 40 | + Assert.Null(loadResult.summary); |
| 41 | + |
| 42 | + // Refer to RemoteGroupExpressionCompiler.MakeAggregatingProjection |
| 43 | + var implicitTotalCount = hasSummary && remoteGrouping; |
| 44 | + |
| 45 | + if(requireTotalCount || implicitTotalCount) |
| 46 | + Assert.True(loadResult.totalCount > 0); |
| 47 | + else |
| 48 | + Assert.Equal(-1, loadResult.totalCount); |
| 49 | + |
| 50 | + var expectedExpressionCount = 0; |
| 51 | + |
| 52 | + if(requireTotalCount && !implicitTotalCount) { |
| 53 | + expectedExpressionCount++; |
| 54 | + Assert.Contains(loadOptions.ExpressionLog, line => line.EndsWith(".Where(obj => (obj < 100)).Count()")); |
| 55 | + } |
| 56 | + |
| 57 | + if(hasSummary) { |
| 58 | + expectedExpressionCount++; |
| 59 | + if(remoteGrouping) |
| 60 | + Assert.Contains(loadOptions.ExpressionLog, line => line.Contains(".GroupBy(obj => new AnonType())")); |
| 61 | + else |
| 62 | + Assert.Contains(loadOptions.ExpressionLog, line => line.EndsWith(".Where(obj => (obj < 100))")); |
| 63 | + } |
| 64 | + |
| 65 | + Assert.Equal(expectedExpressionCount, loadOptions.ExpressionLog.Count); |
| 66 | + } |
| 67 | + |
| 68 | + public static IEnumerable<object[]> CombinatorialBool(int count) { |
| 69 | + var combinationCount = 1 << count; |
| 70 | + for(var i = 0; i < combinationCount; i++) { |
| 71 | + yield return Enumerable.Range(0, count) |
| 72 | + .Select(bit => (i & 1 << bit) != 0) |
| 73 | + .Cast<object>() |
| 74 | + .ToArray(); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments