|
| 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 FluentAssertions; |
| 18 | +using MongoDB.Driver.Linq; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 22 | +{ |
| 23 | + public class CSharp4090Tests : Linq3IntegrationTest |
| 24 | + { |
| 25 | + [Fact] |
| 26 | + public void String_starts_with_with_current_culture_ignore_case_should_work() |
| 27 | + { |
| 28 | + var collection = GetCollection<C>(); |
| 29 | + collection.Database.DropCollection(collection.CollectionNamespace.CollectionName); |
| 30 | + |
| 31 | + collection.InsertMany( |
| 32 | + new[] |
| 33 | + { |
| 34 | + new C { Id = 100, Text = "Apple-Orange-Banana", Match = "apple" }, |
| 35 | + new C { Id = 101, Text = "Apple-Kiwi-Pear", Match = "mango" } |
| 36 | + }); |
| 37 | + |
| 38 | + var find = collection.Find(x => x.Text.StartsWith(x.Match, StringComparison.CurrentCultureIgnoreCase)); |
| 39 | + |
| 40 | + var rendered = find.ToString(); |
| 41 | + rendered.Should().Be("find({ \"$expr\" : { \"$eq\" : [{ \"$indexOfCP\" : [{ \"$toLower\" : \"$Text\" }, { \"$toLower\" : \"$Match\" }] }, 0] } })"); |
| 42 | + |
| 43 | + var results = find.ToList(); |
| 44 | + results.Count.Should().Be(1); |
| 45 | + results[0].Id.Should().Be(100); |
| 46 | + } |
| 47 | + |
| 48 | + [Theory] |
| 49 | + [InlineData(StringComparison.InvariantCulture)] |
| 50 | + [InlineData(StringComparison.InvariantCultureIgnoreCase)] |
| 51 | + [InlineData(StringComparison.Ordinal)] |
| 52 | + [InlineData(StringComparison.OrdinalIgnoreCase)] |
| 53 | + public void Should_throw_not_supported_exception_for_starts_with_with_unsupported_string_comparison_type(StringComparison comparison) |
| 54 | + { |
| 55 | + var collection = GetCollection<C>(); |
| 56 | + |
| 57 | + var exception = Record.Exception(() => collection.Find(x => x.Text.StartsWith("orange", comparison)).ToList()); |
| 58 | + |
| 59 | + exception.Should().BeOfType<ExpressionNotSupportedException>(); |
| 60 | + } |
| 61 | + |
| 62 | +#if NETCOREAPP3_1_OR_GREATER |
| 63 | + [Fact] |
| 64 | + public void String_contains_with_current_culture_ignore_case_should_work() |
| 65 | + { |
| 66 | + var collection = GetCollection<C>(); |
| 67 | + collection.Database.DropCollection(collection.CollectionNamespace.CollectionName); |
| 68 | + |
| 69 | + collection.InsertMany( |
| 70 | + new[] |
| 71 | + { |
| 72 | + new C { Id = 100, Text = "Apple-Orange-Banana", Match = "orange" }, |
| 73 | + new C { Id = 101, Text = "Apple-Kiwi-Pear", Match = "mango" } |
| 74 | + }); |
| 75 | + |
| 76 | + var find = collection.Find(x => x.Text.Contains(x.Match, StringComparison.CurrentCultureIgnoreCase)); |
| 77 | + |
| 78 | + var rendered = find.ToString(); |
| 79 | + rendered.Should().Be("find({ \"$expr\" : { \"$gte\" : [{ \"$indexOfCP\" : [{ \"$toLower\" : \"$Text\" }, { \"$toLower\" : \"$Match\" }] }, 0] } })"); |
| 80 | + |
| 81 | + var results = find.ToList(); |
| 82 | + results.Count.Should().Be(1); |
| 83 | + results[0].Id.Should().Be(100); |
| 84 | + } |
| 85 | + |
| 86 | + [Theory] |
| 87 | + [InlineData(StringComparison.InvariantCulture)] |
| 88 | + [InlineData(StringComparison.InvariantCultureIgnoreCase)] |
| 89 | + [InlineData(StringComparison.Ordinal)] |
| 90 | + [InlineData(StringComparison.OrdinalIgnoreCase)] |
| 91 | + public void Should_throw_not_supported_exception_for_contains_with_unsupported_string_comparison_type(StringComparison comparison) |
| 92 | + { |
| 93 | + var collection = GetCollection<C>(); |
| 94 | + |
| 95 | + var exception = Record.Exception(() => collection.Find(x => x.Text.Contains("orange", comparison)).ToList()); |
| 96 | + |
| 97 | + exception.Should().BeOfType<ExpressionNotSupportedException>(); |
| 98 | + } |
| 99 | +#endif |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void String_ends_with_with_current_culture_ignore_case_should_work() |
| 103 | + { |
| 104 | + var collection = GetCollection<C>(); |
| 105 | + collection.Database.DropCollection(collection.CollectionNamespace.CollectionName); |
| 106 | + |
| 107 | + collection.InsertMany( |
| 108 | + new[] |
| 109 | + { |
| 110 | + new C { Id = 100, Text = "Apple-Orange-Banana", Match = "banana" }, |
| 111 | + new C { Id = 101, Text = "Apple-Kiwi-Pear", Match = "mango" } |
| 112 | + }); |
| 113 | + |
| 114 | + var find = collection.Find(x => x.Text.EndsWith(x.Match, StringComparison.CurrentCultureIgnoreCase)); |
| 115 | + |
| 116 | + var rendered = find.ToString(); |
| 117 | + rendered.Should().Be("find({ \"$expr\" : { \"$let\" : { \"vars\" : { \"string\" : { \"$toLower\" : \"$Text\" }, \"substring\" : { \"$toLower\" : \"$Match\" } }, \"in\" : { \"$gte\" : [{ \"$indexOfCP\" : [\"$$string\", \"$$substring\", { \"$subtract\" : [{ \"$strLenCP\" : \"$$string\" }, { \"$strLenCP\" : \"$$substring\" }] }] }, 0] } } } })"); |
| 118 | + |
| 119 | + var results = find.ToList(); |
| 120 | + results.Count.Should().Be(1); |
| 121 | + results[0].Id.Should().Be(100); |
| 122 | + } |
| 123 | + |
| 124 | + [Theory] |
| 125 | + [InlineData(StringComparison.InvariantCulture)] |
| 126 | + [InlineData(StringComparison.InvariantCultureIgnoreCase)] |
| 127 | + [InlineData(StringComparison.Ordinal)] |
| 128 | + [InlineData(StringComparison.OrdinalIgnoreCase)] |
| 129 | + public void Should_throw_not_supported_exception_for_ends_with_with_unsupported_string_comparison_type(StringComparison comparison) |
| 130 | + { |
| 131 | + var collection = GetCollection<C>(); |
| 132 | + |
| 133 | + var exception = Record.Exception(() => collection.Find(x => x.Text.EndsWith("orange", comparison)).ToList()); |
| 134 | + |
| 135 | + exception.Should().BeOfType<ExpressionNotSupportedException>(); |
| 136 | + } |
| 137 | + |
| 138 | + public class C |
| 139 | + { |
| 140 | + public int Id { get; set; } |
| 141 | + public string Text { get; set; } |
| 142 | + public string Match { get; set; } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments