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.
 
 
 

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