| 
 | 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 Nested__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 | +            .Select(to => to.DictionaryWithArrayOfArraysRepresentation.SelectMany(kvp => new KeyValuePair<string, string>[] { kvp }));  | 
 | 42 | + | 
 | 43 | +        var stages = Translate(collection, queryable);  | 
 | 44 | +        AssertStages(  | 
 | 45 | +            stages,  | 
 | 46 | +            "{ $match : { Name : 'TestName' } }",  | 
 | 47 | +            "{ $project : { _v : { $reduce : { input : { $map : { input : '$DictionaryWithArrayOfArraysRepresentation', as : 'kvp', in : ['$$kvp'] } }, initialValue : [], in : { $concatArrays : ['$$value', '$$this'] } } }, _id : 0 } }");  | 
 | 48 | + | 
 | 49 | +        var result = queryable.Single();  | 
 | 50 | +        result.Select(kvp => kvp.Key).Should().Equal("A", "B", "C");  | 
 | 51 | +    }  | 
 | 52 | + | 
 | 53 | +    [Fact]  | 
 | 54 | +    public void Nested__SelectMany_with_ArrayOfDocuments_representation_should_work()  | 
 | 55 | +    {  | 
 | 56 | +        var collection = Fixture.Collection;  | 
 | 57 | + | 
 | 58 | +        var queryable = collection.AsQueryable()  | 
 | 59 | +            .OfType<C>()  | 
 | 60 | +            .Where(to => to.Name == "TestName")  | 
 | 61 | +            .Select(to => to.DictionaryWithArrayOfDocumentsRepresentation.SelectMany(kvp => new KeyValuePair<string, string>[] { kvp }));  | 
 | 62 | + | 
 | 63 | +        var stages = Translate(collection, queryable);  | 
 | 64 | +        AssertStages(  | 
 | 65 | +            stages,  | 
 | 66 | +            "{ $match : { Name : 'TestName' } }",  | 
 | 67 | +            "{ $project : { _v : { $reduce : { input : { $map : { input : '$DictionaryWithArrayOfDocumentsRepresentation', as : 'kvp', in : ['$$kvp'] } }, initialValue : [], in : { $concatArrays : ['$$value', '$$this'] } } }, _id : 0 } }");  | 
 | 68 | + | 
 | 69 | +        var result = queryable.Single();  | 
 | 70 | +        result.Select(kvp => kvp.Key).Should().Equal("A", "B", "C");  | 
 | 71 | +    }  | 
 | 72 | + | 
 | 73 | +    [Fact]  | 
 | 74 | +    public void Nested_SelectMany_with_Document_representation_should_work()  | 
 | 75 | +    {  | 
 | 76 | +        var collection = Fixture.Collection;  | 
 | 77 | + | 
 | 78 | +        var queryable = collection.AsQueryable()  | 
 | 79 | +            .OfType<C>()  | 
 | 80 | +            .Where(to => to.Name == "TestName")  | 
 | 81 | +            .Select(to => to.DictionaryWithDocumentRepresentation.SelectMany(kvp => new KeyValuePair<string, string>[] { kvp }));  | 
 | 82 | + | 
 | 83 | +        var stages = Translate(collection, queryable);  | 
 | 84 | +        AssertStages(  | 
 | 85 | +            stages,  | 
 | 86 | +            "{ $match : { Name : 'TestName' } }",  | 
 | 87 | +            "{ $project : { _v : { $reduce : { input : { $map : { input : { $objectToArray : '$DictionaryWithDocumentRepresentation' }, as : 'kvp', in : ['$$kvp'] } }, initialValue : [], in : { $concatArrays : ['$$value', '$$this'] } } }, _id : 0 } }");  | 
 | 88 | + | 
 | 89 | +        var result = queryable.Single();  | 
 | 90 | +        result.Select(kvp => kvp.Key).Should().Equal("A", "B", "C");  | 
 | 91 | +    }  | 
 | 92 | + | 
 | 93 | +    [Fact]  | 
 | 94 | +    public void Top_level_SelectMany_with_ArrayOfArrays_representation_should_work()  | 
 | 95 | +    {  | 
 | 96 | +        var collection = Fixture.Collection;  | 
 | 97 | + | 
 | 98 | +        var queryable = collection.AsQueryable()  | 
 | 99 | +            .OfType<C>()  | 
 | 100 | +            .Where(to => to.Name == "TestName")  | 
 | 101 | +            .SelectMany(to => to.DictionaryWithArrayOfArraysRepresentation);  | 
 | 102 | + | 
 | 103 | +        var stages = Translate(collection, queryable);  | 
 | 104 | +        AssertStages(  | 
 | 105 | +            stages,  | 
 | 106 | +            "{ $match : { Name : 'TestName' } }",  | 
 | 107 | +            "{ $project : { _v : '$DictionaryWithArrayOfArraysRepresentation', _id : 0 } }",  | 
 | 108 | +            "{ $unwind : '$_v' }");  | 
 | 109 | + | 
 | 110 | +        var results = queryable.ToList();  | 
 | 111 | +        results.Count.Should().Be(3);  | 
 | 112 | +        results[0].Key.Should().Be("A");  | 
 | 113 | +        results[0].Value.Should().Be("a");  | 
 | 114 | +        results[1].Key.Should().Be("B");  | 
 | 115 | +        results[1].Value.Should().Be("b");  | 
 | 116 | +        results[2].Key.Should().Be("C");  | 
 | 117 | +        results[2].Value.Should().Be("c");  | 
 | 118 | +    }  | 
 | 119 | + | 
 | 120 | +    [Fact]  | 
 | 121 | +    public void Top_level_SelectMany_with_ArrayOfDocuments_representation_should_work()  | 
 | 122 | +    {  | 
 | 123 | +        var collection = Fixture.Collection;  | 
 | 124 | + | 
 | 125 | +        var queryable = collection.AsQueryable()  | 
 | 126 | +            .OfType<C>()  | 
 | 127 | +            .Where(to => to.Name == "TestName")  | 
 | 128 | +            .SelectMany(to => to.DictionaryWithArrayOfDocumentsRepresentation);  | 
 | 129 | + | 
 | 130 | +        var stages = Translate(collection, queryable);  | 
 | 131 | +        AssertStages(  | 
 | 132 | +            stages,  | 
 | 133 | +            "{ $match : { Name : 'TestName' } }",  | 
 | 134 | +            "{ $project : { _v : '$DictionaryWithArrayOfDocumentsRepresentation', _id : 0 } }",  | 
 | 135 | +            "{ $unwind : '$_v' }");  | 
 | 136 | + | 
 | 137 | +        var results = queryable.ToList();  | 
 | 138 | +        results.Count.Should().Be(3);  | 
 | 139 | +        results[0].Key.Should().Be("A");  | 
 | 140 | +        results[0].Value.Should().Be("a");  | 
 | 141 | +        results[1].Key.Should().Be("B");  | 
 | 142 | +        results[1].Value.Should().Be("b");  | 
 | 143 | +        results[2].Key.Should().Be("C");  | 
 | 144 | +        results[2].Value.Should().Be("c");  | 
 | 145 | +    }  | 
 | 146 | + | 
 | 147 | +    [Fact]  | 
 | 148 | +    public void Top_level_SelectMany_with_Document_representation_should_work()  | 
 | 149 | +    {  | 
 | 150 | +        var collection = Fixture.Collection;  | 
 | 151 | + | 
 | 152 | +        var queryable = collection.AsQueryable()  | 
 | 153 | +            .OfType<C>()  | 
 | 154 | +            .Where(to => to.Name == "TestName")  | 
 | 155 | +            .SelectMany(to => to.DictionaryWithDocumentRepresentation);  | 
 | 156 | + | 
 | 157 | +        var stages = Translate(collection, queryable);  | 
 | 158 | +        AssertStages(  | 
 | 159 | +            stages,  | 
 | 160 | +            "{ $match : { Name : 'TestName' } }",  | 
 | 161 | +            "{ $project : { _v : { $objectToArray : '$DictionaryWithDocumentRepresentation' }, _id : 0 } }",  | 
 | 162 | +            "{ $unwind : '$_v' }");  | 
 | 163 | + | 
 | 164 | +        var results = queryable.ToList();  | 
 | 165 | +        results.Count.Should().Be(3);  | 
 | 166 | +        results[0].Key.Should().Be("A");  | 
 | 167 | +        results[0].Value.Should().Be("a");  | 
 | 168 | +        results[1].Key.Should().Be("B");  | 
 | 169 | +        results[1].Value.Should().Be("b");  | 
 | 170 | +        results[2].Key.Should().Be("C");  | 
 | 171 | +        results[2].Value.Should().Be("c");  | 
 | 172 | +    }  | 
 | 173 | + | 
 | 174 | +    public class C  | 
 | 175 | +    {  | 
 | 176 | +        public int Id {  get; set; }  | 
 | 177 | +        public string Name {get;set;}  | 
 | 178 | + | 
 | 179 | +        [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]  | 
 | 180 | +        public Dictionary<string,string> DictionaryWithArrayOfArraysRepresentation { get; set; } = new Dictionary<string, string>();  | 
 | 181 | + | 
 | 182 | +        [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]  | 
 | 183 | +        public Dictionary<string,string> DictionaryWithArrayOfDocumentsRepresentation { get; set; } = new Dictionary<string, string>();  | 
 | 184 | + | 
 | 185 | +        [BsonDictionaryOptions(DictionaryRepresentation.Document)]  | 
 | 186 | +        public Dictionary<string,string> DictionaryWithDocumentRepresentation { get; set; } = new Dictionary<string, string>();  | 
 | 187 | +    }  | 
 | 188 | + | 
 | 189 | +    public sealed class ClassFixture : MongoCollectionFixture<C>  | 
 | 190 | +    {  | 
 | 191 | +        protected override IEnumerable<C> InitialData =>  | 
 | 192 | +        [  | 
 | 193 | +            new C  | 
 | 194 | +            {  | 
 | 195 | +                Id = 1,  | 
 | 196 | +                Name = "TestName",  | 
 | 197 | +                DictionaryWithArrayOfArraysRepresentation = new Dictionary<string, string> { { "A", "a" },  { "B", "b" }, {  "C", "c" } },  | 
 | 198 | +                DictionaryWithArrayOfDocumentsRepresentation = new Dictionary<string, string> { { "A", "a" },  { "B", "b" }, {  "C", "c" } },  | 
 | 199 | +                DictionaryWithDocumentRepresentation = new Dictionary<string, string> { { "A", "a" },  { "B", "b" }, {  "C", "c" } },  | 
 | 200 | +            }  | 
 | 201 | +        ];  | 
 | 202 | +    }  | 
 | 203 | +}  | 
0 commit comments