|
| 1 | +using AwesomeAssertions; |
| 2 | +using NUnit.Framework; |
| 3 | +using OpenAlprWebhookProcessor.Data; |
| 4 | +using OpenAlprWebhookProcessor.Features.LicensePlates.Queries.GetStatistics; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Tests.TestHelpers; |
| 9 | + |
| 10 | +namespace Tests.Data.Repositories |
| 11 | +{ |
| 12 | + [Parallelizable(ParallelScope.Self)] |
| 13 | + [TestFixture] |
| 14 | + public class PlateGroupRepositoryTests : TestBase |
| 15 | + { |
| 16 | + [Test] |
| 17 | + public async Task GetPlateStatisticsAggregationAsync_PlateNumberInBestNumberOnly_ReturnsCorrectCount() |
| 18 | + { |
| 19 | + // Arrange |
| 20 | + var plateNumber = "ABC123"; |
| 21 | + var now = DateTimeOffset.UtcNow; |
| 22 | + var last90DaysEpoch = now.AddDays(-90).ToUnixTimeMilliseconds(); |
| 23 | + |
| 24 | + // Create plate groups where the plate number is only in BestNumber |
| 25 | + var plateGroup1 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-30).ToUnixTimeMilliseconds()); |
| 26 | + var plateGroup2 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-60).ToUnixTimeMilliseconds()); |
| 27 | + var plateGroup3 = TestDataFactory.CreateTestPlateGroup("DIFFERENT", now.AddDays(-10).ToUnixTimeMilliseconds()); |
| 28 | + |
| 29 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup1); |
| 30 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup2); |
| 31 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup3); |
| 32 | + await UnitOfWork.SaveChangesAsync(); |
| 33 | + |
| 34 | + // Act |
| 35 | + var result = await UnitOfWork.PlateGroups.GetPlateStatisticsAggregationAsync( |
| 36 | + plateNumber, last90DaysEpoch, GetCancellationToken()); |
| 37 | + |
| 38 | + // Assert |
| 39 | + result.Should().NotBeNull(); |
| 40 | + result.TotalCount.Should().Be(2); |
| 41 | + result.Last90DaysCount.Should().Be(2); |
| 42 | + result.MinEpoch.Should().Be(plateGroup2.ReceivedOnEpoch); |
| 43 | + result.MaxEpoch.Should().Be(plateGroup1.ReceivedOnEpoch); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public async Task GetPlateStatisticsAggregationAsync_PlateNumberInPossibleNumbersOnly_ReturnsCorrectCount() |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var plateNumber = "XYZ789"; |
| 51 | + var now = DateTimeOffset.UtcNow; |
| 52 | + var last90DaysEpoch = now.AddDays(-90).ToUnixTimeMilliseconds(); |
| 53 | + |
| 54 | + // Create plate groups where the plate number is only in PossibleNumbers |
| 55 | + var plateGroup1 = TestDataFactory.CreateTestPlateGroup("BEST123", now.AddDays(-20).ToUnixTimeMilliseconds()); |
| 56 | + plateGroup1.PossibleNumbers = new List<PlateGroupPossibleNumbers> |
| 57 | + { |
| 58 | + new PlateGroupPossibleNumbers { Id = Guid.NewGuid(), PlateGroupId = plateGroup1.Id, Number = plateNumber } |
| 59 | + }; |
| 60 | + |
| 61 | + var plateGroup2 = TestDataFactory.CreateTestPlateGroup("BEST456", now.AddDays(-50).ToUnixTimeMilliseconds()); |
| 62 | + plateGroup2.PossibleNumbers = new List<PlateGroupPossibleNumbers> |
| 63 | + { |
| 64 | + new PlateGroupPossibleNumbers { Id = Guid.NewGuid(), PlateGroupId = plateGroup2.Id, Number = plateNumber } |
| 65 | + }; |
| 66 | + |
| 67 | + var plateGroup3 = TestDataFactory.CreateTestPlateGroup("DIFFERENT", now.AddDays(-10).ToUnixTimeMilliseconds()); |
| 68 | + |
| 69 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup1); |
| 70 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup2); |
| 71 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup3); |
| 72 | + await UnitOfWork.SaveChangesAsync(); |
| 73 | + |
| 74 | + // Act |
| 75 | + var result = await UnitOfWork.PlateGroups.GetPlateStatisticsAggregationAsync( |
| 76 | + plateNumber, last90DaysEpoch, GetCancellationToken()); |
| 77 | + |
| 78 | + // Assert |
| 79 | + result.Should().NotBeNull(); |
| 80 | + result.TotalCount.Should().Be(2); |
| 81 | + result.Last90DaysCount.Should().Be(2); |
| 82 | + result.MinEpoch.Should().Be(plateGroup2.ReceivedOnEpoch); |
| 83 | + result.MaxEpoch.Should().Be(plateGroup1.ReceivedOnEpoch); |
| 84 | + } |
| 85 | + |
| 86 | + [Test] |
| 87 | + public async Task GetPlateStatisticsAggregationAsync_PlateNumberInBothBestAndPossible_CountsOnlyOnce() |
| 88 | + { |
| 89 | + // Arrange |
| 90 | + var plateNumber = "DUPLICATE123"; |
| 91 | + var now = DateTimeOffset.UtcNow; |
| 92 | + var last90DaysEpoch = now.AddDays(-90).ToUnixTimeMilliseconds(); |
| 93 | + |
| 94 | + // Create a plate group where the plate number is in BOTH BestNumber AND PossibleNumbers |
| 95 | + var plateGroup1 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-30).ToUnixTimeMilliseconds()); |
| 96 | + plateGroup1.PossibleNumbers = new List<PlateGroupPossibleNumbers> |
| 97 | + { |
| 98 | + new PlateGroupPossibleNumbers { Id = Guid.NewGuid(), PlateGroupId = plateGroup1.Id, Number = plateNumber }, |
| 99 | + new PlateGroupPossibleNumbers { Id = Guid.NewGuid(), PlateGroupId = plateGroup1.Id, Number = "OTHER123" } |
| 100 | + }; |
| 101 | + |
| 102 | + // Create another plate group with different plate number |
| 103 | + var plateGroup2 = TestDataFactory.CreateTestPlateGroup("DIFFERENT456", now.AddDays(-60).ToUnixTimeMilliseconds()); |
| 104 | + |
| 105 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup1); |
| 106 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup2); |
| 107 | + await UnitOfWork.SaveChangesAsync(); |
| 108 | + |
| 109 | + // Act |
| 110 | + var result = await UnitOfWork.PlateGroups.GetPlateStatisticsAggregationAsync( |
| 111 | + plateNumber, last90DaysEpoch, GetCancellationToken()); |
| 112 | + |
| 113 | + // Assert |
| 114 | + result.Should().NotBeNull(); |
| 115 | + result.TotalCount.Should().Be(1); // Should count only once, not twice |
| 116 | + result.Last90DaysCount.Should().Be(1); |
| 117 | + result.MinEpoch.Should().Be(plateGroup1.ReceivedOnEpoch); |
| 118 | + result.MaxEpoch.Should().Be(plateGroup1.ReceivedOnEpoch); |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + public async Task GetPlateStatisticsAggregationAsync_MultiplePlateGroupsWithMixedMatches_ReturnsCorrectAggregation() |
| 123 | + { |
| 124 | + // Arrange |
| 125 | + var plateNumber = "MIXED123"; |
| 126 | + var now = DateTimeOffset.UtcNow; |
| 127 | + var last90DaysEpoch = now.AddDays(-90).ToUnixTimeMilliseconds(); |
| 128 | + |
| 129 | + // Plate group 1: BestNumber matches |
| 130 | + var plateGroup1 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-10).ToUnixTimeMilliseconds()); |
| 131 | + |
| 132 | + // Plate group 2: PossibleNumbers matches |
| 133 | + var plateGroup2 = TestDataFactory.CreateTestPlateGroup("BEST456", now.AddDays(-40).ToUnixTimeMilliseconds()); |
| 134 | + plateGroup2.PossibleNumbers = new List<PlateGroupPossibleNumbers> |
| 135 | + { |
| 136 | + new PlateGroupPossibleNumbers { Id = Guid.NewGuid(), PlateGroupId = plateGroup2.Id, Number = plateNumber } |
| 137 | + }; |
| 138 | + |
| 139 | + // Plate group 3: Both BestNumber and PossibleNumbers match (should count only once) |
| 140 | + var plateGroup3 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-70).ToUnixTimeMilliseconds()); |
| 141 | + plateGroup3.PossibleNumbers = new List<PlateGroupPossibleNumbers> |
| 142 | + { |
| 143 | + new PlateGroupPossibleNumbers { Id = Guid.NewGuid(), PlateGroupId = plateGroup3.Id, Number = plateNumber } |
| 144 | + }; |
| 145 | + |
| 146 | + // Plate group 4: No match |
| 147 | + var plateGroup4 = TestDataFactory.CreateTestPlateGroup("NOMATCH789", now.AddDays(-20).ToUnixTimeMilliseconds()); |
| 148 | + |
| 149 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup1); |
| 150 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup2); |
| 151 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup3); |
| 152 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup4); |
| 153 | + await UnitOfWork.SaveChangesAsync(); |
| 154 | + |
| 155 | + // Act |
| 156 | + var result = await UnitOfWork.PlateGroups.GetPlateStatisticsAggregationAsync( |
| 157 | + plateNumber, last90DaysEpoch, GetCancellationToken()); |
| 158 | + |
| 159 | + // Assert |
| 160 | + result.Should().NotBeNull(); |
| 161 | + result.TotalCount.Should().Be(3); // plateGroup1, plateGroup2, plateGroup3 (counted once) |
| 162 | + result.Last90DaysCount.Should().Be(3); // All are within 90 days |
| 163 | + result.MinEpoch.Should().Be(plateGroup3.ReceivedOnEpoch); // Oldest matching |
| 164 | + result.MaxEpoch.Should().Be(plateGroup1.ReceivedOnEpoch); // Newest matching |
| 165 | + } |
| 166 | + |
| 167 | + [Test] |
| 168 | + public async Task GetPlateStatisticsAggregationAsync_WithLast90DaysFiltering_ReturnsCorrectCounts() |
| 169 | + { |
| 170 | + // Arrange |
| 171 | + var plateNumber = "TIMETEST123"; |
| 172 | + var now = DateTimeOffset.UtcNow; |
| 173 | + var last90DaysEpoch = now.AddDays(-90).ToUnixTimeMilliseconds(); |
| 174 | + |
| 175 | + // Plate group 1: Within 90 days |
| 176 | + var plateGroup1 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-30).ToUnixTimeMilliseconds()); |
| 177 | + |
| 178 | + // Plate group 2: Within 90 days |
| 179 | + var plateGroup2 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-60).ToUnixTimeMilliseconds()); |
| 180 | + |
| 181 | + // Plate group 3: Outside 90 days (older) |
| 182 | + var plateGroup3 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-120).ToUnixTimeMilliseconds()); |
| 183 | + |
| 184 | + // Plate group 4: Outside 90 days (much older) |
| 185 | + var plateGroup4 = TestDataFactory.CreateTestPlateGroup(plateNumber, now.AddDays(-200).ToUnixTimeMilliseconds()); |
| 186 | + |
| 187 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup1); |
| 188 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup2); |
| 189 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup3); |
| 190 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup4); |
| 191 | + await UnitOfWork.SaveChangesAsync(); |
| 192 | + |
| 193 | + // Act |
| 194 | + var result = await UnitOfWork.PlateGroups.GetPlateStatisticsAggregationAsync( |
| 195 | + plateNumber, last90DaysEpoch, GetCancellationToken()); |
| 196 | + |
| 197 | + // Assert |
| 198 | + result.Should().NotBeNull(); |
| 199 | + result.TotalCount.Should().Be(4); // All plate groups |
| 200 | + result.Last90DaysCount.Should().Be(2); // Only plateGroup1 and plateGroup2 |
| 201 | + result.MinEpoch.Should().Be(plateGroup4.ReceivedOnEpoch); // Oldest overall |
| 202 | + result.MaxEpoch.Should().Be(plateGroup1.ReceivedOnEpoch); // Newest overall |
| 203 | + } |
| 204 | + |
| 205 | + [Test] |
| 206 | + public async Task GetPlateStatisticsAggregationAsync_NoMatchingPlates_ReturnsZeroAggregation() |
| 207 | + { |
| 208 | + // Arrange |
| 209 | + var plateNumber = "NOTFOUND123"; |
| 210 | + var now = DateTimeOffset.UtcNow; |
| 211 | + var last90DaysEpoch = now.AddDays(-90).ToUnixTimeMilliseconds(); |
| 212 | + |
| 213 | + // Create some plate groups that don't match |
| 214 | + var plateGroup1 = TestDataFactory.CreateTestPlateGroup("DIFFERENT1", now.AddDays(-30).ToUnixTimeMilliseconds()); |
| 215 | + var plateGroup2 = TestDataFactory.CreateTestPlateGroup("DIFFERENT2", now.AddDays(-60).ToUnixTimeMilliseconds()); |
| 216 | + |
| 217 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup1); |
| 218 | + await UnitOfWork.PlateGroups.AddAsync(plateGroup2); |
| 219 | + await UnitOfWork.SaveChangesAsync(); |
| 220 | + |
| 221 | + // Act |
| 222 | + var result = await UnitOfWork.PlateGroups.GetPlateStatisticsAggregationAsync( |
| 223 | + plateNumber, last90DaysEpoch, GetCancellationToken()); |
| 224 | + |
| 225 | + // Assert |
| 226 | + result.Should().NotBeNull(); |
| 227 | + result.TotalCount.Should().Be(0); |
| 228 | + result.Last90DaysCount.Should().Be(0); |
| 229 | + result.MinEpoch.Should().Be(0); |
| 230 | + result.MaxEpoch.Should().Be(0); |
| 231 | + } |
| 232 | + |
| 233 | + [Test] |
| 234 | + public async Task GetPlateStatisticsAggregationAsync_EmptyDatabase_ReturnsZeroAggregation() |
| 235 | + { |
| 236 | + // Arrange |
| 237 | + var plateNumber = "EMPTY123"; |
| 238 | + var now = DateTimeOffset.UtcNow; |
| 239 | + var last90DaysEpoch = now.AddDays(-90).ToUnixTimeMilliseconds(); |
| 240 | + |
| 241 | + // Act (no data in database) |
| 242 | + var result = await UnitOfWork.PlateGroups.GetPlateStatisticsAggregationAsync( |
| 243 | + plateNumber, last90DaysEpoch, GetCancellationToken()); |
| 244 | + |
| 245 | + // Assert |
| 246 | + result.Should().NotBeNull(); |
| 247 | + result.TotalCount.Should().Be(0); |
| 248 | + result.Last90DaysCount.Should().Be(0); |
| 249 | + result.MinEpoch.Should().Be(0); |
| 250 | + result.MaxEpoch.Should().Be(0); |
| 251 | + } |
| 252 | + } |
| 253 | +} |
0 commit comments