|
| 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; |
| 19 | +using MongoDB.Bson.Serialization.Serializers; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira |
| 23 | +{ |
| 24 | + public class CSharp5427Tests : Linq3IntegrationTest |
| 25 | + { |
| 26 | + [Fact] |
| 27 | + public void Where_Mql_Exists_with_property_should_work() |
| 28 | + { |
| 29 | + var collection = GetCollection(); |
| 30 | + |
| 31 | + var queryable = collection.AsQueryable() |
| 32 | + .Where(x => Mql.Exists(x.X)); |
| 33 | + |
| 34 | + var stages = Translate(collection, queryable); |
| 35 | + AssertStages(stages, "{ $match : { X : { $exists : true } } }"); |
| 36 | + |
| 37 | + var results = queryable.ToList(); |
| 38 | + results.Select(x => x.Id).Should().Equal(2, 3); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void Where_Mql_Exists_with_shadow_property_should_work() |
| 43 | + { |
| 44 | + var collection = GetCollection(); |
| 45 | + |
| 46 | + var queryable = collection.AsQueryable() |
| 47 | + .Where(x => Mql.Exists(Mql.Field(x, "X", new NullableSerializer<int>(Int32Serializer.Instance)))); |
| 48 | + |
| 49 | + var stages = Translate(collection, queryable); |
| 50 | + AssertStages(stages, "{ $match : { X : { $exists : true } } }"); |
| 51 | + |
| 52 | + var results = queryable.ToList(); |
| 53 | + results.Select(x => x.Id).Should().Equal(2, 3); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void Where_Mql_IsMissing_with_property_should_work() |
| 58 | + { |
| 59 | + var collection = GetCollection(); |
| 60 | + |
| 61 | + var queryable = collection.AsQueryable() |
| 62 | + .Where(x => Mql.IsMissing(x.X)); |
| 63 | + |
| 64 | + var stages = Translate(collection, queryable); |
| 65 | + AssertStages(stages, "{ $match : { X : { $exists : false } } }"); |
| 66 | + |
| 67 | + var results = queryable.ToList(); |
| 68 | + results.Select(x => x.Id).Should().Equal(1); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void Where_Mql_IsMissing_with_shadow_property_should_work() |
| 73 | + { |
| 74 | + var collection = GetCollection(); |
| 75 | + |
| 76 | + var queryable = collection.AsQueryable() |
| 77 | + .Where(x => Mql.IsMissing(Mql.Field(x, "X", new NullableSerializer<int>(Int32Serializer.Instance)))); |
| 78 | + |
| 79 | + var stages = Translate(collection, queryable); |
| 80 | + AssertStages(stages, "{ $match : { X : { $exists : false } } }"); |
| 81 | + |
| 82 | + var results = queryable.ToList(); |
| 83 | + results.Select(x => x.Id).Should().Equal(1); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void Where_Mql_IsNullOrMissing_with_property_should_work() |
| 88 | + { |
| 89 | + var collection = GetCollection(); |
| 90 | + |
| 91 | + var queryable = collection.AsQueryable() |
| 92 | + .Where(x => Mql.IsNullOrMissing(x.X)); |
| 93 | + |
| 94 | + var stages = Translate(collection, queryable); |
| 95 | + AssertStages(stages, "{ $match : { X : null } }"); |
| 96 | + |
| 97 | + var results = queryable.ToList(); |
| 98 | + results.Select(x => x.Id).Should().Equal(1, 2); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void Where_Mql_IsNullOrMissing_with_shadow_property_should_work() |
| 103 | + { |
| 104 | + var collection = GetCollection(); |
| 105 | + |
| 106 | + var queryable = collection.AsQueryable() |
| 107 | + .Where(x => Mql.IsNullOrMissing(Mql.Field(x, "X", new NullableSerializer<int>(Int32Serializer.Instance)))); |
| 108 | + |
| 109 | + var stages = Translate(collection, queryable); |
| 110 | + AssertStages(stages, "{ $match : { X : null } }"); |
| 111 | + |
| 112 | + var results = queryable.ToList(); |
| 113 | + results.Select(x => x.Id).Should().Equal(1, 2); |
| 114 | + } |
| 115 | + |
| 116 | + private IMongoCollection<C> GetCollection() |
| 117 | + { |
| 118 | + var collection = GetCollection<C>("test"); |
| 119 | + CreateCollection( |
| 120 | + collection.Database.GetCollection<BsonDocument>("test"), |
| 121 | + BsonDocument.Parse("{ _id : 1 }"), |
| 122 | + BsonDocument.Parse("{ _id : 2, X : null }"), |
| 123 | + BsonDocument.Parse("{ _id : 3, X : 3 }")); |
| 124 | + return collection; |
| 125 | + } |
| 126 | + |
| 127 | + private class C |
| 128 | + { |
| 129 | + public int Id { get; set; } |
| 130 | + public int? X { get; set; } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments