Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed May 11, 2024
1 parent a31425a commit 725a3ed
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Modules/Basket/Core/Dtos/Dto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public BasketItemDto(Product item)
public sealed class CustomerBasketDto
{
public required CustomerId CustomerId { get; init; }
public required IReadOnlyCollection<BasketItemDto> Items { get; init; }
public required IReadOnlyList<BasketItemDto> Items { get; init; }

public CustomerBasketDto()
{
Expand All @@ -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));
}
}
5 changes: 3 additions & 2 deletions src/Modules/Basket/Core/Model/Products.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private Products(IReadOnlyList<Product> items)
public static readonly Products Empty = new([]);
public Products AddItem(Product item)
{
ArgumentNullException.ThrowIfNull(item);
var items = new List<Product>(_items);
var index = items.IndexOf(item);
if (index == -1)
Expand Down Expand Up @@ -71,9 +72,9 @@ public IReadOnlyList<T> MapItems<T>(Func<Product, T> map)
switch (_items)
{
case null or {Count: 0}:
return Array.Empty<T>();
return [];
case [var element]:
return new[] { map(element) };
return [map(element)];
}

var array = new T[_items.Count];
Expand Down
62 changes: 62 additions & 0 deletions tests/Modules/Basket.Tests/Core/Model/ProductsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentNullException>(() => 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);
}
}

0 comments on commit 725a3ed

Please sign in to comment.