You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

94 lines
3.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Security.Claims;
  6. using System.Threading.Tasks;
  7. using Cap.Consistency.Infrastructure;
  8. using Cap.Consistency.Store;
  9. using Microsoft.AspNetCore.Builder;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.Logging;
  13. using Xunit;
  14. namespace Cap.Consistency.Test
  15. {
  16. public abstract class MessageManagerTestBase<TMessage> : MessageManagerTestBase<TMessage, string>
  17. where TMessage : ConsistencyMessage
  18. {
  19. }
  20. public abstract class MessageManagerTestBase<TMessage, TKey>
  21. where TMessage : ConsistencyMessage
  22. where TKey : IEquatable<TKey>
  23. {
  24. private const string NullValue = "(null)";
  25. protected virtual bool ShouldSkipDbTests() {
  26. return false;
  27. }
  28. protected virtual void SetupMessageServices(IServiceCollection services, object context = null) {
  29. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  30. services.AddConsistency();
  31. AddMessageStore(services, context);
  32. services.AddSingleton<ILogger<ConsistencyMessageManager>>(new TestLogger<ConsistencyMessageManager>());
  33. }
  34. protected virtual ConsistencyMessageManager CreateManager(object context = null, IServiceCollection services = null, Action<IServiceCollection> configureServices = null) {
  35. if (services == null) {
  36. services = new ServiceCollection();
  37. }
  38. if (context == null) {
  39. context = CreateTestContext();
  40. }
  41. SetupMessageServices(services, context);
  42. configureServices?.Invoke(services);
  43. return services.BuildServiceProvider().GetService<ConsistencyMessageManager>();
  44. }
  45. protected abstract object CreateTestContext();
  46. protected abstract TMessage CreateTestMessage(string payload = "");
  47. protected abstract void AddMessageStore(IServiceCollection services, object context = null);
  48. [Fact]
  49. public async Task CanDeleteMessage() {
  50. if (ShouldSkipDbTests()) {
  51. return;
  52. }
  53. var manager = CreateManager();
  54. var message = CreateTestMessage();
  55. var operateResult = await manager.CreateAsync(message);
  56. Assert.NotNull(operateResult);
  57. Assert.True(operateResult.Succeeded);
  58. var messageId = await manager.GeConsistencyMessageIdAsync(message);
  59. operateResult = await manager.DeleteAsync(message);
  60. Assert.Null(await manager.FindByIdAsync(messageId));
  61. }
  62. [Fact]
  63. public async Task CanFindById() {
  64. if (ShouldSkipDbTests()) {
  65. return;
  66. }
  67. var manager = CreateManager();
  68. var message = CreateTestMessage();
  69. var operateResult = await manager.CreateAsync(message);
  70. Assert.NotNull(operateResult);
  71. Assert.True(operateResult.Succeeded);
  72. var messageId = await manager.GeConsistencyMessageIdAsync(message);
  73. Assert.NotNull(await manager.FindByIdAsync(messageId));
  74. }
  75. }
  76. }