def test_greater_even_to(): amount1 = Amount(50, 23) amount2 = Amount(20, 1) assert amount1 >= amount2 amount1 = Amount(20, 44) amount2 = Amount(20, 44) assert amount1 >= amount2
def test_loading_account_from_created_and_credited_events_adds_to_balance( new_random_account, related_random_account_created_event): credited_event = get_account_credited_event(new_random_account.account_id, Amount(50, 42)) account = Account( EventStream([related_random_account_created_event, credited_event])) assert account.balance == Amount(50, 42)
def test_maximum_debt_cannot_be_lower_than_balance( new_random_account_with_high_maximum_debt): new_random_account_with_high_maximum_debt.debit( new_random_account_with_high_maximum_debt.maximum_debt / Amount(2, 0), UniqueID()) with pytest.raises(ValueError): new_random_account_with_high_maximum_debt.set_maximum_debt( new_random_account_with_high_maximum_debt.maximum_debt / Amount(3, 0), UniqueID())
def test_loading_account_from_created_credited_debited_adds_to_balance( new_random_account, related_random_account_created_event, new_random_account_credit_event, new_random_account_debit_event): account = Account( EventStream([ related_random_account_created_event, new_random_account_credit_event, new_random_account_debit_event ])) amount_credit = Amount(new_random_account_credit_event.dollars, new_random_account_credit_event.cents) amount_debit = Amount(new_random_account_debit_event.dollars, new_random_account_debit_event.cents) final = amount_credit - amount_debit assert account.balance == final
def test_get_by_id_applies_event_stream(account_repo, fake_event_store): account_id = UniqueID() fake_event_store.load_stream = MagicMock( side_effect=return_based_on_id(account_id)) account = account_repo.get_by_id(account_id) assert account.balance == Amount(20) assert account.version == 2
def test_debit_adds_account_debited_event_to_uncommitted_changes( new_random_account_unlimited_debt, new_random_account_debit_event): amount = Amount(new_random_account_debit_event.dollars, new_random_account_debit_event.cents) new_random_account_unlimited_debt.debit(amount, UniqueID()) assert isinstance( new_random_account_unlimited_debt.uncommitted_changes[-1], AccountDebited)
def test_credit_adds_account_credited_event_to_uncommitted_changes( new_random_account, related_random_account_created_event, new_random_account_credit_event): account = Account(EventStream([related_random_account_created_event])) amount = Amount(new_random_account_credit_event.dollars, new_random_account_credit_event.cents) account.credit(amount, UniqueID(new_random_account_credit_event.operation_id)) assert account.uncommitted_changes[-1] == new_random_account_credit_event
def wrapped(target_id): if account_id == target_id: account_created = AccountCreated( operation_id=str(UniqueID()), account_id=str(UniqueID()), client_id=account_id, account_name='test' ) account = Account(EventStream([account_created])) account.set_maximum_debt(Amount(500, 0), UniqueID()) return account return None
def test_division_divider_greater_than_divided(): assert Amount(1, 50) / Amount(5, 70) == Amount(1, 50)
def test_division_dollars_zero_in_divided(): assert Amount(0, 60) / Amount(0, 30) == Amount(0, 2)
def test_division_cents_zero_in_divided(): assert Amount(6, 0) / Amount(1, 50) == Amount(4, 0)
def test_set_maximum_debt_converts_dollars_and_cents_passed_to_amount(fake_account_repo, account_id): debit_account(UniqueID(), account_id, fake_account_repo, AmountDTO(132, 55)) account_called_with: Account = fake_account_repo.save.call_args.args[0] maximum_debt_event: AccountMaximumDebtChanged = account_called_with.uncommitted_changes[-1] assert Amount(maximum_debt_event.dollars, maximum_debt_event.cents) == Amount(132, 55)
def new_account(): account = Account.create(UniqueID(), UniqueID(), UniqueID(), 'test') account.set_maximum_debt(Amount(600), UniqueID()) account.debit(Amount(120), UniqueID()) account.credit(Amount(700), UniqueID()) return account
def test_if_cents_over_100_it_converts_to_dollars(): amount = Amount(8, 253) assert amount.dollars == 10 assert amount.cents == 53
def test_amount_immutable(): amount = Amount(5, 12) with pytest.raises(FrozenInstanceError): amount.dollars = 4
def test_greater_if_first_has_less_dollars_more_cents(): amount1 = Amount(1, 99) amount2 = Amount(43, 12) assert amount2 > amount1
def test_greater_if_first_has_more_dollars_and_less_cents(): amount1 = Amount(9, 21) amount2 = Amount(2, 23) assert amount1 > amount2
def test_two_different_objects_different_amount_not_equal(): amount1 = Amount(3, 1) amount2 = Amount(6, 32) assert amount1 != amount2
def test_two_different_objects_same_amount_are_equal(): amount1 = Amount(5, 43) amount2 = Amount(5, 43) assert amount1 == amount2
def test_total_cents(): assert Amount(9, 42).total_cents == 942
def test_total_dollars(): assert Amount(4, 12).total_dollars == 4.12
def test_division_no_zeros(): assert Amount(50, 80) / Amount(25, 40) == Amount(2, 0)
def test_division_by_zero_dollars_and_cents(): with pytest.raises(ZeroDivisionError): Amount(12, 32) / Amount(0, 0)
def test_credit_account_converts_dollars_and_cents_passed_to_amount(fake_account_repo, account_id): credit_account(UniqueID(), account_id, fake_account_repo, AmountDTO(50, 24)) account_called_with: Account = fake_account_repo.save.call_args.args[0] credit_event: AccountCredited = account_called_with.uncommitted_changes[-1] assert Amount(credit_event.dollars, credit_event.cents) == Amount(50, 24)
def test_negative_of_amount(): assert -Amount(50, 24) == Amount(-50, -24)
def test_division_cents_zero_in_divider(): assert Amount(10, 30) / Amount(2, 0) == Amount(5, 15)
def test_raises_value_error_when_dividing_by_negative_amount(): assert Amount(12, 0) / Amount(-3, 0) == Amount(-4, 0)
def test_division_dollar_zero_in_divider(): assert Amount(2, 50) / Amount(0, 10) == Amount(0, 25)
def test_amount_lower_than_logic(): amount1 = Amount(1, 98) amount2 = Amount(6, 24) assert amount1 < amount2
def test_debit_account_converts_dollars_and_cents_passed_to_amount(fake_account_repo, account_id): debit_account(UniqueID(), account_id, fake_account_repo, AmountDTO(99, 12)) account_called_with: Account = fake_account_repo.save.call_args.args[0] debit_event: AccountDebited = account_called_with.uncommitted_changes[-1] assert Amount(debit_event.dollars, debit_event.cents) == Amount(99, 12)