|
| 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.Linq; |
| 17 | +using FluentAssertions; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | +using MongoDB.Driver.Linq; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 23 | +{ |
| 24 | + public class CSharp4466Tests : Linq3IntegrationTest |
| 25 | + { |
| 26 | + [Fact] |
| 27 | + public void AppendStage_with_null_resultSerializer_should_work() |
| 28 | + { |
| 29 | + var collection = CreateCollection(); |
| 30 | + |
| 31 | + var queryable = |
| 32 | + collection.AsQueryable() |
| 33 | + .AppendStage<C, D>("{ $project : { X : '$_id', _id : 0 } }", resultSerializer: null); |
| 34 | + |
| 35 | + var stages = Translate(collection, queryable, out var outputSerializer); |
| 36 | + |
| 37 | + AssertStages(stages, "{ $project : { X : '$_id', _id : 0 } }"); |
| 38 | + outputSerializer.ValueType.Should().Be(typeof(D)); |
| 39 | + |
| 40 | + var results = queryable.ToList(); |
| 41 | + results.Select(x => x.X).Should().Equal(1, 2); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void AppendStage_with_resultSerializer_should_work() |
| 46 | + { |
| 47 | + var collection = CreateCollection(); |
| 48 | + var resultSerializer = BsonSerializer.LookupSerializer<D>(); |
| 49 | + |
| 50 | + var queryable = |
| 51 | + collection.AsQueryable() |
| 52 | + .AppendStage<C, D>("{ $project : { X : '$_id', _id : 0 } }", resultSerializer); |
| 53 | + |
| 54 | + var stages = Translate(collection, queryable, out var outputSerializer); |
| 55 | + |
| 56 | + AssertStages(stages, "{ $project : { X : '$_id', _id : 0 } }"); |
| 57 | + outputSerializer.Should().BeSameAs(resultSerializer); |
| 58 | + |
| 59 | + var results = queryable.ToList(); |
| 60 | + results.Select(x => x.X).Should().Equal(1, 2); |
| 61 | + } |
| 62 | + |
| 63 | + private IMongoCollection<C> CreateCollection() |
| 64 | + { |
| 65 | + var collection = GetCollection<C>("C"); |
| 66 | + |
| 67 | + CreateCollection( |
| 68 | + collection, |
| 69 | + new C { Id = 1 }, |
| 70 | + new C { Id = 2 }); |
| 71 | + |
| 72 | + return collection; |
| 73 | + } |
| 74 | + |
| 75 | + private class C |
| 76 | + { |
| 77 | + public int Id { get; set; } |
| 78 | + } |
| 79 | + |
| 80 | + private class D |
| 81 | + { |
| 82 | + public int X { get; set; } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments