|
| 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.Linq; |
| 18 | +using FluentAssertions; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 22 | +{ |
| 23 | + public class CSharp3614Tests : Linq3IntegrationTest |
| 24 | + { |
| 25 | + [Fact] |
| 26 | + public void Test() |
| 27 | + { |
| 28 | + var collection = CreateBooksCollection(); |
| 29 | + |
| 30 | + var queryable = collection.AsQueryable() |
| 31 | + .Select(x => new BookDto |
| 32 | + { |
| 33 | + Id = x.Id, |
| 34 | + PageCount = x.PageCount, |
| 35 | + Author = x.Author == null |
| 36 | + ? null |
| 37 | + : new AuthorDto |
| 38 | + { |
| 39 | + Id = x.Author.Id, |
| 40 | + Name = x.Author.Name |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + var stages = Translate(collection, queryable); |
| 45 | + AssertStages(stages, "{ $project : { _id : '$_id', PageCount : '$PageCount', Author : { $cond : { if : { $eq : ['$Author', null] }, then : null, else : { _id : '$Author._id', Name : '$Author.Name' } } } } }"); |
| 46 | + |
| 47 | + var results = queryable.ToList().OrderBy(r => r.Id).ToList(); |
| 48 | + results.Should().HaveCount(2); |
| 49 | + results[0].Should().BeOfType<BookDto>(); |
| 50 | + results[0].Id.Should().Be(1); |
| 51 | + results[0].PageCount.Should().Be(1); |
| 52 | + results[0].Author.Should().BeNull(); |
| 53 | + results[1].Should().BeOfType<BookDto>(); |
| 54 | + results[1].Id.Should().Be(2); |
| 55 | + results[1].PageCount.Should().Be(2); |
| 56 | + results[1].Author.Should().BeOfType<AuthorDto>(); |
| 57 | + results[1].Author.ShouldBeEquivalentTo(new AuthorDto { Id = 2, Name = "Two" }); |
| 58 | + } |
| 59 | + |
| 60 | + private IMongoCollection<Book> CreateBooksCollection() |
| 61 | + { |
| 62 | + var collection = GetCollection<Book>(); |
| 63 | + |
| 64 | + var documents = new[] |
| 65 | + { |
| 66 | + new Book { Id = 1, PageCount = 1, Author = null }, |
| 67 | + new Book { Id = 2, PageCount = 2, Author = new Author { Id = 2, Name = "Two" } } |
| 68 | + }; |
| 69 | + CreateCollection(collection, documents); |
| 70 | + |
| 71 | + return collection; |
| 72 | + } |
| 73 | + |
| 74 | + private class BookDto |
| 75 | + { |
| 76 | + public int Id { get; set; } |
| 77 | + public int PageCount { get; set; } |
| 78 | + public AuthorDto Author { get; set; } |
| 79 | + } |
| 80 | + |
| 81 | + private class AuthorDto |
| 82 | + { |
| 83 | + public int Id { get; set; } |
| 84 | + public string Name { get; set; } |
| 85 | + } |
| 86 | + |
| 87 | + private class Author : IEquatable<Author> |
| 88 | + { |
| 89 | + public int Id { get; set; } |
| 90 | + public string Name { get; set; } |
| 91 | + public bool Equals(Author other) => Id == other.Id && Name == other.Name; |
| 92 | + } |
| 93 | + |
| 94 | + private class Book |
| 95 | + { |
| 96 | + public int Id { get; set; } |
| 97 | + public int PageCount { get; set; } |
| 98 | + public Author Author { get; set; } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments