Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Library.API/Infrastructure/Services/BookOrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ public async Task<ServiceResponse> AddAsync(BookOrder bookOrder)
}
catch (DbUpdateException ex)
{
_logger.LogError(ex, "Error occurred while creating book order {BookOrderId}.", bookOrder.Id);
_logger.LogError(ex, "Error occurred while saving book order.");
return ServiceResponse.Fail(
"An error occurred while saving the book order",
ErrorType.DatabaseError);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred while creating book order {BookOrderId}.", bookOrder.Id);
_logger.LogError(ex, "Error occurred while adding book order.");
return ServiceResponse.Fail(
"An unexpected error occurred while processing your request",
ErrorType.Unknown);
Expand Down
56 changes: 56 additions & 0 deletions tests/Library.API.Tests/Extensions/EnumExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.ComponentModel;

using FluentAssertions;

using Library.API.Extensions;

namespace Library.API.Tests.Extensions;

public enum TestEnumWithDescription
{
[Description("First Value")]
First,
[Description("Second Value")]
Second,
[Description("Third Value")]
Third
}

public enum TestEnumWithoutDescription
{
First,
Second,
Third
}

public class EnumExtensionsTests
{
[Fact]
public void ToDescription_ShouldReturnDescriptionAttribute_WhenEnumHasDescription()
{
// Arrange
var firstDescription = TestEnumWithDescription.First.ToDescription();
var secondDescription = TestEnumWithDescription.Second.ToDescription();
var thirdDescription = TestEnumWithDescription.Third.ToDescription();

// Assert
firstDescription.Should().Be("First Value");
secondDescription.Should().Be("Second Value");
thirdDescription.Should().Be("Third Value");
}

[Fact]
public void ToDescription_ShouldReturnEnumName_WhenEnumHasNoDescription()
{
// Arrange
var firstDescription = TestEnumWithoutDescription.First.ToDescription();
var secondDescription = TestEnumWithoutDescription.Second.ToDescription();
var thirdDescription = TestEnumWithoutDescription.Third.ToDescription();

// Assert
firstDescription.Should().Be("First");
secondDescription.Should().Be("Second");
thirdDescription.Should().Be("Third");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using FluentAssertions;
using MassTransit;
using Moq;

using Library.API.Infrastructure.Factories;

namespace Library.API.Tests.Factories;

public class ConsumerRegistrationBuilderTests
{
[Fact]
public void Add_ShouldSetSuffix_WhenConsumerIsRegistered()
{
// Arrange
var expectedSuffix = "test-suffix";
var _configuratorMock = new Mock<IRegistrationConfigurator>();
var _builder = new ConsumerRegistrationBuilder(_configuratorMock.Object, expectedSuffix);

// Act
_builder.Add<TestConsumer>();

// Assert
SuffixedConsumerDefinition<TestConsumer>.CurrentSuffix.Should().Be(expectedSuffix);
}

// Test consumer class for our tests
private class TestConsumer : IConsumer<TestMessage>
{
public Task Consume(ConsumeContext<TestMessage> context)
{
return Task.CompletedTask;
}
}

// Test message class for our consumer
private class TestMessage
{
}
}
24 changes: 22 additions & 2 deletions tests/Library.API.Tests/Helpers/TestDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,25 @@ public static class TestDataHelper
AuthorId = 2,
ReleaseDate = new DateTime(2010, 1, 1),
}
];
}
];

public static List<BookOrder> BookOrders =>
[
new BookOrder
{
Id = 1,
CheckoutDate = new DateTime(2025, 5, 1),
Status = BookOrderStatus.Placed,
Items = [
new BookOrderItem
{
Id = 1,
BookId = 1,
Book = Books[0],
Quantity = 1,
BookOrderId = 1
}
]
}
];
}
81 changes: 81 additions & 0 deletions tests/Library.API.Tests/Mapping/BookOrderMappingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using FluentAssertions;

using Library.API.Domain.Models;
using Library.API.DTOs;
using Library.API.Extensions;

namespace Library.API.Tests.Mapping;

public class BookOrderMappingTests : MappingTestsBase
{
[Fact]
public void Should_Map_BookOrder_To_BookOrderDto()
{
// Arrange
var order = new BookOrder
{
Id = 1,
Status = BookOrderStatus.Placed,
CheckoutDate = DateTime.UtcNow,
Items = new List<BookOrderItem>
{
new BookOrderItem
{
Id = 1,
BookId = 1,
Book = new Book
{
Id = 1,
Title = "Book 1",
Description = "Book 1 is a book about software engineering."
},
Quantity = 1
}
}
};

// Act
var result = Mapper.Map<BookOrderDto>(order);

// Assert
result.Should().NotBeNull();
result.Id.Should().Be(order.Id);
result.Status.Should().Be(order.Status.ToDescription());
result.CheckoutDate.Should().Be(order.CheckoutDate);
result.Items.Should().HaveCount(order.Items.Count);
result.Items[0].BookId.Should().Be(order.Items[0].BookId);
result.Items[0].Quantity.Should().Be(order.Items[0].Quantity);
result.Items[0].Title.Should().Be(order.Items[0].Book.Title);
}

[Fact]
public void Should_Map_SaveBookOrderDto_To_BookOrder()
{
// Arrange
var dto = new SaveBookOrderDto
{
Items = new List<SaveBookOrderItemDto>
{
new SaveBookOrderItemDto
{
BookId = 1,
Quantity = 1,
}
}
};

// Act
var result = Mapper.Map<BookOrder>(dto);

// Assert
result.Should().NotBeNull();
result.Id.Should().Be(0);
result.Status.Should().Be(BookOrderStatus.Placed);
result.CheckoutDate.Should().Be(default);
result.Items.Should().HaveCount(dto.Items.Count);
result.Items[0].Book.Should().BeNull();
result.Items[0].BookOrderId.Should().Be(0);
result.Items[0].BookId.Should().Be(dto.Items[0].BookId);
result.Items[0].Quantity.Should().Be(dto.Items[0].Quantity);
}
}
4 changes: 3 additions & 1 deletion tests/Library.API.Tests/Mapping/MappingTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public class MappingTestsBase

public MappingTestsBase()
{
var configuration = new MapperConfiguration(cfg => {
var configuration = new MapperConfiguration(cfg =>
{
cfg.AddProfile<DtoToModelProfile>();
cfg.AddProfile<ModelToDtoProfile>();
cfg.AddProfile<ModelToEventProfile>();
});
Mapper = configuration.CreateMapper();
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Library.API.Tests/Mapping/OrderPlacedEventTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using FluentAssertions;

using Library.API.Domain.Models;
using Library.API.Extensions;
using Library.Events.Messages;

namespace Library.API.Tests.Mapping;

public class OrderPlacedEventTests : MappingTestsBase
{
[Fact]
public void Should_Map_BookOrder_To_OrderPlacedEvent()
{
// Arrange
var order = new BookOrder
{
Id = 1,
Status = BookOrderStatus.Placed,
CheckoutDate = DateTime.UtcNow,
Items = new List<BookOrderItem>
{
new BookOrderItem
{
Id = 1,
Book = new Book
{
Id = 1,
Title = "Book 1",
Description = "Book 1 is a book about software engineering."
},
BookId = 1,
Quantity = 1
}
}
};

// Act
var result = Mapper.Map<OrderPlacedEvent>(order);

// Assert
result.Should().NotBeNull();
result.OrderId.Should().Be(order.Id);
result.Status.Should().Be(order.Status.ToDescription());
result.CheckoutDate.Should().Be(order.CheckoutDate);
result.Items.Should().HaveCount(order.Items.Count);
result.Items[0].BookId.Should().Be(order.Items[0].BookId);
result.Items[0].Quantity.Should().Be(order.Items[0].Quantity);
}
}
Loading