Skip to content

Commit

Permalink
Added project for writing unit tests. Created test for Cosmos DB add …
Browse files Browse the repository at this point in the history
…item
  • Loading branch information
Torkelsen committed Nov 20, 2024
1 parent 59e10af commit b6eb8b3
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
/handlenett-backend/serverless/functions/Properties/serviceDependencies.local.json
/handlenett-backend/web-api/HandlenettAPI/bin/Debug/net9.0
/handlenett-backend/web-api/HandlenettAPI/bin/Release/net9.0
/handlenett-backend/web-api/HandlenettAPITests/obj
/handlenett-backend/web-api/HandlenettAPITests/bin/Release/net9.0
6 changes: 6 additions & 0 deletions handlenett-backend/web-api/HandlenettAPI/HandlenettAPI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.11.35208.52
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HandlenettAPI", "HandlenettAPI.csproj", "{F0FB0E48-BC58-4519-9004-410587DCF297}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HandlenettAPITests", "..\HandlenettAPITests\HandlenettAPITests.csproj", "{2777A507-BF10-4977-9ABE-E0E722F7C1E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{F0FB0E48-BC58-4519-9004-410587DCF297}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0FB0E48-BC58-4519-9004-410587DCF297}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0FB0E48-BC58-4519-9004-410587DCF297}.Release|Any CPU.Build.0 = Release|Any CPU
{2777A507-BF10-4977-9ABE-E0E722F7C1E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2777A507-BF10-4977-9ABE-E0E722F7C1E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2777A507-BF10-4977-9ABE-E0E722F7C1E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2777A507-BF10-4977-9ABE-E0E722F7C1E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HandlenettAPI\HandlenettAPI.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Azure.Cosmos;
using Moq;
using System.Net;

namespace HandlenettAPITests.Helpers
{
public static class CosmosResponseFactory
{
public static ItemResponse<T> CreateItemResponse<T>(T item)
{
var responseMock = new Mock<ItemResponse<T>>();
responseMock.SetupGet(r => r.Resource).Returns(item);
responseMock.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.Created);
return responseMock.Object;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using Xunit;
using Microsoft.Azure.Cosmos;
using HandlenettAPI.Services;
using HandlenettAPI.DTO;
using HandlenettAPI.Models;


namespace HandlenettAPITests.Services
{
public class CosmosDBServiceTests
{
private readonly Mock<Container> _mockContainer;
private readonly CosmosDBService _service;

public CosmosDBServiceTests()
{
_mockContainer = new Mock<Container>();
var mockCosmosClient = new Mock<CosmosClient>();

mockCosmosClient
.Setup(client => client.GetContainer("TestDatabase", "TestContainer"))
.Returns(_mockContainer.Object);

_service = new CosmosDBService(mockCosmosClient.Object, "TestDatabase", "TestContainer");
}

[Fact]
public async Task Add_ReturnsCreatedItem()
{
// Arrange: Prepare input data
var itemDto = new ItemPostDTO { Name = "Test Item" };
var username = "testuser";
var expectedItem = new Item
{
Id = Guid.NewGuid().ToString(),
Name = "Test Item",
CreatedBy = username,
UpdatedBy = username
};

// Simulate Cosmos DB's response
_mockContainer
.Setup(container => container.CreateItemAsync(
It.Is<Item>(i => i.Name == expectedItem.Name && i.CreatedBy == expectedItem.CreatedBy),
It.IsAny<PartitionKey>(),
null,
default))
.ReturnsAsync(Helpers.CosmosResponseFactory.CreateItemResponse(expectedItem));


// Act: Call the method under test
var result = await _service.Add(itemDto, username);

// Assert: Verify the returned item
Assert.NotNull(result);
Assert.Equal(expectedItem.Name, result.Name);
Assert.Equal(expectedItem.CreatedBy, result.CreatedBy);

// Verify that the container's CreateItemAsync method was called
_mockContainer.Verify(container => container.CreateItemAsync(
It.Is<Item>(i => i.Name == "Test Item"),
It.IsAny<PartitionKey>(),
null,
default), Times.Once);
}
}
}

0 comments on commit b6eb8b3

Please sign in to comment.