Wrote tests for most services, applied EF core optimisations
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using LiteCharms.Features.MidrandBooks.Products;
|
||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||
using LiteCharms.Features.MidrandBooks.Tests.Common;
|
||||
using LiteCharms.Features.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Tests;
|
||||
|
||||
public class ProductServiceFeatureTests(Fixture fixture, ITestOutputHelper output) : IClassFixture<Fixture>
|
||||
{
|
||||
private readonly ProductService productService = fixture.Services.GetRequiredService<ProductService>();
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task GetProductPriceAsync_ShouldReturn_RetultOneProductPrice()
|
||||
{
|
||||
var result = await productService.GetProductPriceAsync(2, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.NotNull(result.Value);
|
||||
|
||||
output.WriteLine(JsonSerializer.Serialize(result.Value));
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task GetProductPricesAsync_ShouldReturn_RetultProductPriceList()
|
||||
{
|
||||
var result = await productService.GetProductPricesAsync(2, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.NotEmpty(result.Value);
|
||||
|
||||
output.WriteLine(JsonSerializer.Serialize(result.Value));
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task SearchProductsAsync_ShouldReturn_RetultMatchingProducts()
|
||||
{
|
||||
var filter = new ProductFilter
|
||||
{
|
||||
Name = "system",
|
||||
Manufacturer = "techwave",
|
||||
SerialNumber = "2024",
|
||||
MinPrice = 10,
|
||||
MaxPrice = 30
|
||||
};
|
||||
|
||||
var result = await productService.SearchProductsAsync(filter, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.NotEmpty(result.Value);
|
||||
|
||||
output.WriteLine(JsonSerializer.Serialize(result.Value));
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task GetProductAsync_ShouldReturn_RetultOneProduct()
|
||||
{
|
||||
var result = await productService.GetProductAsync(2, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.NotNull(result.Value);
|
||||
|
||||
output.WriteLine(JsonSerializer.Serialize(result.Value));
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task GetProductsAsync_ShouldReturn_RetultProducts()
|
||||
{
|
||||
var range = new DateRange
|
||||
{
|
||||
From = DateOnly.FromDateTime(DateTime.UtcNow.AddDays(-7)),
|
||||
To = DateOnly.FromDateTime(DateTime.UtcNow),
|
||||
MaxRecords = 1000
|
||||
};
|
||||
|
||||
var result = await productService.GetProductsAsync(0, range, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.NotEmpty(result.Value);
|
||||
|
||||
output.WriteLine(JsonSerializer.Serialize(result.Value));
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task UpdateProductStatusAsync_ShouldReturn_ResultTrue()
|
||||
{
|
||||
var result = await productService.UpdateProductStatusAsync(2, true, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task UpdateProductPriceStatusAsync_ShouldReturn_ResultTrue()
|
||||
{
|
||||
var result = await productService.UpdateProductPriceStatusAsync(2, true, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task CreateProductPriceAsync_Should_Return_NewProductPriceId()
|
||||
{
|
||||
var request = new CreateProductPrice
|
||||
{
|
||||
Amount = 29.99m,
|
||||
Discount = 0.00m
|
||||
};
|
||||
|
||||
var result = await productService.CreateProductPriceAsync(2, request, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess, "Product price creation should be successful.");
|
||||
Assert.True(result.Value > 0, "New ProductPriceId should be greater than 0.");
|
||||
|
||||
output.WriteLine($"Created ProductPriceId: {result.Value}");
|
||||
}
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task CreateProductAsync_Result_Returns_ProductId()
|
||||
{
|
||||
var request = new CreateProduct
|
||||
{
|
||||
Name = "Systems Rewired",
|
||||
Description = "[Design], <Code>, AND /CHAORS/ IN ***SYNC***",
|
||||
Summary = "A comprehensive guide to systems thinking and design.",
|
||||
ImageUrl = "https://bookshop.cdn.khongisa.co.za/design/2bf1f9a2-7b25-4fcf-9aa7-08941ea21e6c_1764838499686.webp",
|
||||
Type = ProductTypes.Book,
|
||||
Categories = ["Systems Thinking", "Design", "Programming"],
|
||||
Metadata = new ProductMetadata
|
||||
{
|
||||
CopyrightInfo = "© 2024 John Doe. All rights reserved.",
|
||||
ManufactureDate = "2024-06-01",
|
||||
Manufacturer = "TechWave Publishing",
|
||||
SerialNumber = "SR-2024-0001"
|
||||
}
|
||||
};
|
||||
|
||||
var result = await productService.CreateProductAsync(request, fixture.CancellationToken);
|
||||
|
||||
Assert.True(result.IsSuccess, "Product creation should be successful.");
|
||||
Assert.True(result.Value > 0, "ProductId should be greater than 0.");
|
||||
|
||||
output.WriteLine($"Created ProductId: {result.Value}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user