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.
 
 
 

104 lines
3.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Cap.Consistency.Infrastructure;
  6. using Cap.Consistency.Test;
  7. using Microsoft.AspNetCore.Testing;
  8. using Microsoft.AspNetCore.Testing.xunit;
  9. using Microsoft.EntityFrameworkCore;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Xunit;
  12. namespace Cap.Consistency.EntityFrameworkCore.Test
  13. {
  14. public class MessageStoreTest : MessageManagerTestBase<ConsistencyMessage>, IClassFixture<ScratchDatabaseFixture>
  15. {
  16. private readonly ScratchDatabaseFixture _fixture;
  17. public MessageStoreTest(ScratchDatabaseFixture fixture) {
  18. _fixture = fixture;
  19. }
  20. protected override bool ShouldSkipDbTests() {
  21. return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
  22. }
  23. public class ApplicationDbContext : ConsistencyDbContext<ApplicationMessage, string>
  24. {
  25. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
  26. }
  27. [ConditionalFact]
  28. [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
  29. [OSSkipCondition(OperatingSystems.Linux)]
  30. [OSSkipCondition(OperatingSystems.MacOSX)]
  31. public void CanCreateMessageUsingEF() {
  32. using (var db = CreateContext()) {
  33. var guid = Guid.NewGuid().ToString();
  34. db.Messages.Add(new ConsistencyMessage {
  35. Id = guid,
  36. Payload = "this is message body",
  37. Status = MessageStatus.WaitForSend,
  38. SendTime = DateTime.Now,
  39. UpdateTime = DateTime.Now
  40. });
  41. db.SaveChanges();
  42. Assert.True(db.Messages.Any(u => u.Id == guid));
  43. Assert.NotNull(db.Messages.FirstOrDefault(u => u.Status == MessageStatus.WaitForSend));
  44. }
  45. }
  46. [ConditionalFact]
  47. [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
  48. [OSSkipCondition(OperatingSystems.Linux)]
  49. [OSSkipCondition(OperatingSystems.MacOSX)]
  50. public async Task CanCreateUsingManager() {
  51. var manager = CreateManager();
  52. var guid = Guid.NewGuid().ToString();
  53. var message = new ConsistencyMessage {
  54. Id = guid,
  55. Payload = "this is message body",
  56. Status = MessageStatus.WaitForSend,
  57. SendTime = DateTime.Now,
  58. UpdateTime = DateTime.Now
  59. };
  60. var result = await manager.CreateAsync(message);
  61. Assert.NotNull(result);
  62. Assert.True(result.Succeeded);
  63. result = await manager.DeleteAsync(message);
  64. Assert.NotNull(result);
  65. Assert.True(result.Succeeded);
  66. }
  67. public ConsistencyDbContext CreateContext(bool delete = false) {
  68. var db = DbUtil.Create<ConsistencyDbContext>(_fixture.ConnectionString);
  69. if (delete) {
  70. db.Database.EnsureDeleted();
  71. }
  72. db.Database.EnsureCreated();
  73. return db;
  74. }
  75. protected override object CreateTestContext() {
  76. return CreateContext();
  77. }
  78. protected override ConsistencyMessage CreateTestMessage(string payload = "") {
  79. return new ConsistencyMessage {
  80. Payload = payload
  81. };
  82. }
  83. protected override void AddMessageStore(IServiceCollection services, object context = null) {
  84. services.AddSingleton<IConsistencyMessageStore<ConsistencyMessage>>(new ConsistencyMessageStore<ConsistencyMessage>((ConsistencyDbContext)context));
  85. }
  86. }
  87. public class ApplicationMessage : ConsistencyMessage { }
  88. }