|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +using MongoDB.Driver.TestHelpers; |
| 20 | +using FluentAssertions; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira; |
| 24 | + |
| 25 | +public class CSharp5529Tests : LinqIntegrationTest<CSharp5529Tests.ClassFixture> |
| 26 | +{ |
| 27 | + public CSharp5529Tests(ClassFixture fixture) |
| 28 | + : base(fixture) |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + [Theory] |
| 33 | + [InlineData(1, 1, """{ $group: { _id : 1, __agg0 : { $first : "$X" } } }""", 1)] |
| 34 | + [InlineData(1, 2, """{ $group: { _id : 1, __agg0 : { $last : "$X" } } }""", 2)] |
| 35 | + [InlineData(2, 1, """{ $group: { _id : 1, __agg0 : { $first : "$D.Y" } } }""", 11)] |
| 36 | + [InlineData(2, 2, """{ $group: { _id : 1, __agg0 : { $last : "$D.Y" } } }""", 22)] |
| 37 | + [InlineData(3, 1, """{ $group: { _id : 1, __agg0 : { $first : "$D.E.Z" } } }""", 111)] |
| 38 | + [InlineData(3, 2, """{ $group: { _id : 1, __agg0 : { $last : "$D.E.Z" } } }""", 222)] |
| 39 | + public void First_or_Last_optimization_should_work(int level, int firstOrLast, string expectedGroupStage, int expectedResult) |
| 40 | + { |
| 41 | + var collection = Fixture.Collection; |
| 42 | + |
| 43 | + var queryable = (level, firstOrLast) switch |
| 44 | + { |
| 45 | + (1, 1) => collection.Aggregate().Group(x => 1, g => g.First().X), |
| 46 | + (1, 2) => collection.Aggregate().Group(x => 1, g => g.Last().X), |
| 47 | + (2, 1) => collection.Aggregate().Group(x => 1, g => g.First().D.Y), |
| 48 | + (2, 2) => collection.Aggregate().Group(x => 1, g => g.Last().D.Y), |
| 49 | + (3, 1) => collection.Aggregate().Group(x => 1, g => g.First().D.E.Z), |
| 50 | + (3, 2) => collection.Aggregate().Group(x => 1, g => g.Last().D.E.Z), |
| 51 | + _ => throw new ArgumentException() |
| 52 | + }; |
| 53 | + |
| 54 | + var stages = Translate(collection,queryable); |
| 55 | + AssertStages( |
| 56 | + stages, |
| 57 | + expectedGroupStage, |
| 58 | + """{ $project : { _v : "$__agg0", _id : 0 } }"""); |
| 59 | + |
| 60 | + var result = queryable.Single(); |
| 61 | + result.Should().Be(expectedResult); |
| 62 | + } |
| 63 | + public class C |
| 64 | + { |
| 65 | + public int Id { get; set; } |
| 66 | + public int X { get; set; } |
| 67 | + |
| 68 | + public D D { get; set; } |
| 69 | + } |
| 70 | + |
| 71 | + public class D |
| 72 | + { |
| 73 | + public E E { get; set; } |
| 74 | + public int Y { get; set; } |
| 75 | + } |
| 76 | + |
| 77 | + public class E |
| 78 | + { |
| 79 | + public int Z { get; set; } |
| 80 | + } |
| 81 | + |
| 82 | + public sealed class ClassFixture : MongoCollectionFixture<C> |
| 83 | + { |
| 84 | + protected override IEnumerable<C> InitialData => |
| 85 | + [ |
| 86 | + new C { Id = 1, X = 1, D = new D { E = new E { Z = 111 }, Y = 11 } }, |
| 87 | + new C { Id = 2, X = 2, D = new D { E = new E { Z = 222 }, Y = 22 } }, |
| 88 | + ]; |
| 89 | + } |
| 90 | +} |
0 commit comments