|
| 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.Linq; |
| 18 | +using System.Linq.Expressions; |
| 19 | +using MongoDB.Driver.Linq; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 23 | +{ |
| 24 | + public class CSharp4509Tests : Linq3IntegrationTest |
| 25 | + { |
| 26 | + [Theory] |
| 27 | + [InlineData(JobSortColumn.DriverName, "DriverName", LinqProvider.V2)] |
| 28 | + [InlineData(JobSortColumn.DriverName, "DriverName", LinqProvider.V3)] |
| 29 | + [InlineData(JobSortColumn.JobNumber, "JobNumber", LinqProvider.V2)] |
| 30 | + [InlineData(JobSortColumn.JobNumber, "JobNumber", LinqProvider.V3)] |
| 31 | + public void OrderByDescending_should_work( |
| 32 | + JobSortColumn sortOrder, |
| 33 | + string expectedSortField, |
| 34 | + LinqProvider linqProvider) |
| 35 | + { |
| 36 | + var collection = CreateCollection(linqProvider); |
| 37 | + |
| 38 | + Expression<Func<DbJob, object>> selector = sortOrder switch |
| 39 | + { |
| 40 | + JobSortColumn.JobNumber => j => j.JobNumber, |
| 41 | + JobSortColumn.DriverName => j => j.DriverName, |
| 42 | + _ => throw new NotSupportedException() |
| 43 | + }; |
| 44 | + |
| 45 | + var queryable = |
| 46 | + collection.AsQueryable() |
| 47 | + .OrderByDescending(selector); |
| 48 | + |
| 49 | + var stages = Translate(collection, queryable); |
| 50 | + AssertStages(stages, $"{{ $sort : {{ {expectedSortField} : -1 }} }}"); |
| 51 | + } |
| 52 | + |
| 53 | + private IMongoCollection<DbJob> CreateCollection(LinqProvider linqProvider) |
| 54 | + { |
| 55 | + var collection = GetCollection<DbJob>("jobs", linqProvider); |
| 56 | + return collection; |
| 57 | + } |
| 58 | + |
| 59 | + public class DbJob |
| 60 | + { |
| 61 | + public int JobNumber { get; set; } |
| 62 | + public string DriverName { get; set; } |
| 63 | + } |
| 64 | + |
| 65 | + public enum JobSortColumn { JobNumber, DriverName } |
| 66 | + } |
| 67 | +} |
0 commit comments