Adds Loader, DatabaseLoader and (Db)Client
Adds unit test to test interfaces.
This commit is contained in:
parent
14131f50f9
commit
541295ef05
11
pfbudget/db/client.py
Normal file
11
pfbudget/db/client.py
Normal file
@ -0,0 +1,11 @@
|
||||
from typing import Sequence
|
||||
|
||||
from pfbudget.db.model import Transaction
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, url: str) -> None:
|
||||
self.url = url
|
||||
|
||||
def insert(self, transactions: Sequence[Transaction]) -> None:
|
||||
raise NotImplementedError
|
||||
0
pfbudget/load/__init__.py
Normal file
0
pfbudget/load/__init__.py
Normal file
14
pfbudget/load/database.py
Normal file
14
pfbudget/load/database.py
Normal file
@ -0,0 +1,14 @@
|
||||
from typing import Sequence
|
||||
|
||||
from pfbudget.db.client import Client
|
||||
from pfbudget.db.model import Transaction
|
||||
|
||||
from .load import Loader
|
||||
|
||||
|
||||
class DatabaseLoader(Loader):
|
||||
def __init__(self, client: Client) -> None:
|
||||
self.client = client
|
||||
|
||||
def load(self, transactions: Sequence[Transaction]) -> None:
|
||||
self.client.insert(transactions)
|
||||
10
pfbudget/load/load.py
Normal file
10
pfbudget/load/load.py
Normal file
@ -0,0 +1,10 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Sequence
|
||||
|
||||
from pfbudget.db.model import Transaction
|
||||
|
||||
|
||||
class Loader(ABC):
|
||||
@abstractmethod
|
||||
def load(self, transactions: Sequence[Transaction]) -> None:
|
||||
raise NotImplementedError
|
||||
37
tests/test_load.py
Normal file
37
tests/test_load.py
Normal file
@ -0,0 +1,37 @@
|
||||
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):
|
||||
_ = FakeDatabaseClient("")
|
||||
|
||||
def test_insert(self, loader: Loader):
|
||||
transactions = [
|
||||
BankTransaction(date(2023, 1, 1), "", Decimal("-500"), "Bank#1"),
|
||||
BankTransaction(date(2023, 1, 2), "", Decimal("500"), "Bank#2"),
|
||||
]
|
||||
|
||||
loader.load(transactions)
|
||||
Loading…
x
Reference in New Issue
Block a user