|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using Dapper;
- using DotNetCore.CAP.SqlServer.Diagnostics;
- using Microsoft.Extensions.Logging;
- using Moq;
-
- namespace DotNetCore.CAP.SqlServer.Test
- {
- public abstract class DatabaseTestHost : IDisposable
- {
- protected ILogger<SqlServerStorage> Logger;
- protected CapOptions CapOptions;
- protected SqlServerOptions SqlSeverOptions;
- protected DiagnosticProcessorObserver DiagnosticProcessorObserver;
-
- public bool SqlObjectInstalled;
-
- protected DatabaseTestHost()
- {
- Logger = new Mock<ILogger<SqlServerStorage>>().Object;
- CapOptions = new Mock<CapOptions>().Object;
- SqlSeverOptions = new SqlServerOptions()
- {
- ConnectionString = ConnectionUtil.GetConnectionString()
- };
-
- DiagnosticProcessorObserver = new DiagnosticProcessorObserver(new Mock<IDispatcher>().Object);
-
- InitializeDatabase();
- }
-
- public void Dispose()
- {
- DeleteAllData();
- }
-
- private void InitializeDatabase()
- {
- var masterConn = ConnectionUtil.GetMasterConnectionString();
- var databaseName = ConnectionUtil.GetDatabaseName();
- using (var connection = ConnectionUtil.CreateConnection(masterConn))
- {
- connection.Execute($@"
- IF NOT EXISTS (SELECT * FROM sysdatabases WHERE name = N'{databaseName}')
- CREATE DATABASE [{databaseName}];");
- }
-
- new SqlServerStorage(Logger, CapOptions, SqlSeverOptions, DiagnosticProcessorObserver).InitializeAsync().GetAwaiter().GetResult();
- SqlObjectInstalled = true;
- }
-
-
- private void DeleteAllData()
- {
- var conn = ConnectionUtil.GetConnectionString();
- using (var connection = new SqlConnection(conn))
- {
- var commands = new[] {
- "DISABLE TRIGGER ALL ON ?",
- "ALTER TABLE ? NOCHECK CONSTRAINT ALL",
- "DELETE FROM ?",
- "ALTER TABLE ? CHECK CONSTRAINT ALL",
- "ENABLE TRIGGER ALL ON ?"
- };
-
- foreach (var command in commands)
- {
- connection.Execute(
- "sp_MSforeachtable",
- new { command1 = command },
- commandType: CommandType.StoredProcedure);
- }
- }
- }
- }
- }
|