示例#1
0
def test_create_transaction_api():
    user, token = create_user_and_token_for_tests()
    account = create_account_for_tests()
    employer = create_employer_for_tests(account)
    category = create_finance_category_for_tests()
    transaction_data = Transaction(
        type=TransactionType.INCOME,
        amount=100,
        currency=Currency.USD,
        account_id=account.id,
        category_id=category.id,
        from_employer_id=employer.id,
        date=datetime.utcnow(),
        main_currency_exchange_rate=1,
    )
    response = client.post(
        "/finance/transactions/create",
        data=transaction_data.json(),
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == status.HTTP_200_OK
    response_data = response.json()
    assert response_data["id"] is not None
    assert response_data["account"]["id"] == account.id
    assert response_data["category"]["id"] == category.id
示例#2
0
def _create_accounts() -> List[Account]:
    settings: List[Dict[str, Any]] = [
        {
            "currency": Currency.USD,
            "is_favorite": True
        },
        {
            "currency": Currency.RUB,
            "is_favorite": True
        },
        {
            "currency": Currency.EUR,
            "is_favorite": False
        },
        {
            "currency": Currency.USD,
            "is_favorite": False
        },
    ]
    return [
        create_account_for_tests(order=x,
                                 currency=settings[x]["currency"],
                                 is_favorite=settings[x]["is_favorite"])
        for x in range(NUMBER_OF_ACCOUNTS)
    ]
示例#3
0
def test_delete_employer_api():
    user, token = create_user_and_token_for_tests()
    account = create_account_for_tests()
    employer = create_employer(Employer(name="Test", associated_account_id=account.id))
    response = client.delete(
        "/finance/employers/delete",
        data=employer.json(),
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == status.HTTP_200_OK
示例#4
0
def test_create_transaction_with_negative_amount():
    account = create_account_for_tests()
    with pytest.raises(ValidationError) as e:
        Transaction(
            type=TransactionType.INCOME,
            amount=-100,
            currency=Currency.USD,
            account_id=account.id,
            date=datetime.utcnow(),
        )
    assert "amount should be positive value greater than 0" in str(e.value)
示例#5
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
示例#6
0
def test_update_employer():
    account = create_account_for_tests()
    employer_data = Employer(name="Test", associated_account_id=account.id)
    employer = create_employer(employer_data)
    assert employer.id is not None
    assert employer.updated is not None

    updated_employer = Employer(**employer_data.dict())
    updated_employer.name = "New one"
    updated_employer = update_employer(updated_employer)

    assert updated_employer.id == employer.id
    assert updated_employer.name != employer.name
示例#7
0
def test_create_income_transaction_with_not_matched_currency():
    account = create_account_for_tests(currency=Currency.USD)
    employer = create_employer_for_tests(account)
    with pytest.raises(ValidationError) as e:
        Transaction(
            type=TransactionType.INCOME,
            amount=100,
            currency=Currency.RUB,
            account_id=account.id,
            from_employer_id=employer.id,
            date=datetime.utcnow(),
        )
    assert "account currency didn't match transaction currency" in str(e.value)
示例#8
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
示例#9
0
def test_get_list_of_employers_api():
    user, token = create_user_and_token_for_tests()
    account = create_account_for_tests()
    number_of_employers = 4
    for i in range(number_of_employers):
        employer_data = Employer(name=f"Test {i}", associated_account_id=account.id)
        create_employer(employer_data)
    response = client.get(
        "/finance/employers/get-list",
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == status.HTTP_200_OK
    response_data = response.json()
    assert len(response_data) == number_of_employers
示例#10
0
def test_create_employer_api():
    user, token = create_user_and_token_for_tests()
    account = create_account_for_tests()
    employer_data = Employer(name="Test", associated_account_id=account.id)
    response = client.post(
        "/finance/employers/create",
        data=employer_data.json(),
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == status.HTTP_200_OK
    response_data = response.json()
    assert response_data["id"] is not None
    assert response_data["name"] == employer_data.name
    assert response_data["associated_account"]["id"] == account.id
示例#11
0
def test_get_employers_list():
    account = create_account_for_tests()
    number_of_employers = 4
    for i in range(number_of_employers):
        employer = create_employer(Employer(name=f"Test {i}", associated_account_id=account.id))
        create_transaction_for_tests(amount=i + 1, employer=employer)

    employers = get_employers_list()
    assert len(employers) == number_of_employers
    assert employers[0].earnings == 4
    assert employers[0].earnings_currency is not None
    assert employers[1].earnings == 3
    assert employers[2].earnings == 2
    assert employers[3].earnings == 1
示例#12
0
def test_update_employer_api():
    user, token = create_user_and_token_for_tests()
    account = create_account_for_tests()
    employer = create_employer(Employer(name="Test", associated_account_id=account.id))
    new_data = Employer(**employer.dict())
    new_data.name = "Updated"
    response = client.put(
        "/finance/employers/update",
        data=new_data.json(),
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == status.HTTP_200_OK
    response_data = response.json()
    assert response_data["name"] == new_data.name
示例#13
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
示例#14
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
示例#15
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
示例#16
0
def test_create_employer():
    account = create_account_for_tests()
    employer_data = Employer(name="Test", associated_account_id=account.id)
    employer = create_employer(employer_data)
    assert employer.id is not None
    assert employer.updated is not None
示例#17
0
def test_delete_employer():
    account = create_account_for_tests()
    employer = create_employer(Employer(name="Test", associated_account_id=account.id))
    delete_employer(employer)
    assert len(get_employers_list()) == 0