Implemented order service tests

This commit is contained in:
Khwezi Mngoma
2026-05-29 08:18:29 +02:00
parent 2546c34ffc
commit 4397976ed8
4 changed files with 37 additions and 6 deletions
@@ -114,54 +114,83 @@ public class OrderServiceFeatureTests(Fixture fixture) : IClassFixture<Fixture>
[IntegrationFact] [IntegrationFact]
public async Task AddShippingToOrderAsync_ShouldReturn_ResultWithSuccess() public async Task AddShippingToOrderAsync_ShouldReturn_ResultWithSuccess()
{ {
var request = new CreateShipping(1, 2);
var result = await orderService.AddShippingToOrderAsync(1, request, fixture.CancellationToken);
Assert.True(result.IsSuccess);
Assert.True(result.Value > 0);
} }
[IntegrationFact] [IntegrationFact]
public async Task UpdateShippingStatusAsync_ShouldReturn_ResultWithSuccess() public async Task UpdateShippingStatusAsync_ShouldReturn_ResultWithSuccess()
{ {
var result = await orderService.UpdateShippingStatusAsync(1, ShippingStatuses.Shipped, fixture.CancellationToken);
Assert.True(result.IsSuccess);
} }
[IntegrationFact] [IntegrationFact]
public async Task GetShippingByOrderIdAsync_ShouldReturn_ResultWithShipping() public async Task GetShippingByOrderIdAsync_ShouldReturn_ResultWithShipping()
{ {
var result = await orderService.GetShippingByOrderIdAsync(1, fixture.CancellationToken);
Assert.True(result.IsSuccess);
Assert.NotNull(result.Value);
} }
[IntegrationFact] [IntegrationFact]
public async Task RemoveShippingFromOrderAsync_ShouldReturn_ResultWithSuccess() public async Task RemoveShippingFromOrderAsync_ShouldReturn_ResultWithSuccess()
{ {
var result = await orderService.RemoveShippingFromOrderAsync(1, 1, fixture.CancellationToken);
Assert.True(result.IsSuccess);
} }
[IntegrationFact] [IntegrationFact]
public async Task UpdateShippingTrackingNumberAsync_ShouldReturn_ResultWithSuccess() public async Task UpdateShippingTrackingNumberAsync_ShouldReturn_ResultWithSuccess()
{ {
var result = await orderService.UpdateShippingTrackingNumberAsync(1, 2, "NA0009969397");
Assert.True(result.IsSuccess);
} }
[IntegrationFact] [IntegrationFact]
public async Task CreateShippingProviderAsync_ShouldReturn_ResultWithSuccess() public async Task CreateShippingProviderAsync_ShouldReturn_ResultWithShippingProviderId()
{ {
var request = new CreateShippingProvider(ShippingProviderTypes.FastWay, "FastWay Couriers", 50, "https://www.fastway.co.za/our-services/track-your-parcel");
var result = await orderService.CreateShippingProviderAsync(request, fixture.CancellationToken);
Assert.True(result.IsSuccess);
Assert.True(result.Value > 0);
} }
[IntegrationFact] [IntegrationFact]
public async Task GetShippingProvidersAsync_ShouldReturn_ResultWithShippingProviderList() public async Task GetShippingProvidersAsync_ShouldReturn_ResultWithShippingProviderList()
{ {
var result = await orderService.GetShippingProvidersAsync(true, fixture.CancellationToken);
Assert.True(result.IsSuccess);
Assert.NotEmpty(result.Value);
} }
[IntegrationFact] [IntegrationFact]
public async Task GetShippingProviderAsync_ShouldReturn_ResultWithShippingProvider() public async Task GetShippingProviderAsync_ShouldReturn_ResultWithShippingProvider()
{ {
var result = await orderService.GetShippingProviderAsync(2, fixture.CancellationToken);
Assert.True(result.IsSuccess);
Assert.NotNull(result.Value);
} }
[IntegrationFact] [IntegrationFact]
public async Task UpdateShippingProviderAsync_ShouldReturn_ResultWithSuccess() public async Task UpdateShippingProviderAsync_ShouldReturn_ResultWithSuccess()
{ {
var request = new UpdateShippingProvider(2,true, "FastWay Couriers", 50, "https://www.fastway.co.za/our-services/track-your-parcel");
var result = await orderService.UpdateShippingProviderAsync(request, fixture.CancellationToken);
Assert.True(result.IsSuccess);
} }
} }
@@ -17,7 +17,8 @@ public static class Mappers
Name = entity.Name, Name = entity.Name,
Type = entity.Type, Type = entity.Type,
Price = entity.Price, Price = entity.Price,
Enabled = entity.Enabled Enabled = entity.Enabled,
TrackingUrl = entity.TrackingUrl,
}; };
public static Shipping ToModel(this Orders.Entities.Shipping entity) => new() public static Shipping ToModel(this Orders.Entities.Shipping entity) => new()
@@ -4,7 +4,7 @@ public sealed record CreateOrder(decimal TotalPrice, string? Notes);
public sealed record CreateOrderItem(long AuthorBookId, long ProductPriceId, int Quantity); public sealed record CreateOrderItem(long AuthorBookId, long ProductPriceId, int Quantity);
public sealed record CreateShipping(long OrderId, long AddressId, long ShippingProviderId, string? TrackingNumber); public sealed record CreateShipping(long AddressId, long ShippingProviderId, string? TrackingNumber = null);
public sealed record CreateShippingProvider(ShippingProviderTypes Type, string Name, decimal Price, string TrackingUrl); public sealed record CreateShippingProvider(ShippingProviderTypes Type, string Name, decimal Price, string TrackingUrl);
@@ -249,7 +249,7 @@ public sealed class OrderService(IDbContextFactory<MidrandBooksDbContext> contex
} }
} }
public async ValueTask<Result> AddShippingToOrderAsync(long orderId, CreateShipping request, CancellationToken cancellationToken = default) public async ValueTask<Result<long>> AddShippingToOrderAsync(long orderId, CreateShipping request, CancellationToken cancellationToken = default)
{ {
try try
{ {
@@ -371,7 +371,7 @@ public sealed class OrderService(IDbContextFactory<MidrandBooksDbContext> contex
} }
} }
public async ValueTask<Result> CreateShippingProviderAsync(CreateShippingProvider request, CancellationToken cancellationToken = default) public async ValueTask<Result<long>> CreateShippingProviderAsync(CreateShippingProvider request, CancellationToken cancellationToken = default)
{ {
try try
{ {
@@ -382,6 +382,7 @@ public sealed class OrderService(IDbContextFactory<MidrandBooksDbContext> contex
var shippingProvider = context.ShippingProviders.Add(new Entities.ShippingProvider var shippingProvider = context.ShippingProviders.Add(new Entities.ShippingProvider
{ {
CreatedAt = DateTime.UtcNow,
Name = request.Name, Name = request.Name,
Type = request.Type, Type = request.Type,
Price = request.Price, Price = request.Price,
@@ -389,7 +390,7 @@ public sealed class OrderService(IDbContextFactory<MidrandBooksDbContext> contex
}); });
return await context.SaveChangesAsync(cancellationToken) > 0 return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok() ? Result.Ok(shippingProvider.Entity.Id)
: Result.Fail("Failed to create shipping provider."); : Result.Fail("Failed to create shipping provider.");
} }
catch (Exception ex) catch (Exception ex)