|
| 1 | +using Thinktecture.TestDatabaseContext; |
| 2 | + |
| 3 | +namespace Thinktecture.Query; |
| 4 | + |
| 5 | +// ReSharper disable once InconsistentNaming |
| 6 | +public class MaxMinUuidAggregate : IntegrationTestsBase |
| 7 | +{ |
| 8 | + // Text-ordered ascending so the expected max/min are unambiguous (uuid::text ordering == uuid byte ordering). |
| 9 | + private static readonly Guid _id1 = new("11111111-1111-1111-1111-111111111111"); |
| 10 | + private static readonly Guid _id2 = new("22222222-2222-2222-2222-222222222222"); |
| 11 | + private static readonly Guid _id3 = new("33333333-3333-3333-3333-333333333333"); |
| 12 | + private static readonly Guid _id4 = new("44444444-4444-4444-4444-444444444444"); |
| 13 | + |
| 14 | + public MaxMinUuidAggregate(ITestOutputHelper testOutputHelper, NpgsqlFixture npgsqlFixture) |
| 15 | + : base(testOutputHelper, npgsqlFixture) |
| 16 | + { |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public async Task Should_translate_Max_over_uuid_grouping() |
| 21 | + { |
| 22 | + ArrangeDbContext.TestEntities.AddRange( |
| 23 | + new TestEntity { Id = _id1, Count = 1, RequiredName = "R" }, |
| 24 | + new TestEntity { Id = _id2, Count = 1, RequiredName = "R" }, |
| 25 | + new TestEntity { Id = _id3, Count = 2, RequiredName = "R" }, |
| 26 | + new TestEntity { Id = _id4, Count = 2, RequiredName = "R" }); |
| 27 | + await ArrangeDbContext.SaveChangesAsync(); |
| 28 | + |
| 29 | + var result = await ActDbContext.TestEntities |
| 30 | + .GroupBy(e => e.Count) |
| 31 | + .Select(g => new { Count = g.Key, MaxId = g.Max(e => e.Id) }) |
| 32 | + .OrderBy(x => x.Count) |
| 33 | + .ToListAsync(); |
| 34 | + |
| 35 | + result.Should().HaveCount(2); |
| 36 | + result[0].MaxId.Should().Be(_id2); |
| 37 | + result[1].MaxId.Should().Be(_id4); |
| 38 | + |
| 39 | + ExecutedCommands.Last().Should().Contain("max").And.Contain("text").And.Contain("uuid"); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task Should_translate_Min_over_uuid_grouping() |
| 44 | + { |
| 45 | + ArrangeDbContext.TestEntities.AddRange( |
| 46 | + new TestEntity { Id = _id1, Count = 1, RequiredName = "R" }, |
| 47 | + new TestEntity { Id = _id2, Count = 1, RequiredName = "R" }, |
| 48 | + new TestEntity { Id = _id3, Count = 2, RequiredName = "R" }, |
| 49 | + new TestEntity { Id = _id4, Count = 2, RequiredName = "R" }); |
| 50 | + await ArrangeDbContext.SaveChangesAsync(); |
| 51 | + |
| 52 | + var result = await ActDbContext.TestEntities |
| 53 | + .GroupBy(e => e.Count) |
| 54 | + .Select(g => new { Count = g.Key, MinId = g.Min(e => e.Id) }) |
| 55 | + .OrderBy(x => x.Count) |
| 56 | + .ToListAsync(); |
| 57 | + |
| 58 | + result.Should().HaveCount(2); |
| 59 | + result[0].MinId.Should().Be(_id1); |
| 60 | + result[1].MinId.Should().Be(_id3); |
| 61 | + |
| 62 | + ExecutedCommands.Last().Should().Contain("min").And.Contain("text").And.Contain("uuid"); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task Should_translate_Max_over_nullable_uuid() |
| 67 | + { |
| 68 | + // Parents (Count = 0) referenced by ParentId; excluded from the aggregated group via the Where filter. |
| 69 | + ArrangeDbContext.TestEntities.AddRange( |
| 70 | + new TestEntity { Id = _id1, Count = 0, RequiredName = "R" }, |
| 71 | + new TestEntity { Id = _id2, Count = 0, RequiredName = "R" }); |
| 72 | + ArrangeDbContext.TestEntities.AddRange( |
| 73 | + new TestEntity { Id = _id3, Count = 7, ParentId = null, RequiredName = "R" }, |
| 74 | + new TestEntity { Id = _id4, Count = 7, ParentId = _id1, RequiredName = "R" }, |
| 75 | + new TestEntity { Id = new("55555555-5555-5555-5555-555555555555"), Count = 7, ParentId = _id2, RequiredName = "R" }); |
| 76 | + await ArrangeDbContext.SaveChangesAsync(); |
| 77 | + |
| 78 | + var result = await ActDbContext.TestEntities |
| 79 | + .Where(e => e.Count == 7) |
| 80 | + .GroupBy(e => e.Count) |
| 81 | + .Select(g => g.Max(e => e.ParentId)) |
| 82 | + .ToListAsync(); |
| 83 | + |
| 84 | + result.Should().ContainSingle(); |
| 85 | + result[0].Should().Be(_id2); // max(ParentId) over {null, _id1, _id2} == _id2, nulls ignored |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public async Task Should_translate_full_ntile_groupby_max_pipeline() |
| 90 | + { |
| 91 | + const int rangeCount = 2; |
| 92 | + |
| 93 | + ArrangeDbContext.TestEntities.AddRange( |
| 94 | + new TestEntity { Id = _id1, RequiredName = "R" }, |
| 95 | + new TestEntity { Id = _id2, RequiredName = "R" }, |
| 96 | + new TestEntity { Id = _id3, RequiredName = "R" }, |
| 97 | + new TestEntity { Id = _id4, RequiredName = "R" }); |
| 98 | + await ArrangeDbContext.SaveChangesAsync(); |
| 99 | + |
| 100 | + var tileEnds = await ActDbContext.TestEntities |
| 101 | + .Select(b => new |
| 102 | + { |
| 103 | + b.Id, |
| 104 | + Tile = EF.Functions.NTile(rangeCount, EF.Functions.OrderBy(b.Id)) |
| 105 | + }) |
| 106 | + .AsSubQuery() |
| 107 | + .GroupBy(x => x.Tile) |
| 108 | + .Select(g => new { Tile = g.Key, End = g.Max(x => x.Id) }) |
| 109 | + .OrderBy(x => x.Tile) |
| 110 | + .ToListAsync(); |
| 111 | + |
| 112 | + // 4 rows ordered by Id, 2 buckets: bucket 1 = {_id1, _id2}, bucket 2 = {_id3, _id4}. |
| 113 | + tileEnds.Should().HaveCount(rangeCount); |
| 114 | + tileEnds[0].End.Should().Be(_id2); |
| 115 | + tileEnds[1].End.Should().Be(_id4); |
| 116 | + } |
| 117 | +} |
0 commit comments