示例#1
0
def test_update_transaction_amount():
    old_amount = 100
    transaction = create_transaction_for_tests(old_amount,
                                               TransactionType.REGULAR)
    account = get_account_by_id(transaction.account_id)
    assert account.balance == old_amount * -1

    new_amount = 50
    transaction.amount = new_amount
    update_transaction(transaction, old_amount)
    account = get_account_by_id(transaction.account_id)
    assert account.balance == new_amount * -1
示例#2
0
def test_delete_regular_transaction():
    amount = 100
    transaction = create_transaction_for_tests(
        amount=amount, transaction_type=TransactionType.REGULAR)

    account = get_account_by_id(transaction.account_id)
    assert account.balance == amount * -1

    delete_transaction(transaction)
    assert len(get_transactions_list()) == 0

    account = get_account_by_id(transaction.account_id)
    assert account.balance == 0
示例#3
0
def test_delete_income_transaction():
    amount = 100
    transaction = create_transaction_for_tests(
        amount=amount, transaction_type=TransactionType.INCOME)

    account = get_account_by_id(transaction.account_id)
    assert account.balance == amount

    delete_transaction(transaction)
    assert len(get_transactions_list()) == 0

    account = get_account_by_id(transaction.account_id)
    assert account.balance == 0
示例#4
0
 def validate_currency(cls, value: str, values: Dict) -> str:
     # could be better to get account from context, but pydantic doesn't support it yet
     account = get_account_by_id(values["account_id"])
     assert account
     if value != account.currency:
         raise ValueError(
             "account currency didn't match transaction currency")
     return value
示例#5
0
def _update_related_account_balance(account_id: str, amount: int, increase: bool) -> Account:
    account = get_account_by_id(account_id)
    assert account

    if increase:
        account.balance += amount
    else:
        account.balance -= amount

    return update_account(account)
示例#6
0
def test_create_transfer_transaction_between_same_currency():
    amount = 100
    from_account = create_account_for_tests(currency=Currency.RUB)
    to_account = create_account_for_tests(currency=Currency.RUB)
    transaction_data = Transaction(
        type=TransactionType.TRANSFER,
        amount=amount,
        currency=from_account.currency,
        to_currency=to_account.currency,
        account_id=from_account.id,
        to_account_id=to_account.id,
        date=datetime.utcnow(),
    )
    transaction = create_transaction(transaction_data)

    from_account = get_account_by_id(transaction.account_id)
    # amount was deducted from account
    assert from_account.balance == amount * -1

    to_account = get_account_by_id(transaction.to_account_id)
    # amount was deducted from account
    assert to_account.balance == amount
示例#7
0
def test_delete_transfer_transaction():
    amount = 100
    to_amount = 10
    from_account = create_account_for_tests(currency=Currency.RUB)
    to_account = create_account_for_tests(currency=Currency.USD)
    transaction_data = Transaction(
        type=TransactionType.TRANSFER,
        amount=amount,
        to_amount=to_amount,
        currency=from_account.currency,
        to_currency=to_account.currency,
        account_id=from_account.id,
        to_account_id=to_account.id,
        date=datetime.utcnow(),
    )
    transaction = create_transaction(transaction_data)
    delete_transaction(transaction)

    account = get_account_by_id(transaction.account_id)
    assert account.balance == 0

    to_account = get_account_by_id(transaction.to_account_id)
    assert to_account.balance == 0
示例#8
0
def test_create_regular_transaction():
    amount = 100
    account = create_account_for_tests()
    transaction_data = Transaction(
        type=TransactionType.REGULAR,
        amount=amount,
        currency=Currency.USD,
        account_id=account.id,
        date=datetime.utcnow(),
        main_currency_exchange_rate=10,
    )
    transaction = create_transaction(transaction_data)

    account = get_account_by_id(transaction.account_id)
    # amount was deducted from account
    assert account.balance == amount * -1
示例#9
0
def test_update_transaction_api():
    user, token = create_user_and_token_for_tests()

    transaction = create_transaction_for_tests(100)
    account = get_account_by_id(transaction.account_id)
    assert account.balance == transaction.amount

    transaction.amount = 200
    response = client.put(
        "/finance/transactions/update",
        data=transaction.json(),
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == status.HTTP_200_OK
    response_data = response.json()
    assert response_data["account"]["id"] == account.id
    assert response_data["account"]["balance"] == transaction.amount
示例#10
0
def create_transaction(transaction: Transaction) -> Transaction:
    if not transaction.main_currency_exchange_rate and transaction.currency != MAIN_CURRENCY:
        currency_exchange_rate = get_currency_exchange_rate_for_nearest_date(
            MAIN_CURRENCY, transaction.currency, transaction.date
        )
        assert currency_exchange_rate
        transaction.main_currency_exchange_rate = currency_exchange_rate.rate

    transaction = db_insert_one(transaction)

    if transaction.type == TransactionType.TRANSFER:
        from_account = _update_related_account_balance(
            transaction.account_id, transaction.amount, increase=False
        )
        assert (
            from_account.currency == transaction.currency
        ), "Currency of account should be equal to transaction currency"
        transaction.account = from_account

        assert transaction.to_account_id
        to_account = get_account_by_id(transaction.to_account_id)
        assert to_account

        amount = transaction.amount
        assert (
            to_account.currency == transaction.to_currency
        ), "Currency of account should be equal to transaction currency"
        if to_account.currency != from_account.currency:
            assert transaction.to_amount is not None, "For different currencies to_amount should be present"
            amount = transaction.to_amount

        to_account = _update_related_account_balance(transaction.to_account_id, amount, increase=True)
        transaction.to_account = to_account
    else:
        increase = transaction.type == TransactionType.INCOME.value
        account = _update_related_account_balance(transaction.account_id, transaction.amount, increase)
        transaction.account = account

    return transaction
示例#11
0
def test_create_income_transaction_with_manually_set_main_currency_equivalent(
):
    create_currency_exchange_rate_for_tests(80)

    amount = 100
    account = create_account_for_tests()
    employer = create_employer_for_tests(account)
    transaction_data = Transaction(
        type=TransactionType.INCOME,
        amount=amount,
        currency=Currency.USD,
        account_id=account.id,
        from_employer_id=employer.id,
        date=datetime.utcnow(),
        main_currency_exchange_rate=10,
    )
    transaction = create_transaction(transaction_data)
    assert transaction.id is not None
    assert transaction.updated is not None
    assert transaction.main_currency_exchange_rate is not None
    assert transaction.main_currency_equivalent == 10 * amount

    account = get_account_by_id(transaction.account_id)
    assert account.balance == amount
示例#12
0
def test_create_income_transaction_and_set_main_currency_equivalent():
    rate = 80
    create_currency_exchange_rate_for_tests(rate)

    amount = 100 * MONEY_DIGITS
    account = create_account_for_tests()
    employer = create_employer_for_tests(account)
    transaction_data = Transaction(
        type=TransactionType.INCOME,
        amount=amount,
        currency=Currency.USD,
        account_id=account.id,
        from_employer_id=employer.id,
        date=datetime.utcnow(),
    )
    transaction = create_transaction(transaction_data)
    assert transaction.id is not None
    assert transaction.updated is not None
    assert transaction.main_currency_exchange_rate is not None
    # it was automatically set
    assert transaction.main_currency_equivalent == rate * amount

    account = get_account_by_id(transaction.account_id)
    assert account.balance == amount