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.
 
 
 

98 line
3.5 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using Dapper;
  4. using DotNetCore.CAP.Infrastructure;
  5. using DotNetCore.CAP.Messages;
  6. using Microsoft.Extensions.Options;
  7. using Xunit;
  8. namespace DotNetCore.CAP.PostgreSql.Test
  9. {
  10. [Collection("postgresql")]
  11. public class PostgreSqlStorageConnectionTest : DatabaseTestHost
  12. {
  13. private PostgreSqlStorageConnection _storage;
  14. public PostgreSqlStorageConnectionTest()
  15. {
  16. var options = GetService<IOptions<PostgreSqlOptions>>();
  17. var capOptions = GetService<IOptions<CapOptions>>();
  18. _storage = new PostgreSqlStorageConnection(options, capOptions);
  19. }
  20. [Fact]
  21. public async Task GetPublishedMessageAsync_Test()
  22. {
  23. var sql = @"INSERT INTO ""cap"".""published""(""Id"",""Version"",""Name"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") VALUES(@Id,'v1',@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  24. var insertedId = SnowflakeId.Default().NextId();
  25. var publishMessage = new CapPublishedMessage
  26. {
  27. Id = insertedId,
  28. Name = "PostgreSqlStorageConnectionTest",
  29. Content = "",
  30. StatusName = StatusName.Scheduled
  31. };
  32. using (var connection = ConnectionUtil.CreateConnection())
  33. {
  34. await connection.ExecuteAsync(sql, publishMessage);
  35. }
  36. var message = await _storage.GetPublishedMessageAsync(insertedId);
  37. Assert.NotNull(message);
  38. Assert.Equal("PostgreSqlStorageConnectionTest", message.Name);
  39. Assert.Equal(StatusName.Scheduled, message.StatusName);
  40. }
  41. [Fact]
  42. public void StoreReceivedMessageAsync_Test()
  43. {
  44. var receivedMessage = new CapReceivedMessage
  45. {
  46. Id = SnowflakeId.Default().NextId(),
  47. Name = "PostgreSqlStorageConnectionTest",
  48. Content = "",
  49. Group = "mygroup",
  50. StatusName = StatusName.Scheduled
  51. };
  52. Exception exception = null;
  53. try
  54. {
  55. _storage.StoreReceivedMessage(receivedMessage);
  56. }
  57. catch (Exception ex)
  58. {
  59. exception = ex;
  60. }
  61. Assert.Null(exception);
  62. }
  63. [Fact]
  64. public async Task GetReceivedMessageAsync_Test()
  65. {
  66. var sql = $@"
  67. INSERT INTO ""cap"".""received""(""Id"",""Version"",""Name"",""Group"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"")
  68. VALUES(@Id,'v1',@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  69. var insertedId = SnowflakeId.Default().NextId();
  70. var receivedMessage = new CapReceivedMessage
  71. {
  72. Id = insertedId,
  73. Name = "PostgreSqlStorageConnectionTest",
  74. Content = "",
  75. Group = "mygroup",
  76. StatusName = StatusName.Scheduled
  77. };
  78. using (var connection = ConnectionUtil.CreateConnection())
  79. {
  80. await connection.ExecuteAsync(sql, receivedMessage);
  81. }
  82. var message = await _storage.GetReceivedMessageAsync(insertedId);
  83. Assert.NotNull(message);
  84. Assert.Equal(StatusName.Scheduled, message.StatusName);
  85. Assert.Equal("PostgreSqlStorageConnectionTest", message.Name);
  86. Assert.Equal("mygroup", message.Group);
  87. }
  88. }
  89. }