|
| 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; |
| 20 | +using MongoDB.Driver.Linq; |
| 21 | +using MongoDB.TestHelpers.XunitExtensions; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 25 | +{ |
| 26 | + public class CSharp4457Tests : Linq3IntegrationTest |
| 27 | + { |
| 28 | + [Theory] |
| 29 | + [ParameterAttributeData] |
| 30 | + public void Filter_with_bool_field_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 31 | + { |
| 32 | + var collection = CreateCollection(linqProvider); |
| 33 | + var builder = Builders<C>.Filter; |
| 34 | + var filter = builder.Where(x => x.BoolField); |
| 35 | + |
| 36 | + var rendered = RenderFilter(filter, linqProvider); |
| 37 | + rendered.Should().Be("{ BoolField : true }"); |
| 38 | + |
| 39 | + var results = collection.FindSync(filter).ToList(); |
| 40 | + results.Select(x => x.Id).Should().Equal(1); |
| 41 | + } |
| 42 | + |
| 43 | + [Theory] |
| 44 | + [ParameterAttributeData] |
| 45 | + public void Filter_with_bool_property_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 46 | + { |
| 47 | + var collection = CreateCollection(linqProvider); |
| 48 | + var builder = Builders<C>.Filter; |
| 49 | + var filter = builder.Where(x => x.BoolProperty); |
| 50 | + |
| 51 | + var rendered = RenderFilter(filter, linqProvider); |
| 52 | + rendered.Should().Be("{ BoolProperty : true }"); |
| 53 | + |
| 54 | + var results = collection.FindSync(filter).ToList(); |
| 55 | + results.Select(x => x.Id).Should().Equal(1); |
| 56 | + } |
| 57 | + |
| 58 | + [Theory] |
| 59 | + [ParameterAttributeData] |
| 60 | + public void Filter_with_not_bool_field_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 61 | + { |
| 62 | + var collection = CreateCollection(linqProvider); |
| 63 | + var builder = Builders<C>.Filter; |
| 64 | + var filter = builder.Where(x => !x.BoolField); |
| 65 | + |
| 66 | + var rendered = RenderFilter(filter, linqProvider); |
| 67 | + rendered.Should().Be("{ BoolField : { $ne : true } }"); |
| 68 | + |
| 69 | + var results = collection.FindSync(filter).ToList(); |
| 70 | + results.Select(x => x.Id).Should().Equal(2); |
| 71 | + } |
| 72 | + |
| 73 | + [Theory] |
| 74 | + [ParameterAttributeData] |
| 75 | + public void Filter_with_not_bool_property_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 76 | + { |
| 77 | + var collection = CreateCollection(linqProvider); |
| 78 | + var builder = Builders<C>.Filter; |
| 79 | + var filter = builder.Where(x => !x.BoolProperty); |
| 80 | + |
| 81 | + var rendered = RenderFilter(filter, linqProvider); |
| 82 | + rendered.Should().Be("{ BoolProperty : { $ne : true } }"); |
| 83 | + |
| 84 | + var results = collection.FindSync(filter).ToList(); |
| 85 | + results.Select(x => x.Id).Should().Equal(2); |
| 86 | + } |
| 87 | + |
| 88 | + [Theory] |
| 89 | + [ParameterAttributeData] |
| 90 | + public void Where_with_bool_field_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 91 | + { |
| 92 | + var collection = CreateCollection(linqProvider); |
| 93 | + |
| 94 | + var queryable = |
| 95 | + collection.AsQueryable() |
| 96 | + .Where(x => x.BoolField); |
| 97 | + |
| 98 | + var stages = Translate(collection, queryable); |
| 99 | + AssertStages(stages, "{ $match : { BoolField : true } }"); |
| 100 | + |
| 101 | + var results = queryable.ToList(); |
| 102 | + results.Select(x => x.Id).Should().Equal(1); |
| 103 | + } |
| 104 | + |
| 105 | + [Theory] |
| 106 | + [ParameterAttributeData] |
| 107 | + public void Where_with_bool_property_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 108 | + { |
| 109 | + var collection = CreateCollection(linqProvider); |
| 110 | + |
| 111 | + var queryable = |
| 112 | + collection.AsQueryable() |
| 113 | + .Where(x => x.BoolProperty); |
| 114 | + |
| 115 | + var stages = Translate(collection, queryable); |
| 116 | + AssertStages(stages, "{ $match : { BoolProperty : true } }"); |
| 117 | + |
| 118 | + var results = queryable.ToList(); |
| 119 | + results.Select(x => x.Id).Should().Equal(1); |
| 120 | + } |
| 121 | + |
| 122 | + [Theory] |
| 123 | + [ParameterAttributeData] |
| 124 | + public void Where_with_not_bool_field_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 125 | + { |
| 126 | + var collection = CreateCollection(linqProvider); |
| 127 | + |
| 128 | + var queryable = |
| 129 | + collection.AsQueryable() |
| 130 | + .Where(x => !x.BoolField); |
| 131 | + |
| 132 | + var stages = Translate(collection, queryable); |
| 133 | + AssertStages(stages, "{ $match : { BoolField : { $ne : true } } }"); |
| 134 | + |
| 135 | + var results = queryable.ToList(); |
| 136 | + results.Select(x => x.Id).Should().Equal(2); |
| 137 | + } |
| 138 | + |
| 139 | + [Theory] |
| 140 | + [ParameterAttributeData] |
| 141 | + public void Where_with_not_bool_property_should_work([Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 142 | + { |
| 143 | + var collection = CreateCollection(linqProvider); |
| 144 | + |
| 145 | + var queryable = |
| 146 | + collection.AsQueryable() |
| 147 | + .Where(x => !x.BoolProperty); |
| 148 | + |
| 149 | + var stages = Translate(collection, queryable); |
| 150 | + AssertStages(stages, "{ $match : { BoolProperty : { $ne : true } } }"); |
| 151 | + |
| 152 | + var results = queryable.ToList(); |
| 153 | + results.Select(x => x.Id).Should().Equal(2); |
| 154 | + } |
| 155 | + |
| 156 | + private IMongoCollection<C> CreateCollection(LinqProvider linqProvider) |
| 157 | + { |
| 158 | + var collection = GetCollection<C>("C", linqProvider); |
| 159 | + |
| 160 | + CreateCollection( |
| 161 | + collection, |
| 162 | + new C { Id = 1, BoolField = true, BoolProperty = true }, |
| 163 | + new C { Id = 2, BoolField = false, BoolProperty = false }); |
| 164 | + |
| 165 | + return collection; |
| 166 | + } |
| 167 | + |
| 168 | + private BsonDocument RenderFilter<TDocument>(FilterDefinition<TDocument> filter, LinqProvider linqProvider) |
| 169 | + { |
| 170 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 171 | + var documentSerializer = serializerRegistry.GetSerializer<TDocument>(); |
| 172 | + return filter.Render(documentSerializer, serializerRegistry, linqProvider); |
| 173 | + } |
| 174 | + |
| 175 | + private class C |
| 176 | + { |
| 177 | + public bool BoolField; |
| 178 | + |
| 179 | + public int Id { get; set; } |
| 180 | + public bool BoolProperty { get; set; } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments