From a896e1f2dba5718a42060e78dcc23b6e8f349036 Mon Sep 17 00:00:00 2001 From: Laurents Meyer Date: Wed, 16 Nov 2022 14:54:09 +0100 Subject: [PATCH] Simplify test store. --- .gitignore | 1 + .../CustomConvertersMySqlTest.cs | 2 +- .../MigrationsMySqlTest.cs | 6 +- .../MySqlNorthwindTestStoreFactory.cs | 5 +- .../TestUtilities/MySqlTestStore.cs | 63 +++---------------- 5 files changed, 16 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index 279700026..1ceed1dc6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ development.props QueryBaseline.txt +.docker/ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. diff --git a/test/EFCore.MySql.FunctionalTests/CustomConvertersMySqlTest.cs b/test/EFCore.MySql.FunctionalTests/CustomConvertersMySqlTest.cs index 92cdf6550..c49cdafde 100644 --- a/test/EFCore.MySql.FunctionalTests/CustomConvertersMySqlTest.cs +++ b/test/EFCore.MySql.FunctionalTests/CustomConvertersMySqlTest.cs @@ -434,7 +434,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con { base.OnModelCreating(modelBuilder, context); - var ciCollation = ((MySqlTestStore)TestStore).GetCaseInsensitiveUtf8Mb4Collation(); + var ciCollation = ((MySqlTestStore)TestStore).ServerVersion.Value.DefaultUtf8CiCollation; // Needed to make Can_insert_and_read_back_with_case_insensitive_string_key() work. modelBuilder.Entity() diff --git a/test/EFCore.MySql.FunctionalTests/MigrationsMySqlTest.cs b/test/EFCore.MySql.FunctionalTests/MigrationsMySqlTest.cs index 0dfe9f338..6d13f3295 100644 --- a/test/EFCore.MySql.FunctionalTests/MigrationsMySqlTest.cs +++ b/test/EFCore.MySql.FunctionalTests/MigrationsMySqlTest.cs @@ -1300,9 +1300,9 @@ public override Task Rename_table() protected virtual string DefaultCollation => ((MySqlTestStore)Fixture.TestStore).DatabaseCollation; protected override string NonDefaultCollation - => DefaultCollation == ((MySqlTestStore)Fixture.TestStore).GetCaseSensitiveUtf8Mb4Collation() - ? ((MySqlTestStore)Fixture.TestStore).GetCaseInsensitiveUtf8Mb4Collation() - : ((MySqlTestStore)Fixture.TestStore).GetCaseSensitiveUtf8Mb4Collation(); + => DefaultCollation == ((MySqlTestStore)Fixture.TestStore).ServerVersion.Value.DefaultUtf8CsCollation + ? ((MySqlTestStore)Fixture.TestStore).ServerVersion.Value.DefaultUtf8CiCollation + : ((MySqlTestStore)Fixture.TestStore).ServerVersion.Value.DefaultUtf8CsCollation; protected virtual string NonDefaultCollation2 => "utf8mb4_german2_ci"; diff --git a/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlNorthwindTestStoreFactory.cs b/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlNorthwindTestStoreFactory.cs index 2acc29651..1c7ac9cf4 100644 --- a/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlNorthwindTestStoreFactory.cs +++ b/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlNorthwindTestStoreFactory.cs @@ -1,12 +1,13 @@ using Microsoft.EntityFrameworkCore.TestUtilities; +using Pomelo.EntityFrameworkCore.MySql.Tests; namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities { public class MySqlNorthwindTestStoreFactory : MySqlTestStoreFactory { public static new MySqlNorthwindTestStoreFactory Instance => InstanceCi; - public static MySqlNorthwindTestStoreFactory InstanceCi { get; } = new MySqlNorthwindTestStoreFactory(databaseCollation: MySqlTestStore.ModernCiCollation); - public static MySqlNorthwindTestStoreFactory InstanceCs { get; } = new MySqlNorthwindTestStoreFactory(databaseCollation: MySqlTestStore.ModernCsCollation); + public static MySqlNorthwindTestStoreFactory InstanceCi { get; } = new MySqlNorthwindTestStoreFactory(databaseCollation: AppConfig.ServerVersion.DefaultUtf8CiCollation); + public static MySqlNorthwindTestStoreFactory InstanceCs { get; } = new MySqlNorthwindTestStoreFactory(databaseCollation: AppConfig.ServerVersion.DefaultUtf8CsCollation); public static new MySqlNorthwindTestStoreFactory NoBackslashEscapesInstance { get; } = new MySqlNorthwindTestStoreFactory(true); protected MySqlNorthwindTestStoreFactory(bool noBackslashEscapes = false, string databaseCollation = null) diff --git a/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlTestStore.cs b/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlTestStore.cs index 9a7c0d23f..0b8fe7a76 100644 --- a/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlTestStore.cs +++ b/test/EFCore.MySql.FunctionalTests/TestUtilities/MySqlTestStore.cs @@ -66,22 +66,20 @@ private MySqlTestStore( MySqlGuidFormat guidFormat = MySqlGuidFormat.Default) : base(name, shared) { - DatabaseCharSet = databaseCharSet ?? "utf8mb4"; - DatabaseCollation = databaseCollation ?? ModernCsCollation; _useConnectionString = useConnectionString; _noBackslashEscapes = noBackslashEscapes; + ConnectionString = CreateConnectionString(name, _noBackslashEscapes, guidFormat); + Connection = new MySqlConnection(ConnectionString); + ServerVersion = new Lazy(() => Microsoft.EntityFrameworkCore.ServerVersion.AutoDetect((MySqlConnection)Connection)); + DatabaseCharSet = databaseCharSet ?? "utf8mb4"; + DatabaseCollation = databaseCollation ?? ServerVersion.Value.DefaultUtf8CsCollation; if (scriptPath != null) { _scriptPath = Path.Combine( - Path.GetDirectoryName( - typeof(MySqlTestStore).GetTypeInfo() - .Assembly.Location), scriptPath); + Path.GetDirectoryName(typeof(MySqlTestStore).GetTypeInfo().Assembly.Location) ?? string.Empty, + scriptPath); } - - ConnectionString = CreateConnectionString(name, _noBackslashEscapes, guidFormat); - Connection = new MySqlConnection(ConnectionString); - ServerVersion = new Lazy(() => Microsoft.EntityFrameworkCore.ServerVersion.AutoDetect((MySqlConnection)Connection)); } public static string CreateConnectionString(string name, bool noBackslashEscapes = false, MySqlGuidFormat guidFormat = MySqlGuidFormat.Default) @@ -151,8 +149,6 @@ private bool CreateDatabase(Action clean) using var master = new MySqlConnection(CreateAdminConnectionString()); master.Open(); - SetupCurrentDatabaseCollation(master); - string databaseSetupSql; if (DatabaseExists(Name)) { @@ -217,7 +213,7 @@ public void ExecuteScript(string script) { foreach (var batch in new Regex(@"^/\*\s*GO\s*\*/", RegexOptions.IgnoreCase | RegexOptions.Multiline, TimeSpan.FromMilliseconds(1000.0)) - .Split(EnsureBackwardsCompatibleCollations(Connection, script)) + .Split(script) .Where(b => !string.IsNullOrEmpty(b))) { command.CommandText = batch; @@ -338,48 +334,5 @@ public virtual Task AppendToSqlModeAsync(string mode, MySqlConnection connection return command.ExecuteNonQueryAsync(); } - - public const string ModernCsCollation = "utf8mb4_0900_as_cs"; - public const string LegacyCsCollation = "utf8mb4_bin"; - public const string ModernCiCollation = "utf8mb4_0900_ai_ci"; - public const string LegacyCiCollation = "utf8mb4_general_ci"; - - private string EnsureBackwardsCompatibleCollations(DbConnection connection, string script) - { - if (GetCaseSensitiveUtf8Mb4Collation((MySqlConnection)connection) != ModernCsCollation) - { - script = script.Replace(ModernCsCollation, LegacyCsCollation, StringComparison.OrdinalIgnoreCase); - } - - if (GetCaseInsensitiveUtf8Mb4Collation((MySqlConnection)connection) != ModernCiCollation) - { - script = script.Replace(ModernCiCollation, LegacyCiCollation, StringComparison.OrdinalIgnoreCase); - } - - return script; - } - - private void SetupCurrentDatabaseCollation(DbConnection connection) - => DatabaseCollation = EnsureBackwardsCompatibleCollations(connection, DatabaseCollation); - - public string GetCaseSensitiveUtf8Mb4Collation() - => ServerVersion.Value.Supports.DefaultCharSetUtf8Mb4 - ? ModernCsCollation - : LegacyCsCollation; - - public string GetCaseInsensitiveUtf8Mb4Collation() - => ServerVersion.Value.Supports.DefaultCharSetUtf8Mb4 - ? ModernCiCollation - : LegacyCiCollation; - - private string GetCaseSensitiveUtf8Mb4Collation(MySqlConnection connection) - => Microsoft.EntityFrameworkCore.ServerVersion.AutoDetect(connection).Supports.DefaultCharSetUtf8Mb4 - ? ModernCsCollation - : LegacyCsCollation; - - private string GetCaseInsensitiveUtf8Mb4Collation(MySqlConnection connection) - => Microsoft.EntityFrameworkCore.ServerVersion.AutoDetect(connection).Supports.DefaultCharSetUtf8Mb4 - ? ModernCiCollation - : LegacyCiCollation; } }