Skip to content

Commit ffab9c6

Browse files
committed
CSHARP-2708: Verify that string.IsNullOrEmpty with or without null coalescing argument works in LINQ3.
1 parent 9e8e525 commit ffab9c6

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/LinqProviderAdapterV3.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ internal override BsonDocument TranslateExpressionToFilter<TDocument>(
117117
{
118118
var context = TranslationContext.Create(expression, documentSerializer);
119119
var filter = ExpressionToFilterTranslator.TranslateLambda(context, expression, documentSerializer);
120+
filter = AstSimplifier.SimplifyAndConvert(filter);
120121

121122
return filter.Render().AsBsonDocument;
122123
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)