Exemple #1
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
Exemple #2
0
def create_transaction_for_tests(
    amount: int,
    transaction_type=TransactionType.INCOME,
    employer: Optional[Employer] = None,
    category: Optional[FinanceCategory] = None,
) -> Transaction:
    account = create_account_for_tests()
    if not employer:
        employer = create_employer_for_tests(account)
    return create_transaction(
        Transaction(
            type=transaction_type,
            amount=amount,
            currency=Currency.USD,
            account_id=account.id,
            from_employer_id=employer.id,
            date=datetime.utcnow(),
            main_currency_exchange_rate=100,
            category_id=category and category.id,
        ))
Exemple #3
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
Exemple #4
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
Exemple #5
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
Exemple #6
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
Exemple #7
0
def create_transaction_api(data: Transaction) -> Transaction:
    transaction = create_transaction(data)
    return transaction