From 725a3ed790d521d44cbd1951216db8aa09b5787f Mon Sep 17 00:00:00 2001 From: "Dominik.Kotecki" Date: Sat, 11 May 2024 17:16:02 +0200 Subject: [PATCH] add unit tests --- src/Modules/Basket/Core/Dtos/Dto.cs | 4 +- src/Modules/Basket/Core/Model/Products.cs | 5 +- .../Basket.Tests/Core/Model/ProductsTests.cs | 62 +++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/Modules/Basket/Core/Dtos/Dto.cs b/src/Modules/Basket/Core/Dtos/Dto.cs index d5a8caa..7bef129 100644 --- a/src/Modules/Basket/Core/Dtos/Dto.cs +++ b/src/Modules/Basket/Core/Dtos/Dto.cs @@ -26,7 +26,7 @@ public BasketItemDto(Product item) public sealed class CustomerBasketDto { public required CustomerId CustomerId { get; init; } - public required IReadOnlyCollection Items { get; init; } + public required IReadOnlyList Items { get; init; } public CustomerBasketDto() { @@ -37,6 +37,6 @@ public CustomerBasketDto() public CustomerBasketDto(CustomerBasket basket) { CustomerId = basket.CustomerId; - Items = basket.Products.MapItems(item => new BasketItemDto(item)).ToArray(); + Items = basket.Products.MapItems(item => new BasketItemDto(item)); } } \ No newline at end of file diff --git a/src/Modules/Basket/Core/Model/Products.cs b/src/Modules/Basket/Core/Model/Products.cs index ae5a3a3..ab0e032 100644 --- a/src/Modules/Basket/Core/Model/Products.cs +++ b/src/Modules/Basket/Core/Model/Products.cs @@ -19,6 +19,7 @@ private Products(IReadOnlyList items) public static readonly Products Empty = new([]); public Products AddItem(Product item) { + ArgumentNullException.ThrowIfNull(item); var items = new List(_items); var index = items.IndexOf(item); if (index == -1) @@ -71,9 +72,9 @@ public IReadOnlyList MapItems(Func map) switch (_items) { case null or {Count: 0}: - return Array.Empty(); + return []; case [var element]: - return new[] { map(element) }; + return [map(element)]; } var array = new T[_items.Count]; diff --git a/tests/Modules/Basket.Tests/Core/Model/ProductsTests.cs b/tests/Modules/Basket.Tests/Core/Model/ProductsTests.cs index 1c5e9e2..82fe093 100644 --- a/tests/Modules/Basket.Tests/Core/Model/ProductsTests.cs +++ b/tests/Modules/Basket.Tests/Core/Model/ProductsTests.cs @@ -24,4 +24,66 @@ public void CollectionInitializerWhenNoEmptyTests(Product product) Assert.False(subject.IsEmpty); Assert.Single(subject); } + + [Theory] + [AutoData] + public void AddItemTests(Product product) + { + Products subject = []; + + var result = subject.AddItem(product); + + Assert.Single(result); + Assert.Contains(result, x => x == product); + } + + [Fact] + public void AddNullItemTests() + { + Products subject = []; + + Assert.Throws(() => subject.AddItem(null!)); + } + + + [Theory] + [AutoData] + public void MapOneItemItemsTests(Product product) + { + Products subject = []; + + var result = subject.AddItem(product); + + Assert.Single(result); + + var mapped = result.MapItems(x => x.Quantity); + + Assert.Single(mapped); + } + + [Fact] + public void MapEmptyItemsTests() + { + Products subject = []; + + var mapped = subject.MapItems(x => x.Quantity); + + Assert.Empty(mapped); + } + + [Theory] + [AutoData] + public void MapItemsTests(Product[] products) + { + Products subject = []; + + foreach (var p in products) + { + subject = subject.AddItem(p); + } + + var mapped = subject.MapItems(x => x.Quantity); + + Assert.Equal(products.Length, mapped.Count); + } } \ No newline at end of file