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.

180 lines
6.2KB

  1. using Campaign_Tracker.Server.ExtensionData;
  2. using Campaign_Tracker.Server.LegacyData;
  3. using Campaign_Tracker.Server.LegacyData.Models;
  4. namespace Campaign_Tracker.Server.Tests;
  5. public sealed class LegacyLinkValidatorTests
  6. {
  7. // ── AC #2 — ACL join returns correct record with no ambiguity ────────────
  8. [Fact]
  9. public async Task ValidateAsync_JurisdictionJCode_ExistingRecord_ReturnsSuccess_AC2()
  10. {
  11. var data = new InMemoryLegacyDataAccess(
  12. jurisdictions: [new("FAIR01", "Fairview Borough", null, null, null, null)]);
  13. var sut = new LegacyLinkValidator(data);
  14. var result = await sut.ValidateAsync(LegacyLinkReference.ForJurisdiction("FAIR01"));
  15. Assert.True(result.IsValid);
  16. Assert.Null(result.Error);
  17. }
  18. [Fact]
  19. public async Task ValidateAsync_KitId_ExistingRecord_ReturnsSuccess_AC2()
  20. {
  21. var kit = new LegacyKit(101, "FAIR01", null, null, null, null, false, false,
  22. null, null, null, null, null, null);
  23. var data = new InMemoryLegacyDataAccess(kits: [kit]);
  24. var sut = new LegacyLinkValidator(data);
  25. var result = await sut.ValidateAsync(LegacyLinkReference.ForKit(101));
  26. Assert.True(result.IsValid);
  27. }
  28. [Fact]
  29. public async Task ValidateAsync_ContactId_ExistingRecord_ReturnsSuccess_AC2()
  30. {
  31. var contact = new LegacyContact(1, "FAIR01", "Jane Doe", null, null,
  32. null, null, null, null, null, null, null, null, null, null);
  33. var data = new InMemoryLegacyDataAccess(contacts: [contact]);
  34. var sut = new LegacyLinkValidator(data);
  35. var result = await sut.ValidateAsync(LegacyLinkReference.ForContact(1));
  36. Assert.True(result.IsValid);
  37. }
  38. [Fact]
  39. public async Task ValidateAsync_JurisdictionJCode_IsCaseInsensitive_AC2()
  40. {
  41. var data = new InMemoryLegacyDataAccess(
  42. jurisdictions: [new("FAIR01", "Fairview Borough", null, null, null, null)]);
  43. var sut = new LegacyLinkValidator(data);
  44. var result = await sut.ValidateAsync(LegacyLinkReference.ForJurisdiction("fair01"));
  45. Assert.True(result.IsValid);
  46. }
  47. // ── AC #3 — invalid/non-existent reference rejected with descriptive error ─
  48. [Fact]
  49. public async Task ValidateAsync_JurisdictionJCode_NotFound_ReturnsFailureWithDescription_AC3()
  50. {
  51. var data = new InMemoryLegacyDataAccess(jurisdictions: []);
  52. var sut = new LegacyLinkValidator(data);
  53. var result = await sut.ValidateAsync(LegacyLinkReference.ForJurisdiction("UNKNOWN"));
  54. Assert.False(result.IsValid);
  55. Assert.NotNull(result.Error);
  56. Assert.Contains("UNKNOWN", result.Error);
  57. Assert.Contains("jurisdiction", result.Error, StringComparison.OrdinalIgnoreCase);
  58. }
  59. [Fact]
  60. public async Task ValidateAsync_KitId_NotFound_ReturnsFailureWithDescription_AC3()
  61. {
  62. var data = new InMemoryLegacyDataAccess(kits: []);
  63. var sut = new LegacyLinkValidator(data);
  64. var result = await sut.ValidateAsync(LegacyLinkReference.ForKit(9999));
  65. Assert.False(result.IsValid);
  66. Assert.NotNull(result.Error);
  67. Assert.Contains("9999", result.Error);
  68. Assert.Contains("kit", result.Error, StringComparison.OrdinalIgnoreCase);
  69. }
  70. [Fact]
  71. public async Task ValidateAsync_ContactId_NotFound_ReturnsFailureWithDescription_AC3()
  72. {
  73. var data = new InMemoryLegacyDataAccess(contacts: []);
  74. var sut = new LegacyLinkValidator(data);
  75. var result = await sut.ValidateAsync(LegacyLinkReference.ForContact(9999));
  76. Assert.False(result.IsValid);
  77. Assert.NotNull(result.Error);
  78. Assert.Contains("9999", result.Error);
  79. Assert.Contains("contact", result.Error, StringComparison.OrdinalIgnoreCase);
  80. }
  81. [Fact]
  82. public async Task ValidateAsync_BlankJCode_ReturnsFailureWithDescription_AC3()
  83. {
  84. var data = new InMemoryLegacyDataAccess();
  85. var sut = new LegacyLinkValidator(data);
  86. var result = await sut.ValidateAsync(new LegacyLinkReference(LegacyLinkType.JurisdictionJCode, " "));
  87. Assert.False(result.IsValid);
  88. Assert.NotNull(result.Error);
  89. Assert.Contains("required", result.Error, StringComparison.OrdinalIgnoreCase);
  90. }
  91. [Fact]
  92. public async Task ValidateAsync_KitId_NonIntegerValue_ReturnsFailureWithDescription_AC3()
  93. {
  94. var data = new InMemoryLegacyDataAccess();
  95. var sut = new LegacyLinkValidator(data);
  96. var result = await sut.ValidateAsync(new LegacyLinkReference(LegacyLinkType.KitId, "not-a-number"));
  97. Assert.False(result.IsValid);
  98. Assert.NotNull(result.Error);
  99. Assert.Contains("not-a-number", result.Error);
  100. }
  101. [Fact]
  102. public async Task ValidateAsync_ContactId_NonIntegerValue_ReturnsFailureWithDescription_AC3()
  103. {
  104. var data = new InMemoryLegacyDataAccess();
  105. var sut = new LegacyLinkValidator(data);
  106. var result = await sut.ValidateAsync(new LegacyLinkReference(LegacyLinkType.ContactId, "abc"));
  107. Assert.False(result.IsValid);
  108. Assert.NotNull(result.Error);
  109. }
  110. // ── AC #1 — factory methods produce correct link type and value ───────────
  111. [Fact]
  112. public void ForJurisdiction_SetsCorrectTypeAndValue_AC1()
  113. {
  114. var ref_ = LegacyLinkReference.ForJurisdiction("LAKE02");
  115. Assert.Equal(LegacyLinkType.JurisdictionJCode, ref_.Type);
  116. Assert.Equal("LAKE02", ref_.Value);
  117. }
  118. [Fact]
  119. public void ForKit_SetsCorrectTypeAndValue_AC1()
  120. {
  121. var ref_ = LegacyLinkReference.ForKit(101);
  122. Assert.Equal(LegacyLinkType.KitId, ref_.Type);
  123. Assert.Equal("101", ref_.Value);
  124. }
  125. [Fact]
  126. public void ForContact_SetsCorrectTypeAndValue_AC1()
  127. {
  128. var ref_ = LegacyLinkReference.ForContact(42);
  129. Assert.Equal(LegacyLinkType.ContactId, ref_.Type);
  130. Assert.Equal("42", ref_.Value);
  131. }
  132. [Fact]
  133. public void ForJurisdiction_BlankJCode_Throws_AC1()
  134. {
  135. Assert.Throws<ArgumentException>(() => LegacyLinkReference.ForJurisdiction(""));
  136. Assert.Throws<ArgumentException>(() => LegacyLinkReference.ForJurisdiction(" "));
  137. }
  138. }

Powered by TurnKey Linux.