|
| 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.Serializers; |
| 22 | +using MongoDB.Driver.Linq; |
| 23 | +using Xunit; |
| 24 | + |
| 25 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira; |
| 26 | + |
| 27 | +public class CSharp5587Tests : LinqIntegrationTest<CSharp5587Tests.ClassFixture> |
| 28 | +{ |
| 29 | + public CSharp5587Tests(ClassFixture fixture) |
| 30 | + : base(fixture) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void FindOneAndUpdate_should_use_correct_discriminator() |
| 36 | + { |
| 37 | + var collection = Fixture.Collection; |
| 38 | + |
| 39 | + var lion1 = new Lion { Id = 1, Name = "Lion1" }; |
| 40 | + var updateDefinition1 = Builders<Lion>.Update |
| 41 | + .SetOnInsert(l => l.Id, 1) |
| 42 | + .Set(l => l.Name, lion1.Name); |
| 43 | + collection.OfType<Lion>().FindOneAndUpdate( |
| 44 | + f => f.Name == lion1.Name, |
| 45 | + updateDefinition1, |
| 46 | + new FindOneAndUpdateOptions<Lion> { IsUpsert = true }); |
| 47 | + |
| 48 | + var result = collection.AsQueryable().As(BsonDocumentSerializer.Instance).Single(); |
| 49 | + result.Should().BeEquivalentTo( |
| 50 | + """ |
| 51 | + { |
| 52 | + _id : 1, |
| 53 | + _t : ["Animal", "Cat", "Lion"], |
| 54 | + Name : "Lion1" |
| 55 | + } |
| 56 | + """); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void UpdateOne_should_use_correct_discriminator() |
| 61 | + { |
| 62 | + var collection = Fixture.Collection; |
| 63 | + |
| 64 | + var lion2 = new Lion { Id = 2, Name = "Lion2" }; |
| 65 | + var updateDefinition2 = Builders<Lion>.Update |
| 66 | + .SetOnInsert(l => l.Id, lion2.Id) |
| 67 | + .Set(l => l.Name, lion2.Name); |
| 68 | + collection.OfType<Lion>().UpdateOne( |
| 69 | + f => f.Name == lion2.Name, |
| 70 | + updateDefinition2, |
| 71 | + new UpdateOptions<Lion> { IsUpsert = true }); |
| 72 | + |
| 73 | + var result = collection.AsQueryable().As(BsonDocumentSerializer.Instance).Single(); |
| 74 | + result.Should().BeEquivalentTo( |
| 75 | + """ |
| 76 | + { |
| 77 | + _id : 2, |
| 78 | + _t : ["Animal", "Cat", "Lion"], |
| 79 | + Name : "Lion2" |
| 80 | + } |
| 81 | + """); |
| 82 | + } |
| 83 | + |
| 84 | + [BsonDiscriminator(RootClass = true)] |
| 85 | + [BsonKnownTypes(typeof(Cat), typeof(Dog))] |
| 86 | + public class Animal |
| 87 | + { |
| 88 | + public int Id { get; set; } |
| 89 | + } |
| 90 | + |
| 91 | + [BsonKnownTypes(typeof(Lion), typeof(Tiger))] |
| 92 | + public class Cat : Animal |
| 93 | + { |
| 94 | + } |
| 95 | + |
| 96 | + public class Dog : Animal |
| 97 | + { |
| 98 | + } |
| 99 | + |
| 100 | + public class Lion : Cat |
| 101 | + { |
| 102 | + public string Name { get; set; } |
| 103 | + } |
| 104 | + public class Tiger : Cat |
| 105 | + { |
| 106 | + } |
| 107 | + |
| 108 | + public sealed class ClassFixture : MongoCollectionFixture<Animal> |
| 109 | + { |
| 110 | + public override bool InitializeDataBeforeEachTestCase => true; |
| 111 | + |
| 112 | + protected override IEnumerable<Animal> InitialData => null; |
| 113 | + } |
| 114 | +} |
0 commit comments