budget/tests/test_load.py
Luís Murta 01df97ed46
back_populates option on category relationship
Due to the use of the dataclasses mixin on the SQLAlchemy types, a
back_populates creates a RecursiveError when comparing two types. This
occurs because the dataclass will overwrite the __eq__ operator, and it
doesn't know when to stop comparing relationships.

Removing the dataclasses isn't the best approach, since then __init__,
__eq__ and __repr__ methods would have to be added to all types. Thus
the solution was to remove the relationship on the child (on a
one-to-one relationship) from the __eq__ operation, with the use of the
compare parameter.

Took the opportunity to define more logical __init__ methods on the
`Rule` and child classes.
Also revised the parameter options on some DB types.
2024-01-22 21:45:49 +00:00

39 lines
1.0 KiB
Python

from datetime import date
from decimal import Decimal
from typing import Sequence
import pytest
from pfbudget.db.client import Client
from pfbudget.db.model import BankTransaction, Transaction
from pfbudget.load.database import DatabaseLoader
from pfbudget.load.load import Loader
class FakeDatabaseClient(Client):
def __init__(self, url: str) -> None:
super().__init__(url)
def insert(self, transactions: Sequence[Transaction]) -> None:
pass
@pytest.fixture
def loader() -> Loader:
url = "postgresql://user:pass@127.0.0.1:5432/db"
client = FakeDatabaseClient(url)
return DatabaseLoader(client)
class TestDatabaseLoad:
def test_empty_url(self):
with pytest.raises(AssertionError):
_ = FakeDatabaseClient("")
def test_insert(self, loader: Loader):
transactions = [
BankTransaction(date(2023, 1, 1), "", Decimal("-500"), bank="Bank#1"),
BankTransaction(date(2023, 1, 2), "", Decimal("500"), bank="Bank#2"),
]
loader.load(transactions)