From e72cc784e6b904f3f146f9c844209ab6dc88831b Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 18 Jul 2017 00:29:54 +0800 Subject: [PATCH] add unit tests. --- .../CapMessageStoreTest.cs | 11 -- .../ConnectionUtil.cs | 2 +- .../DatabaseTestHost.cs | 102 +++++++++--------- .../TestDbContext.cs | 13 +++ .../DotNetCore.CAP.SqlServer.Test/TestHost.cs | 7 +- test/DotNetCore.CAP.Test/CAP.BuilderTest.cs | 16 ++- .../ConsistencyOptionsTest.cs | 6 -- test/DotNetCore.CAP.Test/NoopMessageStore.cs | 21 ---- test/DotNetCore.CAP.Test/StateChangerTest.cs | 59 ++++++++++ 9 files changed, 143 insertions(+), 94 deletions(-) delete mode 100644 test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs create mode 100644 test/DotNetCore.CAP.SqlServer.Test/TestDbContext.cs delete mode 100644 test/DotNetCore.CAP.Test/ConsistencyOptionsTest.cs delete mode 100644 test/DotNetCore.CAP.Test/NoopMessageStore.cs create mode 100644 test/DotNetCore.CAP.Test/StateChangerTest.cs diff --git a/test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs b/test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs deleted file mode 100644 index efda431..0000000 --- a/test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs +++ /dev/null @@ -1,11 +0,0 @@ -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 index 80fa0c8..c9ba0b5 100644 --- a/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs +++ b/test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs @@ -1,7 +1,7 @@ using System; using System.Data.SqlClient; -namespace DotNetCore.CAP.EntityFrameworkCore.Test +namespace DotNetCore.CAP.SqlServer.Test { public static class ConnectionUtil { diff --git a/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs index d6b21b6..380a55b 100644 --- a/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs +++ b/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs @@ -1,62 +1,62 @@ using System.Data; -using System.Threading.Tasks; +using System.Threading; using Dapper; using Microsoft.EntityFrameworkCore; -namespace DotNetCore.CAP.EntityFrameworkCore.Test +namespace DotNetCore.CAP.SqlServer.Test { - //public abstract class DatabaseTestHost : TestHost - //{ - // private static bool _sqlObjectInstalled; + public abstract class DatabaseTestHost : TestHost + { + private static bool _sqlObjectInstalled; - // protected override void PostBuildServices() - // { - // base.PostBuildServices(); - // InitializeDatabase(); - // } + protected override void PostBuildServices() + { + base.PostBuildServices(); + InitializeDatabase(); + } - // public override void Dispose() - // { - // DeleteAllData(); - // base.Dispose(); - // } + 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 InitializeDatabase() + { + if (!_sqlObjectInstalled) + { + using (CreateScope()) + { + var storage = GetService(); + var token = new CancellationTokenSource().Token; + storage.InitializeAsync(token).Wait(); + _sqlObjectInstalled = true; + } + } + } - // private void DeleteAllData() - // { - // using (CreateScope()) - // { - // var context = GetService(); + 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); - // } - // } - // } - //} + 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/TestDbContext.cs b/test/DotNetCore.CAP.SqlServer.Test/TestDbContext.cs new file mode 100644 index 0000000..d59bdf1 --- /dev/null +++ b/test/DotNetCore.CAP.SqlServer.Test/TestDbContext.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; + +namespace DotNetCore.CAP.SqlServer.Test +{ + public class TestDbContext : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionString = ConnectionUtil.GetConnectionString(); + optionsBuilder.UseSqlServer(connectionString); + } + } +} \ No newline at end of file diff --git a/test/DotNetCore.CAP.SqlServer.Test/TestHost.cs b/test/DotNetCore.CAP.SqlServer.Test/TestHost.cs index fbc1a94..3b218fc 100644 --- a/test/DotNetCore.CAP.SqlServer.Test/TestHost.cs +++ b/test/DotNetCore.CAP.SqlServer.Test/TestHost.cs @@ -2,7 +2,7 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -namespace DotNetCore.CAP.EntityFrameworkCore.Test +namespace DotNetCore.CAP.SqlServer.Test { public abstract class TestHost : IDisposable { @@ -28,8 +28,9 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test services.AddLogging(); var connectionString = ConnectionUtil.GetConnectionString(); - //services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString }); - //services.AddDbContext(options => options.UseSqlServer(connectionString)); + services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString }); + services.AddSingleton(); + services.AddDbContext(options => options.UseSqlServer(connectionString)); _services = services; } diff --git a/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs b/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs index d63f25d..2b18691 100644 --- a/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs +++ b/test/DotNetCore.CAP.Test/CAP.BuilderTest.cs @@ -8,17 +8,21 @@ namespace DotNetCore.CAP.Test { public class CapBuilderTest { - [Fact] public void CanCreateInstanceAndGetService() { var services = new ServiceCollection(); + services.AddSingleton(); var builder = new CapBuilder(services); Assert.NotNull(builder); var count = builder.Services.Count; Assert.Equal(1, count); + + var provider = services.BuildServiceProvider(); + var capPublisher = provider.GetService(); + Assert.NotNull(capPublisher); } [Fact] @@ -45,6 +49,16 @@ namespace DotNetCore.CAP.Test Assert.NotNull(thingy); } + [Fact] + public void CanResolveCapOptions() + { + var services = new ServiceCollection(); + services.AddCap(x => { }); + var builder = services.BuildServiceProvider(); + var capOptions = builder.GetService(); + Assert.NotNull(capOptions); + } + private class MyProducerService : ICapPublisher { public Task PublishAsync(string topic, string content) diff --git a/test/DotNetCore.CAP.Test/ConsistencyOptionsTest.cs b/test/DotNetCore.CAP.Test/ConsistencyOptionsTest.cs deleted file mode 100644 index b9484aa..0000000 --- a/test/DotNetCore.CAP.Test/ConsistencyOptionsTest.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace CDotNetCore.CAPTest -{ - public class ConsistencyOptionsTest - { - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.Test/NoopMessageStore.cs b/test/DotNetCore.CAP.Test/NoopMessageStore.cs deleted file mode 100644 index 97cb6a7..0000000 --- a/test/DotNetCore.CAP.Test/NoopMessageStore.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using DotNetCore.CAP.Models; - -namespace DotNetCore.CAP.Test -{ - //public class NoopMessageStore : ICapMessageStore - //{ - // public Task ChangeReceivedMessageStateAsync(CapReceivedMessage message, string statusName, - // bool autoSaveChanges = true) - // { - // throw new NotImplementedException(); - // } - - // public Task StoreSentMessageAsync(CapSentMessage message) - // { - // throw new NotImplementedException(); - // } - //} -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.Test/StateChangerTest.cs b/test/DotNetCore.CAP.Test/StateChangerTest.cs new file mode 100644 index 0000000..51101bd --- /dev/null +++ b/test/DotNetCore.CAP.Test/StateChangerTest.cs @@ -0,0 +1,59 @@ +using System; +using DotNetCore.CAP.Infrastructure; +using DotNetCore.CAP.Models; +using DotNetCore.CAP.Processor.States; +using Moq; +using Xunit; + +namespace DotNetCore.CAP.Test +{ + public class StateChangerTest + { + [Fact] + public void ChangeState() + { + // Arrange + var fixture = Create(); + var message = new CapPublishedMessage + { + StatusName = StatusName.Enqueued + }; + var state = Mock.Of(s => s.Name == "s" && s.ExpiresAfter == null); + var mockTransaction = new Mock(); + + // Act + fixture.ChangeState(message, state, mockTransaction.Object); + + // Assert + Assert.Equal(message.StatusName, "s"); + Assert.Null(message.ExpiresAt); + Mock.Get(state).Verify(s => s.Apply(message, mockTransaction.Object), Times.Once); + mockTransaction.Verify(t => t.UpdateMessage(message), Times.Once); + mockTransaction.Verify(t => t.CommitAsync(), Times.Never); + } + + [Fact] + public void ChangeState_ExpiresAfter() + { + // Arrange + var fixture = Create(); + var message = new CapPublishedMessage + { + StatusName = StatusName.Enqueued + }; + var state = Mock.Of(s => s.Name == "s" && s.ExpiresAfter == TimeSpan.FromHours(1)); + var mockTransaction = new Mock(); + + // Act + fixture.ChangeState(message, state, mockTransaction.Object); + + // Assert + Assert.Equal(message.StatusName, "s"); + Assert.NotNull(message.ExpiresAt); + mockTransaction.Verify(t => t.UpdateMessage(message), Times.Once); + mockTransaction.Verify(t => t.CommitAsync(), Times.Never); + } + + private StateChanger Create() => new StateChanger(); + } +}