diff --git a/app/Bookstore.Domain.Tests/BookTests.cs b/app/Bookstore.Domain.Tests/BookTests.cs index dbac4f9e..b1249cfe 100644 --- a/app/Bookstore.Domain.Tests/BookTests.cs +++ b/app/Bookstore.Domain.Tests/BookTests.cs @@ -1,39 +1,43 @@ using Bookstore.Domain.Books; +using Bookstore.Domain.Tests.Builders; namespace Bookstore.Domain.Tests { public class BookTests { - [Fact] - public void IsInStock_ReturnsTrue_When_QuantityIsGreaterThanZero() + [Theory] + [InlineData(1, true)] + [InlineData(0, false)] + public void IsInStock_ReturnsTrue_When_QuantityIsGreaterThanZero(int quantity, bool expectedResult) { - var book = new Book("Test", "Test", "1234567890123", 1, 1, 1, 1, 1, 1); - Assert.True(book.IsInStock); - - book = new Book("Test", "Test", "1234567890123", 1, 1, 1, 1, 1, 0); - Assert.False(book.IsInStock); + var book = new BookBuilder() + .Quantity(quantity) + .Build(); + + Assert.Equal(expectedResult, book.IsInStock); } - [Fact] - public void IsLowInStock_ReturnsTrue_When_QuantityIsLessThanOrEqualToThreshold() + [Theory] + [InlineData(Book.LowBookThreshold - 1, true)] + [InlineData(Book.LowBookThreshold, true)] + [InlineData(Book.LowBookThreshold + 1, false)] + public void IsLowInStock_ReturnsTrue_When_QuantityIsLessThanOrEqualToThreshold(int quantity, bool expectedResult) { - var threshold = Book.LowBookThreshold; - - var book = new Book("Test", "Test", "1234567890123", 1, 1, 1, 1, 1, threshold - 1); - Assert.True(book.IsLowInStock); - - book = new Book("Test", "Test", "1234567890123", 1, 1, 1, 1, 1, threshold); - Assert.True(book.IsLowInStock); - - book = new Book("Test", "Test", "1234567890123", 1, 1, 1, 1, 1, threshold + 1); - Assert.False(book.IsLowInStock); + var book = new BookBuilder() + .Quantity(quantity) + .Build(); + + Assert.Equal(expectedResult, book.IsLowInStock); } [Fact] public void ReduceStockLevel_ReducesQuantityBySpecifiedAmount_When_Executed() { - var book = new Book("Test", "Test", "1234567890123", 1, 1, 1, 1, 1, 100); - var amountToReduce = 50; + var book = new BookBuilder() + .Quantity(100) + .Build(); + + const int amountToReduce = 50; var expectedQuantity = book.Quantity - amountToReduce; book.ReduceStockLevel(amountToReduce); @@ -44,12 +48,15 @@ public void ReduceStockLevel_ReducesQuantityBySpecifiedAmount_When_Executed() [Fact] public void ReduceStockLevel_DoesNotReduceQuantityBelowZero_When_Executed() { - var book = new Book("Test", "Test", "1234567890123", 1, 1, 1, 1, 1, 50); - var amountToReduce = 60; + var book = new BookBuilder() + .Quantity(50) + .Build(); + + const int amountToReduce = 60; book.ReduceStockLevel(amountToReduce); - Assert.True(book.Quantity == 0); + Assert.Equal(0, book.Quantity); } } } \ No newline at end of file diff --git a/app/Bookstore.Domain.Tests/Builders/BookBuilder.cs b/app/Bookstore.Domain.Tests/Builders/BookBuilder.cs new file mode 100644 index 00000000..52476ebd --- /dev/null +++ b/app/Bookstore.Domain.Tests/Builders/BookBuilder.cs @@ -0,0 +1,97 @@ +using Bookstore.Domain.Books; + +namespace Bookstore.Domain.Tests.Builders; + +public class BookBuilder +{ + private string name = "test"; + private string author = "author"; + private string isbn = "12345678"; + private int publisherId = 1; + private int bookTypeId = 2; + private int genreId = 3; + private int conditionId = 4; + private decimal price = 5; + private int quantity = 6; + private int? year = 2000; + private string? summary = "book summary"; + private string? coverImageUrl = "http://cover.dummy"; + + public Book Build() + { + return new Book(name, author, isbn, publisherId, bookTypeId, genreId, conditionId, price, quantity, year, + summary, coverImageUrl); + } + + public BookBuilder Name(string value) + { + name = value; + return this; + } + + public BookBuilder Author(string value) + { + author = value; + return this; + } + + public BookBuilder ISBN(string value) + { + isbn = value; + return this; + } + + public BookBuilder PublisherId(int value) + { + publisherId = value; + return this; + } + + public BookBuilder BookTypeId(int value) + { + bookTypeId = value; + return this; + } + + public BookBuilder GenreId(int value) + { + genreId = value; + return this; + } + + public BookBuilder ConditionId(int value) + { + conditionId = value; + return this; + } + + public BookBuilder Price(decimal value) + { + price = value; + return this; + } + + public BookBuilder Quantity(int value) + { + quantity = value; + return this; + } + + public BookBuilder Year(int? value) + { + year = value; + return this; + } + + public BookBuilder Summary(string? value) + { + summary = value; + return this; + } + + public BookBuilder CoverImageUrl(string? value) + { + coverImageUrl = value; + return this; + } +} \ No newline at end of file