Skip to content

Commit c531702

Browse files
test: AddCohortDistributionData unit test coverage (#829)
* chore: remove unnecessary using statements * test: add unit test coverage
1 parent 53fc73b commit c531702

4 files changed

Lines changed: 122 additions & 2 deletions

File tree

application/CohortManager/src/Functions/CohortDistributionServices/AddCohortDistributionData/AddCohortDistributionData.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
namespace NHS.CohortManager.CohortDistributionDataServices;
22

33
using System;
4-
using System.Diagnostics;
54
using System.IO;
65
using System.Net;
76
using System.Text;
87
using System.Text.Json;
98
using System.Threading.Tasks;
109
using Common;
11-
using Common.Interfaces;
1210
using DataServices.Client;
1311
using Microsoft.Azure.Functions.Worker;
1412
using Microsoft.Azure.Functions.Worker.Http;

application/CohortManager/src/Functions/Functions.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpParserTests", "..\..\..
222222
EndProject
223223
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileNameParserTests", "..\..\..\..\tests\UnitTests\SharedTests\FileNameParserTests\FileNameParserTests.csproj", "{0A68E48A-8249-4A74-B5B7-5921017D07BD}"
224224
EndProject
225+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddCohortDistributionDataTests", "..\..\..\..\tests\UnitTests\CohortDistributionTests\AddCohortDistributionDataTests\AddCohortDistributionDataTests.csproj", "{5D5C7C1C-37FE-4671-8BA2-138447049EE3}"
226+
EndProject
225227
Global
226228
GlobalSection(SolutionConfigurationPlatforms) = preSolution
227229
Debug|Any CPU = Debug|Any CPU
@@ -616,6 +618,10 @@ Global
616618
{C86FDA63-AA51-4B5F-A69B-DEF0266E268F}.Debug|Any CPU.Build.0 = Debug|Any CPU
617619
{C86FDA63-AA51-4B5F-A69B-DEF0266E268F}.Release|Any CPU.ActiveCfg = Release|Any CPU
618620
{C86FDA63-AA51-4B5F-A69B-DEF0266E268F}.Release|Any CPU.Build.0 = Release|Any CPU
621+
{5D5C7C1C-37FE-4671-8BA2-138447049EE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
622+
{5D5C7C1C-37FE-4671-8BA2-138447049EE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
623+
{5D5C7C1C-37FE-4671-8BA2-138447049EE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
624+
{5D5C7C1C-37FE-4671-8BA2-138447049EE3}.Release|Any CPU.Build.0 = Release|Any CPU
619625
EndGlobalSection
620626
GlobalSection(SolutionProperties) = preSolution
621627
HideSolutionNode = FALSE
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
namespace NHS.CohortManager.Tests.UnitTests.AddCohortDistributionDataTests;
2+
3+
using System.Net;
4+
using System.Text.Json;
5+
using Common;
6+
using DataServices.Client;
7+
using Model;
8+
using Moq;
9+
using NHS.CohortManager.CohortDistributionDataServices;
10+
using NHS.CohortManager.Tests.TestUtils;
11+
12+
[TestClass]
13+
public class AddCohortDistributionDataTests : DatabaseTestBaseSetup<AddCohortDistributionDataFunction>
14+
{
15+
private static readonly Mock<IExceptionHandler> _handleException = new();
16+
private static readonly Mock<IDataServiceClient<CohortDistribution>> _cohortDistributionDataService = new();
17+
private CohortDistributionParticipant _participantCsvRecord = new();
18+
19+
public AddCohortDistributionDataTests() : base((conn, logger, transaction, command, response) =>
20+
new AddCohortDistributionDataFunction(
21+
logger,
22+
response,
23+
_handleException.Object,
24+
_cohortDistributionDataService.Object))
25+
{
26+
CreateHttpResponseMock();
27+
}
28+
29+
[TestInitialize]
30+
public void TestInitialize()
31+
{
32+
_cohortDistributionDataService.Reset();
33+
_service = new AddCohortDistributionDataFunction(
34+
_loggerMock.Object,
35+
_createResponseMock.Object,
36+
_handleException.Object,
37+
_cohortDistributionDataService.Object);
38+
_participantCsvRecord = new CohortDistributionParticipant() { NhsNumber = "1234567890" };
39+
}
40+
41+
[DataRow("")]
42+
[DataRow("Invalid request body")]
43+
[TestMethod]
44+
public async Task Run_InvalidRequest_ReturnsInternalServerError(string badRequest)
45+
{
46+
// Arrange
47+
_request = SetupRequest(badRequest);
48+
49+
// Act
50+
var result = await _service.RunAsync(_request.Object);
51+
52+
// Assert
53+
Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode);
54+
}
55+
56+
[TestMethod]
57+
public async Task Run_InsertCohortDistributionDataSucceeds_ReturnsOk()
58+
{
59+
// Arrange
60+
var participantRecord = JsonSerializer.Serialize(_participantCsvRecord);
61+
_request = SetupRequest(participantRecord);
62+
63+
_cohortDistributionDataService.Setup(x => x.Add(It.IsAny<CohortDistribution>())).Returns(Task.FromResult(true));
64+
65+
// Act
66+
var result = await _service.RunAsync(_request.Object);
67+
68+
// Assert
69+
Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
70+
}
71+
72+
[TestMethod]
73+
public async Task Run_InsertCohortDistributionDataFails_ReturnsInternalServerError()
74+
{
75+
// Arrange
76+
var participantRecord = JsonSerializer.Serialize(_participantCsvRecord);
77+
_request = SetupRequest(participantRecord);
78+
79+
_cohortDistributionDataService.Setup(x => x.Add(It.IsAny<CohortDistribution>())).Returns(Task.FromResult(false));
80+
81+
// Act
82+
var result = await _service.RunAsync(_request.Object);
83+
84+
// Assert
85+
Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode);
86+
}
87+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="Moq" Version="4.20.72" />
16+
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
17+
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="../../../../application/CohortManager/src/Functions/CohortDistributionServices/AddCohortDistributionData/AddCohortDistributionData.csproj" />
22+
<ProjectReference Include="../../../TestUtils/TestUtils.csproj" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
27+
</ItemGroup>
28+
29+
</Project>

0 commit comments

Comments
 (0)