|
| 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 MongoDB.Driver.Linq; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 21 | +{ |
| 22 | + public class CSharp2708Tests |
| 23 | + { |
| 24 | + [Fact] |
| 25 | + public void IsNullOrEmpty_in_aggregate_match_should_work() |
| 26 | + { |
| 27 | + var collection = GetCollection(); |
| 28 | + var subject = collection.Aggregate(); |
| 29 | + |
| 30 | + var aggregate = subject.Match(x => string.IsNullOrEmpty(x.S)); |
| 31 | + |
| 32 | + var stages = Linq3TestHelpers.Translate(collection, aggregate); |
| 33 | + var expectedStages = new[] |
| 34 | + { |
| 35 | + "{ $match : { S : { $in : [null, ''] } } }" |
| 36 | + }; |
| 37 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void IsNullOrEmpty_with_null_coalescing_operator_in_aggregate_match_should_work() |
| 42 | + { |
| 43 | + var collection = GetCollection(); |
| 44 | + var subject = collection.Aggregate(); |
| 45 | + |
| 46 | + var aggregate = subject.Match(x => string.IsNullOrEmpty(x.S ?? "")); |
| 47 | + |
| 48 | + var stages = Linq3TestHelpers.Translate(collection, aggregate); |
| 49 | + var expectedStages = new[] |
| 50 | + { |
| 51 | + "{ $match : { $expr : { $in : [{ $ifNull : ['$S', ''] }, [null, '']] } } }" // requires use of $expr |
| 52 | + }; |
| 53 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void Null_coalescing_operator_in_aggregate_project_should_work() |
| 58 | + { |
| 59 | + var collection = GetCollection(); |
| 60 | + var subject = collection.Aggregate(); |
| 61 | + |
| 62 | + var aggregate = subject.Project(x => x.S ?? ""); |
| 63 | + |
| 64 | + var stages = Linq3TestHelpers.Translate(collection, aggregate); |
| 65 | + var expectedStages = new[] |
| 66 | + { |
| 67 | + "{ $project : { _v : { $ifNull : ['$S', ''] }, _id : 0 } }" |
| 68 | + }; |
| 69 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void Null_coalescing_operator_in_queryable_select_should_work() |
| 74 | + { |
| 75 | + var collection = GetCollection(); |
| 76 | + var subject = collection.AsQueryable(); |
| 77 | + |
| 78 | + var queryable = subject.Select(x => x.S ?? ""); |
| 79 | + |
| 80 | + var stages = Linq3TestHelpers.Translate(collection, queryable); |
| 81 | + var expectedStages = new[] |
| 82 | + { |
| 83 | + "{ $project : { _v : { $ifNull : ['$S', ''] }, _id : 0 } }" |
| 84 | + }; |
| 85 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void IsNullOrEmpty_in_queryable_where_should_work() |
| 90 | + { |
| 91 | + var collection = GetCollection(); |
| 92 | + var subject = collection.AsQueryable(); |
| 93 | + |
| 94 | + var queryable = subject.Where(x => string.IsNullOrEmpty(x.S)); |
| 95 | + |
| 96 | + var stages = Linq3TestHelpers.Translate(collection, queryable); |
| 97 | + var expectedStages = new[] |
| 98 | + { |
| 99 | + "{ $match : { S : { $in : [null, ''] } } }" |
| 100 | + }; |
| 101 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void IsNullOrEmpty_with_null_coalescing_operator_in_queryable_where_should_work() |
| 106 | + { |
| 107 | + var collection = GetCollection(); |
| 108 | + var subject = collection.AsQueryable(); |
| 109 | + |
| 110 | + var queryable = subject.Where(x => string.IsNullOrEmpty(x.S ?? "")); |
| 111 | + |
| 112 | + var stages = Linq3TestHelpers.Translate(collection, queryable); |
| 113 | + var expectedStages = new[] |
| 114 | + { |
| 115 | + "{ $match : { $expr : { $in : [{ $ifNull : ['$S', ''] }, [null, '']] } } }" // requires use of $expr |
| 116 | + }; |
| 117 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 118 | + } |
| 119 | + |
| 120 | + private IMongoCollection<C> GetCollection() |
| 121 | + { |
| 122 | + var client = DriverTestConfiguration.Linq3Client; |
| 123 | + var database = client.GetDatabase("foo"); |
| 124 | + return database.GetCollection<C>("foo"); |
| 125 | + } |
| 126 | + |
| 127 | + private class C |
| 128 | + { |
| 129 | + public string S { get; set; } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments