For DB connections that want to keep a session alive, there's a new `DatabaseSession` class that holds a SQLAlchemy session inside and offers methods similar to the `Database` class. The `Database` moves to use the `DatabaseSession` to remove duplicated code.
43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
from datetime import date
|
|
from decimal import Decimal
|
|
import pytest
|
|
|
|
from pfbudget.db.client import Client
|
|
from pfbudget.db.model import Base, Transaction
|
|
|
|
|
|
@pytest.fixture
|
|
def client() -> Client:
|
|
url = "sqlite://"
|
|
client = Client(url, execution_options={"schema_translate_map": {"pfbudget": None}})
|
|
Base.metadata.create_all(client.engine)
|
|
return client
|
|
|
|
|
|
class TestDatabase:
|
|
def test_initialization(self, client: Client):
|
|
pass
|
|
|
|
def test_insert_transactions(self, client: Client):
|
|
transactions = [
|
|
Transaction(date(2023, 1, 1), "", Decimal("-500")),
|
|
Transaction(date(2023, 1, 2), "", Decimal("500")),
|
|
]
|
|
|
|
with client.session as session:
|
|
session.insert(transactions)
|
|
assert session.select(Transaction) == transactions
|
|
|
|
def test_insert_transactions_independent_sessions(self, client: Client):
|
|
transactions = [
|
|
Transaction(date(2023, 1, 1), "", Decimal("-500")),
|
|
Transaction(date(2023, 1, 2), "", Decimal("500")),
|
|
]
|
|
|
|
client.insert(transactions)
|
|
result = client.select(Transaction)
|
|
for i, transaction in enumerate(result):
|
|
assert transactions[i].date == transaction.date
|
|
assert transactions[i].description == transaction.description
|
|
assert transactions[i].amount == transaction.amount
|