-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: added tests for accounts_manager methods
- Loading branch information
1 parent
c9d1cb0
commit 43f566e
Showing
2 changed files
with
100 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,121 @@ | ||
from datetime import datetime | ||
import time | ||
from typing import Iterable | ||
|
||
from eth_account.signers.local import ( | ||
LocalAccount, | ||
) | ||
import pytest | ||
from sqlalchemy.orm import Session | ||
|
||
from backend.database_handler.accounts_manager import AccountsManager | ||
from backend.database_handler.errors import AccountNotFoundError | ||
from backend.database_handler.transactions_processor import TransactionsProcessor | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
@pytest.fixture | ||
def accounts_manager( | ||
session: Session, transactions_processor: TransactionsProcessor | ||
) -> Iterable[TransactionsProcessor]: | ||
yield AccountsManager(session, transactions_processor) | ||
def accounts_manager(session: Session): | ||
yield AccountsManager(session) | ||
|
||
|
||
def test_create_new_account(accounts_manager: AccountsManager): | ||
account = accounts_manager.create_new_account() | ||
assert isinstance(account, LocalAccount) | ||
|
||
account_data = accounts_manager.get_account_or_fail(account.address) | ||
assert account_data["id"] == account.address | ||
|
||
def test_accounts_manager(accounts_manager: AccountsManager): | ||
|
||
def test_create_new_account_with_address(accounts_manager: AccountsManager): | ||
address = "0x9F0e84243496AcFB3Cd99D02eA59673c05901501" | ||
accounts_manager.create_new_account_with_address(address) | ||
|
||
account_data = accounts_manager.get_account_or_fail(address) | ||
assert account_data["id"] == address | ||
|
||
|
||
def test_create_new_account_with_invalid_address(accounts_manager: AccountsManager): | ||
invalid_address = "invalid_address" | ||
with pytest.raises(ValueError): | ||
accounts_manager.create_new_account_with_address(invalid_address) | ||
|
||
|
||
def test_is_valid_address(accounts_manager: AccountsManager): | ||
valid_address = "0x9F0e84243496AcFB3Cd99D02eA59673c05901501" | ||
invalid_address = "invalid_address" | ||
|
||
assert accounts_manager.is_valid_address(valid_address) is True | ||
assert accounts_manager.is_valid_address(invalid_address) is False | ||
|
||
|
||
def test_get_account(accounts_manager: AccountsManager): | ||
address = "0x9F0e84243496AcFB3Cd99D02eA59673c05901501" | ||
accounts_manager.create_new_account_with_address(address) | ||
|
||
account = accounts_manager.get_account(address) | ||
assert account is not None | ||
assert account.id == address | ||
|
||
non_existent_address = "0x0000000000000000000000000000000000000000" | ||
non_existent_account = accounts_manager.get_account(non_existent_address) | ||
assert non_existent_account is None | ||
|
||
|
||
def test_get_account_or_fail(accounts_manager: AccountsManager): | ||
address = "0x9F0e84243496AcFB3Cd99D02eA59673c05901501" | ||
accounts_manager.create_new_account_with_address(address) | ||
|
||
account_data = accounts_manager.get_account_or_fail(address) | ||
assert account_data["id"] == address | ||
|
||
non_existent_address = "0x0000000000000000000000000000000000000000" | ||
with pytest.raises(AccountNotFoundError): | ||
accounts_manager.get_account_or_fail(non_existent_address) | ||
|
||
|
||
def test_get_account_balance(accounts_manager: AccountsManager): | ||
address = "0x9F0e84243496AcFB3Cd99D02eA59673c05901501" | ||
accounts_manager.create_new_account_with_address(address) | ||
|
||
balance = accounts_manager.get_account_balance(address) | ||
assert balance == 0 | ||
|
||
non_existent_address = "0x0000000000000000000000000000000000000000" | ||
non_existent_balance = accounts_manager.get_account_balance(non_existent_address) | ||
assert non_existent_balance == 0 | ||
|
||
|
||
def test_update_account_balance(accounts_manager: AccountsManager): | ||
address = "0x9F0e84243496AcFB3Cd99D02eA59673c05901501" | ||
accounts_manager.create_new_account_with_address(address) | ||
|
||
new_balance = 100 | ||
accounts_manager.update_account_balance(address, new_balance) | ||
|
||
updated_balance = accounts_manager.get_account_balance(address) | ||
assert updated_balance == new_balance | ||
|
||
non_existent_address = "0x0000000000000000000000000000000000000000" | ||
accounts_manager.update_account_balance(non_existent_address, new_balance) | ||
|
||
created_account_balance = accounts_manager.get_account_balance(non_existent_address) | ||
assert created_account_balance == new_balance | ||
|
||
|
||
def test_accounts_manager_update_timestamp(accounts_manager: AccountsManager): | ||
address = "0x9F0e84243496AcFB3Cd99D02eA59673c05901501" | ||
accounts_manager.create_new_account_with_address(address) | ||
|
||
account_data = accounts_manager.get_account_or_fail(address) | ||
first_updated_at = account_data["updated_at"] | ||
assert datetime.fromisoformat(first_updated_at) | ||
first_datetime = datetime.fromisoformat(first_updated_at) | ||
|
||
time.sleep(0.1) | ||
# Perform an action that should update the timestamp | ||
accounts_manager.update_account_balance(address, 100) | ||
|
||
account_data = accounts_manager.get_account_or_fail(address) | ||
assert account_data["id"] == address | ||
second_updated_at = account_data["updated_at"] | ||
assert datetime.fromisoformat(second_updated_at) | ||
assert first_updated_at < second_updated_at | ||
second_datetime = datetime.fromisoformat(second_updated_at) | ||
|
||
assert ( | ||
second_datetime > first_datetime | ||
), f"Expected {second_datetime} to be later than {first_datetime}" |