Skip to content

Commit 5276c39

Browse files
committed
CSHARP-3865: LINQ3: Support ToLowerInvariant and ToUpperInvariant.
1 parent 36a96cb commit 5276c39

File tree

3 files changed

+266
-2
lines changed

3 files changed

+266
-2
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/StringExpressionToRegexFilterTranslator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ static StringExpressionToRegexFilterTranslator()
115115
__modifierMethods = new[]
116116
{
117117
StringMethod.ToLower,
118+
StringMethod.ToLowerInvariant,
118119
StringMethod.ToUpper,
120+
StringMethod.ToUpperInvariant,
119121
StringMethod.Trim,
120122
StringMethod.TrimEnd,
121123
StringMethod.TrimStart,
@@ -407,8 +409,8 @@ private static Modifiers TranslateModifier(Modifiers modifiers, MethodCallExpres
407409
{
408410
switch (modifierExpression.Method.Name)
409411
{
410-
case "ToLower": return TranslateToLower(modifiers, modifierExpression);
411-
case "ToUpper": return TranslateToUpper(modifiers, modifierExpression);
412+
case "ToLower": case "ToLowerInvariant": return TranslateToLower(modifiers, modifierExpression);
413+
case "ToUpper": case "ToUpperInvariant": return TranslateToUpper(modifiers, modifierExpression);
412414
case "Trim": return TranslateTrim(modifiers, modifierExpression);
413415
case "TrimEnd": return TranslateTrimEnd(modifiers, modifierExpression);
414416
case "TrimStart": return TranslateTrimStart(modifiers, modifierExpression);

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/MethodTranslators/EqualsMethodToFilterTranslator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ private static AstFilter Translate(TranslationContext context, Expression expres
6565
(fieldExpression, valueExpression) = (expression1, expression2);
6666
}
6767

68+
if (StringExpressionToRegexFilterTranslator.CanTranslateComparisonExpression(fieldExpression, AstComparisonFilterOperator.Eq, valueExpression))
69+
{
70+
return StringExpressionToRegexFilterTranslator.TranslateComparisonExpression(context, expression, fieldExpression, AstComparisonFilterOperator.Eq, valueExpression);
71+
}
72+
6873
var field = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression);
6974
var value = valueExpression.GetConstantValue<object>(containingExpression: expression);
7075
var serializedValue = SerializationHelper.SerializeValue(field.Serializer, value);
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
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 CSharp3865Tests
23+
{
24+
[Fact]
25+
public void Expression_using_ToLower_should_work()
26+
{
27+
var collection = GetCollection();
28+
29+
var queryable =
30+
from product in collection.AsQueryable()
31+
select new { Result = product.FriendlyName.ToLower() };
32+
33+
var stages = Linq3TestHelpers.Translate(collection, queryable);
34+
var expectedStages = new[]
35+
{
36+
"{ $project : { Result : { $toLower : '$FriendlyName' }, _id : 0 } }"
37+
};
38+
Linq3TestHelpers.AssertStages(stages, expectedStages);
39+
}
40+
41+
[Fact]
42+
public void Expression_using_ToLowerInvariant_should_work()
43+
{
44+
var collection = GetCollection();
45+
46+
var queryable =
47+
from product in collection.AsQueryable()
48+
select new { Result = product.FriendlyName.ToLowerInvariant() };
49+
50+
var stages = Linq3TestHelpers.Translate(collection, queryable);
51+
var expectedStages = new[]
52+
{
53+
"{ $project : { Result : { $toLower : '$FriendlyName' }, _id : 0 } }"
54+
};
55+
Linq3TestHelpers.AssertStages(stages, expectedStages);
56+
}
57+
58+
[Fact]
59+
public void Expression_using_ToUpper_should_work()
60+
{
61+
var collection = GetCollection();
62+
63+
var queryable =
64+
from product in collection.AsQueryable()
65+
select new { Result = product.FriendlyName.ToUpper() };
66+
67+
var stages = Linq3TestHelpers.Translate(collection, queryable);
68+
var expectedStages = new[]
69+
{
70+
"{ $project : { Result : { $toUpper : '$FriendlyName' }, _id : 0 } }"
71+
};
72+
Linq3TestHelpers.AssertStages(stages, expectedStages);
73+
}
74+
75+
[Fact]
76+
public void Expression_using_ToUpperInvariant_should_work()
77+
{
78+
var collection = GetCollection();
79+
80+
var queryable =
81+
from product in collection.AsQueryable()
82+
select new { Result = product.FriendlyName.ToUpperInvariant() };
83+
84+
var stages = Linq3TestHelpers.Translate(collection, queryable);
85+
var expectedStages = new[]
86+
{
87+
"{ $project : { Result : { $toUpper : '$FriendlyName' }, _id : 0 } }"
88+
};
89+
Linq3TestHelpers.AssertStages(stages, expectedStages);
90+
}
91+
92+
[Fact]
93+
public void Filter_using_ToLower_with_equality_operator_should_work()
94+
{
95+
var collection = GetCollection();
96+
var friendlyName = "Widget";
97+
98+
var queryable =
99+
from product in collection.AsQueryable()
100+
where product.FriendlyName.ToLower() == friendlyName.ToLower()
101+
select product;
102+
103+
var stages = Linq3TestHelpers.Translate(collection, queryable);
104+
var expectedStages = new[]
105+
{
106+
"{ $match : { FriendlyName : /^widget$/is } }"
107+
};
108+
Linq3TestHelpers.AssertStages(stages, expectedStages);
109+
}
110+
111+
[Fact]
112+
public void Filter_using_ToLower_with_Equals_should_work()
113+
{
114+
var collection = GetCollection();
115+
var friendlyName = "Widget";
116+
117+
var queryable =
118+
from product in collection.AsQueryable()
119+
where product.FriendlyName.ToLower().Equals(friendlyName.ToLower())
120+
select product;
121+
122+
var stages = Linq3TestHelpers.Translate(collection, queryable);
123+
var expectedStages = new[]
124+
{
125+
"{ $match : { FriendlyName : /^widget$/is } }"
126+
};
127+
Linq3TestHelpers.AssertStages(stages, expectedStages);
128+
}
129+
130+
[Fact]
131+
public void Filter_using_ToLowerInvariant_with_equality_operator_should_work()
132+
{
133+
var collection = GetCollection();
134+
var friendlyName = "Widget";
135+
136+
var queryable =
137+
from product in collection.AsQueryable()
138+
where product.FriendlyName.ToLowerInvariant() == friendlyName.ToLower()
139+
select product;
140+
141+
var stages = Linq3TestHelpers.Translate(collection, queryable);
142+
var expectedStages = new[]
143+
{
144+
"{ $match : { FriendlyName : /^widget$/is } }"
145+
};
146+
Linq3TestHelpers.AssertStages(stages, expectedStages);
147+
}
148+
149+
[Fact]
150+
public void Filter_using_ToLowerInvariant_with_Equals_should_work()
151+
{
152+
var collection = GetCollection();
153+
var friendlyName = "Widget";
154+
155+
var queryable =
156+
from product in collection.AsQueryable()
157+
where product.FriendlyName.ToLowerInvariant().Equals(friendlyName.ToLower())
158+
select product;
159+
160+
var stages = Linq3TestHelpers.Translate(collection, queryable);
161+
var expectedStages = new[]
162+
{
163+
"{ $match : { FriendlyName : /^widget$/is } }"
164+
};
165+
Linq3TestHelpers.AssertStages(stages, expectedStages);
166+
}
167+
168+
[Fact]
169+
public void Filter_using_ToUpper_with_equality_operator_should_work()
170+
{
171+
var collection = GetCollection();
172+
var friendlyName = "Widget";
173+
174+
var queryable =
175+
from product in collection.AsQueryable()
176+
where product.FriendlyName.ToUpper() == friendlyName.ToUpper()
177+
select product;
178+
179+
var stages = Linq3TestHelpers.Translate(collection, queryable);
180+
var expectedStages = new[]
181+
{
182+
"{ $match : { FriendlyName : /^WIDGET$/is } }"
183+
};
184+
Linq3TestHelpers.AssertStages(stages, expectedStages);
185+
}
186+
187+
[Fact]
188+
public void Filter_using_ToUpper_with_Equals_should_work()
189+
{
190+
var collection = GetCollection();
191+
var friendlyName = "Widget";
192+
193+
var queryable =
194+
from product in collection.AsQueryable()
195+
where product.FriendlyName.ToUpper().Equals(friendlyName.ToUpper())
196+
select product;
197+
198+
var stages = Linq3TestHelpers.Translate(collection, queryable);
199+
var expectedStages = new[]
200+
{
201+
"{ $match : { FriendlyName : /^WIDGET$/is } }"
202+
};
203+
Linq3TestHelpers.AssertStages(stages, expectedStages);
204+
}
205+
206+
[Fact]
207+
public void Filter_using_ToUpperInvariant_with_equality_operator_should_work()
208+
{
209+
var collection = GetCollection();
210+
var friendlyName = "Widget";
211+
212+
var queryable =
213+
from product in collection.AsQueryable()
214+
where product.FriendlyName.ToUpperInvariant() == friendlyName.ToUpper()
215+
select product;
216+
217+
var stages = Linq3TestHelpers.Translate(collection, queryable);
218+
var expectedStages = new[]
219+
{
220+
"{ $match : { FriendlyName : /^WIDGET$/is } }"
221+
};
222+
Linq3TestHelpers.AssertStages(stages, expectedStages);
223+
}
224+
225+
[Fact]
226+
public void ToUpperInvariant_with_Equals_should_work()
227+
{
228+
var collection = GetCollection();
229+
var friendlyName = "Widget";
230+
231+
var queryable =
232+
from product in collection.AsQueryable()
233+
where product.FriendlyName.ToUpperInvariant().Equals(friendlyName.ToUpper())
234+
select product;
235+
236+
var stages = Linq3TestHelpers.Translate(collection, queryable);
237+
var expectedStages = new[]
238+
{
239+
"{ $match : { FriendlyName : /^WIDGET$/is } }"
240+
};
241+
Linq3TestHelpers.AssertStages(stages, expectedStages);
242+
}
243+
244+
private IMongoCollection<Product> GetCollection()
245+
{
246+
var client = DriverTestConfiguration.Linq3Client;
247+
var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
248+
return database.GetCollection<Product>(DriverTestConfiguration.CollectionNamespace.CollectionName);
249+
}
250+
251+
private class Product
252+
{
253+
public int Id { get; set; }
254+
public string FriendlyName { get; set; }
255+
}
256+
}
257+
}

0 commit comments

Comments
 (0)