Skip to content

Commit

Permalink
Add test for cache clearing when fetching first question in a section
Browse files Browse the repository at this point in the history
  • Loading branch information
andymantell committed Sep 30, 2024
1 parent 1f5b63f commit 43bd501
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CO.CDP.AwsServices;
using CO.CDP.OrganisationApp.Models;
using CO.CDP.OrganisationApp.Pages.Forms;
using CO.CDP.OrganisationApp.Pages.Forms.ChoiceProviderStrategies;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand All @@ -12,6 +13,7 @@ public class FormsQuestionPageModelTest
{
private readonly Mock<IFormsEngine> _formsEngineMock;
private readonly Mock<ITempDataService> _tempDataServiceMock;
private readonly Mock<IChoiceProviderService> _choiceProviderServiceMock;
private readonly Mock<IFileHostManager> _fileHostManagerMock;
private readonly FormsQuestionPageModel _pageModel;
private readonly Guid TextQuestionId = Guid.NewGuid();
Expand All @@ -25,13 +27,22 @@ public FormsQuestionPageModelTest()
Questions = [new FormQuestion { Id = TextQuestionId, Type = FormQuestionType.Text, SummaryTitle = "Sample Question" }]
};

_formsEngineMock.Setup(f => f.ExecuteChoiceProviderStrategy(It.IsAny<CO.CDP.Forms.WebApiClient.FormQuestionOptions>()))
.ReturnsAsync(["Choices"]);

_formsEngineMock.Setup(f => f.GetFormSectionAsync(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>()))
.ReturnsAsync(form);
_tempDataServiceMock = new Mock<ITempDataService>();
_fileHostManagerMock = new Mock<IFileHostManager>();
_choiceProviderServiceMock = new Mock<IChoiceProviderService>();
_tempDataServiceMock.Setup(t => t.PeekOrDefault<FormQuestionAnswerState>(It.IsAny<string>()))
.Returns(new FormQuestionAnswerState());
_tempDataServiceMock.Setup(t => t.Remove(It.IsAny<string>()));
_pageModel = new FormsQuestionPageModel(_formsEngineMock.Object, _tempDataServiceMock.Object, _fileHostManagerMock.Object);

_pageModel.OrganisationId = Guid.NewGuid();
_pageModel.FormId = Guid.NewGuid();
_pageModel.SectionId = Guid.NewGuid();
}

[Fact]
Expand Down Expand Up @@ -59,6 +70,29 @@ public async Task OnGetAsync_ReturnsPage_WhenCurrentQuestionIsNotNull()
_pageModel.PartialViewModel.Should().NotBeNull();
}

[Fact]
public async Task OnGetAsync_ClearsFormSectionCache_WhenPreviousQuestionIsNull()
{
var formQuestion = new FormQuestion { Id = Guid.NewGuid(), Type = FormQuestionType.Text };
_formsEngineMock.Setup(f => f.GetCurrentQuestion(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid?>()))
.ReturnsAsync(formQuestion);

_formsEngineMock.Setup(f => f.GetPreviousQuestion(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>()))
.ReturnsAsync((FormQuestion?)null);

var tempDataKey = $"Form_{_pageModel.OrganisationId}_{_pageModel.FormId}_{_pageModel.SectionId}";

_tempDataServiceMock.Setup(td => td.Keys).Returns([tempDataKey]);

var result = await _pageModel.OnGetAsync();

_tempDataServiceMock.Verify(t => t.Remove(tempDataKey), Times.AtLeastOnce);

result.Should().BeOfType<PageResult>();
_pageModel.PartialViewName.Should().Be("_FormElementTextInput");
_pageModel.PartialViewModel.Should().NotBeNull();
}

[Fact]
public async Task OnPostAsync_RedirectsToPageNotFound_WhenCurrentQuestionIsNull()
{
Expand Down
1 change: 1 addition & 0 deletions Frontend/CO.CDP.OrganisationApp/IFormsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface IFormsEngine

Task SaveUpdateAnswers(Guid formId, Guid sectionId, Guid organisationId, FormQuestionAnswerState answerSet);
Task<string> CreateShareCodeAsync(Guid formId, Guid organisationId);
Task<List<string>?> ExecuteChoiceProviderStrategy(Forms.WebApiClient.FormQuestionOptions options);
}
3 changes: 3 additions & 0 deletions Frontend/CO.CDP.OrganisationApp/ITempDataService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

namespace CO.CDP.OrganisationApp;

public interface ITempDataService
{
IEnumerable<string> Keys { get; }

void Put<T>(string key, T value);

T? Get<T>(string key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private void SaveAnswerToTempData(FormQuestion question, FormAnswer? answer)

private void ClearSectionCache()
{
foreach (var key in TempData.Keys.ToList())
foreach (var key in tempDataService.Keys.ToList())
{
if (key.StartsWith($"Form_{OrganisationId}_{FormId}_{SectionId}"))
{
Expand Down
2 changes: 2 additions & 0 deletions Frontend/CO.CDP.OrganisationApp/TempDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace CO.CDP.OrganisationApp;

public class TempDataService(ITempDataDictionary tempData) : ITempDataService
{
public IEnumerable<string> Keys => tempData.Keys;

public void Put<T>(string key, T value)
{
tempData[key] = JsonSerializer.Serialize(value);
Expand Down

0 comments on commit 43bd501

Please sign in to comment.