|
| 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 MongoDB.Driver.Core.Misc; |
| 20 | +using MongoDB.Driver.Core.TestHelpers.XunitExtensions; |
| 21 | +using MongoDB.Driver.Linq; |
| 22 | +using MongoDB.TestHelpers.XunitExtensions; |
| 23 | +using Xunit; |
| 24 | + |
| 25 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators |
| 26 | +{ |
| 27 | + public class IsNullOrWhiteSpaceMethodToAggregationExpressionTranslatorTests : Linq3IntegrationTest |
| 28 | + { |
| 29 | + [Theory] |
| 30 | + [ParameterAttributeData] |
| 31 | + public void Project_IsNullOrWhiteSpace_using_anonymous_class_should_return_expected_results( |
| 32 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 33 | + { |
| 34 | + var collection = CreateCollection(linqProvider); |
| 35 | + |
| 36 | + var find = collection.Find("{}") |
| 37 | + .Project(x => new { R = string.IsNullOrWhiteSpace(x.S) }) |
| 38 | + .SortBy(x => x.Id); |
| 39 | + |
| 40 | + var projection = TranslateFindProjection(collection, find); |
| 41 | + if (linqProvider == LinqProvider.V2) |
| 42 | + { |
| 43 | + projection.Should().Be("{ S : 1, _id : 0 }"); // LINQ2 will execute part of the projection client side |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + RequireServer.Check().Supports(Feature.FindProjectionExpressions, Feature.TrimOperator); |
| 48 | + projection.Should().Be("{ R : { $or : [{ $eq : ['$S', null] }, { $eq : [{ $trim : { input : '$S' } }, ''] }] }, _id : 0 }"); |
| 49 | + } |
| 50 | + |
| 51 | + var results = find.ToList(); |
| 52 | + results.Select(x => x.R).Should().Equal(true, true, true, true, false); |
| 53 | + } |
| 54 | + |
| 55 | + [Theory] |
| 56 | + [ParameterAttributeData] |
| 57 | + public void Project_IsNullOrWhiteSpace_using_named_class_should_return_expected_results( |
| 58 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 59 | + { |
| 60 | + var collection = CreateCollection(linqProvider); |
| 61 | + |
| 62 | + var find = collection.Find("{}") |
| 63 | + .Project(x => new Result { R = string.IsNullOrWhiteSpace(x.S) }) |
| 64 | + .SortBy(x => x.Id); |
| 65 | + |
| 66 | + var projection = TranslateFindProjection(collection, find); |
| 67 | + if (linqProvider == LinqProvider.V2) |
| 68 | + { |
| 69 | + projection.Should().Be("{ S : 1, _id : 0 }"); // LINQ2 will execute part of the projection client side |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + RequireServer.Check().Supports(Feature.FindProjectionExpressions, Feature.TrimOperator); |
| 74 | + projection.Should().Be("{ R : { $or : [{ $eq : ['$S', null] }, { $eq : [{ $trim : { input : '$S' } }, ''] }] }, _id : 0 }"); |
| 75 | + } |
| 76 | + |
| 77 | + var results = find.ToList(); |
| 78 | + results.Select(x => x.R).Should().Equal(true, true, true, true, false); |
| 79 | + } |
| 80 | + |
| 81 | + [Theory] |
| 82 | + [ParameterAttributeData] |
| 83 | + public void Select_IsNullOrWhiteSpace_using_scalar_result_should_return_expected_results( |
| 84 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 85 | + { |
| 86 | + var collection = CreateCollection(linqProvider); |
| 87 | + |
| 88 | + var queryable = collection.AsQueryable() |
| 89 | + .OrderBy(x => x.Id) |
| 90 | + .Select(x => string.IsNullOrWhiteSpace(x.S)); |
| 91 | + |
| 92 | + if (linqProvider == LinqProvider.V2) |
| 93 | + { |
| 94 | + var exception = Record.Exception(() => Translate(collection, queryable)); |
| 95 | + exception.Should().BeOfType<NotSupportedException>(); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + RequireServer.Check().Supports(Feature.TrimOperator); |
| 100 | + var stages = Translate(collection, queryable); |
| 101 | + AssertStages( |
| 102 | + stages, |
| 103 | + "{ $sort : { _id : 1 } }", |
| 104 | + "{ $project : { _v : { $or : [{ $eq : ['$S', null] }, { $eq : [{ $trim : { input : '$S' } }, ''] }] }, _id : 0 } }"); |
| 105 | + |
| 106 | + var results = queryable.ToList(); |
| 107 | + results.Should().Equal(true, true, true, true, false); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + [Theory] |
| 112 | + [ParameterAttributeData] |
| 113 | + public void Select_IsNullOrWhiteSpace_using_anonymous_class_should_return_expected_results( |
| 114 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 115 | + { |
| 116 | + var collection = CreateCollection(linqProvider); |
| 117 | + |
| 118 | + var queryable = collection.AsQueryable() |
| 119 | + .OrderBy(x => x.Id) |
| 120 | + .Select(x => new { R = string.IsNullOrWhiteSpace(x.S) }); |
| 121 | + |
| 122 | + if (linqProvider == LinqProvider.V2) |
| 123 | + { |
| 124 | + var exception = Record.Exception(() => Translate(collection, queryable)); |
| 125 | + exception.Should().BeOfType<NotSupportedException>(); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + RequireServer.Check().Supports(Feature.TrimOperator); |
| 130 | + var stages = Translate(collection, queryable); |
| 131 | + AssertStages( |
| 132 | + stages, |
| 133 | + "{ $sort : { _id : 1 } }", |
| 134 | + "{ $project : { R : { $or : [{ $eq : ['$S', null] }, { $eq : [{ $trim : { input : '$S' } }, ''] }] }, _id : 0 } }"); |
| 135 | + |
| 136 | + var results = queryable.ToList(); |
| 137 | + results.Select(x => x.R).Should().Equal(true, true, true, true, false); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + [Theory] |
| 142 | + [ParameterAttributeData] |
| 143 | + public void Select_IsNullOrWhiteSpace_using_named_class_should_return_expected_results( |
| 144 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 145 | + { |
| 146 | + var collection = CreateCollection(linqProvider); |
| 147 | + |
| 148 | + var queryable = collection.AsQueryable() |
| 149 | + .OrderBy(x => x.Id) |
| 150 | + .Select(x => new Result { R = string.IsNullOrWhiteSpace(x.S) }); |
| 151 | + |
| 152 | + if (linqProvider == LinqProvider.V2) |
| 153 | + { |
| 154 | + var exception = Record.Exception(() => Translate(collection, queryable)); |
| 155 | + exception.Should().BeOfType<NotSupportedException>(); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + RequireServer.Check().Supports(Feature.TrimOperator); |
| 160 | + var stages = Translate(collection, queryable); |
| 161 | + AssertStages( |
| 162 | + stages, |
| 163 | + "{ $sort : { _id : 1 } }", |
| 164 | + "{ $project : { R : { $or : [{ $eq : ['$S', null] }, { $eq : [{ $trim : { input : '$S' } }, ''] }] }, _id : 0 } }"); |
| 165 | + |
| 166 | + var results = queryable.ToList(); |
| 167 | + results.Select(x => x.R).Should().Equal(true, true, true, true, false); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private IMongoCollection<C> CreateCollection(LinqProvider linqProvider) |
| 172 | + { |
| 173 | + var collection = GetCollection<C>(linqProvider: linqProvider); |
| 174 | + CreateCollection( |
| 175 | + collection, |
| 176 | + new C { Id = 1, S = null }, |
| 177 | + new C { Id = 2, S = "" }, |
| 178 | + new C { Id = 3, S = " " }, |
| 179 | + new C { Id = 4, S = " \t\r\n" }, |
| 180 | + new C { Id = 5, S = "abc" }); |
| 181 | + return collection; |
| 182 | + } |
| 183 | + |
| 184 | + public class C |
| 185 | + { |
| 186 | + public int Id { get; set; } |
| 187 | + public string S { get; set; } |
| 188 | + } |
| 189 | + |
| 190 | + public class Result |
| 191 | + { |
| 192 | + public bool R { get; set; } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments