コード例 #1
0
def test_raises_value_error_when_value_less_than_zero(
    mocked_locked_session,
):
    # ACT / ASSERT
    with pytest.raises(ValueError):
        TransactionInteractor.deposit(
            account_id=2,
            value=Decimal("-3.21"),
        )
コード例 #2
0
def test_raises_account_not_found(
    mocked_locked_session,
    mocked_account_interactor,
):
    # ARRANGE
    mocked_account_interactor.find_one.side_effect = AccountNotFound

    # ACT / Assert
    with pytest.raises(AccountNotFound):
        TransactionInteractor.deposit(
            account_id=2,
            value=Decimal("5.20"),
        )
コード例 #3
0
def test_return(
    mocked_locked_session,
    mocked_repository,
    mocked_account_interactor,
    account,
    transaction,
):
    # ARRANGE
    mocked_repository.create.return_value = transaction
    mocked_account_interactor.find_one.return_value = account.dict()

    expected_return = {
        "id": None,
        "value": Decimal("0.01"),
        "processed_at": datetime(2020, 1, 1, 12, 30, 59),
        "account_id": 2,
        "account": {
            "balance": Decimal("2.00"),
            "created_at": datetime(2020, 1, 1, 0, 0),
            "id": 2,
            "is_active": True,
            "max_daily_withdrawal": None,
            "person_id": 1,
        },
    }

    # ACT
    with freeze_time("2020-01-01 12:30:59"):
        actual_return = TransactionInteractor.deposit(
            account_id=account.id,
            value=transaction.value,
        )

    # ASSERT
    assert actual_return == expected_return
コード例 #4
0
def test_adds_transaction_value_to_account_balance(
    mocked_locked_session,
    mocked_repository,
    mocked_account_interactor,
    account,
    transaction,
):
    # ARRANGE
    mocked_repository.create.return_value = transaction
    mocked_account_interactor.find_one.return_value = account.dict()

    expected_transaction = transaction.copy()
    expected_transaction.account.balance += transaction.value

    # ACT
    with freeze_time("2020-01-01 12:30:59"):
        TransactionInteractor.deposit(
            account_id=account.id,
            value=transaction.value,
        )

    # ASSERT
    mocked_repository.create.call_args_list[0][0][0] == expected_transaction
コード例 #5
0
def create_transaction(
    account_id: int, creatable_transaction: CreatableTransaction
) -> ExposableTransaction:
    if creatable_transaction.value == 0:
        raise ValueError("Transaction value must not be 0")

    elif creatable_transaction.value > 0:
        transaction = TransactionInteractor.deposit(
            account_id=account_id,
            value=creatable_transaction.value,
        )

    else:
        transaction = TransactionInteractor.withdrawal(
            account_id=account_id,
            value=creatable_transaction.value,
        )

    return ExposableTransaction(**transaction)