mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-04 16:49:36 +00:00
added new tests for product search, namely for checking recommendations and creating a wishlist
This commit is contained in:
parent
dd9fdc04f0
commit
7b2e8d1645
@ -9,4 +9,6 @@ public interface IProductService
|
||||
Task<List<string>> StartNewSearchAndReturnWishlist(Message message, CancellationToken cancellationToken);
|
||||
|
||||
Task<List<string>> GetProductFromSearch(Message message, CancellationToken cancellationToken);
|
||||
|
||||
Task<List<string>> GetRecommendationsForProductFromSearch(Message message, CancellationToken cancellationToken);
|
||||
}
|
@ -1,16 +1,12 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using Microsoft.VisualBasic;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using ShoppingAssistantApi.Application.Models.Dtos;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using ShoppingAssistantApi.Application.IServices;
|
||||
using ShoppingAssistantApi.Application.Models.OpenAi;
|
||||
using ShoppingAssistantApi.Application.Models.ProductSearch;
|
||||
using ShoppingAssistantApi.Domain.Entities;
|
||||
using ShoppingAssistantApi.Domain.Enums;
|
||||
using ShoppingAssistantApi.Tests.TestExtentions;
|
||||
using ShoppingAssistantApi.Infrastructure.Services;
|
||||
|
||||
namespace ShoppingAssistantApi.Tests.Tests;
|
||||
|
||||
@ -26,6 +22,54 @@ public class ProductTests : TestsBase
|
||||
_productServiceMock = new Mock<IProductService>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartNewSearchAndReturnWishlist_CreatesWishlistObject()
|
||||
{
|
||||
var message = new Message
|
||||
{
|
||||
Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"),
|
||||
Text = "what are the best graphics cards you know?",
|
||||
CreatedById = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"),
|
||||
Role = "user"
|
||||
};
|
||||
var cancellationToken = CancellationToken.None;
|
||||
|
||||
var expectedProductList = new List<string> { "NVIDIA GeForce RTX 3080", "AMD Radeon RX 6900 XT" };
|
||||
_productServiceMock.Setup(x => x.GetProductFromSearch(message, cancellationToken))
|
||||
.ReturnsAsync(expectedProductList);
|
||||
|
||||
Wishlist createdWishList = null;
|
||||
var expectedOpenAiMessage = new OpenAiMessage
|
||||
{
|
||||
Role = OpenAiRole.User,
|
||||
Content = "{ \"Name\": [{ \"Name\": \"NVIDIA GeForce RTX 3080\" }, { \"Name\": \"AMD Radeon RX 6900 XT\" }] }"
|
||||
};
|
||||
_openAiServiceMock.Setup(x => x.GetChatCompletion(It.IsAny<ChatCompletionRequest>(), cancellationToken))
|
||||
.ReturnsAsync(expectedOpenAiMessage);
|
||||
_productServiceMock
|
||||
.Setup(x => x.StartNewSearchAndReturnWishlist(It.IsAny<Message>(), cancellationToken))
|
||||
.ReturnsAsync(() =>
|
||||
{
|
||||
createdWishList = new Wishlist
|
||||
{
|
||||
Name = "Test Wishlist",
|
||||
CreatedById = ObjectId.GenerateNewId(),
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Type = "Test Type"
|
||||
};
|
||||
return new List<string>();
|
||||
});
|
||||
|
||||
await _productServiceMock.Object.StartNewSearchAndReturnWishlist(message, cancellationToken);
|
||||
|
||||
var openAiContent = JObject.Parse(expectedOpenAiMessage.Content);
|
||||
var productNames = openAiContent["Name"].ToObject<List<ProductName>>();
|
||||
var productList = productNames.Select(info => info.Name).ToList();
|
||||
|
||||
Assert.Equal(expectedProductList, productList);
|
||||
Assert.True(openAiContent.ContainsKey("Name"));
|
||||
Assert.NotNull(createdWishList);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProductFromSearch_ReturnsProductList()
|
||||
@ -38,24 +82,52 @@ public class ProductTests : TestsBase
|
||||
Role = "user"
|
||||
};
|
||||
var cancellationToken = CancellationToken.None;
|
||||
|
||||
var productServiceMock = new Mock<IProductService>();
|
||||
|
||||
var expectedProductList = new List<string> { "NVIDIA GeForce RTX 3080", "AMD Radeon RX 6900 XT" };
|
||||
productServiceMock.Setup(x => x.GetProductFromSearch(message, cancellationToken))
|
||||
_productServiceMock.Setup(x => x.GetProductFromSearch(message, cancellationToken))
|
||||
.ReturnsAsync(expectedProductList);
|
||||
|
||||
var openAiServiceMock = new Mock<IOpenAiService>();
|
||||
|
||||
var expectedOpenAiMessage = new OpenAiMessage
|
||||
{
|
||||
Role = OpenAiRole.User,
|
||||
Content = "[\n { \"Name\": \"NVIDIA GeForce RTX 3080\" },\n { \"Name\": \"AMD Radeon RX 6900 XT\" }\n]"
|
||||
Content = "{ \"Name\": [{ \"Name\": \"NVIDIA GeForce RTX 3080\" }, { \"Name\": \"AMD Radeon RX 6900 XT\" }] }"
|
||||
};
|
||||
openAiServiceMock.Setup(x => x.GetChatCompletion(It.IsAny<ChatCompletionRequest>(), cancellationToken))
|
||||
_openAiServiceMock.Setup(x => x.GetChatCompletion(It.IsAny<ChatCompletionRequest>(), cancellationToken))
|
||||
.ReturnsAsync(expectedOpenAiMessage);
|
||||
|
||||
var productList = JsonConvert.DeserializeObject<List<ProductName>>(expectedOpenAiMessage.Content).Select(info => info.Name).ToList();
|
||||
|
||||
var openAiContent = JObject.Parse(expectedOpenAiMessage.Content);
|
||||
var productNames = openAiContent["Name"].ToObject<List<ProductName>>();
|
||||
var productList = productNames.Select(info => info.Name).ToList();
|
||||
|
||||
Assert.Equal(expectedProductList, productList);
|
||||
Assert.True(openAiContent.ContainsKey("Name"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetRecommendationsForProductFromSearch_ReturnsRecommendations()
|
||||
{
|
||||
var message = new Message
|
||||
{
|
||||
Id = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"),
|
||||
Text = "get recommendations for this product",
|
||||
CreatedById = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab"),
|
||||
Role = "user"
|
||||
};
|
||||
var cancellationToken = CancellationToken.None;
|
||||
|
||||
var expectedOpenAiMessage = new OpenAiMessage
|
||||
{
|
||||
Role = OpenAiRole.User,
|
||||
Content = "{ \"Recommendation\": [\"Recommendation 1\", \"Recommendation 2\"] }"
|
||||
};
|
||||
_openAiServiceMock.Setup(x => x.GetChatCompletion(It.IsAny<ChatCompletionRequest>(), cancellationToken))
|
||||
.ReturnsAsync(expectedOpenAiMessage);
|
||||
|
||||
var recommendations = await _productServiceMock.Object.GetRecommendationsForProductFromSearch(message, cancellationToken);
|
||||
|
||||
var openAiContent = JObject.Parse(expectedOpenAiMessage.Content);
|
||||
Assert.NotNull(openAiContent);
|
||||
Assert.True(openAiContent.ContainsKey("Recommendation"));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user