From 32e7452d4a30c05c81fc0dadd2d37e79f692d696 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Wed, 28 Jun 2017 18:27:04 +0800 Subject: [PATCH] Add database connection unit test using ef --- README.md | 3 + .../ConnectionUtil.cs | 85 ++++++----- .../DatabaseTestHost.cs | 7 +- .../DbUtil.cs | 27 ---- .../DefaultPocoTest.cs | 53 ------- ...etCore.CAP.EntityFrameworkCore.Test.csproj | 2 + .../EnsuranceTest.cs | 12 -- .../MessageStoreTest.cs | 93 ++++-------- .../TestHost.cs | 2 +- .../Utilities/ScratchDatabaseFixture.cs | 22 --- .../Utilities/SqlServerTestStore.cs | 139 ------------------ .../Utilities/TestEnvironment.cs | 20 --- .../config.json | 7 - 13 files changed, 81 insertions(+), 391 deletions(-) delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/DbUtil.cs delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/DefaultPocoTest.cs delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/EnsuranceTest.cs delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/ScratchDatabaseFixture.cs delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/SqlServerTestStore.cs delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/TestEnvironment.cs delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/config.json diff --git a/README.md b/README.md index 874ab32..a1d293c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +[![Build status](https://ci.appveyor.com/api/projects/status/4mpe0tbu7n126vyw?svg=true)](https://ci.appveyor.com/project/yuleyule66/cap) +[![Build status](https://ci.appveyor.com/api/projects/status/4mpe0tbu7n126vyw/branch/master?svg=true)](https://ci.appveyor.com/project/yuleyule66/cap/branch/master) + # cap A .net core middleware of eventually consistent in distributed architectures, now developing... diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/ConnectionUtil.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/ConnectionUtil.cs index 9a834ab..7f29500 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/ConnectionUtil.cs +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/ConnectionUtil.cs @@ -3,44 +3,49 @@ using System.Data.SqlClient; namespace DotNetCore.CAP.EntityFrameworkCore.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.EntityFrameworkCore.Test"; - - private const string DefaultConnectionStringTemplate = @"Server=.\sqlexpress;Database={0};Trusted_Connection=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; - } - } + 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.EntityFrameworkCore.Test"; + + //private const string DefaultConnectionStringTemplate = @"Server=.\sqlexpress;Database={0};Trusted_Connection=True;"; + 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() + { + //if (Environment.GetEnvironmentVariable("ASPNETCore_Environment") == "Development") + //{ + // return "Server=192.168.2.206;Initial Catalog=Test2;User Id=cmswuliu;Password=h7xY81agBn*Veiu3;MultipleActiveResultSets=True"; + //} + 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; + } + } } diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs index 9b69e25..be9182d 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs @@ -1,4 +1,5 @@ using System.Data; +using System.Threading.Tasks; using Dapper; using Microsoft.EntityFrameworkCore; @@ -26,10 +27,10 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test { using (CreateScope()) { - var context = GetService(); + var context = GetService(); context.Database.EnsureDeleted(); context.Database.Migrate(); - _sqlObjectInstalled = true; + _sqlObjectInstalled = true; } } } @@ -38,7 +39,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test { using (CreateScope()) { - var context = GetService(); + var context = GetService(); var commands = new[] { diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DbUtil.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DbUtil.cs deleted file mode 100644 index ec747db..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DbUtil.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public static class DbUtil - { - public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null) { - return ConfigureDbServices(connectionString, services); - } - - public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null) where TContext : DbContext { - if (services == null) { - services = new ServiceCollection(); - } - services.AddSingleton(); - services.AddDbContext(options => options.UseSqlServer(connectionString)); - return services; - } - - public static TContext Create(string connectionString) where TContext : DbContext { - var serviceProvider = ConfigureDbServices(connectionString).BuildServiceProvider(); - return serviceProvider.GetRequiredService(); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DefaultPocoTest.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DefaultPocoTest.cs deleted file mode 100644 index 146a452..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DefaultPocoTest.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Threading.Tasks; -using DotNetCore.CAP.Infrastructure; -using Microsoft.AspNetCore.Builder.Internal; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Xunit; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public class DefaultPocoTest : IClassFixture - { - private readonly ApplicationBuilder _builder; - - public DefaultPocoTest(ScratchDatabaseFixture fixture) - { - var services = new ServiceCollection(); - - services - .AddDbContext(o => o.UseSqlServer(fixture.ConnectionString)) - .AddConsistency() - .AddEntityFrameworkStores(); - - services.AddLogging(); - - var provider = services.BuildServiceProvider(); - _builder = new ApplicationBuilder(provider); - - using (var scoped = provider.GetRequiredService().CreateScope()) - using (var db = scoped.ServiceProvider.GetRequiredService()) - { - db.Database.EnsureCreated(); - } - } - - [Fact] - public async Task EnsureStartupUsageWorks() - { - var messageStore = _builder.ApplicationServices.GetRequiredService(); - var messageManager = _builder.ApplicationServices.GetRequiredService(); - - Assert.NotNull(messageStore); - Assert.NotNull(messageManager); - - var message = new CapSentMessage(); - - var operateResult = await messageManager.StoreSentMessageAsync(message); - Assert.True(operateResult.Succeeded); - - operateResult = await messageManager.RemoveSentMessageAsync(message); - Assert.True(operateResult.Succeeded); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DotNetCore.CAP.EntityFrameworkCore.Test.csproj b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DotNetCore.CAP.EntityFrameworkCore.Test.csproj index 7605c1a..8a35540 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DotNetCore.CAP.EntityFrameworkCore.Test.csproj +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DotNetCore.CAP.EntityFrameworkCore.Test.csproj @@ -37,10 +37,12 @@ + + diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/EnsuranceTest.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/EnsuranceTest.cs deleted file mode 100644 index 2b010cd..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/EnsuranceTest.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Xunit; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public class EnsuranceTest : DatabaseTestHost - { - [Fact] - public void Ensure() - { - } - } -} diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs index 002b2e2..f5a6831 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs @@ -1,73 +1,58 @@ using System; using System.Linq; -using System.Threading.Tasks; using DotNetCore.CAP.Infrastructure; -using DotNetCore.CAP.Test; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Xunit; namespace DotNetCore.CAP.EntityFrameworkCore.Test { - public class MessageStoreTest : MessageManagerTestBase, IClassFixture + public class MessageStoreTest : DatabaseTestHost { - private readonly ScratchDatabaseFixture _fixture; - - public MessageStoreTest(ScratchDatabaseFixture fixture) - { - _fixture = fixture; - } - - public class ApplicationDbContext : CapDbContext - { - public ApplicationDbContext(DbContextOptions options) : base(options) - { - } - } - [Fact] public void CanCreateSentMessageUsingEF() { using (var db = CreateContext()) { var guid = Guid.NewGuid().ToString(); - db.CapSentMessages.Add(new CapSentMessage + var message = new CapSentMessage { Id = guid, Content = "this is message body", StateName = StateName.Enqueued - }); + }; + db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; db.SaveChanges(); + Assert.True(db.CapSentMessages.Any(u => u.Id == guid)); Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StateName == StateName.Enqueued)); } } - [Fact] - public async Task CanCreateUsingManager() - { - var manager = CreateManager(); - var guid = Guid.NewGuid().ToString(); - var message = new CapSentMessage - { - Id = guid, - Content = "this is message body", - StateName = StateName.Enqueued, - }; + //[Fact] + //public async Task CanCreateUsingManager() + //{ + // var manager = CreateManager(); + // var guid = Guid.NewGuid().ToString(); + // var message = new CapSentMessage + // { + // Id = guid, + // Content = "this is message body", + // StateName = StateName.Enqueued, + // }; - var result = await manager.StoreSentMessageAsync(message); - Assert.NotNull(result); - Assert.True(result.Succeeded); + // var result = await manager.StoreSentMessageAsync(message); + // Assert.NotNull(result); + // Assert.True(result.Succeeded); - result = await manager.RemoveSentMessageAsync(message); - Assert.NotNull(result); - Assert.True(result.Succeeded); - } + // result = await manager.RemoveSentMessageAsync(message); + // Assert.NotNull(result); + // Assert.True(result.Succeeded); + //} - public CapDbContext CreateContext(bool delete = false) + public TestDbContext CreateContext(bool delete = false) { - var db = DbUtil.Create(_fixture.ConnectionString); + var db = Provider.GetRequiredService(); if (delete) { db.Database.EnsureDeleted(); @@ -75,31 +60,5 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test db.Database.EnsureCreated(); return db; } - - protected override object CreateTestContext() - { - return CreateContext(); - } - - protected override void AddMessageStore(IServiceCollection services, object context = null) - { - services.AddSingleton(new CapMessageStore((CapDbContext)context)); - } - - protected override CapSentMessage CreateTestSentMessage(string content = "") - { - return new CapSentMessage - { - Content = content - }; - } - - protected override CapReceivedMessage CreateTestReceivedMessage(string content = "") - { - return new CapReceivedMessage() - { - Content = content - }; - } - } + } } \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs index ba66382..f5b6e76 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs @@ -29,7 +29,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test var connectionString = ConnectionUtil.GetConnectionString(); //services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString }); - services.AddDbContext(options => options.UseSqlServer(connectionString)); + services.AddDbContext(options => options.UseSqlServer(connectionString)); _services = services; } diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/ScratchDatabaseFixture.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/ScratchDatabaseFixture.cs deleted file mode 100644 index ed832d2..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/ScratchDatabaseFixture.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Internal; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public class ScratchDatabaseFixture : IDisposable - { - private LazyRef _testStore; - - public ScratchDatabaseFixture() { - _testStore = new LazyRef(() => SqlServerTestStore.CreateScratch()); - } - - public string ConnectionString => _testStore.Value.Connection.ConnectionString; - - public void Dispose() { - if (_testStore.HasValue) { - _testStore.Value?.Dispose(); - } - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/SqlServerTestStore.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/SqlServerTestStore.cs deleted file mode 100644 index aee9e0e..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/SqlServerTestStore.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using System.Data.Common; -using System.Data.SqlClient; -using System.IO; -using System.Threading; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public class SqlServerTestStore : IDisposable - { - public const int CommandTimeout = 90; - - public static string CreateConnectionString(string name) { - var connStrBuilder = new SqlConnectionStringBuilder(TestEnvironment.Config["Test:SqlServer:DefaultConnectionString"]) { - InitialCatalog = name - }; - - return connStrBuilder.ConnectionString; - } - - public static SqlServerTestStore CreateScratch(bool createDatabase = true) - => new SqlServerTestStore(GetScratchDbName()).CreateTransient(createDatabase); - - private SqlConnection _connection; - private readonly string _name; - private bool _deleteDatabase; - - private SqlServerTestStore(string name) { - _name = name; - } - - private static string GetScratchDbName() { - string name; - do { - name = "Scratch_" + Guid.NewGuid(); - } while (DatabaseExists(name) - || DatabaseFilesExist(name)); - - return name; - } - - private static void WaitForExists(SqlConnection connection) { - var retryCount = 0; - while (true) { - try { - connection.Open(); - - connection.Close(); - - return; - } - catch (SqlException e) { - if (++retryCount >= 30 - || (e.Number != 233 && e.Number != -2 && e.Number != 4060)) { - throw; - } - - SqlConnection.ClearPool(connection); - - Thread.Sleep(100); - } - } - } - - private SqlServerTestStore CreateTransient(bool createDatabase) { - _connection = new SqlConnection(CreateConnectionString(_name)); - - if (createDatabase) { - using (var master = new SqlConnection(CreateConnectionString("master"))) { - master.Open(); - using (var command = master.CreateCommand()) { - command.CommandTimeout = CommandTimeout; - command.CommandText = $"{Environment.NewLine}CREATE DATABASE [{_name}]"; - - command.ExecuteNonQuery(); - - WaitForExists(_connection); - } - } - _connection.Open(); - } - - _deleteDatabase = true; - return this; - } - - private static bool DatabaseExists(string name) { - using (var master = new SqlConnection(CreateConnectionString("master"))) { - master.Open(); - - using (var command = master.CreateCommand()) { - command.CommandTimeout = CommandTimeout; - command.CommandText = $@"SELECT COUNT(*) FROM sys.databases WHERE name = N'{name}'"; - - return (int)command.ExecuteScalar() > 0; - } - } - } - - private static bool DatabaseFilesExist(string name) { - var userFolder = Environment.GetEnvironmentVariable("USERPROFILE") ?? - Environment.GetEnvironmentVariable("HOME"); - return userFolder != null - && (File.Exists(Path.Combine(userFolder, name + ".mdf")) - || File.Exists(Path.Combine(userFolder, name + "_log.ldf"))); - } - - private void DeleteDatabase(string name) { - using (var master = new SqlConnection(CreateConnectionString("master"))) { - master.Open(); - - using (var command = master.CreateCommand()) { - command.CommandTimeout = CommandTimeout; - // Query will take a few seconds if (and only if) there are active connections - - // SET SINGLE_USER will close any open connections that would prevent the drop - command.CommandText - = string.Format(@"IF EXISTS (SELECT * FROM sys.databases WHERE name = N'{0}') - BEGIN - ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; - DROP DATABASE [{0}]; - END", name); - - command.ExecuteNonQuery(); - } - } - } - - public DbConnection Connection => _connection; - - public void Dispose() { - _connection.Dispose(); - - if (_deleteDatabase) { - DeleteDatabase(_name); - } - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/TestEnvironment.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/TestEnvironment.cs deleted file mode 100644 index 328925e..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Utilities/TestEnvironment.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.IO; -using Microsoft.Extensions.Configuration; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public class TestEnvironment - { - public static IConfiguration Config { get; } - - static TestEnvironment() { - var configBuilder = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("config.json", optional: true) - .AddJsonFile("config.test.json", optional: true) - .AddEnvironmentVariables(); - - Config = configBuilder.Build(); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/config.json b/test/DotNetCore.CAP.EntityFrameworkCore.Test/config.json deleted file mode 100644 index 9433626..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Test": { - "SqlServer": { - "DefaultConnectionString": "Server=192.168.2.206;Initial Catalog=Test;User Id=cmswuliu;Password=h7xY81agBn*Veiu3;MultipleActiveResultSets=True" - } - } -} \ No newline at end of file