Skip to content

Commit d0b8de5

Browse files
authored
CSHARP-1906: Test that case insensitive searches work in LINQ. (#840)
1 parent 2f9e964 commit d0b8de5

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 System.Text.RegularExpressions;
19+
using FluentAssertions;
20+
using MongoDB.Driver.Linq;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
24+
{
25+
public class CSharp1906Tests : Linq3IntegrationTest
26+
{
27+
[Fact]
28+
public void Using_ToLower_should_work()
29+
{
30+
var collection = CreateCollection();
31+
var lowerCaseValues = new[] { "abc", "def" }; // ensure all are lower case at compile time
32+
var queryable = collection.AsQueryable()
33+
.Where(c => lowerCaseValues.Contains(c.S.ToLower()));
34+
35+
var stages = Translate(collection, queryable);
36+
AssertStages(stages, "{ $match : { $expr : { $in : [{ $toLower : '$S' }, ['abc', 'def']] } } }");
37+
38+
var results = queryable.ToList().OrderBy(x => x.Id).ToList();
39+
results.Select(x => x.Id).Should().Equal(1, 2);
40+
}
41+
42+
[Fact]
43+
public void Using_regular_expression_should_work()
44+
{
45+
var collection = CreateCollection();
46+
var regularExpresssion = new StringOrRegularExpression[] { new Regex("ABC", RegexOptions.IgnoreCase), new Regex("DEF", RegexOptions.IgnoreCase) };
47+
var queryable = collection.AsQueryable()
48+
.Where(c => c.S.StringIn(regularExpresssion));
49+
50+
var stages = Translate(collection, queryable);
51+
AssertStages(stages, "{ $match : { S : { $in : [/ABC/i, /DEF/i] } } }");
52+
53+
var results = queryable.ToList().OrderBy(x => x.Id).ToList();
54+
results.Select(x => x.Id).Should().Equal(1, 2);
55+
}
56+
57+
private IMongoCollection<C> CreateCollection()
58+
{
59+
var collection = GetCollection<C>();
60+
61+
var documents = new[]
62+
{
63+
new C { Id = 1, S = "aBc" },
64+
new C { Id = 2, S = "dEf" },
65+
new C { Id = 3, S = "gHi" }
66+
};
67+
CreateCollection(collection, documents);
68+
69+
return collection;
70+
}
71+
72+
public class C
73+
{
74+
public int Id { get; set; }
75+
public string S { get; set; }
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)