Skip to content

Commit

Permalink
refactor(statement): use transaction class to encapsulate statement e…
Browse files Browse the repository at this point in the history
…nums
  • Loading branch information
benjamin-awd committed Oct 6, 2023
1 parent 28a7d03 commit 995db9b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 152 deletions.
24 changes: 16 additions & 8 deletions monopoly/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,34 @@

from pandas import DataFrame

from monopoly.helpers.constants import BankStatement
from monopoly.helpers.constants import AccountType, BankNames, BankStatement
from monopoly.pdf import PdfPage

logger = logging.getLogger(__name__)


@dataclass
class StatementConfig:
bank_name: str
account_type: str
bank_name: BankNames
account_type: AccountType
statement_date_format: str
transaction_pattern: str
transaction_date_format: str
date_pattern: str
multiline_transactions: bool = False

# Convert enums to strings
def __post_init__(self):
self.bank_name = self.bank_name.value
self.account_type = self.account_type.value


@dataclass
class Transaction:
date: str
description: str
amount: float


@dataclass
class Statement:
Expand Down Expand Up @@ -51,11 +63,7 @@ def _process_line(self, line: str, page: list[str], idx: int) -> dict:
except IndexError as err:
logger.debug(err)

return {
BankStatement.DATE: date,
BankStatement.DESCRIPTION: description,
BankStatement.AMOUNT: amount,
}
return vars(Transaction(date, description, amount))
return None

@cached_property
Expand Down
50 changes: 9 additions & 41 deletions tests/citibank/test_citibank_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@

from monopoly.bank import Statement
from monopoly.banks.citibank import Citibank
from monopoly.helpers.constants import BankStatement
from monopoly.statement import Transaction


def test_citibank_transform_cross_year(citibank: Citibank, statement: Statement):
raw_df = pd.DataFrame(
[
{
BankStatement.DATE: "09 JAN",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: "31.45",
},
{
BankStatement.DATE: "12 DEC",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: "29.80",
},
Transaction("09 JAN", "Shopee Singapore", "31.45"),
Transaction("12 DEC", "UNIQLO SINGAPORE", "29.80"),
]
)
statement.statement_date = datetime(2024, 1, 1)
Expand All @@ -29,16 +21,8 @@ def test_citibank_transform_cross_year(citibank: Citibank, statement: Statement)

expected_data = pd.DataFrame(
[
{
BankStatement.DATE: "2024-01-09",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: 31.45,
},
{
BankStatement.DATE: "2023-12-12",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: 29.80,
},
Transaction("2024-01-09", "Shopee Singapore", 31.45),
Transaction("2023-12-12", "UNIQLO SINGAPORE", 29.80),
]
)

Expand All @@ -48,16 +32,8 @@ def test_citibank_transform_cross_year(citibank: Citibank, statement: Statement)
def test_citibank_transform_within_year(citibank: Citibank, statement: Statement):
raw_df = pd.DataFrame(
[
{
BankStatement.DATE: "09 JUN",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: "31.45",
},
{
BankStatement.DATE: "12 JUN",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: "29.80",
},
Transaction("09 JUN", "Shopee Singapore", "31.45"),
Transaction("12 JUN", "UNIQLO SINGAPORE", "29.80"),
]
)

Expand All @@ -68,16 +44,8 @@ def test_citibank_transform_within_year(citibank: Citibank, statement: Statement

expected_data = pd.DataFrame(
[
{
BankStatement.DATE: "2023-06-09",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: 31.45,
},
{
BankStatement.DATE: "2023-06-12",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: 29.80,
},
Transaction("2023-06-09", "Shopee Singapore", 31.45),
Transaction("2023-06-12", "UNIQLO SINGAPORE", 29.80),
]
)

Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from monopoly.banks.hsbc import Hsbc
from monopoly.banks.ocbc import Ocbc
from monopoly.gmail import Message, MessageAttachment
from monopoly.helpers.constants import AccountType, BankNames
from monopoly.pdf import PdfConfig, PdfParser


Expand Down Expand Up @@ -67,8 +68,8 @@ def statement(monkeypatch, statement_config):
@pytest.fixture(scope="session")
def statement_config():
statement_config = StatementConfig(
account_type="Savings",
bank_name="Example Bank",
account_type=AccountType.CREDIT,
bank_name=BankNames.OCBC,
statement_date_format=None,
transaction_pattern=None,
transaction_date_format=None,
Expand Down
50 changes: 9 additions & 41 deletions tests/hsbc/test_hsbc_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@

from monopoly.bank import Statement
from monopoly.banks.hsbc import Hsbc
from monopoly.helpers.constants import BankStatement
from monopoly.statement import Transaction


def test_hsbc_transform_cross_year(hsbc: Hsbc, statement: Statement):
raw_df = pd.DataFrame(
[
{
BankStatement.DATE: "09 Jan",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: "31.45",
},
{
BankStatement.DATE: "12 Dec",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: "29.80",
},
Transaction("09 Jan", "Shopee Singapore", "31.45"),
Transaction("12 Dec", "UNIQLO SINGAPORE", "29.80"),
]
)
statement.statement_date = datetime(2024, 1, 1)
Expand All @@ -29,16 +21,8 @@ def test_hsbc_transform_cross_year(hsbc: Hsbc, statement: Statement):

expected_data = pd.DataFrame(
[
{
BankStatement.DATE: "2024-01-09",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: 31.45,
},
{
BankStatement.DATE: "2023-12-12",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: 29.80,
},
Transaction("2024-01-09", "Shopee Singapore", 31.45),
Transaction("2023-12-12", "UNIQLO SINGAPORE", 29.80),
]
)

Expand All @@ -48,16 +32,8 @@ def test_hsbc_transform_cross_year(hsbc: Hsbc, statement: Statement):
def test_hsbc_transform_within_year(hsbc: Hsbc, statement: Statement):
raw_df = pd.DataFrame(
[
{
BankStatement.DATE: "09 Jun",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: "31.45",
},
{
BankStatement.DATE: "12 Jun",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: "29.80",
},
Transaction("09 Jun", "Shopee Singapore", "31.45"),
Transaction("12 Jun", "UNIQLO SINGAPORE", "29.80"),
]
)

Expand All @@ -68,16 +44,8 @@ def test_hsbc_transform_within_year(hsbc: Hsbc, statement: Statement):

expected_data = pd.DataFrame(
[
{
BankStatement.DATE: "2023-06-09",
BankStatement.DESCRIPTION: "Shopee Singapore SINGAPORE SG",
BankStatement.AMOUNT: 31.45,
},
{
BankStatement.DATE: "2023-06-12",
BankStatement.DESCRIPTION: "UNIQLO SINGAPORE PTE. SINGAPORE SG",
BankStatement.AMOUNT: 29.80,
},
Transaction("2023-06-09", "Shopee Singapore", 31.45),
Transaction("2023-06-12", "UNIQLO SINGAPORE", 29.80),
]
)

Expand Down
26 changes: 11 additions & 15 deletions tests/ocbc/test_ocbc_load.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import os
import tempfile
from datetime import datetime

import pandas as pd
from pandas.testing import assert_frame_equal

from monopoly.bank import Statement
from monopoly.banks.ocbc import Ocbc
from monopoly.helpers.constants import ROOT_DIR, BankStatement
from monopoly.helpers.constants import ROOT_DIR
from monopoly.statement import Transaction


def test_ocbc_write_to_local_csv(ocbc: Ocbc, statement: Statement):
transformed_df = pd.DataFrame(
[
{
BankStatement.DATE: "2024-01-12",
BankStatement.DESCRIPTION: "FAIRPRICE FINEST SINGAPORE SG",
BankStatement.AMOUNT: 18.49,
},
{
BankStatement.DATE: "2023-12-28",
BankStatement.DESCRIPTION: "DA PAOLO GASTRONOMIA SING — SINGAPORE SG",
BankStatement.AMOUNT: 19.69,
},
Transaction("2024-01-12", "FAIRPRICE FINEST", 18.49),
Transaction("2023-12-28", "DA PAOLO GASTRONOMIA", 19.69),
]
)
statement.statement_date = datetime(2024, 1, 1)
ocbc.load(transformed_df, statement)
statement.statement_date = datetime(2999, 1, 1)
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
csv_file_path = temp_file.name
ocbc.load(df=transformed_df, statement=statement, csv_file_path=csv_file_path)

local_df = pd.read_csv(os.path.join(ROOT_DIR, "output", "OCBC-Credit-2024-01.csv"))
assert_frame_equal(transformed_df, local_df)
local_df = pd.read_csv(os.path.join(csv_file_path))
assert_frame_equal(transformed_df, local_df)
50 changes: 9 additions & 41 deletions tests/ocbc/test_ocbc_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@

from monopoly.bank import Statement
from monopoly.banks.ocbc import Ocbc
from monopoly.helpers.constants import BankStatement
from monopoly.statement import Transaction


def test_ocbc_transform_cross_year(ocbc: Ocbc, statement: Statement):
raw_df = pd.DataFrame(
[
{
BankStatement.DATE: "12/01",
BankStatement.DESCRIPTION: "FAIRPRICE FINEST SINGAPORE SG",
BankStatement.AMOUNT: "18.49",
},
{
BankStatement.DATE: "28/12",
BankStatement.DESCRIPTION: "DA PAOLO GASTRONOMIA SING — SINGAPORE SG",
BankStatement.AMOUNT: "19.69",
},
Transaction("12/01", "FAIRPRICE FINEST", "18.49"),
Transaction("28/12", "DA PAOLO GASTRONOMIA", "19.69"),
]
)
statement.statement_date = datetime(2024, 1, 1)
Expand All @@ -29,16 +21,8 @@ def test_ocbc_transform_cross_year(ocbc: Ocbc, statement: Statement):

expected_data = pd.DataFrame(
[
{
BankStatement.DATE: "2024-01-12",
BankStatement.DESCRIPTION: "FAIRPRICE FINEST SINGAPORE SG",
BankStatement.AMOUNT: 18.49,
},
{
BankStatement.DATE: "2023-12-28",
BankStatement.DESCRIPTION: "DA PAOLO GASTRONOMIA SING — SINGAPORE SG",
BankStatement.AMOUNT: 19.69,
},
Transaction("2024-01-12", "FAIRPRICE FINEST", 18.49),
Transaction("2023-12-28", "DA PAOLO GASTRONOMIA", 19.69),
]
)

Expand All @@ -48,16 +32,8 @@ def test_ocbc_transform_cross_year(ocbc: Ocbc, statement: Statement):
def test_ocbc_transform_within_year(ocbc: Ocbc, statement: Statement):
raw_df = pd.DataFrame(
[
{
BankStatement.DATE: "12/06",
BankStatement.DESCRIPTION: "FAIRPRICE FINEST SINGAPORE SG",
BankStatement.AMOUNT: "18.49",
},
{
BankStatement.DATE: "12/06",
BankStatement.DESCRIPTION: "DA PAOLO GASTRONOMIA SING — SINGAPORE SG",
BankStatement.AMOUNT: "19.69",
},
Transaction("12/06", "FAIRPRICE FINEST", "18.49"),
Transaction("12/06", "DA PAOLO GASTRONOMIA", "19.69"),
]
)
statement.statement_date = datetime(2023, 7, 1)
Expand All @@ -67,16 +43,8 @@ def test_ocbc_transform_within_year(ocbc: Ocbc, statement: Statement):

expected_data = pd.DataFrame(
[
{
BankStatement.DATE: "2023-06-12",
BankStatement.DESCRIPTION: "FAIRPRICE FINEST SINGAPORE SG",
BankStatement.AMOUNT: 18.49,
},
{
BankStatement.DATE: "2023-06-12",
BankStatement.DESCRIPTION: "DA PAOLO GASTRONOMIA SING — SINGAPORE SG",
BankStatement.AMOUNT: 19.69,
},
Transaction("2023-06-12", "FAIRPRICE FINEST", 18.49),
Transaction("2023-06-12", "DA PAOLO GASTRONOMIA", 19.69),
]
)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_generate_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def test_generate_blob_name(bank: Bank):

statement_date = datetime(2023, 8, 1)
expected_blob_name = (
"bank_name=Example Bank/"
"account_type=Savings/"
"bank_name=ocbc/"
"account_type=credit/"
"year=2023/"
"month=8/"
"Example Bank-Savings-2023-08-12345foo.csv"
"ocbc-credit-2023-08-12345foo.csv"
)

actual_blob_name = generate_name("blob", bank.statement_config, statement_date)
Expand All @@ -27,7 +27,7 @@ def test_generate_blob_name(bank: Bank):

def test_generate_file_name(bank: Bank):
statement_date = datetime(2023, 8, 1)
expected_file_name = "Example Bank-Savings-2023-08.csv"
expected_file_name = "ocbc-credit-2023-08.csv"

actual_file_name = generate_name("file", bank.statement_config, statement_date)

Expand Down

0 comments on commit 995db9b

Please sign in to comment.