|  | 
|  | 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; | 
|  | 17 | +using System.Collections.Generic; | 
|  | 18 | +using System.Linq; | 
|  | 19 | +using FluentAssertions; | 
|  | 20 | +using MongoDB.Bson; | 
|  | 21 | +using MongoDB.Bson.Serialization.Attributes; | 
|  | 22 | +using MongoDB.Driver.Linq; | 
|  | 23 | +using MongoDB.TestHelpers.XunitExtensions; | 
|  | 24 | +using Xunit; | 
|  | 25 | + | 
|  | 26 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira | 
|  | 27 | +{ | 
|  | 28 | +    public class CSharp5162Tests : Linq3IntegrationTest | 
|  | 29 | +    { | 
|  | 30 | +        [Theory] | 
|  | 31 | +        [ParameterAttributeData] | 
|  | 32 | +        public void Builders_Projection_Expression_with_camel_casing_should_work( | 
|  | 33 | +            [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) | 
|  | 34 | +        { | 
|  | 35 | +            var collection = GetCamelCollection(linqProvider); | 
|  | 36 | + | 
|  | 37 | +            var projection = Builders<CamelDocument>.Projection.Expression(x => new CamelDocument { Id = x.Id, Name = x.Name }); | 
|  | 38 | +            var aggregate = collection.Aggregate().Project(projection); | 
|  | 39 | + | 
|  | 40 | +            var stages = Translate(collection, aggregate); | 
|  | 41 | +            if (linqProvider == LinqProvider.V2) | 
|  | 42 | +            { | 
|  | 43 | +                AssertStages(stages, "{ $project : { _id : 1, name : 1 } }"); | 
|  | 44 | +            } | 
|  | 45 | +            else | 
|  | 46 | +            { | 
|  | 47 | +                AssertStages(stages, "{ $project : { _id : '$_id', name : '$name' } }"); | 
|  | 48 | +            } | 
|  | 49 | + | 
|  | 50 | +            var result = aggregate.ToList().Single(); | 
|  | 51 | +            result.Id.Should().Be(1); | 
|  | 52 | +            result.Name.Should().Be("John Doe"); | 
|  | 53 | +        } | 
|  | 54 | + | 
|  | 55 | +        [Theory] | 
|  | 56 | +        [ParameterAttributeData] | 
|  | 57 | +        public void Builders_Projection_Expression_with_pascal_casing_should_work( | 
|  | 58 | +            [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) | 
|  | 59 | +        { | 
|  | 60 | +            var collection = GetPascalCollection(linqProvider); | 
|  | 61 | + | 
|  | 62 | +            var projection = Builders<PascalDocument>.Projection.Expression(x => new PascalDocument { Id = x.Id, Name = x.Name }); | 
|  | 63 | +            var aggregate = collection.Aggregate().Project(projection); | 
|  | 64 | + | 
|  | 65 | +            var stages = Translate(collection, aggregate); | 
|  | 66 | +            if (linqProvider == LinqProvider.V2) | 
|  | 67 | +            { | 
|  | 68 | +                stages = NormalizeProjectFieldOrder(stages); | 
|  | 69 | +                AssertStages(stages, "{ $project : { _id : 1, Name : 1 } }"); | 
|  | 70 | +            } | 
|  | 71 | +            else | 
|  | 72 | +            { | 
|  | 73 | +                AssertStages(stages, "{ $project : { _id : '$_id', Name : '$Name' } }"); | 
|  | 74 | +            } | 
|  | 75 | + | 
|  | 76 | +            var result = aggregate.ToList().Single(); | 
|  | 77 | +            result.Id.Should().Be(1); | 
|  | 78 | +            result.Name.Should().Be("John Doe"); | 
|  | 79 | +        } | 
|  | 80 | + | 
|  | 81 | +        [Theory] | 
|  | 82 | +        [ParameterAttributeData] | 
|  | 83 | +        public void FindExpressionDefinition_with_camel_casing_should_work( | 
|  | 84 | +            [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) | 
|  | 85 | +        { | 
|  | 86 | +            var collection = GetCamelCollection(linqProvider); | 
|  | 87 | + | 
|  | 88 | +            var projection = new FindExpressionProjectionDefinition<CamelDocument, CamelDocument>(x => new CamelDocument { Id = x.Id, Name = x.Name }); | 
|  | 89 | +            var aggregate = collection.Aggregate().Project(projection); | 
|  | 90 | + | 
|  | 91 | +            var stages = Translate(collection, aggregate); | 
|  | 92 | +            if (linqProvider == LinqProvider.V2) | 
|  | 93 | +            { | 
|  | 94 | +                AssertStages(stages, "{ $project : { _id : 1, name : 1 } }"); | 
|  | 95 | +            } | 
|  | 96 | +            else | 
|  | 97 | +            { | 
|  | 98 | +                AssertStages(stages, "{ $project : { _id : '$_id', name : '$name' } }"); | 
|  | 99 | +            } | 
|  | 100 | + | 
|  | 101 | +            var result = aggregate.ToList().Single(); | 
|  | 102 | +            result.Id.Should().Be(1); | 
|  | 103 | +            result.Name.Should().Be("John Doe"); | 
|  | 104 | +        } | 
|  | 105 | + | 
|  | 106 | +        [Theory] | 
|  | 107 | +        [ParameterAttributeData] | 
|  | 108 | +        public void FindExpressionDefinition_with_pascal_casing_should_work( | 
|  | 109 | +            [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) | 
|  | 110 | +        { | 
|  | 111 | +            var collection = GetPascalCollection(linqProvider); | 
|  | 112 | + | 
|  | 113 | +            var projection = new FindExpressionProjectionDefinition<PascalDocument, PascalDocument>(x => new PascalDocument { Id = x.Id, Name = x.Name }); | 
|  | 114 | +            var aggregate = collection.Aggregate().Project(projection); | 
|  | 115 | + | 
|  | 116 | +            var stages = Translate(collection, aggregate); | 
|  | 117 | +            if (linqProvider == LinqProvider.V2) | 
|  | 118 | +            { | 
|  | 119 | +                stages = NormalizeProjectFieldOrder(stages); | 
|  | 120 | +                AssertStages(stages, "{ $project : { _id : 1, Name : 1 } }"); | 
|  | 121 | +            } | 
|  | 122 | +            else | 
|  | 123 | +            { | 
|  | 124 | +                AssertStages(stages, "{ $project : { _id : '$_id', Name : '$Name' } }"); | 
|  | 125 | +            } | 
|  | 126 | + | 
|  | 127 | +            var result = aggregate.ToList().Single(); | 
|  | 128 | +            result.Id.Should().Be(1); | 
|  | 129 | +            result.Name.Should().Be("John Doe"); | 
|  | 130 | +        } | 
|  | 131 | + | 
|  | 132 | +        private IMongoCollection<CamelDocument> GetCamelCollection(LinqProvider linqProvider) | 
|  | 133 | +        { | 
|  | 134 | +            var collection = GetCollection<CamelDocument>("test", linqProvider); | 
|  | 135 | +            var document = new CamelDocument { Id = 1, Name = "John Doe" }; | 
|  | 136 | +            CreateCollection(collection, document); | 
|  | 137 | +            return collection; | 
|  | 138 | +        } | 
|  | 139 | + | 
|  | 140 | +        private IMongoCollection<PascalDocument> GetPascalCollection(LinqProvider linqProvider) | 
|  | 141 | +        { | 
|  | 142 | +            var collection = GetCollection<PascalDocument>("test", linqProvider); | 
|  | 143 | +            var document = new PascalDocument { Id = 1, Name = "John Doe" }; | 
|  | 144 | +            CreateCollection(collection, document); | 
|  | 145 | +            return collection; | 
|  | 146 | +        } | 
|  | 147 | + | 
|  | 148 | +        private List<BsonDocument> NormalizeProjectFieldOrder(List<BsonDocument> stages) | 
|  | 149 | +        { | 
|  | 150 | +            if (stages.Count == 1 && | 
|  | 151 | +                stages[0] is BsonDocument projectStage && | 
|  | 152 | +                projectStage.ElementCount == 1 && | 
|  | 153 | +                projectStage.GetElement(0).Name == "$project" && | 
|  | 154 | +                projectStage[0] is BsonDocument projection && | 
|  | 155 | +                projection.ElementCount == 2 && | 
|  | 156 | +                projection.Names.SequenceEqual(["Name", "_id"])) | 
|  | 157 | +            { | 
|  | 158 | +                stages[0]["$project"] = new BsonDocument | 
|  | 159 | +                { | 
|  | 160 | +                    { "_id", projection["_id"] }, | 
|  | 161 | +                    { "Name", projection["Name"] } | 
|  | 162 | +                }; | 
|  | 163 | +            } | 
|  | 164 | + | 
|  | 165 | +            return stages; | 
|  | 166 | +        } | 
|  | 167 | + | 
|  | 168 | +        private class CamelDocument | 
|  | 169 | +        { | 
|  | 170 | +            public int Id { get; set; } | 
|  | 171 | +            [BsonElement("name")] public string Name { get; set; } | 
|  | 172 | +            [BsonElement("activeSince")] public DateTime ActiveSince { get; set; } | 
|  | 173 | +            [BsonElement("isActive")] public bool IsActive { get; set; } | 
|  | 174 | +        } | 
|  | 175 | + | 
|  | 176 | +        private class PascalDocument | 
|  | 177 | +        { | 
|  | 178 | +            public int Id { get; set; } | 
|  | 179 | +            public string Name { get; set; } | 
|  | 180 | +            public DateTime ActiveSince { get; set; } | 
|  | 181 | +            public bool IsActive { get; set; } | 
|  | 182 | +        } | 
|  | 183 | +    } | 
|  | 184 | +} | 
0 commit comments