|
| 1 | +using Digdir.Domain.Dialogporten.Application.Features.V1.EndUser.Dialogs.Queries.Search; |
| 2 | +using Digdir.Domain.Dialogporten.Application.Integration.Tests.Common; |
| 3 | +using FluentAssertions; |
| 4 | +using static Digdir.Domain.Dialogporten.Application.Integration.Tests.Common.Common; |
| 5 | + |
| 6 | +namespace Digdir.Domain.Dialogporten.Application.Integration.Tests.Features.V1.EndUser.Dialogs.Queries.Search; |
| 7 | + |
| 8 | +[Collection(nameof(DialogCqrsCollectionFixture))] |
| 9 | +public class DueAtFilterTests : ApplicationCollectionFixture |
| 10 | +{ |
| 11 | + public DueAtFilterTests(DialogApplication application) : base(application) { } |
| 12 | + |
| 13 | + [Theory, MemberData(nameof(DueAtTestData))] |
| 14 | + public async Task Should_Filter_On_Due_Date(DateFilterTestData testData) |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + var currentYear = DateTimeOffset.UtcNow.Year; |
| 18 | + |
| 19 | + var oneYearInTheFuture = currentYear + 1; |
| 20 | + var twoYearsInTheFuture = currentYear + 2; |
| 21 | + var threeYearsInTheFuture = currentYear + 3; |
| 22 | + var fourYearsInTheFuture = currentYear + 4; |
| 23 | + |
| 24 | + var dialogOneYearInTheFuture = await Application.CreateDialogWithDateInYear(oneYearInTheFuture, DueAt); |
| 25 | + var dialogTwoYearsInTheFuture = await Application.CreateDialogWithDateInYear(twoYearsInTheFuture, DueAt); |
| 26 | + var dialogThreeYearsInTheFuture = await Application.CreateDialogWithDateInYear(threeYearsInTheFuture, DueAt); |
| 27 | + var dialogFourYearsInTheFuture = await Application.CreateDialogWithDateInYear(fourYearsInTheFuture, DueAt); |
| 28 | + |
| 29 | + // Act |
| 30 | + var response = await Application.Send(new SearchDialogQuery |
| 31 | + { |
| 32 | + Party = [Party], |
| 33 | + DueAfter = testData.AfterYear.HasValue ? CreateDateFromYear(testData.AfterYear.Value) : null, |
| 34 | + DueBefore = testData.BeforeYear.HasValue ? CreateDateFromYear(testData.BeforeYear.Value) : null |
| 35 | + }); |
| 36 | + |
| 37 | + // Assert |
| 38 | + response.TryPickT0(out var result, out _).Should().BeTrue(); |
| 39 | + result.Should().NotBeNull(); |
| 40 | + |
| 41 | + result.Items.Should().HaveCount(testData.ExpectedCount); |
| 42 | + foreach (var year in testData.ExpectedYears) |
| 43 | + { |
| 44 | + var dialogId = year switch |
| 45 | + { |
| 46 | + _ when year == oneYearInTheFuture => dialogOneYearInTheFuture, |
| 47 | + _ when year == twoYearsInTheFuture => dialogTwoYearsInTheFuture, |
| 48 | + _ when year == threeYearsInTheFuture => dialogThreeYearsInTheFuture, |
| 49 | + _ when year == fourYearsInTheFuture => dialogFourYearsInTheFuture, |
| 50 | + _ => throw new ArgumentOutOfRangeException() |
| 51 | + }; |
| 52 | + |
| 53 | + result.Items.Should().ContainSingle(x => x.Id == dialogId); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public async Task Cannot_Filter_On_DueAfter_With_Value_Greater_Than_DueBefore() |
| 59 | + { |
| 60 | + // Act |
| 61 | + var response = await Application.Send(new SearchDialogQuery |
| 62 | + { |
| 63 | + Party = [Party], |
| 64 | + DueAfter = CreateDateFromYear(2022), |
| 65 | + DueBefore = CreateDateFromYear(2021) |
| 66 | + }); |
| 67 | + |
| 68 | + // Assert |
| 69 | + response.TryPickT1(out var result, out _).Should().BeTrue(); |
| 70 | + result.Should().NotBeNull(); |
| 71 | + } |
| 72 | + |
| 73 | + public static IEnumerable<object[]> DueAtTestData() |
| 74 | + { |
| 75 | + var currentYear = DateTimeOffset.UtcNow.Year; |
| 76 | + |
| 77 | + // The numbers added to "currentYear" here represent future years relative to the current year. |
| 78 | + // This is done to create test data for dialogs that are due "soon" (1 to 4 years ahead). |
| 79 | + // This approach ensures that the tests remain valid and relevant regardless of the current date. |
| 80 | + return new List<object[]> |
| 81 | + { |
| 82 | + new object[] |
| 83 | + { |
| 84 | + new DateFilterTestData |
| 85 | + { |
| 86 | + AfterYear = currentYear + 3, |
| 87 | + BeforeYear = null, |
| 88 | + ExpectedCount = 2, |
| 89 | + ExpectedYears = [currentYear + 3, currentYear + 4] |
| 90 | + } |
| 91 | + }, |
| 92 | + new object[] |
| 93 | + { |
| 94 | + new DateFilterTestData |
| 95 | + { |
| 96 | + AfterYear = null, |
| 97 | + BeforeYear = currentYear + 2, |
| 98 | + ExpectedCount = 2, |
| 99 | + ExpectedYears = [currentYear + 1, currentYear + 2] |
| 100 | + } |
| 101 | + }, |
| 102 | + new object[] |
| 103 | + { |
| 104 | + new DateFilterTestData |
| 105 | + { |
| 106 | + AfterYear = currentYear + 1, |
| 107 | + BeforeYear = currentYear + 2, |
| 108 | + ExpectedCount = 2, |
| 109 | + ExpectedYears = [currentYear + 1, currentYear + 2] |
| 110 | + } |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | +} |
0 commit comments