|
| 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 FluentAssertions; |
| 17 | +using MongoDB.Bson.Serialization; |
| 18 | +using MongoDB.Driver.Linq; |
| 19 | +using MongoDB.TestHelpers.XunitExtensions; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira |
| 23 | +{ |
| 24 | + public class CSharp4742Tests : Linq3IntegrationTest |
| 25 | + { |
| 26 | + [Theory] |
| 27 | + [ParameterAttributeData] |
| 28 | + public void Find_with_identity_projection_should_work( |
| 29 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 30 | + { |
| 31 | + var collection = GetCollection(linqProvider); |
| 32 | + var projection = Builders<C>.Projection.Expression(x => x); |
| 33 | + |
| 34 | + var find = collection |
| 35 | + .Find("{}") |
| 36 | + .Project(projection); |
| 37 | + |
| 38 | + var renderedProjection = TranslateFindProjection(collection, find); |
| 39 | + renderedProjection.Should().BeNull(); |
| 40 | + |
| 41 | + var result = find.Single(); |
| 42 | + result.Id.Should().Be(1); |
| 43 | + result.X.Should().Be(2); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory] |
| 47 | + [ParameterAttributeData] |
| 48 | + public void Aggregate_Project_with_identity_projection_should_work( |
| 49 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 50 | + { |
| 51 | + var collection = GetCollection(linqProvider); |
| 52 | + var projection = Builders<C>.Projection.Expression(x => x); |
| 53 | + |
| 54 | + var aggregate = collection |
| 55 | + .Aggregate() |
| 56 | + .Project(projection); |
| 57 | + |
| 58 | + var stages = Translate(collection, aggregate); |
| 59 | + stages.Should().BeEmpty(); |
| 60 | + |
| 61 | + var result = aggregate.Single(); |
| 62 | + result.Id.Should().Be(1); |
| 63 | + result.X.Should().Be(2); |
| 64 | + } |
| 65 | + |
| 66 | + [Theory] |
| 67 | + [ParameterAttributeData] |
| 68 | + public void ExpressionProjectionDefinition_with_identity_projection_Render_should_work( |
| 69 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 70 | + { |
| 71 | + var collection = GetCollection(linqProvider); |
| 72 | + var projection = new ExpressionProjectionDefinition<C, C>(x => x, translationOptions: null); |
| 73 | + var sourceSerializer = collection.DocumentSerializer; |
| 74 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 75 | + |
| 76 | + var renderedProjection = projection.Render(sourceSerializer, serializerRegistry, linqProvider); |
| 77 | + |
| 78 | + renderedProjection.Document.Should().BeNull(); |
| 79 | + renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer); |
| 80 | + } |
| 81 | + |
| 82 | + [Theory] |
| 83 | + [ParameterAttributeData] |
| 84 | + public void ExpressionProjectionDefinition_with_identity_projection_RenderForFind_should_work( |
| 85 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 86 | + { |
| 87 | + var collection = GetCollection(linqProvider); |
| 88 | + var projection = new ExpressionProjectionDefinition<C, C>(x => x, translationOptions: null); |
| 89 | + var sourceSerializer = collection.DocumentSerializer; |
| 90 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 91 | + |
| 92 | + var renderedProjection = projection.RenderForFind(sourceSerializer, serializerRegistry, linqProvider); |
| 93 | + |
| 94 | + renderedProjection.Document.Should().BeNull(); |
| 95 | + if (linqProvider == LinqProvider.V2) |
| 96 | + { |
| 97 | + renderedProjection.ProjectionSerializer.ValueType.Should().Be(typeof(C)); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + [Theory] |
| 106 | + [ParameterAttributeData] |
| 107 | + public void FindExpressionProjectionDefinition_with_identity_projection_Render_should_work( |
| 108 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 109 | + { |
| 110 | + var collection = GetCollection(linqProvider); |
| 111 | + var projection = new FindExpressionProjectionDefinition<C, C>(x => x); |
| 112 | + var sourceSerializer = collection.DocumentSerializer; |
| 113 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 114 | + |
| 115 | + var renderedProjection = projection.Render(sourceSerializer, serializerRegistry, linqProvider); |
| 116 | + |
| 117 | + renderedProjection.Document.Should().BeNull(); |
| 118 | + if (linqProvider == LinqProvider.V2) |
| 119 | + { |
| 120 | + renderedProjection.ProjectionSerializer.ValueType.Should().Be(typeof(C)); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + [Theory] |
| 129 | + [ParameterAttributeData] |
| 130 | + public void FindExpressionProjectionDefinition_with_identity_projection_RenderForFind_should_work( |
| 131 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 132 | + { |
| 133 | + var collection = GetCollection(linqProvider); |
| 134 | + var projection = new FindExpressionProjectionDefinition<C, C>(x => x); |
| 135 | + var sourceSerializer = collection.DocumentSerializer; |
| 136 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 137 | + |
| 138 | + var renderedProjection = projection.RenderForFind(sourceSerializer, serializerRegistry, linqProvider); |
| 139 | + |
| 140 | + renderedProjection.Document.Should().BeNull(); |
| 141 | + if (linqProvider == LinqProvider.V2) |
| 142 | + { |
| 143 | + renderedProjection.ProjectionSerializer.ValueType.Should().Be(typeof(C)); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + renderedProjection.ProjectionSerializer.Should().BeSameAs(sourceSerializer); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private IMongoCollection<C> GetCollection(LinqProvider linqProvider) |
| 152 | + { |
| 153 | + var collection = GetCollection<C>("test", linqProvider); |
| 154 | + CreateCollection( |
| 155 | + collection, |
| 156 | + new C { Id = 1, X = 2 }); |
| 157 | + return collection; |
| 158 | + } |
| 159 | + |
| 160 | + private class C |
| 161 | + { |
| 162 | + public int Id { get; set; } |
| 163 | + public int X { get; set; } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments