Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated tests with builder and paramters #45

Merged
merged 1 commit into from
Aug 7, 2023
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
55 changes: 31 additions & 24 deletions app/Bookstore.Domain.Tests/BookTests.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}
}
}
97 changes: 97 additions & 0 deletions app/Bookstore.Domain.Tests/Builders/BookBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}