def test_it_calculates_copies_from_receive_events(): actions = [ BookAction(BookActionName.RECEIVE, 2, book_id=1), BookAction(BookActionName.RECEIVE, 3, book_id=1) ] book = Book(actions, book_id=1) assert book.copies.to_int() == 5
def test_it_calculates_copies_from_receive_release_and_sell_events(): actions = [ BookAction(BookActionName.RECEIVE, 3, book_id=1), BookAction(BookActionName.RECEIVE, 6, book_id=1), BookAction(BookActionName.RELEASE, 2, book_id=1, receiver_id=1), BookAction(BookActionName.SELL, 3, book_id=1) ] book = Book(actions, book_id=1) assert book.copies.to_int() == 4
def test_it_raises_error_if_selled_copies_equals_to_zero(): BookAction.save_in_transaction = lambda self: None BookAction.delete_in_transaction = lambda self: None released = BookAction(BookActionName.RELEASE, 5, book_id=1, receiver_id=1, id=2, inserted_at=datetime.datetime(2021, 1, 2)) actions = [ BookAction(BookActionName.RECEIVE, 10, book_id=1, inserted_at=datetime.datetime(2021, 1, 1)), released ] book = Book(actions, book_id=1) with pytest.raises(AssertionError): book.sell_released(Copies(0), release_id=3)
def test_it_raises_error_if_released_id_does_not_exist(): BookAction.save_in_transaction = lambda self: None BookAction.delete_in_transaction = lambda self: None released = BookAction(BookActionName.RELEASE, 5, book_id=1, receiver_id=1, id=2, inserted_at=datetime.datetime(2021, 1, 2)) actions = [ BookAction(BookActionName.RECEIVE, 10, book_id=1, inserted_at=datetime.datetime(2021, 1, 1)), released ] book = Book(actions, book_id=1) with pytest.raises(ValueError): book.sell_released(Copies(3), release_id=3)
def test_it_sells_all_released_books(): BookAction.save_in_transaction = lambda self: setattr(self, 'saved', True) BookAction.delete_in_transaction = ( lambda self: setattr(self, 'deleted', True)) released = BookAction(BookActionName.RELEASE, 5, book_id=1, receiver_id=1, id=2, inserted_at=datetime.datetime(2021, 1, 2)) actions = [ BookAction(BookActionName.RECEIVE, 10, book_id=1, inserted_at=datetime.datetime(2021, 1, 1)), released ] book = Book(actions, book_id=1) action = book.sell_released(Copies(5), release_id=2) assert action.name == BookActionName.SELL assert action.copies == 5 assert released.deleted
def test_it_apply_events_ordered_by_date_desc(): actions = [ BookAction(BookActionName.RELEASE, 10, book_id=1, receiver_id=1, inserted_at=datetime.datetime(2021, 2, 1)), BookAction(BookActionName.RECEIVE, 10, book_id=1, inserted_at=datetime.datetime(2021, 1, 1)), BookAction(BookActionName.SELL, 5, book_id=1, inserted_at=datetime.datetime(2021, 2, 15)), BookAction(BookActionName.RECEIVE, 6, book_id=1, inserted_at=datetime.datetime(2021, 2, 10)), ] book = Book(actions, book_id=1) assert book.copies.to_int() == 1
def test_it_calculates_copies_for_books_ids(): books_ids = [1, 2, 3, 4] events = [ BookAction(BookActionName.RECEIVE, 5, book_id=1), BookAction(BookActionName.RECEIVE, 10, book_id=2), BookAction(BookActionName.RECEIVE, 15, book_id=3), BookAction(BookActionName.RELEASE, 3, book_id=2, receiver_id=1), BookAction(BookActionName.RELEASE, 2, book_id=1, receiver_id=1), BookAction(BookActionName.RECEIVE, 1, book_id=4), BookAction(BookActionName.SELL, 4, book_id=3), BookAction(BookActionName.RECEIVE, 3, book_id=1), ] expected = { 1: 6, 2: 7, 3: 11, 4: 1 } assert calculate(books_ids, events) == expected
def test_it_creates_receive_action_name_for_catalog(): action = BookAction(BookActionName.RECEIVE, 5, book_id=1) catalog_action = action.format_for_catalog(None) assert catalog_action['name'] == 'otrzymano'
def test_it_raises_error_with_sell_action_and_receiver_id(): with pytest.raises(AssertionError): BookAction(BookActionName.SELL, 5, book_id=1, receiver_id=1)
def test_it_is_created_with_sell_action(): BookAction(BookActionName.SELL, 5, book_id=1)
def test_comment_could_be_none(): assert BookAction(BookActionName.RECEIVE, 1, book_id=1, comment=None)
def test_it_contains_comment(): book_action = BookAction(BookActionName.RECEIVE, 1, book_id=1) book_action.comment = 'Test comment' assert book_action.comment == 'Test comment'
def test_it_is_created_with_receive_action(): assert BookAction(BookActionName.RECEIVE, 5, book_id=1)
def test_it_is_created_with_release_action_and_receiver_id(): BookAction(BookActionName.RELEASE, 5, book_id=1, receiver_id=1)
def test_it_creates_release_action_name_for_catalog(): action = BookAction(BookActionName.RELEASE, 5, book_id=1, receiver_id=1) catalog_action = action.format_for_catalog(lambda id: {'name': 'Anonim'}) assert catalog_action['name'] == 'wydano'
def test_it_creates_sell_action_name_for_catalog(): action = BookAction(BookActionName.SELL, 5, book_id=1) catalog_action = action.format_for_catalog(lambda id: {'name': 'Anonim'}) assert catalog_action['name'] == 'sprzedano'
def test_it_raises_error_with_release_action_without_receiver_id(): with pytest.raises(AssertionError): BookAction(BookActionName.RELEASE, 5, book_id=1)
def test_it_creates_empty_string_for_empty_comment(): action = BookAction(BookActionName.RELEASE, 5, book_id=1, receiver_id=1) catalog_action = action.format_for_catalog(lambda id: {'name': 'Anonim'}) assert catalog_action['comment'] == ''
def test_it_raises_error_with_less_than_zero_copies(): with pytest.raises(AssertionError): BookAction(BookActionName.RECEIVE, -1, book_id=1)