diff --git a/CAP.sln b/CAP.sln index 5a08718..35a9e3d 100644 --- a/CAP.sln +++ b/CAP.sln @@ -53,12 +53,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{10C0818D build\version.props = build\version.props EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.EntityFrameworkCore.Test", "test\DotNetCore.CAP.EntityFrameworkCore.Test\DotNetCore.CAP.EntityFrameworkCore.Test.csproj", "{69370370-9873-4D6A-965D-D1E16694047D}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.Test", "test\DotNetCore.CAP.Test\DotNetCore.CAP.Test.csproj", "{F608B509-A99B-4AC7-8227-42051DD4A578}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.SqlServer", "src\DotNetCore.CAP.SqlServer\DotNetCore.CAP.SqlServer.csproj", "{3B577468-6792-4EF1-9237-15180B176A24}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.SqlServer.Test", "test\DotNetCore.CAP.SqlServer.Test\DotNetCore.CAP.SqlServer.Test.csproj", "{DA00FA38-C4B9-4F55-8756-D480FBC1084F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -81,10 +81,6 @@ Global {9961B80E-0718-4280-B2A0-271B003DE26B}.Debug|Any CPU.Build.0 = Debug|Any CPU {9961B80E-0718-4280-B2A0-271B003DE26B}.Release|Any CPU.ActiveCfg = Release|Any CPU {9961B80E-0718-4280-B2A0-271B003DE26B}.Release|Any CPU.Build.0 = Release|Any CPU - {69370370-9873-4D6A-965D-D1E16694047D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {69370370-9873-4D6A-965D-D1E16694047D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {69370370-9873-4D6A-965D-D1E16694047D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {69370370-9873-4D6A-965D-D1E16694047D}.Release|Any CPU.Build.0 = Release|Any CPU {F608B509-A99B-4AC7-8227-42051DD4A578}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F608B509-A99B-4AC7-8227-42051DD4A578}.Debug|Any CPU.Build.0 = Debug|Any CPU {F608B509-A99B-4AC7-8227-42051DD4A578}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -92,6 +88,10 @@ Global {3B577468-6792-4EF1-9237-15180B176A24}.Debug|Any CPU.Build.0 = Debug|Any CPU {3B577468-6792-4EF1-9237-15180B176A24}.Release|Any CPU.ActiveCfg = Release|Any CPU {3B577468-6792-4EF1-9237-15180B176A24}.Release|Any CPU.Build.0 = Release|Any CPU + {DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -102,8 +102,8 @@ Global {2F095ED9-5BC9-4512-9013-A47685FB2508} = {3A6B6931-A123-477A-9469-8B468B5385AF} {C42CDE33-0878-4BA0-96F2-4CB7C8FDEAAD} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} {9961B80E-0718-4280-B2A0-271B003DE26B} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} - {69370370-9873-4D6A-965D-D1E16694047D} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0} {F608B509-A99B-4AC7-8227-42051DD4A578} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0} {3B577468-6792-4EF1-9237-15180B176A24} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} + {DA00FA38-C4B9-4F55-8756-D480FBC1084F} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0} EndGlobalSection EndGlobal diff --git a/test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs b/test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs new file mode 100644 index 0000000..efda431 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace DotNetCore.CAP.EntityFrameworkCore.Test +{ + public class CapMessageStoreTest + { + } +} \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs b/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs new file mode 100644 index 0000000..80fa0c8 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs @@ -0,0 +1,47 @@ +using System; +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=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 new file mode 100644 index 0000000..d6b21b6 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs @@ -0,0 +1,62 @@ +using System.Data; +using System.Threading.Tasks; +using Dapper; +using Microsoft.EntityFrameworkCore; + +namespace DotNetCore.CAP.EntityFrameworkCore.Test +{ + //public abstract class DatabaseTestHost : TestHost + //{ + // private static bool _sqlObjectInstalled; + + // protected override void PostBuildServices() + // { + // base.PostBuildServices(); + // InitializeDatabase(); + // } + + // public override void Dispose() + // { + // DeleteAllData(); + // base.Dispose(); + // } + + // private void InitializeDatabase() + // { + // if (!_sqlObjectInstalled) + // { + // using (CreateScope()) + // { + // var context = GetService(); + // context.Database.EnsureDeleted(); + // context.Database.Migrate(); + // _sqlObjectInstalled = true; + // } + // } + // } + + // private void DeleteAllData() + // { + // using (CreateScope()) + // { + // var context = GetService(); + + // 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) + // { + // context.Database.GetDbConnection().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 new file mode 100644 index 0000000..177d825 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/DotNetCore.CAP.SqlServer.Test.csproj @@ -0,0 +1,48 @@ + + + + netcoreapp1.1 + true + DotNetCore.CAP.SqlServer.Test + DotNetCore.CAP.SqlServer.Test + true + $(PackageTargetFallback);dnxcore50;portable-net451+win8 + 1.1.1 + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs b/test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs new file mode 100644 index 0000000..e59f393 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs @@ -0,0 +1,168 @@ +namespace DotNetCore.CAP.EntityFrameworkCore.Test +{ + //public class EFMessageStoreTest : DatabaseTestHost + //{ + // [Fact] + // public void CanCreateSentMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapPublishedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.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.StatusName == StatusName.Enqueued)); + // } + // } + + // [Fact] + // public void CanUpdateSentMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapPublishedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // selectedMessage.StatusName = StatusName.Succeeded; + // selectedMessage.Content = "Test"; + // db.SaveChanges(); + + // selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); + // Assert.NotNull(selectedMessage); + // Assert.True(selectedMessage.Content == "Test"); + // } + // } + + // [Fact] + // public void CanRemoveSentMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapPublishedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // db.CapSentMessages.Remove(selectedMessage); + // db.SaveChanges(); + // selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.Null(selectedMessage); + // } + // } + + // [Fact] + // public void CanCreateReceivedMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapReceivedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // Assert.True(db.CapReceivedMessages.Any(u => u.Id == guid)); + // Assert.NotNull(db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued)); + // } + // } + + // [Fact] + // public void CanUpdateReceivedMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapReceivedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // selectedMessage.StatusName = StatusName.Succeeded; + // selectedMessage.Content = "Test"; + // db.SaveChanges(); + + // selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); + // Assert.NotNull(selectedMessage); + // Assert.True(selectedMessage.Content == "Test"); + // } + // } + + // [Fact] + // public void CanRemoveReceivedMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapReceivedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // db.CapReceivedMessages.Remove(selectedMessage); + // db.SaveChanges(); + // selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.Null(selectedMessage); + // } + // } + + // public TestDbContext CreateContext(bool delete = false) + // { + // var db = Provider.GetRequiredService(); + // if (delete) + // { + // db.Database.EnsureDeleted(); + // } + // db.Database.EnsureCreated(); + // return db; + // } + //} +} \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/Properties/AssemblyInfo.cs b/test/DotNetCore.CAP.SqlServer.Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a995715 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,18 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DotNetCore.CAP.EntityFrameworkCore.Test")] +[assembly: AssemblyTrademark("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("7442c942-1ddc-40e4-8f1b-654e721eaa45")] \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/TestHost.cs b/test/DotNetCore.CAP.SqlServer.Test/TestHost.cs new file mode 100644 index 0000000..fbc1a94 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/TestHost.cs @@ -0,0 +1,97 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +namespace DotNetCore.CAP.EntityFrameworkCore.Test +{ + public abstract class TestHost : IDisposable + { + protected IServiceCollection _services; + 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(); + + var connectionString = ConnectionUtil.GetConnectionString(); + //services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString }); + //services.AddDbContext(options => options.UseSqlServer(connectionString)); + + _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.Test/CAP.BuilderTest.cs b/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs index 282b36d..d63f25d 100644 --- a/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs +++ b/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs @@ -1,8 +1,5 @@ using System; -using System.Threading; using System.Threading.Tasks; -using DotNetCore.CAP.Processor; -using DotNetCore.CAP.Models; using Microsoft.Extensions.DependencyInjection; using Xunit; using System.Data; @@ -11,42 +8,42 @@ namespace DotNetCore.CAP.Test { public class CapBuilderTest { - //[Fact] - //public void CanOverrideMessageStore() - //{ - // var services = new ServiceCollection(); - // services.AddCap().AddMessageStore(); - // var thingy = services.BuildServiceProvider() - // .GetRequiredService() as MyMessageStore; - - // Assert.NotNull(thingy); - ////} + [Fact] + public void CanCreateInstanceAndGetService() + { + var services = new ServiceCollection(); + services.AddSingleton(); + var builder = new CapBuilder(services); + Assert.NotNull(builder); - //[Fact] - //public void CanOverrideJobs() - //{ - // var services = new ServiceCollection(); - // services.AddCap().AddJobs(); + var count = builder.Services.Count; + Assert.Equal(1, count); + } - // var thingy = services.BuildServiceProvider() - // .GetRequiredService() as MyJobTest; + [Fact] + public void CanAddCapService() + { + var services = new ServiceCollection(); + services.AddCap(x => { }); + var builder = services.BuildServiceProvider(); - // Assert.NotNull(thingy); - //} + var markService = builder.GetService(); + Assert.NotNull(markService); + } - //[Fact] - //public void CanOverrideProducerService() - //{ - // var services = new ServiceCollection(); - // services.AddCap(x=> { }); - // var thingy = services.BuildServiceProvider() - // .GetRequiredService() as MyProducerService; + [Fact] + public void CanOverridePublishService() + { + var services = new ServiceCollection(); + services.AddCap(x => { }).AddProducerService(); - // Assert.NotNull(thingy); - //} + var thingy = services.BuildServiceProvider() + .GetRequiredService() as MyProducerService; + Assert.NotNull(thingy); + } private class MyProducerService : ICapPublisher { @@ -69,6 +66,6 @@ namespace DotNetCore.CAP.Test { throw new NotImplementedException(); } - } + } } } \ No newline at end of file