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.
 
 
 

93 lines
3.3 KiB

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