|
| 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.Linq; |
| 17 | +using FluentAssertions; |
| 18 | +using MongoDB.Driver.Linq; |
| 19 | +using MongoDB.TestHelpers.XunitExtensions; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira |
| 23 | +{ |
| 24 | + public class CSharp4700Tests : Linq3IntegrationTest |
| 25 | + { |
| 26 | + [Theory] |
| 27 | + [ParameterAttributeData] |
| 28 | + public void OrderBy_Count_should_work( |
| 29 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 30 | + { |
| 31 | + var collection = GetCollection(linqProvider); |
| 32 | + |
| 33 | + var queryable = collection.AsQueryable() |
| 34 | + .GroupBy(x => x.Name) |
| 35 | + .OrderBy(x => x.Count()); |
| 36 | + |
| 37 | + var stages = Translate(collection, queryable); |
| 38 | + var results = queryable.ToList(); |
| 39 | + |
| 40 | + if (linqProvider == LinqProvider.V2) |
| 41 | + { |
| 42 | + AssertStages( |
| 43 | + stages, |
| 44 | + "{ $group : { _id : '$Name', __agg0 : { $sum : 1 } } }", |
| 45 | + "{ $sort : { __agg0 : 1 } }"); |
| 46 | + |
| 47 | + results.Should().HaveCount(2); |
| 48 | + results[0].Key.Should().Be("Jane"); |
| 49 | + results[0].Count().Should().Be(0); // this result is incorrect in LINQ2 |
| 50 | + results[1].Key.Should().Be("John"); |
| 51 | + results[1].Count().Should().Be(0); // this result is incorrect in LINQ2 |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + AssertStages( |
| 56 | + stages, |
| 57 | + "{ $group : { _id : '$Name', _elements : { $push : '$$ROOT' } } }", |
| 58 | + "{ $project : { _id : 0, _document : '$$ROOT', _key1 : { $size : '$_elements' } } }", |
| 59 | + "{ $sort : { _key1 : 1 } }", |
| 60 | + "{ $replaceRoot : { newRoot : '$_document' } }"); |
| 61 | + |
| 62 | + results.Should().HaveCount(2); |
| 63 | + results[0].Key.Should().Be("Jane"); |
| 64 | + results[0].Count().Should().Be(1); |
| 65 | + results[1].Key.Should().Be("John"); |
| 66 | + results[1].Count().Should().Be(2); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private IMongoCollection<C> GetCollection(LinqProvider linqProvider) |
| 71 | + { |
| 72 | + var collection = GetCollection<C>("test", linqProvider); |
| 73 | + CreateCollection( |
| 74 | + collection, |
| 75 | + new C { Id = 1, Name = "John" }, |
| 76 | + new C { Id = 2, Name = "John" }, |
| 77 | + new C { Id = 6, Name = "Jane" }); |
| 78 | + return collection; |
| 79 | + } |
| 80 | + |
| 81 | + private class C |
| 82 | + { |
| 83 | + public int Id { get; set; } |
| 84 | + public string Name { get; set; } |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | +} |
0 commit comments