|
| 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.Attributes; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 22 | +{ |
| 23 | + public class CSharp1555Tests : Linq3IntegrationTest |
| 24 | + { |
| 25 | + [Fact] |
| 26 | + public void Queryable_should_work() |
| 27 | + { |
| 28 | + var collection = CreatePeopleCollection(); |
| 29 | + var queryable = collection.AsQueryable(); |
| 30 | + |
| 31 | + var stages = Translate(collection, queryable); |
| 32 | + AssertStages(stages, new string[0]); |
| 33 | + |
| 34 | + var result = queryable.ToList().Single(); |
| 35 | + result.ShouldBeEquivalentTo(new Person { Id = 1, Name = "A" }); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Select_new_Person_should_work() |
| 40 | + { |
| 41 | + var collection = CreatePeopleCollection(); |
| 42 | + var queryable = collection.AsQueryable() |
| 43 | + .Select(p => new Person { Id = p.Id, Name = p.Name }); |
| 44 | + |
| 45 | + var stages = Translate(collection, queryable); |
| 46 | + AssertStages(stages, "{ $project : { _id : '$_id', Name : '$Name' } }"); |
| 47 | + |
| 48 | + var result = queryable.ToList().Single(); |
| 49 | + result.ShouldBeEquivalentTo(new Person { Id = 1, Name = "A" }); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void Select_new_Person_without_Name_should_work() |
| 54 | + { |
| 55 | + var collection = CreatePeopleCollection(); |
| 56 | + var queryable = collection.AsQueryable() |
| 57 | + .Select(p => new Person { Id = p.Id }); |
| 58 | + |
| 59 | + var stages = Translate(collection, queryable); |
| 60 | + AssertStages(stages, "{ $project : { _id : '$_id' } }"); |
| 61 | + |
| 62 | + var result = queryable.ToList().Single(); |
| 63 | + result.ShouldBeEquivalentTo(new Person { Id = 1, Name = null }); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Select_new_Person_without_Id_should_work() |
| 68 | + { |
| 69 | + var collection = CreatePeopleCollection(); |
| 70 | + var queryable = collection.AsQueryable() |
| 71 | + .Select(p => new Person { Name = p.Name }); |
| 72 | + |
| 73 | + var stages = Translate(collection, queryable); |
| 74 | + AssertStages(stages, "{ $project : { Name : '$Name', _id : 0 } }"); |
| 75 | + |
| 76 | + var result = queryable.ToList().Single(); |
| 77 | + result.ShouldBeEquivalentTo(new Person { Id = 0, Name = "A" }); |
| 78 | + } |
| 79 | + |
| 80 | + private IMongoCollection<Person> CreatePeopleCollection() |
| 81 | + { |
| 82 | + var collection = GetCollection<Person>(); |
| 83 | + |
| 84 | + var documents = new[] |
| 85 | + { |
| 86 | + new Person { Id = 1, Name = "A" } |
| 87 | + }; |
| 88 | + CreateCollection(collection, documents); |
| 89 | + |
| 90 | + return collection; |
| 91 | + } |
| 92 | + |
| 93 | + private class Person |
| 94 | + { |
| 95 | + [BsonIgnoreIfNull] |
| 96 | + public int Id { get; set; } |
| 97 | + [BsonIgnoreIfNull] |
| 98 | + public string Name { get; set; } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments