Skip to content

Commit

Permalink
Updated chat logic. Created unit and integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisIvanov committed Jun 17, 2024
1 parent 387e546 commit 7e20238
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 151 deletions.
4 changes: 2 additions & 2 deletions src/server/CookingApp/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public async Task<IActionResult> GetChatsByApiGenId([FromRoute] string apiGenId)
return Ok(chats);
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteChat([FromQuery] string id)
[HttpDelete("chat/{id}")]
public async Task<IActionResult> DeleteChat([FromRoute] string id)
{
_logger.LogInformation(TaskInformationMessages.ChatService.DeleteUserChatAttempt);
var result = await _chatService.DeleteAsync(id);
Expand Down
31 changes: 19 additions & 12 deletions src/server/CookingApp/Services/ChatService/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,22 @@ public async Task UpdateTitle(string id, string newTitle)

public async Task<int> DeleteAsync(string id)
{
var chat = await _chatRepo.GetByIdAsync(id);
if (chat != null)
try
{
var chat = await GetByApiGenIdAsync(id);
if (chat != null)
{
_logger.LogInformation(SuccessMessages.ChatService.DeleteOperationSuccess);
await _chatRepo.DeleteAsync(chat);
return 1;
}
}
catch (Exception ex)
{
_logger.LogInformation(SuccessMessages.ChatService.DeleteOperationSuccess);
await _chatRepo.DeleteAsync(chat);
return 1;
_logger.LogInformation(ExceptionMessages.ChatService.DeleteOperationFail);
_logger.LogError(ex.Message);
}

_logger.LogInformation(ExceptionMessages.ChatService.DeleteOperationFail);
return 0;
}

Expand Down Expand Up @@ -110,14 +117,14 @@ public async Task<ChatCompletionCreateResponse> CreateChatAsync(string request)
N = 1,
});

// Creates a new Chat where later interaction will be stored
//var userChat = CreateNewChat(completionResult, userId);

// Testing purposes
var userChat = CreateNewChat(completionResult, request, "a1b2c3");

if (completionResult.Successful)
{

// Creates a new Chat where later interaction will be stored
//var userChat = CreateNewChat(completionResult, userId);

// Testing purposes
var userChat = CreateNewChat(completionResult, request, "a1b2c3");
_logger.LogInformation(SuccessMessages.ChatGPT.ResponseSuccess);
// workout if info is needed inside the logger
_logger.LogInformation($"{JsonSerializer.Serialize(completionResult)}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,172 @@ public async Task EnsureExistingChatIsFoundAndContentIsUpdated()
{
// Arrange
var initialMessage = "I need a simple and delicious recipe for dinner.";
var initialChat = await _chatService.CreateChatAsync(initialMessage);
var initialChat = _chatService.CreateChatAsync(initialMessage);

var updatedMessage = "What beverage can you recommend for this dish?";
await _chatService.UpdateChatAsync(updatedMessage, initialChat.Id);

// Act
var retrievedChat = await _chatService.GetByIdAsync(initialChat.Id);
if (initialChat.IsCompletedSuccessfully)
{
var chatId = initialChat.Result.Id;
var updatedMessage = "What beverage can you recommend for this dish?";
var chatUpdate = _chatService.UpdateChatAsync(updatedMessage, chatId);

// Assert
Assert.NotNull(retrievedChat);
Assert.Equal(initialChat.Id, retrievedChat.Id);
Assert.Contains(retrievedChat.Requests, r => r.Message == updatedMessage);
if (chatUpdate.IsCompletedSuccessfully)
{
// Act
var retrievedChat = await _chatService.GetByApiGenIdAsync(chatId);

// Assert
Assert.NotNull(retrievedChat);
Assert.Equal(initialChat.Result.Id, retrievedChat.Id);
Assert.Contains(retrievedChat.Requests, r => r.Message == updatedMessage);
}
}
}

[Fact]
public async Task EnsureChatIsDeleted()
{
// Arrange
var initialMessage = "I need a simple and delicious recipe for dinner.";
var newChat = await _chatService.CreateChatAsync(initialMessage);
var newChat = _chatService.CreateChatAsync(initialMessage);

if (newChat.IsCompletedSuccessfully)
{
var chatId = newChat.Result.Id;

// Act
var actual = _chatService.DeleteAsync(chatId);
var expected = 1;

if (actual.IsCompletedSuccessfully)
{
var retrievedChat = await _chatService.GetByApiGenIdAsync(chatId);

// Assert
Assert.Null(retrievedChat);
Assert.Equal(expected, actual.Result);
}
}
}

[Fact]
public async Task EnsureNewChatIsStoredInTheDataBase()
{
// Arrange
var userInput = "I need a simple and delicious recipe for dinner.";
var newChat = _chatService.CreateChatAsync(userInput);

if (newChat.IsCompletedSuccessfully)
{
// Act
var retrievedChat = await _chatService.GetByApiGenIdAsync(newChat.Result.Id);

// Assert
Assert.NotNull(retrievedChat);
}
}

[Theory]
[InlineData("I need a simple and delicious recipe for dinner.")]
[InlineData("What delicious meal can I cook with mackerel?")]
[InlineData("Tell me a great lemonade recipe.")]
public async Task EnsureNewChatRequestEqualsUserInput(string userInput)
{
// Arrange
var newChat = _chatService.CreateChatAsync(userInput);

if (newChat.IsCompletedSuccessfully)
{
// Act
var retrievedChat = await _chatService.GetByApiGenIdAsync(newChat.Result.Id);

// Assert
Assert.Equal(userInput, retrievedChat.Requests.Select(r => r.Message).FirstOrDefault());
}
}

[Fact]
public async Task EnsureUpdatedChatReturnsResponse()
{
// Arrange
var userInput = "I need a simple and delicious recipe for dinner.";
var initialChat = await _chatService.CreateChatAsync(userInput);

var updatedContent = "What beverage can you recommend for this dish?";

// Act
await _chatService.DeleteAsync(newChat.Id);
var retrievedChat = await _chatService.GetByIdAsync(newChat.Id);
var response = _chatService.UpdateChatAsync(updatedContent, initialChat.Id);

if (response.IsCompletedSuccessfully)
{
// Assert
Assert.NotNull(response.Result);
}
}

[Fact]
public async Task EnsureExistingChatIsFoundAndRequestContentIsUpdated()
{
// Arrange
var userInput = "I need a simple and delicious recipe for dinner.";
var initialChat = await _chatService.CreateChatAsync(userInput);

var updatedContent = "I have fish, potatoes and lemons.";
var result = _chatService.UpdateChatAsync(updatedContent, initialChat.Id);

if (result.IsCompletedSuccessfully)
{
// Act
var retrievedChat = await _chatService.GetByApiGenIdAsync(initialChat.Id);
var actual = retrievedChat.Requests.Count;
var expected = 2;

// Assert
Assert.Equal(expected, actual);
}
}

[Fact]
public async Task EnsureExistingChatIsFoundAndResponsesContentIsUpdated()
{
// Arrange
var userInput = "I need a simple and delicious recipe for dinner.";
var initialChat = await _chatService.CreateChatAsync(userInput);

var updatedContent = "I have fish, potatoes and lemons.";
await _chatService.UpdateChatAsync(updatedContent, initialChat.Id);

// Act
var retrievedChat = _chatService.GetByApiGenIdAsync(initialChat.Id);


if (retrievedChat.IsCompletedSuccessfully)
{
var actual = retrievedChat.Result.Responses.Count;
var expected = 2;

// Assert
Assert.Equal(expected, actual);
}
}

[Fact]
public async Task EnsureExistingChatIsFoundAndIdsMatch()
{
// Arrange
var userInput = "I need a simple and delicious recipe for dinner.";
var initialChat = await _chatService.CreateChatAsync(userInput);

var updatedContent = "What beverage can you recommend for this dish?";
var result = _chatService.UpdateChatAsync(updatedContent, initialChat.Id);

if (result.IsCompletedSuccessfully)
{
// Act
var retrievedChat = await _chatService.GetByApiGenIdAsync(initialChat.Id);

// Assert
Assert.Null(retrievedChat);
// Assert
Assert.Equal(initialChat.Id, retrievedChat.ApiGeneratedId);
}
}
}

Expand Down
Loading

0 comments on commit 7e20238

Please sign in to comment.