|
| 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 FluentAssertions; |
| 19 | +using MongoDB.Bson; |
| 20 | +using MongoDB.Bson.Serialization.Attributes; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 24 | +{ |
| 25 | + public class CSharp2107Tests : Linq3IntegrationTest |
| 26 | + { |
| 27 | + [Fact] |
| 28 | + public void Aggregate_Project_should_work() |
| 29 | + { |
| 30 | + var collection = CreateCollection(); |
| 31 | + |
| 32 | + var aggregate = collection.Aggregate() |
| 33 | + .Project(doc => new |
| 34 | + { |
| 35 | + UserIsCustomer = doc.Users.Where(user => user.Identity.IdentityType == IdentityType.Type1) |
| 36 | + }); |
| 37 | + |
| 38 | + var stages = Translate(collection, aggregate); |
| 39 | + AssertStages(stages, "{ $project : { UserIsCustomer : { $filter : { input : '$Users', as : 'user', cond : { $eq : ['$$user.Identity.IdentityType', 'Type1'] } } }, _id : 0 } }"); |
| 40 | + |
| 41 | + var results = aggregate.ToList(); |
| 42 | + results.Should().HaveCount(1); |
| 43 | + results[0].UserIsCustomer.Select(u => u.UserId).Should().Equal(1); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void Queryable_Select_should_work() |
| 48 | + { |
| 49 | + var collection = CreateCollection(); |
| 50 | + |
| 51 | + var queryable = collection.AsQueryable() |
| 52 | + .Select(doc => new |
| 53 | + { |
| 54 | + UserIsCustomer = doc.Users.Where(user => user.Identity.IdentityType == IdentityType.Type1) |
| 55 | + }); |
| 56 | + |
| 57 | + var stages = Translate(collection, queryable); |
| 58 | + AssertStages(stages, "{ $project : { UserIsCustomer : { $filter : { input : '$Users', as : 'user', cond : { $eq : ['$$user.Identity.IdentityType', 'Type1'] } } }, _id : 0 } }"); |
| 59 | + |
| 60 | + var results = queryable.ToList(); |
| 61 | + results.Should().HaveCount(1); |
| 62 | + results[0].UserIsCustomer.Select(u => u.UserId).Should().Equal(1); |
| 63 | + } |
| 64 | + |
| 65 | + private IMongoCollection<Customer> CreateCollection() |
| 66 | + { |
| 67 | + var collection = GetCollection<Customer>(); |
| 68 | + |
| 69 | + var documents = new[] |
| 70 | + { |
| 71 | + new Customer |
| 72 | + { |
| 73 | + Id = 1, |
| 74 | + Users = new[] |
| 75 | + { |
| 76 | + new User { UserId = 1, Identity = new Identity { IdentityType = IdentityType.Type1 } }, |
| 77 | + new User { UserId = 2, Identity = new Identity { IdentityType = IdentityType.Type2 } } |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + CreateCollection(collection, documents); |
| 82 | + |
| 83 | + return collection; |
| 84 | + } |
| 85 | + |
| 86 | + private class Customer |
| 87 | + { |
| 88 | + public int Id { get; set; } |
| 89 | + public IEnumerable<User> Users { get; set; } |
| 90 | + } |
| 91 | + |
| 92 | + private class User |
| 93 | + { |
| 94 | + public int UserId { get; set; } |
| 95 | + public Identity Identity { get; set; } |
| 96 | + } |
| 97 | + |
| 98 | + private class Identity |
| 99 | + { |
| 100 | + [BsonRepresentation(BsonType.String)] |
| 101 | + public IdentityType IdentityType { get; set; } |
| 102 | + } |
| 103 | + |
| 104 | + private enum IdentityType |
| 105 | + { |
| 106 | + Type1, |
| 107 | + Type2 |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments