-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit includes the CheckoutBook command along with its corresponding command handler and method.
- Loading branch information
Showing
9 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from protean import current_domain, handle | ||
from protean.fields import Identifier | ||
|
||
from lending import Book, Patron, checkout | ||
from lending.domain import lending | ||
|
||
|
||
@lending.command(part_of="Patron") | ||
class CheckoutBook: | ||
patron_id = Identifier(required=True, identifier=True) | ||
book_id = Identifier(required=True) | ||
branch_id = Identifier(required=True) | ||
|
||
|
||
@lending.command_handler(part_of="Patron") | ||
class CheckoutCommandHandler: | ||
@handle(CheckoutBook) | ||
def handle_checkout_book(self, command: CheckoutBook) -> None: | ||
patron = current_domain.repository_for(Patron).get(command.patron_id) | ||
book = current_domain.repository_for(Book).get(command.book_id) | ||
|
||
checkout(patron, book, command.branch_id)() | ||
current_domain.repository_for(Patron).add(patron) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
10 changes: 10 additions & 0 deletions
10
tests/lending/app/bdd/checkout_handlers/features/check_out_a_book.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Feature: Check Out a Book | ||
|
||
Scenario: Patron checks out a book on hold | ||
Given a circulating book is available | ||
And a patron is logged in | ||
And the patron has a hold on the book | ||
When the patron checks out the book | ||
Then the checkout is successfully completed | ||
And the checkout has a validity of CHECKOUT_PERIOD | ||
And the hold is marked as checked out |
Empty file.
75 changes: 75 additions & 0 deletions
75
tests/lending/app/bdd/checkout_handlers/step_defs/checkout_steps.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from datetime import date, timedelta | ||
|
||
import pytest | ||
from protean import current_domain, g | ||
from pytest_bdd import given, then, when | ||
from pytest_bdd.parsers import cfparse | ||
|
||
from lending import CheckoutBook, HoldStatus, HoldType, Patron, PlaceHold | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def reset_globals(): | ||
yield | ||
|
||
if hasattr(g, "current_user"): | ||
delattr(g, "current_user") | ||
if hasattr(g, "current_book"): | ||
delattr(g, "current_book") | ||
if hasattr(g, "current_exception"): | ||
delattr(g, "current_exception") | ||
|
||
|
||
@given("a circulating book is available") | ||
def circulating_book_available(book): | ||
g.current_book = book | ||
|
||
|
||
@given("a patron is logged in") | ||
def patron_logged_in(regular_patron): | ||
g.current_user = regular_patron | ||
|
||
|
||
@given("the patron has a hold on the book") | ||
def patron_has_hold_on_book(): | ||
command = PlaceHold( | ||
patron_id=g.current_user.id, | ||
book_id=g.current_book.id, | ||
branch_id="1", | ||
hold_type=HoldType.CLOSED_ENDED.value, | ||
) | ||
current_domain.process(command) | ||
|
||
|
||
@when("the patron checks out the book") | ||
def patron_checks_out_book(): | ||
command = CheckoutBook( | ||
patron_id=g.current_user.id, | ||
book_id=g.current_book.id, | ||
branch_id="1", | ||
) | ||
current_domain.process(command) | ||
|
||
|
||
@then("the checkout is successfully completed") | ||
def checkout_completed(): | ||
message = current_domain.event_store.store.read_last_message( | ||
f"library::patron-{g.current_user.id}" | ||
) | ||
assert message.metadata.type == "Library.BookCheckedOut.v1" | ||
|
||
|
||
@then(cfparse("the checkout has a validity of {validity_days_config}")) | ||
def checkout_validity(validity_days_config): | ||
patron = current_domain.repository_for(Patron).get(g.current_user.id) | ||
checkout = patron.checkouts[0] | ||
assert checkout.due_on == date.today() + timedelta( | ||
days=current_domain.config["custom"][validity_days_config] | ||
) | ||
|
||
|
||
@then("the hold is marked as checked out") | ||
def hold_marked_checked_out(): | ||
patron = current_domain.repository_for(Patron).get(g.current_user.id) | ||
hold = patron.holds[0] | ||
assert hold.status == HoldStatus.CHECKED_OUT.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from pytest_bdd import scenarios | ||
|
||
from .step_defs.checkout_steps import * # noqa: F403 | ||
|
||
scenarios("./features") |