Skip to content

Commit

Permalink
feat: add AdminEventService Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
praivesi committed Sep 28, 2024
1 parent 42c6d00 commit 65be6a9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 24 deletions.
17 changes: 16 additions & 1 deletion src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public interface IAdminEventRepository
/// <param name="eventDetails">Event details instance.</param>
/// <returns>Returns the updated record of the event details.</returns>
Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails);

/// <summary>
/// Deletes the event details.
/// </summary>
/// <param name="eventDetails">Event details instance.</param>
/// <returns>Removed EventID of event details instance.</returns>
Task<Guid> DeleteEvent(AdminEventDetails eventDetails);
}

/// <summary>
Expand All @@ -65,7 +72,6 @@ public async Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails)
public async Task<List<AdminEventDetails>> GetEvents()
{
var tableClient = await GetTableClientAsync();

var eventDetailsList = new List<AdminEventDetails>();

await foreach (var entity in tableClient.QueryAsync<AdminEventDetails>())
Expand Down Expand Up @@ -101,6 +107,15 @@ public async Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails
return eventDetails;
}

public async Task<Guid> DeleteEvent(AdminEventDetails eventDetails)
{
var tableClient = await GetTableClientAsync();

await tableClient.DeleteEntityAsync(eventDetails.PartitionKey, eventDetails.RowKey);

return eventDetails.EventId;
}

private async Task<TableClient> GetTableClientAsync()
{
TableClient tableClient = _tableServiceClient.GetTableClient(_storageAccountSettings.TableStorage.TableName);
Expand Down
15 changes: 15 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Services/AdminEventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public interface IAdminEventService
/// <param name="eventDetails">Event details to update.</param>
/// <returns>Returns the updated event details.</returns>
Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails);

/// <summary>
/// Deletes the event details.
/// </summary>
/// <param name="eventDetails">Event details to update.</param>
/// <returns>Removed EventID of event details instance.</returns>
Task<Guid> DeleteEvent(AdminEventDetails eventDetails);
}

/// <summary>
Expand Down Expand Up @@ -75,6 +82,14 @@ public async Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails

return result;
}

/// <inheritdoc />
public async Task<Guid> DeleteEvent(AdminEventDetails eventDetails)
{
var result = await this._repository.DeleteEvent(eventDetails).ConfigureAwait(false);

return result;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Configuration;

using Azure;
using Azure;
using Azure.Data.Tables;

using AzureOpenAIProxy.ApiApp.Configurations;
Expand All @@ -14,15 +12,13 @@
using NSubstitute;
using NSubstitute.ExceptionExtensions;

using Xunit.Sdk;

namespace AzureOpenAIProxy.ApiApp.Tests.Repositories;

public class AdminEventRepositoryTests
{
private StorageAccountSettings mockSettings;
private TableServiceClient mockTableServiceClient;
private TableClient mockTableClient;
private readonly StorageAccountSettings mockSettings;
private readonly TableServiceClient mockTableServiceClient;
private readonly TableClient mockTableClient;

public AdminEventRepositoryTests()
{
Expand Down Expand Up @@ -163,4 +159,23 @@ await mockTableClient.Received(1)
Arg.Any<Azure.ETag>(),
TableUpdateMode.Replace);
}

[Fact]
public async Task Given_Instance_When_DeleteEvent_Invoked_Then_It_Should_Invoke_DeleteEntityAsync_Method()
{
// Arrange
var eventDetails = new AdminEventDetails();
var repository = new AdminEventRepository(mockTableServiceClient, mockSettings);
var eventId = Guid.NewGuid();

eventDetails.EventId = eventId;

// Act
Guid deletedEventId = await repository.DeleteEvent(eventDetails);

// Assert
deletedEventId.Should().Be(eventId);
await mockTableClient.Received(1)
.DeleteEntityAsync(Arg.Any<string>(), Arg.Any<string>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace AzureOpenAIProxy.ApiApp.Tests.Services;

public class AdminEventServiceTests
{
private readonly IAdminEventRepository mockRepository;

public AdminEventServiceTests()
{
mockRepository = Substitute.For<IAdminEventRepository>();
}

[Fact]
public void Given_ServiceCollection_When_AddAdminEventService_Invoked_Then_It_Should_Contain_AdminEventService()
{
Expand All @@ -33,8 +40,7 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti
{
// Arrange
var eventDetails = new AdminEventDetails();
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);
var service = new AdminEventService(mockRepository);

// Act
Func<Task> func = async () => await service.CreateEvent(eventDetails);
Expand All @@ -47,8 +53,7 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti
public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);
var service = new AdminEventService(mockRepository);

// Act
Func<Task> func = async () => await service.GetEvents();
Expand All @@ -65,12 +70,11 @@ public async Task Given_Failure_In_Get_Entity_When_GetEvent_Invoked_Then_It_Shou
{
// Arrange
var eventId = Guid.NewGuid();
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);
var service = new AdminEventService(mockRepository);

var exception = new RequestFailedException(statusCode, "Request Failed", default, default);

repository.GetEvent(Arg.Any<Guid>()).ThrowsAsync(exception);
mockRepository.GetEvent(Arg.Any<Guid>()).ThrowsAsync(exception);

// Act
Func<Task> func = () => service.GetEvent(eventId);
Expand All @@ -85,8 +89,7 @@ public async Task Given_Failure_In_Get_Entity_When_GetEvent_Invoked_Then_It_Shou
public async Task Given_Exist_EventId_When_GetEvent_Invoked_Then_It_Should_Return_AdminEventDetails(string eventId)
{
// Arrange
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);
var service = new AdminEventService(mockRepository);

var eventDetails = new AdminEventDetails
{
Expand All @@ -95,7 +98,7 @@ public async Task Given_Exist_EventId_When_GetEvent_Invoked_Then_It_Should_Retur

var guid = Guid.Parse(eventId);

repository.GetEvent(guid).Returns(Task.FromResult(eventDetails));
mockRepository.GetEvent(guid).Returns(Task.FromResult(eventDetails));

// Act
var result = await service.GetEvent(guid);
Expand All @@ -105,18 +108,37 @@ public async Task Given_Exist_EventId_When_GetEvent_Invoked_Then_It_Should_Retur
}

[Fact]
public void Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Throw_Exception()
public async Task Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Return_Updated_Event_Details()
{
// Arrange
var eventId = Guid.NewGuid();
var eventDetails = new AdminEventDetails();
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);
var service = new AdminEventService(mockRepository);

mockRepository.UpdateEvent(eventId, eventDetails).Returns(eventDetails);

// Act
Func<Task> func = async () => await service.UpdateEvent(eventId, eventDetails);
var updatedEventDetails = await service.UpdateEvent(eventId, eventDetails);

// Assert
func.Should().ThrowAsync<NotImplementedException>();
updatedEventDetails.Should().BeEquivalentTo(eventDetails);
}

[Fact]
public async Task Given_Instance_When_DeleteEvent_Invoked_Then_It_Should_Return_Deleted_Event_Id()
{
// Arrange
var eventId = Guid.NewGuid();
var eventDetails = new AdminEventDetails();
var service = new AdminEventService(mockRepository);

eventDetails.EventId = eventId;
mockRepository.DeleteEvent(eventDetails).Returns(eventDetails.EventId);

// Act
var deletedEventId = await service.DeleteEvent(eventDetails);

// Assert
deletedEventId.Should().Be(eventId);
}
}

0 comments on commit 65be6a9

Please sign in to comment.