CREATEPROCEDURE `usp_customers_update_v2` (IN customerName VARCHAR(255) CHARACTERSET utf8mb4) BEGIN UPDATE Customers SET Name = customerName WHERE Id =1; END
建立測試資料表
中文欄位需指定字元集: CHARACTER SET utf8mb4
1
CREATETABLE IF NOTEXISTS Customers( Id INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) CHARACTERSET utf8mb4);
建立測試資料庫
帳號:root 密碼:example
1
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=example -p 3306:3306 mysql:5.7.19 --default-authentication-plugin=mysql_native_password
// CustomerAdapterTests.cs using MyProject.Adapter.Base; using MyProject.Adapter.Common; using MyProject.Adapter.MySql; using MyProject.DataClass.DTO.MySql; using MyProject.DataClass.Logger; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging;
[SetUp] publicvoidSetup() { var builder = new ConfigurationBuilder() .SetBasePath(AppContext.BaseDirectory) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
var configuration = builder.Build(); _connectionString = configuration.GetConnectionString("DB_connection") ?? thrownew InvalidOperationException("Missing connection string in configuration");
_testDatabaseManager = new TestDatabaseManager(_connectionString); _testDatabaseManager.InitializeDatabase();
var serviceProvider = Substitute.For<IServiceProvider>(); serviceProvider.GetService(typeof(IConfiguration)).Returns(configuration); DBConfig.Configure(serviceProvider);
var extendLogger = new ExtendLogger(Substitute.For<ILogger<ExtendLogger>>()); serviceProvider.GetService(typeof(ExtendLogger)).Returns(extendLogger); PerformanceLogger.Configure(serviceProvider); }
privatevoidCreateProcedureCustomerUpdate() { ExecuteNonQuery(_connection, @"CREATE PROCEDURE `usp_customers_update` (IN customerName VARCHAR(255) CHARACTER SET utf8mb4) BEGIN UPDATE Customers SET Name = customerName WHERE Id = 1; END" ); }
privatevoidCreateTableCustomer() { ExecuteNonQuery(_connection, "CREATE TABLE IF NOT EXISTS Customers( Id INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) CHARACTER SET utf8mb4);"); ExecuteNonQuery(_connection, "INSERT INTO Customers (Name) VALUES ('Art Huang');"); }