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.

PostgreSqlStorageConnectionTest.cs 3.4 KiB

7 years ago
7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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""(""Id"",""Name"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") VALUES(@Id,@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  23. var insertedId = SnowflakeId.Default().NextId();
  24. var publishMessage = new CapPublishedMessage
  25. {
  26. Id = insertedId,
  27. Name = "PostgreSqlStorageConnectionTest",
  28. Content = "",
  29. StatusName = StatusName.Scheduled
  30. };
  31. using (var connection = ConnectionUtil.CreateConnection())
  32. {
  33. await connection.ExecuteAsync(sql, publishMessage);
  34. }
  35. var message = await _storage.GetPublishedMessageAsync(insertedId);
  36. Assert.NotNull(message);
  37. Assert.Equal("PostgreSqlStorageConnectionTest", message.Name);
  38. Assert.Equal(StatusName.Scheduled, message.StatusName);
  39. }
  40. [Fact]
  41. public void StoreReceivedMessageAsync_Test()
  42. {
  43. var receivedMessage = new CapReceivedMessage
  44. {
  45. Id = SnowflakeId.Default().NextId(),
  46. Name = "PostgreSqlStorageConnectionTest",
  47. Content = "",
  48. Group = "mygroup",
  49. StatusName = StatusName.Scheduled
  50. };
  51. Exception exception = null;
  52. try
  53. {
  54. _storage.StoreReceivedMessage(receivedMessage);
  55. }
  56. catch (Exception ex)
  57. {
  58. exception = ex;
  59. }
  60. Assert.Null(exception);
  61. }
  62. [Fact]
  63. public async Task GetReceivedMessageAsync_Test()
  64. {
  65. var sql = $@"
  66. INSERT INTO ""cap"".""received""(""Id"",""Name"",""Group"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"")
  67. VALUES(@Id,@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  68. var insertedId = SnowflakeId.Default().NextId();
  69. var receivedMessage = new CapReceivedMessage
  70. {
  71. Id= insertedId,
  72. Name = "PostgreSqlStorageConnectionTest",
  73. Content = "",
  74. Group = "mygroup",
  75. StatusName = StatusName.Scheduled
  76. };
  77. using (var connection = ConnectionUtil.CreateConnection())
  78. {
  79. await connection.ExecuteAsync(sql, receivedMessage);
  80. }
  81. var message = await _storage.GetReceivedMessageAsync(insertedId);
  82. Assert.NotNull(message);
  83. Assert.Equal(StatusName.Scheduled, message.StatusName);
  84. Assert.Equal("PostgreSqlStorageConnectionTest", message.Name);
  85. Assert.Equal("mygroup", message.Group);
  86. }
  87. }
  88. }