Commit e72cc784 authored by Savorboard's avatar Savorboard

add unit tests.

parent c1db4267
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
using System; using System;
using System.Data.SqlClient; using System.Data.SqlClient;
namespace DotNetCore.CAP.EntityFrameworkCore.Test namespace DotNetCore.CAP.SqlServer.Test
{ {
public static class ConnectionUtil public static class ConnectionUtil
{ {
......
using System.Data; using System.Data;
using System.Threading.Tasks; using System.Threading;
using Dapper; using Dapper;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace DotNetCore.CAP.EntityFrameworkCore.Test namespace DotNetCore.CAP.SqlServer.Test
{ {
//public abstract class DatabaseTestHost : TestHost public abstract class DatabaseTestHost : TestHost
//{ {
// private static bool _sqlObjectInstalled; private static bool _sqlObjectInstalled;
// protected override void PostBuildServices() protected override void PostBuildServices()
// { {
// base.PostBuildServices(); base.PostBuildServices();
// InitializeDatabase(); InitializeDatabase();
// } }
// public override void Dispose() public override void Dispose()
// { {
// DeleteAllData(); DeleteAllData();
// base.Dispose(); base.Dispose();
// } }
// private void InitializeDatabase() private void InitializeDatabase()
// { {
// if (!_sqlObjectInstalled) if (!_sqlObjectInstalled)
// { {
// using (CreateScope()) using (CreateScope())
// { {
// var context = GetService<TestDbContext>(); var storage = GetService<SqlServerStorage>();
// context.Database.EnsureDeleted(); var token = new CancellationTokenSource().Token;
// context.Database.Migrate(); storage.InitializeAsync(token).Wait();
// _sqlObjectInstalled = true; _sqlObjectInstalled = true;
// } }
// } }
// } }
// private void DeleteAllData() private void DeleteAllData()
// { {
// using (CreateScope()) using (CreateScope())
// { {
// var context = GetService<TestDbContext>(); var context = GetService<TestDbContext>();
// var commands = new[] var commands = new[]
// { {
// "DISABLE TRIGGER ALL ON ?", "DISABLE TRIGGER ALL ON ?",
// "ALTER TABLE ? NOCHECK CONSTRAINT ALL", "ALTER TABLE ? NOCHECK CONSTRAINT ALL",
// "DELETE FROM ?", "DELETE FROM ?",
// "ALTER TABLE ? CHECK CONSTRAINT ALL", "ALTER TABLE ? CHECK CONSTRAINT ALL",
// "ENABLE TRIGGER ALL ON ?" "ENABLE TRIGGER ALL ON ?"
// }; };
// foreach (var command in commands) foreach (var command in commands)
// { {
// context.Database.GetDbConnection().Execute( context.Database.GetDbConnection().Execute(
// "sp_MSforeachtable", "sp_MSforeachtable",
// new {command1 = command}, new { command1 = command },
// commandType: CommandType.StoredProcedure); commandType: CommandType.StoredProcedure);
// } }
// } }
// } }
//} }
} }
\ No newline at end of file
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
...@@ -2,7 +2,7 @@ using System; ...@@ -2,7 +2,7 @@ using System;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace DotNetCore.CAP.EntityFrameworkCore.Test namespace DotNetCore.CAP.SqlServer.Test
{ {
public abstract class TestHost : IDisposable public abstract class TestHost : IDisposable
{ {
...@@ -28,8 +28,9 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test ...@@ -28,8 +28,9 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test
services.AddLogging(); services.AddLogging();
var connectionString = ConnectionUtil.GetConnectionString(); var connectionString = ConnectionUtil.GetConnectionString();
//services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString }); services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString });
//services.AddDbContext<TestDbContext>(options => options.UseSqlServer(connectionString)); services.AddSingleton<SqlServerStorage>();
services.AddDbContext<TestDbContext>(options => options.UseSqlServer(connectionString));
_services = services; _services = services;
} }
......
...@@ -8,17 +8,21 @@ namespace DotNetCore.CAP.Test ...@@ -8,17 +8,21 @@ namespace DotNetCore.CAP.Test
{ {
public class CapBuilderTest public class CapBuilderTest
{ {
[Fact] [Fact]
public void CanCreateInstanceAndGetService() public void CanCreateInstanceAndGetService()
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ICapPublisher, MyProducerService>(); services.AddSingleton<ICapPublisher, MyProducerService>();
var builder = new CapBuilder(services); var builder = new CapBuilder(services);
Assert.NotNull(builder); Assert.NotNull(builder);
var count = builder.Services.Count; var count = builder.Services.Count;
Assert.Equal(1, count); Assert.Equal(1, count);
var provider = services.BuildServiceProvider();
var capPublisher = provider.GetService<ICapPublisher>();
Assert.NotNull(capPublisher);
} }
[Fact] [Fact]
...@@ -45,6 +49,16 @@ namespace DotNetCore.CAP.Test ...@@ -45,6 +49,16 @@ namespace DotNetCore.CAP.Test
Assert.NotNull(thingy); Assert.NotNull(thingy);
} }
[Fact]
public void CanResolveCapOptions()
{
var services = new ServiceCollection();
services.AddCap(x => { });
var builder = services.BuildServiceProvider();
var capOptions = builder.GetService<CapOptions>();
Assert.NotNull(capOptions);
}
private class MyProducerService : ICapPublisher private class MyProducerService : ICapPublisher
{ {
public Task PublishAsync(string topic, string content) public Task PublishAsync(string topic, string content)
......
namespace CDotNetCore.CAPTest
{
public class ConsistencyOptionsTest
{
}
}
\ No newline at end of file
using System;
using System.Threading;
using System.Threading.Tasks;
using DotNetCore.CAP.Models;
namespace DotNetCore.CAP.Test
{
//public class NoopMessageStore : ICapMessageStore
//{
// public Task<OperateResult> ChangeReceivedMessageStateAsync(CapReceivedMessage message, string statusName,
// bool autoSaveChanges = true)
// {
// throw new NotImplementedException();
// }
// public Task<OperateResult> StoreSentMessageAsync(CapSentMessage message)
// {
// throw new NotImplementedException();
// }
//}
}
\ No newline at end of file
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<IState>(s => s.Name == "s" && s.ExpiresAfter == null);
var mockTransaction = new Mock<IStorageTransaction>();
// 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<IState>(s => s.Name == "s" && s.ExpiresAfter == TimeSpan.FromHours(1));
var mockTransaction = new Mock<IStorageTransaction>();
// 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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment