From 2811ba58f3144a1c264b81ff338a8c04c3b93b39 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 10 Dec 2019 21:13:44 +0800 Subject: [PATCH] Remove the database integration test --- .../ConnectionUtil.cs | 7 -- .../DatabaseTestHost.cs | 56 ----------- .../DotNetCore.CAP.MongoDB.Test.csproj | 25 ----- .../MongoDBMonitoringApiTest.cs | 73 -------------- .../MongoDBStorageConnectionTest.cs | 85 ---------------- .../MongoDBStorageTest.cs | 21 ---- .../MongoDBTransactionTest.cs | 76 -------------- .../ConnectionUtil.cs | 47 --------- .../DatabaseTestHost.cs | 66 ------------- .../DotNetCore.CAP.PostgreSql.Test.csproj | 24 ----- .../PostgreSqlStorageConnectionTest.cs | 98 ------------------ .../PostgreSqlStorageTest.cs | 44 --------- .../TestHost.cs | 99 ------------------- .../ConnectionUtil.cs | 47 --------- .../DatabaseTestHost.cs | 82 --------------- .../DotNetCore.CAP.SqlServer.Test.csproj | 33 ------- .../SqlServerStorageConnectionTest.cs | 94 ------------------ .../SqlServerStorageTest.cs | 43 -------- 18 files changed, 1020 deletions(-) delete mode 100644 test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs delete mode 100644 test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs delete mode 100644 test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj delete mode 100644 test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs delete mode 100644 test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs delete mode 100644 test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs delete mode 100644 test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs delete mode 100644 test/DotNetCore.CAP.PostgreSql.Test/ConnectionUtil.cs delete mode 100644 test/DotNetCore.CAP.PostgreSql.Test/DatabaseTestHost.cs delete mode 100644 test/DotNetCore.CAP.PostgreSql.Test/DotNetCore.CAP.PostgreSql.Test.csproj delete mode 100644 test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageConnectionTest.cs delete mode 100644 test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageTest.cs delete mode 100644 test/DotNetCore.CAP.PostgreSql.Test/TestHost.cs delete mode 100644 test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs delete mode 100644 test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs delete mode 100644 test/DotNetCore.CAP.SqlServer.Test/DotNetCore.CAP.SqlServer.Test.csproj delete mode 100644 test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs delete mode 100644 test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageTest.cs diff --git a/test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs b/test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs deleted file mode 100644 index da8de03..0000000 --- a/test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DotNetCore.CAP.MongoDB.Test -{ - public class ConnectionUtil - { - public static string ConnectionString = "mongodb://localhost:27017"; - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs deleted file mode 100644 index 582da3f..0000000 --- a/test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Threading; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using MongoDB.Driver; - -namespace DotNetCore.CAP.MongoDB.Test -{ - public abstract class DatabaseTestHost : IDisposable - { - private string _connectionString; - - protected IServiceProvider Provider { get; private set; } - protected IMongoClient MongoClient => Provider.GetService(); - protected IMongoDatabase Database => MongoClient.GetDatabase(MongoDBOptions.Value.DatabaseName); - protected CapOptions CapOptions => Provider.GetService>().Value; - protected IOptions MongoDBOptions => Provider.GetService>(); - - protected DatabaseTestHost() - { - CreateServiceCollection(); - CreateDatabase(); - } - - private void CreateDatabase() - { - Provider.GetService().InitializeAsync(CancellationToken.None).GetAwaiter().GetResult(); - } - - protected virtual void AddService(ServiceCollection serviceCollection) - { - - } - - private void CreateServiceCollection() - { - var services = new ServiceCollection(); - services.AddOptions(); - services.AddLogging(); - _connectionString = ConnectionUtil.ConnectionString; - services.AddOptions(); - services.Configure(x => x.DatabaseConnection = _connectionString); - - services.AddSingleton(x => new MongoClient(_connectionString)); - services.AddSingleton(); - - AddService(services); - Provider = services.BuildServiceProvider(); - } - - public void Dispose() - { - MongoClient.DropDatabase(MongoDBOptions.Value.DatabaseName); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj b/test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj deleted file mode 100644 index e15a979..0000000 --- a/test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - netcoreapp2.1 - false - - - - - - - - - - - all - runtime; build; native; contentfiles; analyzers - - - - - - - - diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs deleted file mode 100644 index 58a6969..0000000 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Linq; -using DotNetCore.CAP.Dashboard.Monitoring; -using DotNetCore.CAP.Infrastructure; -using DotNetCore.CAP.Messages; -using FluentAssertions; -using Xunit; - -namespace DotNetCore.CAP.MongoDB.Test -{ - [Collection("MongoDB")] - public class MongoDBMonitoringApiTest : DatabaseTestHost - { - private readonly MongoDBMonitoringApi _api; - - public MongoDBMonitoringApiTest() - { - _api = new MongoDBMonitoringApi(MongoClient, MongoDBOptions); - - var collection = Database.GetCollection(MongoDBOptions.Value.PublishedCollection); - collection.InsertMany(new[] - { - new PublishedMessage - { - Id = SnowflakeId.Default().NextId(), - Added = DateTime.Now.AddHours(-1), - StatusName = "Failed", - Version = "v1", - Content = "abc" - }, - new PublishedMessage - { - Id = SnowflakeId.Default().NextId(), - Added = DateTime.Now, - StatusName = "Failed", - Version = "v1", - Content = "bbc" - } - }); - } - - [Fact] - public void HourlyFailedJobs_Test() - { - var result = _api.HourlyFailedJobs(MessageType.Publish); - result.Should().HaveCount(24); - } - - [Fact] - public void Messages_Test() - { - var messages = - _api.Messages(new MessageQueryDto - { - MessageType = MessageType.Publish, - StatusName = StatusName.Failed, - Content = "b", - CurrentPage = 1, - PageSize = 1 - }); - - messages.Should().HaveCount(1); - messages.First().Content.Should().Contain("b"); - } - - [Fact] - public void PublishedFailedCount_Test() - { - var count = _api.PublishedFailedCount(); - count.Should().BeGreaterThan(1); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs deleted file mode 100644 index 37537f9..0000000 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using DotNetCore.CAP.Infrastructure; -using DotNetCore.CAP.Messages; -using FluentAssertions; -using Microsoft.Extensions.DependencyInjection; -using MongoDB.Driver; -using Xunit; - -namespace DotNetCore.CAP.MongoDB.Test -{ - [Collection("MongoDB")] - public class MongoDBStorageConnectionTest : DatabaseTestHost - { - private IStorageConnection _connection => - Provider.GetService().GetConnection(); - - [Fact] - public void StoreReceivedMessageAsync_TestAsync() - { - var messageContext = new MessageContext - { - Group = "test", - Name = "test", - Content = "test-content" - }; - - _connection.StoreReceivedMessage(new ReceivedMessage() - { - Id = SnowflakeId.Default().NextId(), - Group=messageContext.Group, - Content=messageContext.Content, - Name=messageContext.Name, - Version="v1" - }); - } - - [Fact] - public void ChangeReceivedState_Test() - { - StoreReceivedMessageAsync_TestAsync(); - var collection = Database.GetCollection(MongoDBOptions.Value.ReceivedCollection); - - var msg = collection.Find(x => true).FirstOrDefault(); - _connection.ChangeReceivedState(msg.Id, StatusName.Scheduled).Should().BeTrue(); - collection.Find(x => x.Id == msg.Id).FirstOrDefault()?.StatusName.Should().Be(StatusName.Scheduled); - } - - [Fact] - public async void GetReceivedMessagesOfNeedRetry_TestAsync() - { - var msgs = await _connection.GetReceivedMessagesOfNeedRetry(); - - msgs.Should().BeEmpty(); - - var id = SnowflakeId.Default().NextId(); - - var msg = new CapReceivedMessage - { - Id = id, - Group = "test", - Name = "test", - Content = "test-content", - StatusName = StatusName.Failed - }; - _connection.StoreReceivedMessage(msg); - - var collection = Database.GetCollection(MongoDBOptions.Value.ReceivedCollection); - - var updateDef = Builders - .Update.Set(x => x.Added, DateTime.Now.AddMinutes(-5)); - - await collection.UpdateOneAsync(x => x.Id == id, updateDef); - - msgs = await _connection.GetReceivedMessagesOfNeedRetry(); - msgs.Should().HaveCountGreaterThan(0); - } - - [Fact] - public void GetReceivedMessageAsync_Test() - { - var msg = _connection.GetReceivedMessageAsync(1); - msg.Should().NotBeNull(); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs deleted file mode 100644 index fb15cee..0000000 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs +++ /dev/null @@ -1,21 +0,0 @@ -using FluentAssertions; -using MongoDB.Driver; -using Xunit; - -namespace DotNetCore.CAP.MongoDB.Test -{ - [Collection("MongoDB")] - public class MongoDBStorageTest : DatabaseTestHost - { - [Fact] - public void InitializeAsync_Test() - { - var names = MongoClient.ListDatabaseNames()?.ToList(); - names.Should().Contain(MongoDBOptions.Value.DatabaseName); - - var collections = Database.ListCollectionNames()?.ToList(); - collections.Should().Contain(MongoDBOptions.Value.PublishedCollection); - collections.Should().Contain(MongoDBOptions.Value.ReceivedCollection); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs deleted file mode 100644 index dd7351a..0000000 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using FluentAssertions; -using MongoDB.Bson; -using MongoDB.Driver; -using Xunit; - -namespace DotNetCore.CAP.MongoDB.Test -{ - [Collection("MongoDB")] - public class MongoDBTransactionTest : DatabaseTestHost - { - [Fact] - public void MongoDB_Connection_Test() - { - var names = MongoClient.ListDatabaseNames(); - names.ToList().Should().NotBeNullOrEmpty(); - } - - [Fact(Skip = "Because of Appveyor dose not support MongoDB 4.0, so we skip this test for now.")] - public void Transaction_Test() - { - var document = new BsonDocument - { - { "name", "MongoDB" }, - { "type", "Database" }, - { "count", 1 }, - { "info", new BsonDocument - { - { "x", 203 }, - { "y", 102 } - }} - }; - var db = MongoClient.GetDatabase("test"); - var collection1 = db.GetCollection("test1"); - var collection2 = db.GetCollection("test2"); - using (var sesstion = MongoClient.StartSession()) - { - sesstion.StartTransaction(); - collection1.InsertOne(document); - collection2.InsertOne(document); - sesstion.CommitTransaction(); - } - var filter = new BsonDocument("name", "MongoDB"); - collection1.CountDocuments(filter).Should().BeGreaterThan(0); - collection2.CountDocuments(filter).Should().BeGreaterThan(0); - } - - [Fact(Skip = "Because of Appveyor dose not support MongoDB 4.0, so we skip this test for now.")] - public void Transaction_Rollback_Test() - { - var document = new BsonDocument - { - {"name", "MongoDB"}, - {"date", DateTimeOffset.Now.ToString()} - }; - var db = MongoClient.GetDatabase("test"); - - var collection = db.GetCollection("test3"); - var collection4 = db.GetCollection("test4"); - - using (var session = MongoClient.StartSession()) - { - session.IsInTransaction.Should().BeFalse(); - session.StartTransaction(); - session.IsInTransaction.Should().BeTrue(); - collection.InsertOne(session, document); - collection4.InsertOne(session, new BsonDocument { { "name", "MongoDB" } }); - - session.AbortTransaction(); - } - var filter = new BsonDocument("name", "MongoDB"); - collection.CountDocuments(filter).Should().Be(0); - collection4.CountDocuments(filter).Should().Be(0); - } - } -} diff --git a/test/DotNetCore.CAP.PostgreSql.Test/ConnectionUtil.cs b/test/DotNetCore.CAP.PostgreSql.Test/ConnectionUtil.cs deleted file mode 100644 index b293529..0000000 --- a/test/DotNetCore.CAP.PostgreSql.Test/ConnectionUtil.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using Npgsql; - -namespace DotNetCore.CAP.PostgreSql.Test -{ - public static class ConnectionUtil - { - private const string DatabaseVariable = "Cap_PostgreSql_DatabaseName"; - private const string ConnectionStringTemplateVariable = "Cap_PostgreSql_ConnectionStringTemplate"; - - private const string MasterDatabaseName = "postgres"; - private const string DefaultDatabaseName = @"DotNetCore.CAP.PostgreSql.Test"; - - private const string DefaultConnectionStringTemplate = - @"Server=localhost;Database={0};UserId=postgres;Password=123123;"; - - public static string GetDatabaseName() - { - return Environment.GetEnvironmentVariable(DatabaseVariable) ?? DefaultDatabaseName; - } - - public static string GetMasterConnectionString() - { - return string.Format(GetConnectionStringTemplate(), MasterDatabaseName); - } - - public static string GetConnectionString() - { - return string.Format(GetConnectionStringTemplate(), GetDatabaseName()); - } - - private static string GetConnectionStringTemplate() - { - return - Environment.GetEnvironmentVariable(ConnectionStringTemplateVariable) ?? - DefaultConnectionStringTemplate; - } - - public static NpgsqlConnection CreateConnection(string connectionString = null) - { - connectionString = connectionString ?? GetConnectionString(); - var connection = new NpgsqlConnection(connectionString); - connection.Open(); - return connection; - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.PostgreSql.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.PostgreSql.Test/DatabaseTestHost.cs deleted file mode 100644 index ec59a74..0000000 --- a/test/DotNetCore.CAP.PostgreSql.Test/DatabaseTestHost.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System.Threading; -using Dapper; -using Microsoft.EntityFrameworkCore; - -namespace DotNetCore.CAP.PostgreSql.Test -{ - public abstract class DatabaseTestHost : TestHost - { - private static bool _sqlObjectInstalled; - public static object _lock = new object(); - - protected override void PostBuildServices() - { - base.PostBuildServices(); - lock (_lock) - { - if (!_sqlObjectInstalled) - { - InitializeDatabase(); - } - } - } - - public override void Dispose() - { - DeleteAllData(); - base.Dispose(); - } - - private void InitializeDatabase() - { - using (CreateScope()) - { - var storage = GetService(); - var token = new CancellationTokenSource().Token; - CreateDatabase(); - storage.InitializeAsync(token).GetAwaiter().GetResult(); - _sqlObjectInstalled = true; - } - } - - private void CreateDatabase() - { - var masterConn = ConnectionUtil.GetMasterConnectionString(); - var databaseName = ConnectionUtil.GetDatabaseName(); - using (var connection = ConnectionUtil.CreateConnection(masterConn)) - { - connection.Execute($@" -DROP DATABASE IF EXISTS ""{databaseName}""; -CREATE DATABASE ""{databaseName}"";"); - } - } - - private void DeleteAllData() - { - var conn = ConnectionUtil.GetConnectionString(); - - using (var connection = ConnectionUtil.CreateConnection(conn)) - { - connection.Execute($@" -TRUNCATE TABLE ""cap"".""published""; -TRUNCATE TABLE ""cap"".""received"";"); - } - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.PostgreSql.Test/DotNetCore.CAP.PostgreSql.Test.csproj b/test/DotNetCore.CAP.PostgreSql.Test/DotNetCore.CAP.PostgreSql.Test.csproj deleted file mode 100644 index 3fafd7c..0000000 --- a/test/DotNetCore.CAP.PostgreSql.Test/DotNetCore.CAP.PostgreSql.Test.csproj +++ /dev/null @@ -1,24 +0,0 @@ - - - - netcoreapp2.1 - false - - - - - - - - - all - runtime; build; native; contentfiles; analyzers - - - - - - - - - diff --git a/test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageConnectionTest.cs b/test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageConnectionTest.cs deleted file mode 100644 index 323f16d..0000000 --- a/test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageConnectionTest.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System; -using System.Threading.Tasks; -using Dapper; -using DotNetCore.CAP.Infrastructure; -using DotNetCore.CAP.Messages; -using Microsoft.Extensions.Options; -using Xunit; - -namespace DotNetCore.CAP.PostgreSql.Test -{ - [Collection("postgresql")] - public class PostgreSqlStorageConnectionTest : DatabaseTestHost - { - private PostgreSqlStorageConnection _storage; - - public PostgreSqlStorageConnectionTest() - { - var options = GetService>(); - var capOptions = GetService>(); - _storage = new PostgreSqlStorageConnection(options, capOptions); - } - - [Fact] - public async Task GetPublishedMessageAsync_Test() - { - var sql = @"INSERT INTO ""cap"".""published""(""Id"",""Version"",""Name"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") VALUES(@Id,'v1',@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; - var insertedId = SnowflakeId.Default().NextId(); - var publishMessage = new CapPublishedMessage - { - Id = insertedId, - Name = "PostgreSqlStorageConnectionTest", - Content = "", - StatusName = StatusName.Scheduled - }; - using (var connection = ConnectionUtil.CreateConnection()) - { - await connection.ExecuteAsync(sql, publishMessage); - } - var message = await _storage.GetPublishedMessageAsync(insertedId); - Assert.NotNull(message); - Assert.Equal("PostgreSqlStorageConnectionTest", message.Name); - Assert.Equal(StatusName.Scheduled, message.StatusName); - } - - [Fact] - public void StoreReceivedMessageAsync_Test() - { - var receivedMessage = new CapReceivedMessage - { - Id = SnowflakeId.Default().NextId(), - Name = "PostgreSqlStorageConnectionTest", - Content = "", - Group = "mygroup", - StatusName = StatusName.Scheduled - }; - - Exception exception = null; - try - { - _storage.StoreReceivedMessage(receivedMessage); - } - catch (Exception ex) - { - exception = ex; - } - Assert.Null(exception); - } - - [Fact] - public async Task GetReceivedMessageAsync_Test() - { - var sql = $@" - INSERT INTO ""cap"".""received""(""Id"",""Version"",""Name"",""Group"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") - VALUES(@Id,'v1',@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; - var insertedId = SnowflakeId.Default().NextId(); - var receivedMessage = new CapReceivedMessage - { - Id = insertedId, - Name = "PostgreSqlStorageConnectionTest", - Content = "", - Group = "mygroup", - StatusName = StatusName.Scheduled - }; - - using (var connection = ConnectionUtil.CreateConnection()) - { - await connection.ExecuteAsync(sql, receivedMessage); - } - - var message = await _storage.GetReceivedMessageAsync(insertedId); - - Assert.NotNull(message); - Assert.Equal(StatusName.Scheduled, message.StatusName); - Assert.Equal("PostgreSqlStorageConnectionTest", message.Name); - Assert.Equal("mygroup", message.Group); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageTest.cs b/test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageTest.cs deleted file mode 100644 index 4bf1d77..0000000 --- a/test/DotNetCore.CAP.PostgreSql.Test/PostgreSqlStorageTest.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Dapper; -using Xunit; - -namespace DotNetCore.CAP.PostgreSql.Test -{ - [Collection("postgresql")] - public class SqlServerStorageTest : DatabaseTestHost - { - private readonly string _masterDbConnectionString; - private readonly string _dbConnectionString; - - public SqlServerStorageTest() - { - _masterDbConnectionString = ConnectionUtil.GetMasterConnectionString(); - _dbConnectionString = ConnectionUtil.GetConnectionString(); - } - - [Fact] - public void Database_IsExists() - { - using (var connection = ConnectionUtil.CreateConnection(_masterDbConnectionString)) - { - var databaseName = ConnectionUtil.GetDatabaseName(); - var sql = $@"select * from pg_database where datname = '{databaseName}'"; - var result = connection.QueryFirstOrDefault(sql); - Assert.NotNull(result); - Assert.True(databaseName.Equals(result, System.StringComparison.CurrentCultureIgnoreCase)); - } - } - - [Theory] - [InlineData("cap.published")] - [InlineData("cap.received")] - public void DatabaseTable_IsExists(string tableName) - { - using (var connection = ConnectionUtil.CreateConnection(_dbConnectionString)) - { - var sql = $"SELECT to_regclass('{tableName}') is not null;"; - var result = connection.QueryFirstOrDefault(sql); - Assert.True(result); - } - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.PostgreSql.Test/TestHost.cs b/test/DotNetCore.CAP.PostgreSql.Test/TestHost.cs deleted file mode 100644 index 7c6df65..0000000 --- a/test/DotNetCore.CAP.PostgreSql.Test/TestHost.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using Microsoft.Extensions.DependencyInjection; - -namespace DotNetCore.CAP.PostgreSql.Test -{ - public abstract class TestHost : IDisposable - { - protected IServiceCollection _services; - protected string _connectionString; - private IServiceProvider _provider; - private IServiceProvider _scopedProvider; - - public TestHost() - { - CreateServiceCollection(); - PreBuildServices(); - BuildServices(); - PostBuildServices(); - } - - protected IServiceProvider Provider => _scopedProvider ?? _provider; - - private void CreateServiceCollection() - { - var services = new ServiceCollection(); - - services.AddOptions(); - services.AddLogging(); - - _connectionString = ConnectionUtil.GetConnectionString(); - - services.AddOptions(); - services.Configure(x => x.ConnectionString = _connectionString); - services.AddSingleton(); - - _services = services; - } - - protected virtual void PreBuildServices() - { - } - - private void BuildServices() - { - _provider = _services.BuildServiceProvider(); - } - - protected virtual void PostBuildServices() - { - } - - public IDisposable CreateScope() - { - var scope = CreateScope(_provider); - var loc = scope.ServiceProvider; - _scopedProvider = loc; - return new DelegateDisposable(() => - { - if (_scopedProvider == loc) - { - _scopedProvider = null; - } - scope.Dispose(); - }); - } - - public IServiceScope CreateScope(IServiceProvider provider) - { - var scope = provider.GetService().CreateScope(); - return scope; - } - - public T GetService() => Provider.GetService(); - - public T Ensure(ref T service) - where T : class - => service ?? (service = GetService()); - - public virtual void Dispose() - { - (_provider as IDisposable)?.Dispose(); - } - - private class DelegateDisposable : IDisposable - { - private Action _dispose; - - public DelegateDisposable(Action dispose) - { - _dispose = dispose; - } - - public void Dispose() - { - _dispose(); - } - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs b/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs deleted file mode 100644 index 93e45d1..0000000 --- a/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Data.SqlClient; - -namespace DotNetCore.CAP.SqlServer.Test -{ - public static class ConnectionUtil - { - private const string DatabaseVariable = "Cap_SqlServer_DatabaseName"; - private const string ConnectionStringTemplateVariable = "Cap_SqlServer_ConnectionStringTemplate"; - - private const string MasterDatabaseName = "master"; - private const string DefaultDatabaseName = @"DotNetCore.CAP.SqlServer.Test"; - - private const string DefaultConnectionStringTemplate = - @"Server=192.168.2.206;Initial Catalog={0};User Id=sa;Password=123123;MultipleActiveResultSets=True"; - - public static string GetDatabaseName() - { - return Environment.GetEnvironmentVariable(DatabaseVariable) ?? DefaultDatabaseName; - } - - public static string GetMasterConnectionString() - { - return string.Format(GetConnectionStringTemplate(), MasterDatabaseName); - } - - public static string GetConnectionString() - { - return string.Format(GetConnectionStringTemplate(), GetDatabaseName()); - } - - private static string GetConnectionStringTemplate() - { - return - Environment.GetEnvironmentVariable(ConnectionStringTemplateVariable) ?? - DefaultConnectionStringTemplate; - } - - public static SqlConnection CreateConnection(string connectionString = null) - { - connectionString = connectionString ?? GetConnectionString(); - var connection = new SqlConnection(connectionString); - connection.Open(); - return connection; - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs deleted file mode 100644 index 4e2e5d7..0000000 --- a/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Data; -using System.Data.SqlClient; -using Dapper; -using DotNetCore.CAP.SqlServer.Diagnostics; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using Moq; - -namespace DotNetCore.CAP.SqlServer.Test -{ - public abstract class DatabaseTestHost : IDisposable - { - protected ILogger Logger; - protected IOptions CapOptions; - protected IOptions SqlSeverOptions; - protected DiagnosticProcessorObserver DiagnosticProcessorObserver; - - public bool SqlObjectInstalled; - - protected DatabaseTestHost() - { - Logger = new Mock>().Object; - - var capOptions = new Mock>(); - capOptions.Setup(x => x.Value).Returns(new CapOptions()); - CapOptions = capOptions.Object; - - var options = new Mock>(); - options.Setup(x => x.Value).Returns(new SqlServerOptions { ConnectionString = ConnectionUtil.GetConnectionString() }); - SqlSeverOptions = options.Object; - - DiagnosticProcessorObserver = new DiagnosticProcessorObserver(new Mock().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); - } - } - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/DotNetCore.CAP.SqlServer.Test.csproj b/test/DotNetCore.CAP.SqlServer.Test/DotNetCore.CAP.SqlServer.Test.csproj deleted file mode 100644 index 9f954b0..0000000 --- a/test/DotNetCore.CAP.SqlServer.Test/DotNetCore.CAP.SqlServer.Test.csproj +++ /dev/null @@ -1,33 +0,0 @@ - - - - netcoreapp2.1 - false - - - - - - - - - - - - - all - runtime; build; native; contentfiles; analyzers - - - - - - - - - - - - - - diff --git a/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs b/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs deleted file mode 100644 index b019a18..0000000 --- a/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageConnectionTest.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.Threading.Tasks; -using Dapper; -using DotNetCore.CAP.Infrastructure; -using DotNetCore.CAP.Messages; -using Xunit; - -namespace DotNetCore.CAP.SqlServer.Test -{ - [Collection("sqlserver")] - public class SqlServerStorageConnectionTest : DatabaseTestHost - { - private readonly SqlServerStorageConnection _storage; - - public SqlServerStorageConnectionTest() - { - _storage = new SqlServerStorageConnection(SqlSeverOptions, CapOptions); - } - - [Fact] - public async Task GetPublishedMessageAsync_Test() - { - var sql = "INSERT INTO [Cap].[Published]([Id],[Version],[Name],[Content],[Retries],[Added],[ExpiresAt],[StatusName]) VALUES(@Id,'v1',@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; - var insertedId = SnowflakeId.Default().NextId(); - var publishMessage = new CapPublishedMessage - { - Id= insertedId, - Name = "SqlServerStorageConnectionTest", - Content = "", - StatusName = StatusName.Scheduled - }; - - using (var connection = ConnectionUtil.CreateConnection()) - { - await connection.ExecuteAsync(sql, publishMessage); - } - - var message = await _storage.GetPublishedMessageAsync(insertedId); - Assert.NotNull(message); - Assert.Equal("SqlServerStorageConnectionTest", message.Name); - Assert.Equal(StatusName.Scheduled, message.StatusName); - } - - [Fact] - public void StoreReceivedMessageAsync_Test() - { - var receivedMessage = new CapReceivedMessage - { - Name = "SqlServerStorageConnectionTest", - Content = "", - Group = "mygroup", - StatusName = StatusName.Scheduled - }; - - Exception exception = null; - try - { - _storage.StoreReceivedMessage(receivedMessage); - } - catch (Exception ex) - { - exception = ex; - } - Assert.Null(exception); - } - - [Fact] - public async Task GetReceivedMessageAsync_Test() - { - var sql = @"INSERT INTO [Cap].[Received]([Id],[Version],[Name],[Group],[Content],[Retries],[Added],[ExpiresAt],[StatusName]) VALUES(@Id,'v1',@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; - var insertedId = SnowflakeId.Default().NextId(); - var receivedMessage = new CapReceivedMessage - { - Id= insertedId, - Name = "SqlServerStorageConnectionTest", - Content = "", - Group = "mygroup", - StatusName = StatusName.Scheduled - }; - - using (var connection = ConnectionUtil.CreateConnection()) - { - await connection.ExecuteAsync(sql, receivedMessage); - } - - var message = await _storage.GetReceivedMessageAsync(insertedId); - - Assert.NotNull(message); - Assert.Equal(StatusName.Scheduled, message.StatusName); - Assert.Equal("SqlServerStorageConnectionTest", message.Name); - Assert.Equal("mygroup", message.Group); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageTest.cs b/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageTest.cs deleted file mode 100644 index 427ed30..0000000 --- a/test/DotNetCore.CAP.SqlServer.Test/SqlServerStorageTest.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Dapper; -using Xunit; - -namespace DotNetCore.CAP.SqlServer.Test -{ - [Collection("sqlserver")] - public class SqlServerStorageTest : DatabaseTestHost - { - [Fact] - public void Database_IsExists() - { - var master = ConnectionUtil.GetMasterConnectionString(); - using (var connection = ConnectionUtil.CreateConnection(master)) - { - var databaseName = ConnectionUtil.GetDatabaseName(); - var sql = $@" -IF EXISTS (SELECT * FROM sysdatabases WHERE name = N'{databaseName}') -SELECT 'True' -ELSE -SELECT 'False'"; - var result = connection.QueryFirst(sql); - Assert.True(result); - } - } - - [Theory] - [InlineData("[Cap].[Published]")] - [InlineData("[Cap].[Received]")] - public void DatabaseTable_IsExists(string tableName) - { - using (var connection = ConnectionUtil.CreateConnection()) - { - var sql = $@" -IF OBJECT_ID(N'{tableName}',N'U') IS NOT NULL -SELECT 'True' -ELSE -SELECT 'False'"; - var result = connection.QueryFirst(sql); - Assert.True(result); - } - } - } -} \ No newline at end of file