Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Feb 10, 2024
1 parent edf384f commit 8997035
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 169 deletions.
12 changes: 12 additions & 0 deletions src/Modules/Catalog/Core/Model/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
using System.Collections;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;

namespace Catalog.Core.Model;

public readonly record struct ProductName(string Name);

public sealed record Tag(string Name);

[CollectionBuilder(typeof(Tags), "Create")]
public sealed record Tags(IReadOnlyCollection<Tag> Value) : IEnumerable<Tag>
{
public static readonly Tags Empty = new Tags(Array.Empty<Tag>());
public bool HasElements() => Value is { Count: > 0 };

public static Tags Create(ReadOnlySpan<Tag> value)
{
if (value is {Length: 0})
{
return Tags.Empty;
}
return new Tags(value.ToArray());
}
public IEnumerator<Tag> GetEnumerator()
{
return Value.GetEnumerator();
Expand Down
11 changes: 10 additions & 1 deletion src/Modules/Catalog/Core/Repository/IProductFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum SortOrder
public sealed class Filter
{
private readonly int _page = 1;
private readonly int _pageSize = 12;
public int Page
{
get => _page;
Expand All @@ -21,7 +22,15 @@ public int Page
}
}

public int PageSize { get; init; } = 12;
public int PageSize
{
get => _pageSize;
init
{
_pageSize = value <= 0 ? 12 : value;
}
}

public string? Query { get; init; }
public decimal? PriceFrom { get; init; }
public decimal? PriceTo { get; init; }
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
Data: [
{
Id: {
Value: Guid_1
},
ProductName: {
Name: nivea
},
ProductDescription: {
Description: nivea
},
Price: {
CurrentPrice: {
Value: 10.0
},
PromotionalPrice: {
Value: 1.0
}
},
AvailableQuantity: {
Value: 10
},
Tags: [
{
Name: body
}
]
},
{
Id: {
Value: Guid_2
},
ProductName: {
Name: Nivea
},
ProductDescription: {
Description: xDDD
},
Price: {
CurrentPrice: {
Value: 10.0
},
PromotionalPrice: {
Value: 5.0
}
},
AvailableQuantity: {
Value: 10
},
Tags: [
{
Name: hair
}
]
},
{
Id: {
Value: Guid_3
},
ProductName: {
Name: Nivea Men
},
ProductDescription: {
Description: xDDD
},
Price: {
CurrentPrice: {
Value: 10.0
},
PromotionalPrice: {
Value: 9.0
}
},
AvailableQuantity: {
Value: 10
},
Tags: [
{
Name: hair
}
]
}
],
Metadata: {
TagFiltersMetaData: {
Filters: [
{
Tag: hair,
Count: 2
},
{
Tag: body,
Count: 1
}
]
},
Prices: {
MinPrice: 1.0,
MaxPrice: 9.0
}
},
Count: 3,
Total: 3,
IsEmpty: false
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,29 @@ public async Task FilterProductWhenNiveaProductExistsShouldReturnOneProductWithN
await Verify(subject);
}

[Fact]
public async Task TestFilters()
{
// Arrange
var product1 = new Product(ProductId.New(), new ProductName("nivea"), new ProductDescription("nivea"),
new ProductPrice(new Price(10m), new Price(1m)), new AvailableQuantity(10), [new Tag("body")]);
var product2 = new Product(ProductId.New(), new ProductName("Nivea"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), [new Tag("hair")]);
var product3 = new Product(ProductId.New(), new ProductName("Nivea Men"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(9m)), new AvailableQuantity(10), [new Tag("hair")]);
await _productsWriter.AddProducts([ product1, product2, product3 ]);
// Act

var subject = await _productFilter.FilterProducts(new Filter() { Query = "nivea", PageSize = 3, Page = 1 });

// Assert
subject.ShouldNotBeNull();
subject.Data.ShouldNotBeEmpty();
subject.Count.ShouldBe(3u);

await Verify(subject);
}

public Task InitializeAsync()
{
return Task.CompletedTask;
Expand Down

0 comments on commit 8997035

Please sign in to comment.