|
| 1 | +#region License |
| 2 | +// The PostgreSQL License |
| 3 | +// |
| 4 | +// Copyright (C) 2016 The Npgsql Development Team |
| 5 | +// |
| 6 | +// Permission to use, copy, modify, and distribute this software and its |
| 7 | +// documentation for any purpose, without fee, and without a written |
| 8 | +// agreement is hereby granted, provided that the above copyright notice |
| 9 | +// and this paragraph and the following two paragraphs appear in all copies. |
| 10 | +// |
| 11 | +// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY |
| 12 | +// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, |
| 13 | +// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS |
| 14 | +// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF |
| 15 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | +// |
| 17 | +// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
| 18 | +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 19 | +// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 20 | +// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS |
| 21 | +// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 22 | +#endregion |
| 23 | + |
| 24 | +using Npgsql; |
| 25 | +using NUnit.Framework; |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.Data.Common; |
| 29 | +using System.Data.Entity; |
| 30 | +using System.Linq; |
| 31 | +using System.Text; |
| 32 | +using System.ComponentModel.DataAnnotations.Schema; |
| 33 | +using System.Data.Entity.Core.Metadata.Edm; |
| 34 | +using System.Data.Entity.Core.Objects; |
| 35 | +using System.Data.Entity.Infrastructure; |
| 36 | +using NpgsqlTypes; |
| 37 | +using System.Text.RegularExpressions; |
| 38 | + |
| 39 | +namespace EntityFramework6.Npgsql.Tests |
| 40 | +{ |
| 41 | + class PatternMatchingTests : EntityFrameworkTestBase |
| 42 | + { |
| 43 | + [Test] |
| 44 | + [TestCase("blog", "blog", "BLOG", TestName = "Case-sensitive")] |
| 45 | + [TestCase("^blog$", "blog", "some \nblog\n name", TestName = "^ and $ match beginning and end")] |
| 46 | + [TestCase("some .* name", "some blog name", "some \n name", TestName = ". matches all except \\n")] |
| 47 | + [TestCase("some blog name", "some blog name", "someblogname", TestName = "Whitespace not ignored in pattern")] |
| 48 | + public void MatchRegex(string pattern, string matchingInput, string mismatchingInput) |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + using (var context = new BloggingContext(ConnectionString)) |
| 52 | + { |
| 53 | + context.Database.Log = Console.Out.WriteLine; |
| 54 | + |
| 55 | + context.Blogs.Add(new Blog() { Name = matchingInput }); |
| 56 | + context.Blogs.Add(new Blog() { Name = mismatchingInput }); |
| 57 | + context.SaveChanges(); |
| 58 | + } |
| 59 | + |
| 60 | + // Act |
| 61 | + // Ensure correctness of a test case |
| 62 | + var netMatchResult = Regex.IsMatch(matchingInput, pattern); |
| 63 | + var netMismatchResult = Regex.IsMatch(mismatchingInput, pattern); |
| 64 | + |
| 65 | + List<string> pgMatchResults; |
| 66 | + List<string> pgMismatchResults; |
| 67 | + List<string> pgMatchWithOptionsResults; |
| 68 | + List<string> pgMismatchWithOptionsResults; |
| 69 | + using (var context = new BloggingContext(ConnectionString)) |
| 70 | + { |
| 71 | + pgMatchResults = (from b in context.Blogs |
| 72 | + where NpgsqlTextFunctions.MatchRegex(b.Name, pattern) |
| 73 | + select b.Name).ToList(); |
| 74 | + |
| 75 | + pgMismatchResults = (from b in context.Blogs |
| 76 | + where !NpgsqlTextFunctions.MatchRegex(b.Name, pattern) |
| 77 | + select b.Name).ToList(); |
| 78 | + |
| 79 | + pgMatchWithOptionsResults = (from b in context.Blogs |
| 80 | + where NpgsqlTextFunctions.MatchRegex(b.Name, pattern, RegexOptions.None) |
| 81 | + select b.Name).ToList(); |
| 82 | + |
| 83 | + pgMismatchWithOptionsResults = (from b in context.Blogs |
| 84 | + where !NpgsqlTextFunctions.MatchRegex(b.Name, pattern, RegexOptions.None) |
| 85 | + select b.Name).ToList(); |
| 86 | + } |
| 87 | + |
| 88 | + // Assert |
| 89 | + Assert.That(netMatchResult, Is.True); |
| 90 | + Assert.That(netMismatchResult, Is.False); |
| 91 | + |
| 92 | + Assert.That(pgMatchResults.Count, Is.EqualTo(1)); |
| 93 | + Assert.That(pgMatchResults[0], Is.EqualTo(matchingInput)); |
| 94 | + Assert.That(pgMismatchResults.Count, Is.EqualTo(1)); |
| 95 | + Assert.That(pgMismatchResults[0], Is.EqualTo(mismatchingInput)); |
| 96 | + |
| 97 | + Assert.That(pgMatchWithOptionsResults.Count, Is.EqualTo(1)); |
| 98 | + Assert.That(pgMatchWithOptionsResults[0], Is.EqualTo(matchingInput)); |
| 99 | + Assert.That(pgMismatchWithOptionsResults.Count, Is.EqualTo(1)); |
| 100 | + Assert.That(pgMismatchWithOptionsResults[0], Is.EqualTo(mismatchingInput)); |
| 101 | + } |
| 102 | + |
| 103 | + [Test] |
| 104 | + [TestCase(RegexOptions.IgnoreCase, "some", "SOME", "placeholder", TestName = "IgnoreCase")] |
| 105 | + [TestCase(RegexOptions.IgnorePatternWhitespace, "s o m e", "some", "s o m e", TestName = "IgnorePatternWhitespace")] |
| 106 | + [TestCase(RegexOptions.Multiline, "^blog$", "some \nblog\n name", "placeholder", TestName = "Multiline")] |
| 107 | + [TestCase(RegexOptions.Singleline, "some .* name", "some \n name", "placeholder", TestName = "Singleline")] |
| 108 | + public void MatchRegexOptions(RegexOptions options, string pattern, string matchingInput, string mismatchingInput) |
| 109 | + { |
| 110 | + // Arrange |
| 111 | + using (var context = new BloggingContext(ConnectionString)) |
| 112 | + { |
| 113 | + context.Database.Log = Console.Out.WriteLine; |
| 114 | + |
| 115 | + context.Blogs.Add(new Blog() { Name = matchingInput }); |
| 116 | + context.Blogs.Add(new Blog() { Name = mismatchingInput }); |
| 117 | + context.SaveChanges(); |
| 118 | + } |
| 119 | + |
| 120 | + // Act |
| 121 | + // Ensure correctness of a test case |
| 122 | + var netMatchResult = Regex.IsMatch(matchingInput, pattern, options); |
| 123 | + var netMismatchResult = Regex.IsMatch(mismatchingInput, pattern, options); |
| 124 | + |
| 125 | + List<string> pgMatchResults; |
| 126 | + List<string> pgMismatchResults; |
| 127 | + using (var context = new BloggingContext(ConnectionString)) |
| 128 | + { |
| 129 | + pgMatchResults = (from b in context.Blogs |
| 130 | + where NpgsqlTextFunctions.MatchRegex(b.Name, pattern, options) |
| 131 | + select b.Name).ToList(); |
| 132 | + |
| 133 | + pgMismatchResults = (from b in context.Blogs |
| 134 | + where !NpgsqlTextFunctions.MatchRegex(b.Name, pattern, options) |
| 135 | + select b.Name).ToList(); |
| 136 | + } |
| 137 | + |
| 138 | + // Assert |
| 139 | + Assert.That(netMatchResult, Is.True); |
| 140 | + Assert.That(netMismatchResult, Is.False); |
| 141 | + |
| 142 | + Assert.That(pgMatchResults.Count, Is.EqualTo(1)); |
| 143 | + Assert.That(pgMatchResults[0], Is.EqualTo(matchingInput)); |
| 144 | + Assert.That(pgMismatchResults.Count, Is.EqualTo(1)); |
| 145 | + Assert.That(pgMismatchResults[0], Is.EqualTo(mismatchingInput)); |
| 146 | + } |
| 147 | + |
| 148 | + [Test] |
| 149 | + [TestCase(RegexOptions.RightToLeft)] |
| 150 | + [TestCase(RegexOptions.ECMAScript)] |
| 151 | + public void MatchRegex_NotSupportedOption(RegexOptions options) |
| 152 | + { |
| 153 | + using (var context = new BloggingContext(ConnectionString)) |
| 154 | + { |
| 155 | + Assert.That(() => |
| 156 | + { |
| 157 | + var results = (from b in context.Blogs |
| 158 | + where NpgsqlTextFunctions.MatchRegex(b.Name, "Some pattern", options) |
| 159 | + select b.Name).ToList(); |
| 160 | + }, Throws.InnerException.TypeOf<NotSupportedException>()); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments