-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added project for writing unit tests. Created test for Cosmos DB add …
…item
- Loading branch information
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
handlenett-backend/web-api/HandlenettAPITests/HandlenettAPITests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
17 changes: 17 additions & 0 deletions
17
handlenett-backend/web-api/HandlenettAPITests/Helpers/CosmosResponseFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
handlenett-backend/web-api/HandlenettAPITests/Services/CosmosDBServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |