|
| 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 FluentAssertions; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | +using MongoDB.Driver.Core.Misc; |
| 20 | +using MongoDB.Driver.Core.TestHelpers.XunitExtensions; |
| 21 | +using MongoDB.Driver.Linq; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 25 | +{ |
| 26 | + public class CSharp4524Tests : Linq3IntegrationTest |
| 27 | + { |
| 28 | + [Fact] |
| 29 | + public void Find_with_projection_using_LINQ2_should_work() |
| 30 | + { |
| 31 | + var collection = CreateCollection(LinqProvider.V2); |
| 32 | + var find = collection.Find("{}").Project(x => new SpawnData(x.StartDate, x.SpawnPeriod)); |
| 33 | + |
| 34 | + var results = find.ToList(); |
| 35 | + var projection = find.Options.Projection; |
| 36 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 37 | + var documentSerializer = serializerRegistry.GetSerializer<MyData>(); |
| 38 | + var renderedProjection = projection.Render(documentSerializer, serializerRegistry, LinqProvider.V2); |
| 39 | + renderedProjection.Document.Should().Be("{ SpawnPeriod : 1, StartDate : 1, _id : 0 }"); |
| 40 | + |
| 41 | + results.Should().HaveCount(1); |
| 42 | + results[0].Date.Should().Be(new DateTime(2023, 1, 2, 3, 4, 5, DateTimeKind.Utc)); |
| 43 | + results[0].Period.Should().Be(SpawnPeriod.LIVE); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void Find_with_projection_using_LINQ3_should_work() |
| 48 | + { |
| 49 | + RequireServer.Check().Supports(Feature.FindProjectionExpressions); |
| 50 | + var collection = CreateCollection(LinqProvider.V3); |
| 51 | + var find = collection.Find("{}").Project(x => new SpawnData(x.StartDate, x.SpawnPeriod)); |
| 52 | + |
| 53 | + var results = find.ToList(); |
| 54 | + |
| 55 | + var projection = find.Options.Projection; |
| 56 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 57 | + var documentSerializer = serializerRegistry.GetSerializer<MyData>(); |
| 58 | + var renderedProjection = projection.Render(documentSerializer, serializerRegistry, LinqProvider.V3); |
| 59 | + renderedProjection.Document.Should().Be("{ Date : '$StartDate', Period : '$SpawnPeriod', _id : 0 }"); |
| 60 | + |
| 61 | + results.Should().HaveCount(1); |
| 62 | + results[0].Date.Should().Be(new DateTime(2023, 1, 2, 3, 4, 5, DateTimeKind.Utc)); |
| 63 | + results[0].Period.Should().Be(SpawnPeriod.LIVE); |
| 64 | + } |
| 65 | + |
| 66 | + private IMongoCollection<MyData> CreateCollection(LinqProvider linqProvider) |
| 67 | + { |
| 68 | + var collection = GetCollection<MyData>("data", linqProvider); |
| 69 | + |
| 70 | + CreateCollection( |
| 71 | + collection, |
| 72 | + new MyData { Id = 1, StartDate = new DateTime(2023, 1, 2, 3, 4, 5, DateTimeKind.Utc), SpawnPeriod = SpawnPeriod.LIVE }); |
| 73 | + |
| 74 | + return collection; |
| 75 | + } |
| 76 | + |
| 77 | + public class MyData |
| 78 | + { |
| 79 | + public int Id { get; set; } |
| 80 | + public DateTime StartDate; |
| 81 | + public SpawnPeriod SpawnPeriod; |
| 82 | + } |
| 83 | + |
| 84 | + public enum SpawnPeriod { LIVE, MIDNIGHT, MORNING, EVENING } |
| 85 | + |
| 86 | + public struct SpawnData |
| 87 | + { |
| 88 | + public readonly DateTime Date; |
| 89 | + public readonly SpawnPeriod Period; |
| 90 | + |
| 91 | + public SpawnData(DateTime date, SpawnPeriod period) |
| 92 | + { |
| 93 | + // Normally there is more complex handling here, value-type semantics are important, there are custom comparison operators, etc. hence the point of this struct. |
| 94 | + Date = date; |
| 95 | + Period = period; |
| 96 | + } |
| 97 | + |
| 98 | + public bool Equals(SpawnData other) => Date == other.Date && Period == other.Period; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments