No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

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