|
| 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 MongoDB.Driver.TestHelpers; |
| 19 | +using FluentAssertions; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira; |
| 23 | + |
| 24 | +public class CSharp4251Tests : LinqIntegrationTest<CSharp4251Tests.ClassFixture> |
| 25 | +{ |
| 26 | + public CSharp4251Tests(ClassFixture fixture) |
| 27 | + : base(fixture) |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void Test1() |
| 33 | + { |
| 34 | + var collection = Fixture.Collection; |
| 35 | + |
| 36 | + var queryable = collection.AsQueryable() |
| 37 | + .Select(n => |
| 38 | + new MappedObject { Items = n.Items.Select(k => new KeyValuedObject() { Count = k.Value.Count, Description = k.Value.Description, Name = k.Key }).ToList() } |
| 39 | + ); |
| 40 | + |
| 41 | + var stages = Translate(collection, queryable); |
| 42 | + AssertStages(stages, "{ $project : { Items : { $map : { input : { $objectToArray : '$Items' }, as : 'k', in : { Count : '$$k.v.Count', Description : '$$k.v.Description', Name : '$$k.k' } } }, _id : 0 } }"); |
| 43 | + } |
| 44 | + |
| 45 | + public class DatabaseObject |
| 46 | + { |
| 47 | + public int Id { get; set; } |
| 48 | + public Dictionary<string, DatabaseValueObject> Items { get; set; } = new Dictionary<string, DatabaseValueObject>(); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + public class DatabaseValueObject |
| 53 | + { |
| 54 | + public string Description { get; set; } = string.Empty; |
| 55 | + public int Count { get; set; } |
| 56 | + } |
| 57 | + |
| 58 | + public class MappedObject |
| 59 | + { |
| 60 | + public List<KeyValuedObject> Items { get; set; } = new List<KeyValuedObject>(); |
| 61 | + } |
| 62 | + |
| 63 | + public class KeyValuedObject |
| 64 | + { |
| 65 | + public string Name { get; set; } = string.Empty; |
| 66 | + public int Count { get; set; } |
| 67 | + public string Description { get; set; } = string.Empty; |
| 68 | + } |
| 69 | + |
| 70 | + public sealed class ClassFixture : MongoCollectionFixture<DatabaseObject> |
| 71 | + { |
| 72 | + protected override IEnumerable<DatabaseObject> InitialData => null; |
| 73 | + // [ |
| 74 | + // new DatabaseObject { } |
| 75 | + // ]; |
| 76 | + } |
| 77 | +} |
0 commit comments