|
| 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 System.Linq; |
| 18 | +using FluentAssertions; |
| 19 | +using MongoDB.Bson.Serialization.Attributes; |
| 20 | +using MongoDB.Bson.Serialization.Options; |
| 21 | +using MongoDB.Driver.Linq; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 25 | +{ |
| 26 | + public class CSharp4317Tests : Linq3IntegrationTest |
| 27 | + { |
| 28 | + [Fact] |
| 29 | + public void Projection_of_ArrayOfDocuments_dictionary_keys_and_values_should_work() |
| 30 | + { |
| 31 | + var collection = CreateCollection(); |
| 32 | + var projectStage = "{ $project : { Keys : '$Data.k', Values : '$Data.v', _id : 0 } }"; |
| 33 | + |
| 34 | + var queryable = collection |
| 35 | + .AsQueryable() |
| 36 | + .Select(x => new { |
| 37 | + Keys = x.Data.Select(y => y.Key), |
| 38 | + Values = x.Data.Select(y => y.Value) |
| 39 | + }); |
| 40 | + |
| 41 | + var stages = Translate(collection, queryable); |
| 42 | + AssertStages(stages, projectStage); |
| 43 | + |
| 44 | + var result = queryable.First(); |
| 45 | + result.Keys.Should().Equal(1, 2); |
| 46 | + result.Values.Should().Equal("v1", "v2"); |
| 47 | + } |
| 48 | + |
| 49 | + private IMongoCollection<C> CreateCollection() |
| 50 | + { |
| 51 | + var collection = GetCollection<C>("C"); |
| 52 | + |
| 53 | + CreateCollection( |
| 54 | + collection, |
| 55 | + new C { Id = 1, Data = new Dictionary<int, string> { { 1, "v1" }, { 2, "v2" } } }); |
| 56 | + |
| 57 | + return collection; |
| 58 | + } |
| 59 | + |
| 60 | + private class C |
| 61 | + { |
| 62 | + public int Id { get; set; } |
| 63 | + |
| 64 | + [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] |
| 65 | + public Dictionary<int, string> Data { get; set; } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments