From f5d9c3e80e4eb606a950259ca8e5c446928ee496 Mon Sep 17 00:00:00 2001 From: stasex Date: Tue, 24 Oct 2023 23:24:23 +0300 Subject: [PATCH] added some fix for tests --- .../Services/ProductService.cs | 147 +++++++++--------- .../Tests/ProductsTests.cs | 6 +- .../ProductTests.cs | 1 + 3 files changed, 76 insertions(+), 78 deletions(-) diff --git a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs index 1d87086..ce0fad0 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs @@ -1,4 +1,5 @@ -using ShoppingAssistantApi.Application.IServices; +using System.Diagnostics; +using ShoppingAssistantApi.Application.IServices; using ShoppingAssistantApi.Application.Models.CreateDtos; using ShoppingAssistantApi.Application.Models.Dtos; using ShoppingAssistantApi.Application.Models.OpenAi; @@ -24,7 +25,6 @@ public class ProductService : IProductService public async IAsyncEnumerable SearchProductAsync(string wishlistId, MessageCreateDto message, CancellationToken cancellationToken) { - bool checker = false; var isFirstMessage = _wishlistsService .GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result; @@ -78,11 +78,9 @@ public class ProductService : IProductService Event = SearchEventType.Suggestion, Data = "Laptop" }; - - checker = true; } - if(isFirstMessage!=null && checker==false) + if(isFirstMessage!=null) { var previousMessages = _wishlistsService .GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result.Items.ToList(); @@ -105,91 +103,91 @@ public class ProductService : IProductService }; var suggestionBuffer = new Suggestion(); - var messageBuffer = new MessagePart(); - var productBuffer = new ProductName(); - var currentDataType = SearchEventType.Wishlist; - var dataTypeHolder = string.Empty; + var messageBuffer = new MessagePart(); + var productBuffer = new ProductName(); + var currentDataType = SearchEventType.Wishlist; + var dataTypeHolder = string.Empty; - await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken)) - { - if (data.Contains("[")) + await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken)) { - if (dataTypeHolder=="[Message]" && messageBuffer.Text!=null) + if (data.Contains("[")) { - _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto() + if (dataTypeHolder=="[Message]" && messageBuffer.Text!=null) { - Text = messageBuffer.Text, - }, cancellationToken); - } - if (dataTypeHolder=="[Products]" && productBuffer.Name!=null) - { - _wishlistsService.AddProductToPersonalWishlistAsync(wishlistId, new ProductCreateDto() + _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto() + { + Text = messageBuffer.Text, + }, cancellationToken); + } + if (dataTypeHolder=="[Products]" && productBuffer.Name!=null) { - Url = "", - Name = productBuffer.Name, - Rating = 0, - Description = "", - ImagesUrls = new []{"", ""}, - WasOpened = false - }, cancellationToken); + _wishlistsService.AddProductToPersonalWishlistAsync(wishlistId, new ProductCreateDto() + { + Url = "", + Name = productBuffer.Name, + Rating = 0, + Description = "", + ImagesUrls = new []{"", ""}, + WasOpened = false + }, cancellationToken); + } + dataTypeHolder = string.Empty; + dataTypeHolder += data; } - dataTypeHolder = string.Empty; - dataTypeHolder += data; - } - else if (data.Contains("]")) - { - dataTypeHolder += data; - currentDataType = DetermineDataType(dataTypeHolder); - } - - else if (dataTypeHolder=="[" && !data.Contains("[")) - { - dataTypeHolder += data; - } - - else - { - switch (currentDataType) + else if (data.Contains("]")) { - case SearchEventType.Message: - yield return new ServerSentEvent - { - Event = SearchEventType.Message, - Data = data - }; - messageBuffer.Text += data; - break; + dataTypeHolder += data; + currentDataType = DetermineDataType(dataTypeHolder); + } - case SearchEventType.Suggestion: - suggestionBuffer.Text += data; - if (data.Contains(";")) - { + else if (dataTypeHolder=="[" && !data.Contains("[")) + { + dataTypeHolder += data; + } + + else + { + switch (currentDataType) + { + case SearchEventType.Message: yield return new ServerSentEvent { - Event = SearchEventType.Suggestion, - Data = suggestionBuffer.Text + Event = SearchEventType.Message, + Data = data }; - suggestionBuffer.Text = string.Empty; - } - break; - case SearchEventType.Product: - productBuffer.Name += data; - if (data.Contains(";")) - { - yield return new ServerSentEvent + messageBuffer.Text += data; + break; + + case SearchEventType.Suggestion: + suggestionBuffer.Text += data; + if (data.Contains(";")) { - Event = SearchEventType.Product, - Data = productBuffer.Name - }; - productBuffer.Name = string.Empty; - } - break; + yield return new ServerSentEvent + { + Event = SearchEventType.Suggestion, + Data = suggestionBuffer.Text + }; + suggestionBuffer.Text = string.Empty; + } + break; + + case SearchEventType.Product: + productBuffer.Name += data; + if (data.Contains(";")) + { + yield return new ServerSentEvent + { + Event = SearchEventType.Product, + Data = productBuffer.Name + }; + productBuffer.Name = string.Empty; + } + break; + } } } } - - } } private SearchEventType DetermineDataType(string dataTypeHolder) @@ -215,4 +213,5 @@ public class ProductService : IProductService return SearchEventType.Wishlist; } } + } \ No newline at end of file diff --git a/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs b/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs index d120697..21ea9fe 100644 --- a/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs +++ b/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs @@ -19,15 +19,13 @@ public class ProductsTests : TestsBase public async Task StreamDataToClient_ReturnsExpectedResponse() { // Arrange - var wishlistId = "your_wishlist_id"; + var wishlistId = "ab79cde6f69abcd3efab65cd"; var message = new MessageCreateDto { Text = "Your message text" }; // Act - var response = await _httpClient.PostAsJsonAsync($"http://localhost:5183/api/products/search/{"ab79cde6f69abcd3efab65cd"}", message); + var response = await _httpClient.PostAsJsonAsync($"http://127.0.0.1:5183//api/products/search/{wishlistId}", message); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - - // Додайте додаткові перевірки на відповідь, якщо необхідно } } \ No newline at end of file diff --git a/ShoppingAssistantApi.UnitTests/ProductTests.cs b/ShoppingAssistantApi.UnitTests/ProductTests.cs index 1ba4861..2c2ac96 100644 --- a/ShoppingAssistantApi.UnitTests/ProductTests.cs +++ b/ShoppingAssistantApi.UnitTests/ProductTests.cs @@ -168,5 +168,6 @@ public class ProductTests // Assert Assert.NotNull(actualSseEvents); + Assert.Equal(3, actualSseEvents.Count); } } \ No newline at end of file