|
| 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.Collections.Generic; |
| 17 | +using MongoDB.Driver.Linq; |
| 18 | +using MongoDB.TestHelpers.XunitExtensions; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 22 | +{ |
| 23 | + public class CSharp4562Tests : Linq3IntegrationTest |
| 24 | + { |
| 25 | + [Theory] |
| 26 | + [ParameterAttributeData] |
| 27 | + public void SortBy_using_constant_should_work( |
| 28 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 29 | + { |
| 30 | + var collection = CreateCollection(linqProvider); |
| 31 | + |
| 32 | + var aggregate = GetTranslationsSortedByEnglish(collection); |
| 33 | + |
| 34 | + var stages = Translate(collection, aggregate); |
| 35 | + AssertStages(stages, "{ $sort : { 'Text.en' : 1 } }"); |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [ParameterAttributeData] |
| 40 | + public void SortBy_using_parameter_should_work( |
| 41 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 42 | + { |
| 43 | + var collection = CreateCollection(linqProvider); |
| 44 | + var language = "sp"; |
| 45 | + |
| 46 | + var aggregate = GetSortedTranslations(collection, language); |
| 47 | + |
| 48 | + var stages = Translate(collection, aggregate); |
| 49 | + AssertStages(stages, "{ $sort : { 'Text.sp' : 1 } }"); |
| 50 | + } |
| 51 | + |
| 52 | + private IMongoCollection<Translation> CreateCollection(LinqProvider linqProvider) |
| 53 | + { |
| 54 | + var collection = GetCollection<Translation>("test", linqProvider); |
| 55 | + return collection; |
| 56 | + } |
| 57 | + |
| 58 | + private class Translation |
| 59 | + { |
| 60 | + public int Id { get; set; } |
| 61 | + public Dictionary<string, string> Text { get; set; } |
| 62 | + } |
| 63 | + |
| 64 | + private static IAggregateFluent<Translation> GetSortedTranslations(IMongoCollection<Translation> collection, string language) |
| 65 | + { |
| 66 | + return collection.Aggregate() |
| 67 | + .SortBy(c => c.Text[language]); |
| 68 | + } |
| 69 | + |
| 70 | + private static IAggregateFluent<Translation> GetTranslationsSortedByEnglish(IMongoCollection<Translation> collection) |
| 71 | + { |
| 72 | + return collection.Aggregate() |
| 73 | + .SortBy(c => c.Text["en"]); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments