|  | 
|  | 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 MongoDB.Bson.Serialization.Attributes; | 
|  | 21 | +using MongoDB.Bson.Serialization.Options; | 
|  | 22 | +using Xunit; | 
|  | 23 | + | 
|  | 24 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira; | 
|  | 25 | + | 
|  | 26 | +public class CSharp1913Tests : LinqIntegrationTest<CSharp1913Tests.ClassFixture> | 
|  | 27 | +{ | 
|  | 28 | +    public CSharp1913Tests(ClassFixture fixture) | 
|  | 29 | +        : base(fixture) | 
|  | 30 | +    { | 
|  | 31 | +    } | 
|  | 32 | + | 
|  | 33 | +    [Fact] | 
|  | 34 | +    public void SelectMany_with_ArrayOfArrays_representation_should_work() | 
|  | 35 | +    { | 
|  | 36 | +        var collection = Fixture.Collection; | 
|  | 37 | + | 
|  | 38 | +        var queryable = collection.AsQueryable() | 
|  | 39 | +            .OfType<C>() | 
|  | 40 | +            .Where( to => to.Name == "TestName" ) | 
|  | 41 | +            .SelectMany( to => to.DictionaryWithArrayOfArraysRepresentation ); | 
|  | 42 | + | 
|  | 43 | +        var stages = Translate(collection, queryable); | 
|  | 44 | +        AssertStages( | 
|  | 45 | +            stages, | 
|  | 46 | +            "{ $match : { Name : 'TestName' } }", | 
|  | 47 | +            "{ $project : { _v : '$DictionaryWithArrayOfArraysRepresentation', _id : 0 } }", | 
|  | 48 | +            "{ $unwind : '$_v' }"); | 
|  | 49 | + | 
|  | 50 | +        var results = queryable.ToList(); | 
|  | 51 | +        results.Count.Should().Be(3); | 
|  | 52 | +        results[0].Key.Should().Be("A"); | 
|  | 53 | +        results[0].Value.Should().Be("a"); | 
|  | 54 | +        results[1].Key.Should().Be("B"); | 
|  | 55 | +        results[1].Value.Should().Be("b"); | 
|  | 56 | +        results[2].Key.Should().Be("C"); | 
|  | 57 | +        results[2].Value.Should().Be("c"); | 
|  | 58 | +    } | 
|  | 59 | + | 
|  | 60 | +    [Fact] | 
|  | 61 | +    public void SelectMany_with_ArrayOfDocuments_representation_should_work() | 
|  | 62 | +    { | 
|  | 63 | +        var collection = Fixture.Collection; | 
|  | 64 | + | 
|  | 65 | +        var queryable = collection.AsQueryable() | 
|  | 66 | +            .OfType<C>() | 
|  | 67 | +            .Where( to => to.Name == "TestName" ) | 
|  | 68 | +            .SelectMany( to => to.DictionaryWithArrayOfDocumentsRepresentation ); | 
|  | 69 | + | 
|  | 70 | +        var stages = Translate(collection, queryable); | 
|  | 71 | +        AssertStages( | 
|  | 72 | +            stages, | 
|  | 73 | +            "{ $match : { Name : 'TestName' } }", | 
|  | 74 | +            "{ $project : { _v : '$DictionaryWithArrayOfDocumentsRepresentation', _id : 0 } }", | 
|  | 75 | +            "{ $unwind : '$_v' }"); | 
|  | 76 | + | 
|  | 77 | +        var results = queryable.ToList(); | 
|  | 78 | +        results.Count.Should().Be(3); | 
|  | 79 | +        results[0].Key.Should().Be("A"); | 
|  | 80 | +        results[0].Value.Should().Be("a"); | 
|  | 81 | +        results[1].Key.Should().Be("B"); | 
|  | 82 | +        results[1].Value.Should().Be("b"); | 
|  | 83 | +        results[2].Key.Should().Be("C"); | 
|  | 84 | +        results[2].Value.Should().Be("c"); | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    [Fact] | 
|  | 88 | +    public void SelectMany_with_Document_representation_should_work() | 
|  | 89 | +    { | 
|  | 90 | +        var collection = Fixture.Collection; | 
|  | 91 | + | 
|  | 92 | +        var queryable = collection.AsQueryable() | 
|  | 93 | +            .OfType<C>() | 
|  | 94 | +            .Where( to => to.Name == "TestName" ) | 
|  | 95 | +            .SelectMany( to => to.DictionaryWithDocumentRepresentation); | 
|  | 96 | + | 
|  | 97 | +        var stages = Translate(collection, queryable); | 
|  | 98 | +        AssertStages( | 
|  | 99 | +            stages, | 
|  | 100 | +            "{ $match : { Name : 'TestName' } }", | 
|  | 101 | +            "{ $project : { _v : { $objectToArray : '$DictionaryWithDocumentRepresentation' }, _id : 0 } }", | 
|  | 102 | +            "{ $unwind : '$_v' }"); | 
|  | 103 | + | 
|  | 104 | +        var results = queryable.ToList(); | 
|  | 105 | +        results.Count.Should().Be(3); | 
|  | 106 | +        results[0].Key.Should().Be("A"); | 
|  | 107 | +        results[0].Value.Should().Be("a"); | 
|  | 108 | +        results[1].Key.Should().Be("B"); | 
|  | 109 | +        results[1].Value.Should().Be("b"); | 
|  | 110 | +        results[2].Key.Should().Be("C"); | 
|  | 111 | +        results[2].Value.Should().Be("c"); | 
|  | 112 | +    } | 
|  | 113 | + | 
|  | 114 | +    public class C | 
|  | 115 | +    { | 
|  | 116 | +        public int Id {  get; set; } | 
|  | 117 | +        public string Name {get;set;} | 
|  | 118 | + | 
|  | 119 | +        [BsonDictionaryOptions( DictionaryRepresentation.ArrayOfArrays )] | 
|  | 120 | +        public Dictionary<string,string> DictionaryWithArrayOfArraysRepresentation { get; set; } = new Dictionary<string, string>(); | 
|  | 121 | + | 
|  | 122 | + | 
|  | 123 | +        [BsonDictionaryOptions( DictionaryRepresentation.ArrayOfDocuments )] | 
|  | 124 | +        public Dictionary<string,string> DictionaryWithArrayOfDocumentsRepresentation { get; set; } = new Dictionary<string, string>(); | 
|  | 125 | + | 
|  | 126 | +        [BsonDictionaryOptions( DictionaryRepresentation.Document )] | 
|  | 127 | +        public Dictionary<string,string> DictionaryWithDocumentRepresentation { get; set; } = new Dictionary<string, string>(); | 
|  | 128 | +    } | 
|  | 129 | + | 
|  | 130 | +    public sealed class ClassFixture : MongoCollectionFixture<C> | 
|  | 131 | +    { | 
|  | 132 | +        protected override IEnumerable<C> InitialData => | 
|  | 133 | +        [ | 
|  | 134 | +            new C | 
|  | 135 | +            { | 
|  | 136 | +                Id = 1, | 
|  | 137 | +                Name = "TestName", | 
|  | 138 | +                DictionaryWithArrayOfArraysRepresentation = new Dictionary<string, string> { { "A", "a" },  { "B", "b" }, {  "C", "c" } }, | 
|  | 139 | +                DictionaryWithArrayOfDocumentsRepresentation = new Dictionary<string, string> { { "A", "a" },  { "B", "b" }, {  "C", "c" } }, | 
|  | 140 | +                DictionaryWithDocumentRepresentation = new Dictionary<string, string> { { "A", "a" },  { "B", "b" }, {  "C", "c" } }, | 
|  | 141 | +            } | 
|  | 142 | +        ]; | 
|  | 143 | +    } | 
|  | 144 | +} | 
0 commit comments