瀏覽代碼

comment test project

master
yangxiaodong 7 年之前
父節點
當前提交
17b28e667e
共有 8 個文件被更改,包括 445 次插入446 次删除
  1. +55
    -55
      test/Cap.Consistency.EntityFrameworkCore.Test/DefaultPocoTest.cs
  2. +89
    -89
      test/Cap.Consistency.EntityFrameworkCore.Test/MessageStoreTest.cs
  3. +62
    -62
      test/Cap.Consistency.EntityFrameworkCore.Test/MessageStoreWithGenericsTest.cs
  4. +52
    -52
      test/Cap.Consistency.Test/ConsistencyBuilderTest.cs
  5. +61
    -61
      test/Cap.Consistency.Test/ConsistencyMessageManagerTest.cs
  6. +32
    -32
      test/Cap.Consistency.Test/NoopMessageStore.cs
  7. +0
    -1
      test/Cap.Consistency.Test/OperateResultTest.cs
  8. +94
    -94
      test/Shared/MessageManagerTestBase.cs

+ 55
- 55
test/Cap.Consistency.EntityFrameworkCore.Test/DefaultPocoTest.cs 查看文件

@@ -1,55 +1,55 @@
using System.Threading.Tasks;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Store;
using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Cap.Consistency.EntityFrameworkCore.Test
{
public class DefaultPocoTest : IClassFixture<ScratchDatabaseFixture>
{
private readonly ApplicationBuilder _builder;
public DefaultPocoTest(ScratchDatabaseFixture fixture) {
var services = new ServiceCollection();
services
.AddDbContext<ConsistencyDbContext>(o => o.UseSqlServer(fixture.ConnectionString))
.AddConsistency()
.AddEntityFrameworkStores<ConsistencyDbContext>();
services.AddLogging();
var provider = services.BuildServiceProvider();
_builder = new ApplicationBuilder(provider);
using (var scoped = provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
using (var db = scoped.ServiceProvider.GetRequiredService<ConsistencyDbContext>()) {
db.Database.EnsureCreated();
}
}
[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
public async Task EnsureStartupUsageWorks() {
var messageStore = _builder.ApplicationServices.GetRequiredService<IConsistencyMessageStore>();
var messageManager = _builder.ApplicationServices.GetRequiredService<ConsistencyMessageManager>();
Assert.NotNull(messageStore);
Assert.NotNull(messageManager);
var user = new ConsistencyMessage();
var operateResult = await messageManager.CreateAsync(user);
Assert.True(operateResult.Succeeded);
operateResult = await messageManager.DeleteAsync(user);
Assert.True(operateResult.Succeeded);
}
}
}
//using System.Threading.Tasks;
//using Cap.Consistency.Infrastructure;
//using Cap.Consistency.Store;
//using Microsoft.AspNetCore.Builder.Internal;
//using Microsoft.AspNetCore.Testing.xunit;
//using Microsoft.EntityFrameworkCore;
//using Microsoft.Extensions.DependencyInjection;
//using Xunit;
//namespace Cap.Consistency.EntityFrameworkCore.Test
//{
// public class DefaultPocoTest : IClassFixture<ScratchDatabaseFixture>
// {
// private readonly ApplicationBuilder _builder;
// public DefaultPocoTest(ScratchDatabaseFixture fixture) {
// var services = new ServiceCollection();
// services
// .AddDbContext<ConsistencyDbContext>(o => o.UseSqlServer(fixture.ConnectionString))
// .AddConsistency()
// .AddEntityFrameworkStores<ConsistencyDbContext>();
// services.AddLogging();
// var provider = services.BuildServiceProvider();
// _builder = new ApplicationBuilder(provider);
// using (var scoped = provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
// using (var db = scoped.ServiceProvider.GetRequiredService<ConsistencyDbContext>()) {
// db.Database.EnsureCreated();
// }
// }
// [ConditionalFact]
// [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
// [OSSkipCondition(OperatingSystems.Linux)]
// [OSSkipCondition(OperatingSystems.MacOSX)]
// public async Task EnsureStartupUsageWorks() {
// var messageStore = _builder.ApplicationServices.GetRequiredService<IConsistencyMessageStore>();
// var messageManager = _builder.ApplicationServices.GetRequiredService<IConsistencyMessageStore >();
// Assert.NotNull(messageStore);
// Assert.NotNull(messageManager);
// var user = new ConsistencyMessage();
// var operateResult = await messageManager.CreateAsync(user);
// Assert.True(operateResult.Succeeded);
// operateResult = await messageManager.DeleteAsync(user);
// Assert.True(operateResult.Succeeded);
// }
// }
//}

+ 89
- 89
test/Cap.Consistency.EntityFrameworkCore.Test/MessageStoreTest.cs 查看文件

@@ -1,103 +1,103 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Store;
using Cap.Consistency.Test;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
//using System;
//using System.Linq;
//using System.Threading.Tasks;
//using Cap.Consistency.Infrastructure;
//using Cap.Consistency.Store;
//using Cap.Consistency.Test;
//using Microsoft.AspNetCore.Testing;
//using Microsoft.AspNetCore.Testing.xunit;
//using Microsoft.EntityFrameworkCore;
//using Microsoft.Extensions.DependencyInjection;
//using Xunit;

namespace Cap.Consistency.EntityFrameworkCore.Test
{
public class MessageStoreTest : MessageManagerTestBase<ConsistencyMessage>, IClassFixture<ScratchDatabaseFixture>
{
private readonly ScratchDatabaseFixture _fixture;
//namespace Cap.Consistency.EntityFrameworkCore.Test
//{
// public class MessageStoreTest : MessageManagerTestBase<ConsistencyMessage>, IClassFixture<ScratchDatabaseFixture>
// {
// private readonly ScratchDatabaseFixture _fixture;

public MessageStoreTest(ScratchDatabaseFixture fixture) {
_fixture = fixture;
}
// public MessageStoreTest(ScratchDatabaseFixture fixture) {
// _fixture = fixture;
// }

protected override bool ShouldSkipDbTests() {
return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
}
// protected override bool ShouldSkipDbTests() {
// return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
// }

public class ApplicationDbContext : ConsistencyDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {
}
}
// public class ApplicationDbContext : ConsistencyDbContext
// {
// public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {
// }
// }

[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
public void CanCreateMessageUsingEF() {
using (var db = CreateContext()) {
var guid = Guid.NewGuid().ToString();
db.Messages.Add(new ConsistencyMessage {
Id = guid,
Payload = "this is message body",
Status = MessageStatus.WaitForSend,
SendTime = DateTime.Now,
UpdateTime = DateTime.Now
});
// [ConditionalFact]
// [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
// [OSSkipCondition(OperatingSystems.Linux)]
// [OSSkipCondition(OperatingSystems.MacOSX)]
// public void CanCreateMessageUsingEF() {
// using (var db = CreateContext()) {
// var guid = Guid.NewGuid().ToString();
// db.Messages.Add(new ConsistencyMessage {
// Id = guid,
// Payload = "this is message body",
// Status = MessageStatus.WaitForSend,
// SendTime = DateTime.Now,
// UpdateTime = DateTime.Now
// });

db.SaveChanges();
Assert.True(db.Messages.Any(u => u.Id == guid));
Assert.NotNull(db.Messages.FirstOrDefault(u => u.Status == MessageStatus.WaitForSend));
}
}
// db.SaveChanges();
// Assert.True(db.Messages.Any(u => u.Id == guid));
// Assert.NotNull(db.Messages.FirstOrDefault(u => u.Status == MessageStatus.WaitForSend));
// }
// }

[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
public async Task CanCreateUsingManager() {
var manager = CreateManager();
var guid = Guid.NewGuid().ToString();
var message = new ConsistencyMessage {
Id = guid,
Payload = "this is message body",
Status = MessageStatus.WaitForSend,
SendTime = DateTime.Now,
UpdateTime = DateTime.Now
};
// [ConditionalFact]
// [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
// [OSSkipCondition(OperatingSystems.Linux)]
// [OSSkipCondition(OperatingSystems.MacOSX)]
// public async Task CanCreateUsingManager() {
// var manager = CreateManager();
// var guid = Guid.NewGuid().ToString();
// var message = new ConsistencyMessage {
// Id = guid,
// Payload = "this is message body",
// Status = MessageStatus.WaitForSend,
// SendTime = DateTime.Now,
// UpdateTime = DateTime.Now
// };

var result = await manager.CreateAsync(message);
Assert.NotNull(result);
Assert.True(result.Succeeded);
// var result = await manager.CreateAsync(message);
// Assert.NotNull(result);
// Assert.True(result.Succeeded);

result = await manager.DeleteAsync(message);
Assert.NotNull(result);
Assert.True(result.Succeeded);
}
// result = await manager.DeleteAsync(message);
// Assert.NotNull(result);
// Assert.True(result.Succeeded);
// }

public ConsistencyDbContext CreateContext(bool delete = false) {
var db = DbUtil.Create<ConsistencyDbContext>(_fixture.ConnectionString);
if (delete) {
db.Database.EnsureDeleted();
}
db.Database.EnsureCreated();
return db;
}
// public ConsistencyDbContext CreateContext(bool delete = false) {
// var db = DbUtil.Create<ConsistencyDbContext>(_fixture.ConnectionString);
// if (delete) {
// db.Database.EnsureDeleted();
// }
// db.Database.EnsureCreated();
// return db;
// }

protected override object CreateTestContext() {
return CreateContext();
}
// protected override object CreateTestContext() {
// return CreateContext();
// }

protected override ConsistencyMessage CreateTestMessage(string payload = "") {
return new ConsistencyMessage {
Payload = payload
};
}
// protected override ConsistencyMessage CreateTestMessage(string payload = "") {
// return new ConsistencyMessage {
// Payload = payload
// };
// }

protected override void AddMessageStore(IServiceCollection services, object context = null) {
services.AddSingleton<IConsistencyMessageStore>(new ConsistencyMessageStore<ConsistencyDbContext>((ConsistencyDbContext)context));
}
}
// protected override void AddMessageStore(IServiceCollection services, object context = null) {
// services.AddSingleton<IConsistencyMessageStore>(new ConsistencyMessageStore<ConsistencyDbContext>((ConsistencyDbContext)context));
// }
// }

public class ApplicationMessage : ConsistencyMessage { }
}
// public class ApplicationMessage : ConsistencyMessage { }
//}

+ 62
- 62
test/Cap.Consistency.EntityFrameworkCore.Test/MessageStoreWithGenericsTest.cs 查看文件

@@ -1,62 +1,62 @@
using System;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Store;
using Cap.Consistency.Test;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Cap.Consistency.EntityFrameworkCore.Test
{
public class MessageStoreWithGenericsTest : MessageManagerTestBase<MessageWithGenerics, string>, IClassFixture<ScratchDatabaseFixture>
{
private readonly ScratchDatabaseFixture _fixture;
public MessageStoreWithGenericsTest(ScratchDatabaseFixture fixture) {
_fixture = fixture;
}
protected override void AddMessageStore(IServiceCollection services, object context = null) {
services.AddSingleton<IConsistencyMessageStore>(new MessageStoreWithGenerics((ContextWithGenerics)context));
}
protected override object CreateTestContext() {
return CreateContext();
}
public ContextWithGenerics CreateContext() {
var db = DbUtil.Create<ContextWithGenerics>(_fixture.ConnectionString);
db.Database.EnsureCreated();
return db;
}
protected override MessageWithGenerics CreateTestMessage(string payload = "") {
return new MessageWithGenerics() {
Payload = payload,
SendTime = DateTime.Now,
Status = MessageStatus.WaitForSend,
UpdateTime = DateTime.Now
};
}
protected override bool ShouldSkipDbTests() {
return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
}
}
public class MessageWithGenerics : ConsistencyMessage
{
}
public class MessageStoreWithGenerics : ConsistencyMessageStore<ContextWithGenerics>
{
public MessageStoreWithGenerics(ContextWithGenerics context) : base(context) {
}
}
public class ContextWithGenerics : ConsistencyDbContext
{
public ContextWithGenerics() {
}
}
}
//using System;
//using Cap.Consistency.Infrastructure;
//using Cap.Consistency.Store;
//using Cap.Consistency.Test;
//using Microsoft.AspNetCore.Testing;
//using Microsoft.Extensions.DependencyInjection;
//using Xunit;
//namespace Cap.Consistency.EntityFrameworkCore.Test
//{
// public class MessageStoreWithGenericsTest : MessageManagerTestBase<MessageWithGenerics, string>, IClassFixture<ScratchDatabaseFixture>
// {
// private readonly ScratchDatabaseFixture _fixture;
// public MessageStoreWithGenericsTest(ScratchDatabaseFixture fixture) {
// _fixture = fixture;
// }
// protected override void AddMessageStore(IServiceCollection services, object context = null) {
// services.AddSingleton<IConsistencyMessageStore>(new MessageStoreWithGenerics((ContextWithGenerics)context));
// }
// protected override object CreateTestContext() {
// return CreateContext();
// }
// public ContextWithGenerics CreateContext() {
// var db = DbUtil.Create<ContextWithGenerics>(_fixture.ConnectionString);
// db.Database.EnsureCreated();
// return db;
// }
// protected override MessageWithGenerics CreateTestMessage(string payload = "") {
// return new MessageWithGenerics() {
// Payload = payload,
// SendTime = DateTime.Now,
// Status = MessageStatus.WaitForSend,
// UpdateTime = DateTime.Now
// };
// }
// protected override bool ShouldSkipDbTests() {
// return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
// }
// }
// public class MessageWithGenerics : ConsistencyMessage
// {
// }
// public class MessageStoreWithGenerics : ConsistencyMessageStore<ContextWithGenerics>
// {
// public MessageStoreWithGenerics(ContextWithGenerics context) : base(context) {
// }
// }
// public class ContextWithGenerics : ConsistencyDbContext
// {
// public ContextWithGenerics() {
// }
// }
//}

+ 52
- 52
test/Cap.Consistency.Test/ConsistencyBuilderTest.cs 查看文件

@@ -1,52 +1,52 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Store;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Cap.Consistency.Test
{
public class ConsistencyBuilderTest
{
[Fact]
public void CanOverrideMessageStore() {
var services = new ServiceCollection();
services.AddConsistency().AddMessageStore<MyUberThingy>();
var thingy = services.BuildServiceProvider().GetRequiredService<IConsistencyMessageStore>() as MyUberThingy;
Assert.NotNull(thingy);
}
private class MyUberThingy : IConsistencyMessageStore
{
public Task<OperateResult> CreateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public Task<OperateResult> DeleteAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public void Dispose() {
throw new NotImplementedException();
}
public Task<ConsistencyMessage> FindByIdAsync(string messageId, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public Task<string> GeConsistencyMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public Task<string> GetMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public Task<OperateResult> UpdateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
}
}
}
//using System;
//using System.Threading;
//using System.Threading.Tasks;
//using Cap.Consistency.Infrastructure;
//using Cap.Consistency.Store;
//using Microsoft.Extensions.DependencyInjection;
//using Xunit;
//namespace Cap.Consistency.Test
//{
// public class ConsistencyBuilderTest
// {
// [Fact]
// public void CanOverrideMessageStore() {
// var services = new ServiceCollection();
// services.AddConsistency().AddMessageStore<MyUberThingy>();
// var thingy = services.BuildServiceProvider().GetRequiredService<IConsistencyMessageStore>() as MyUberThingy;
// Assert.NotNull(thingy);
// }
// private class MyUberThingy : IConsistencyMessageStore
// {
// public Task<OperateResult> CreateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }
// public Task<OperateResult> DeleteAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }
// public void Dispose() {
// throw new NotImplementedException();
// }
// public Task<ConsistencyMessage> FindByIdAsync(string messageId, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }
// public Task<string> GeConsistencyMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }
// public Task<string> GetMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }
// public Task<OperateResult> UpdateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }
// }
// }
//}

+ 61
- 61
test/Cap.Consistency.Test/ConsistencyMessageManagerTest.cs 查看文件

@@ -1,74 +1,74 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Store;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
//using System;
//using System.Threading;
//using System.Threading.Tasks;
//using Cap.Consistency.Infrastructure;
//using Cap.Consistency.Store;
//using Microsoft.AspNetCore.Http;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Logging;
//using Moq;
//using Xunit;

namespace Cap.Consistency.Test
{
public class ConsistencyMessageManagerTest
{
[Fact]
public void EnsureDefaultServicesDefaultsWithStoreWorks() {
var services = new ServiceCollection()
.AddTransient<IConsistencyMessageStore, NoopMessageStore>();
services.AddConsistency();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLogging();
//namespace Cap.Consistency.Test
//{
// public class ConsistencyMessageManagerTest
// {
// [Fact]
// public void EnsureDefaultServicesDefaultsWithStoreWorks() {
// var services = new ServiceCollection()
// .AddTransient<IConsistencyMessageStore, NoopMessageStore>();
// services.AddConsistency();
// services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// services.AddLogging();

var manager = services.BuildServiceProvider()
.GetRequiredService<ConsistencyMessageManager>();
// var manager = services.BuildServiceProvider()
// .GetRequiredService<IConsistencyMessageStore >();

Assert.NotNull(manager);
}
// Assert.NotNull(manager);
// }

[Fact]
public void AddMessageManagerWithCustomerMannagerReturnsSameInstance() {
var services = new ServiceCollection()
.AddTransient<IConsistencyMessageStore, NoopMessageStore>()
.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// [Fact]
// public void AddMessageManagerWithCustomerMannagerReturnsSameInstance() {
// var services = new ServiceCollection()
// .AddTransient<IConsistencyMessageStore, NoopMessageStore>()
// .AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

services.AddLogging();
// services.AddLogging();

//services.AddConsistency()
// .AddConsistencyMessageManager<CustomMessageManager>();
// //services.AddConsistency()
// // .AddConsistencyMessageManager<CustomMessageManager>();

var provider = services.BuildServiceProvider();
// var provider = services.BuildServiceProvider();

Assert.Same(provider.GetRequiredService<ConsistencyMessageManager>(),
provider.GetRequiredService<CustomMessageManager>());
}
// Assert.Same(provider.GetRequiredService<IConsistencyMessageStore >(),
// provider.GetRequiredService<CustomMessageManager>());
// }

public class CustomMessageManager : ConsistencyMessageManager
{
public CustomMessageManager()
: base(new Mock<IConsistencyMessageStore>().Object, null, null) {
}
}
// public class CustomMessageManager : IConsistencyMessageStore
// {
// public CustomMessageManager()
// : base(new Mock<IConsistencyMessageStore>().Object, null, null) {
// }
// }

[Fact]
public async Task CreateCallsStore() {
var store = new Mock<IConsistencyMessageStore>();
var message = new ConsistencyMessage { SendTime = DateTime.Now };
store.Setup(x => x.CreateAsync(message, CancellationToken.None)).ReturnsAsync(OperateResult.Success).Verifiable();
var messageManager = TestConsistencyMessageManager(store.Object);
// [Fact]
// public async Task CreateCallsStore() {
// var store = new Mock<IConsistencyMessageStore>();
// var message = new ConsistencyMessage { SendTime = DateTime.Now };
// store.Setup(x => x.CreateAsync(message, CancellationToken.None)).ReturnsAsync(OperateResult.Success).Verifiable();
// var messageManager = TestConsistencyMessageManager(store.Object);

var result = await messageManager.CreateAsync(message);
// var result = await messageManager.CreateAsync(message);

Assert.True(result.Succeeded);
store.VerifyAll();
}
// Assert.True(result.Succeeded);
// store.VerifyAll();
// }

public ConsistencyMessageManager TestConsistencyMessageManager(IConsistencyMessageStore store = null) {
store = store ?? new Mock<IConsistencyMessageStore>().Object;
var mockLogger = new Mock<ILogger<ConsistencyMessageManager>>().Object;
var manager = new ConsistencyMessageManager(store, null, mockLogger);
return manager;
}
}
}
// public IConsistencyMessageStore TestConsistencyMessageManager(IConsistencyMessageStore store = null) {
// store = store ?? new Mock<IConsistencyMessageStore>().Object;
// var mockLogger = new Mock<ILogger<IConsistencyMessageStore >>().Object;
// var manager = new IConsistencyMessageStore (store, null, mockLogger);
// return manager;
// }
// }
//}

+ 32
- 32
test/Cap.Consistency.Test/NoopMessageStore.cs 查看文件

@@ -1,39 +1,39 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Store;
//using System;
//using System.Threading;
//using System.Threading.Tasks;
//using Cap.Consistency.Infrastructure;
//using Cap.Consistency.Store;

namespace Cap.Consistency.Test
{
public class NoopMessageStore : IConsistencyMessageStore
{
public Task<OperateResult> CreateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
//namespace Cap.Consistency.Test
//{
// public class NoopMessageStore : IConsistencyMessageStore
// {
// public Task<OperateResult> CreateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }

public Task<OperateResult> DeleteAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
// public Task<OperateResult> DeleteAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }

public void Dispose() {
throw new NotImplementedException();
}
// public void Dispose() {
// throw new NotImplementedException();
// }

public Task<ConsistencyMessage> FindByIdAsync(string messageId, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
// public Task<ConsistencyMessage> FindByIdAsync(string messageId, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }

public Task<string> GeConsistencyMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
// public Task<string> GeConsistencyMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }

public Task<string> GetMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
// public Task<string> GetMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }

public Task<OperateResult> UpdateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
}
}
// public Task<OperateResult> UpdateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
// throw new NotImplementedException();
// }
// }
//}

+ 0
- 1
test/Cap.Consistency.Test/OperateResultTest.cs 查看文件

@@ -1,5 +1,4 @@
using System.Linq;
using Cap.Consistency.Store;
using Xunit;

namespace Cap.Consistency.Test


+ 94
- 94
test/Shared/MessageManagerTestBase.cs 查看文件

@@ -1,94 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Threading.Tasks;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Store;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
namespace Cap.Consistency.Test
{
public abstract class MessageManagerTestBase<TMessage> : MessageManagerTestBase<TMessage, string>
where TMessage : ConsistencyMessage
{
}
public abstract class MessageManagerTestBase<TMessage, TKey>
where TMessage : ConsistencyMessage
where TKey : IEquatable<TKey>
{
private const string NullValue = "(null)";
protected virtual bool ShouldSkipDbTests() {
return false;
}
protected virtual void SetupMessageServices(IServiceCollection services, object context = null) {
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddConsistency();
AddMessageStore(services, context);
services.AddSingleton<ILogger<ConsistencyMessageManager>>(new TestLogger<ConsistencyMessageManager>());
}
protected virtual ConsistencyMessageManager CreateManager(object context = null, IServiceCollection services = null, Action<IServiceCollection> configureServices = null) {
if (services == null) {
services = new ServiceCollection();
}
if (context == null) {
context = CreateTestContext();
}
SetupMessageServices(services, context);
configureServices?.Invoke(services);
return services.BuildServiceProvider().GetService<ConsistencyMessageManager>();
}
protected abstract object CreateTestContext();
protected abstract TMessage CreateTestMessage(string payload = "");
protected abstract void AddMessageStore(IServiceCollection services, object context = null);
[Fact]
public async Task CanDeleteMessage() {
if (ShouldSkipDbTests()) {
return;
}
var manager = CreateManager();
var message = CreateTestMessage();
var operateResult = await manager.CreateAsync(message);
Assert.NotNull(operateResult);
Assert.True(operateResult.Succeeded);
var messageId = await manager.GeConsistencyMessageIdAsync(message);
operateResult = await manager.DeleteAsync(message);
Assert.Null(await manager.FindByIdAsync(messageId));
}
[Fact]
public async Task CanFindById() {
if (ShouldSkipDbTests()) {
return;
}
var manager = CreateManager();
var message = CreateTestMessage();
var operateResult = await manager.CreateAsync(message);
Assert.NotNull(operateResult);
Assert.True(operateResult.Succeeded);
var messageId = await manager.GeConsistencyMessageIdAsync(message);
Assert.NotNull(await manager.FindByIdAsync(messageId));
}
}
}
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Linq.Expressions;
//using System.Security.Claims;
//using System.Threading.Tasks;
//using Cap.Consistency.Infrastructure;
//using Cap.Consistency.Store;
//using Microsoft.AspNetCore.Builder;
//using Microsoft.AspNetCore.Http;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Logging;
//using Xunit;
//namespace Cap.Consistency.Test
//{
// public abstract class MessageManagerTestBase<TMessage> : MessageManagerTestBase<TMessage, string>
// where TMessage : ConsistencyMessage
// {
// }
// public abstract class MessageManagerTestBase<TMessage, TKey>
// where TMessage : ConsistencyMessage
// where TKey : IEquatable<TKey>
// {
// private const string NullValue = "(null)";
// protected virtual bool ShouldSkipDbTests() {
// return false;
// }
// protected virtual void SetupMessageServices(IServiceCollection services, object context = null) {
// services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// services.AddConsistency();
// AddMessageStore(services, context);
// services.AddSingleton<ILogger<IConsistencyMessageStore >>(new TestLogger<IConsistencyMessageStore >());
// }
// protected virtual IConsistencyMessageStore CreateManager(object context = null, IServiceCollection services = null, Action<IServiceCollection> configureServices = null) {
// if (services == null) {
// services = new ServiceCollection();
// }
// if (context == null) {
// context = CreateTestContext();
// }
// SetupMessageServices(services, context);
// configureServices?.Invoke(services);
// return services.BuildServiceProvider().GetService<IConsistencyMessageStore >();
// }
// protected abstract object CreateTestContext();
// protected abstract TMessage CreateTestMessage(string payload = "");
// protected abstract void AddMessageStore(IServiceCollection services, object context = null);
// [Fact]
// public async Task CanDeleteMessage() {
// if (ShouldSkipDbTests()) {
// return;
// }
// var manager = CreateManager();
// var message = CreateTestMessage();
// var operateResult = await manager.CreateAsync(message);
// Assert.NotNull(operateResult);
// Assert.True(operateResult.Succeeded);
// var messageId = await manager.GeConsistencyMessageIdAsync(message);
// operateResult = await manager.DeleteAsync(message);
// Assert.Null(await manager.FindByIdAsync(messageId));
// }
// [Fact]
// public async Task CanFindById() {
// if (ShouldSkipDbTests()) {
// return;
// }
// var manager = CreateManager();
// var message = CreateTestMessage();
// var operateResult = await manager.CreateAsync(message);
// Assert.NotNull(operateResult);
// Assert.True(operateResult.Succeeded);
// var messageId = await manager.GeConsistencyMessageIdAsync(message);
// Assert.NotNull(await manager.FindByIdAsync(messageId));
// }
// }
//}

Loading…
取消
儲存