first commit
This commit is contained in:
152
test_import.py
Normal file
152
test_import.py
Normal file
@@ -0,0 +1,152 @@
|
||||
"""Tests for the YNAB import helpers in main.py."""
|
||||
from main import (
|
||||
_wf_to_ynab_millis, _build_import_ids, _convert_wf_to_ynab_transactions,
|
||||
_classify_for_import, _format_amount_dollars, _preview_import,
|
||||
)
|
||||
|
||||
|
||||
def test_wf_to_ynab_millis():
|
||||
# Credit card charge (no debitCreditType) -> outflow (negative)
|
||||
assert _wf_to_ynab_millis({"transactionAmount": 9.11}) == -9110
|
||||
# Credit card refund (debitCreditType=CREDIT) -> inflow (positive)
|
||||
assert _wf_to_ynab_millis({"transactionAmount": 1.64, "debitCreditType": "CREDIT"}) == 1640
|
||||
# Debit card charge (debitCreditType=DEBIT) -> outflow (negative)
|
||||
assert _wf_to_ynab_millis({"transactionAmount": 194.32, "debitCreditType": "DEBIT"}) == -194320
|
||||
# Debit card credit (debitCreditType=CREDIT) -> inflow (positive)
|
||||
assert _wf_to_ynab_millis({"transactionAmount": 225.0, "debitCreditType": "CREDIT"}) == 225000
|
||||
# Pending (no debitCreditType) -> outflow (negative)
|
||||
assert _wf_to_ynab_millis({"transactionAmount": 2.48}) == -2480
|
||||
print("test_wf_to_ynab_millis: PASS")
|
||||
|
||||
|
||||
def test_build_import_ids():
|
||||
amounts = [-9110, -9110, -5000]
|
||||
dates = ["2026-03-18", "2026-03-18", "2026-03-17"]
|
||||
ids = _build_import_ids(amounts, dates)
|
||||
assert ids == [
|
||||
"YNAB:-9110:2026-03-18:1",
|
||||
"YNAB:-9110:2026-03-18:2",
|
||||
"YNAB:-5000:2026-03-17:1",
|
||||
], f"Got {ids}"
|
||||
print("test_build_import_ids: PASS")
|
||||
|
||||
|
||||
def test_format_amount_dollars():
|
||||
assert _format_amount_dollars(-9110) == "$-9.11"
|
||||
assert _format_amount_dollars(225000) == "$+225.00"
|
||||
assert _format_amount_dollars(-194320) == "$-194.32"
|
||||
print("test_format_amount_dollars: PASS")
|
||||
|
||||
|
||||
def test_convert_wf_to_ynab_transactions():
|
||||
wf_txns = [
|
||||
{
|
||||
"transactionAmount": 9.11,
|
||||
"transactionDate": 1773817200000,
|
||||
"transactionDescription": "WALGREENS #10276 MORRISVILLE NC",
|
||||
"status": "POSTED",
|
||||
},
|
||||
{
|
||||
"transactionAmount": 2.48,
|
||||
"transactionDate": 1774076400000,
|
||||
"transactionDescription": "CULVERS",
|
||||
"status": "PENDING",
|
||||
},
|
||||
]
|
||||
result = _convert_wf_to_ynab_transactions(wf_txns, "acct-123")
|
||||
|
||||
assert result[0]["amount"] == -9110
|
||||
assert result[0]["cleared"] == "cleared"
|
||||
assert result[0]["flag_color"] == "blue"
|
||||
assert result[0]["account_id"] == "acct-123"
|
||||
assert result[0]["payee_name"] == "WALGREENS #10276 MORRISVILLE NC"
|
||||
assert "YNAB:-9110:" in result[0]["import_id"]
|
||||
|
||||
assert result[1]["amount"] == -2480
|
||||
assert result[1]["cleared"] == "uncleared"
|
||||
assert result[1]["flag_color"] == "blue"
|
||||
print("test_convert_wf_to_ynab_transactions: PASS")
|
||||
|
||||
|
||||
def test_classify_for_import():
|
||||
wf_txns = [
|
||||
{
|
||||
"transactionAmount": 9.11,
|
||||
"transactionDate": 1773817200000,
|
||||
"transactionDescription": "WALGREENS #10276",
|
||||
"status": "POSTED",
|
||||
},
|
||||
{
|
||||
"transactionAmount": 2.48,
|
||||
"transactionDate": 1774076400000,
|
||||
"transactionDescription": "CULVERS",
|
||||
"status": "PENDING",
|
||||
},
|
||||
{
|
||||
"transactionAmount": 50.00,
|
||||
"transactionDate": 1773817200000,
|
||||
"transactionDescription": "NEW TXN",
|
||||
"status": "POSTED",
|
||||
},
|
||||
]
|
||||
ynab_ready = _convert_wf_to_ynab_transactions(wf_txns, "acct-123")
|
||||
|
||||
# Derive dates to build matching YNAB transactions
|
||||
from main import _wf_date_to_iso
|
||||
date0 = _wf_date_to_iso(1773817200000)
|
||||
date1 = _wf_date_to_iso(1774076400000)
|
||||
|
||||
ynab_txns = [
|
||||
# Uncleared txn matching posted WALGREENS -> pending_to_posted
|
||||
{
|
||||
"id": "ynab-1", "date": date0, "payee_name": "WALGREENS",
|
||||
"category_name": None, "amount_milliCurrency": -9110, "memo": None,
|
||||
"account_id": "acct-123", "account_name": "Credit", "cleared": "uncleared",
|
||||
"import_id": "YNAB:-9110:" + date0 + ":1",
|
||||
},
|
||||
# Cleared txn with same import_id as CULVERS -> duplicate
|
||||
{
|
||||
"id": "ynab-2", "date": date1, "payee_name": "CULVERS",
|
||||
"category_name": None, "amount_milliCurrency": -2480, "memo": None,
|
||||
"account_id": "acct-123", "account_name": "Credit", "cleared": "cleared",
|
||||
"import_id": "YNAB:-2480:" + date1 + ":1",
|
||||
},
|
||||
]
|
||||
|
||||
classification = _classify_for_import(ynab_ready, ynab_txns)
|
||||
|
||||
assert len(classification.pending_to_posted) == 1
|
||||
assert classification.pending_to_posted[0][1]["id"] == "ynab-1"
|
||||
|
||||
assert len(classification.duplicates) == 1
|
||||
assert classification.duplicates[0]["flag_color"] == "red"
|
||||
|
||||
assert len(classification.new) == 1
|
||||
assert classification.new[0]["payee_name"] == "NEW TXN"
|
||||
|
||||
print("test_classify_for_import: PASS")
|
||||
|
||||
|
||||
def test_preview():
|
||||
"""Visual test - just make sure it doesn't crash."""
|
||||
wf_txns = [
|
||||
{"transactionAmount": 9.11, "transactionDate": 1773817200000,
|
||||
"transactionDescription": "WALGREENS #10276", "status": "POSTED"},
|
||||
{"transactionAmount": 50.00, "transactionDate": 1773817200000,
|
||||
"transactionDescription": "NEW TXN", "status": "POSTED"},
|
||||
]
|
||||
ynab_ready = _convert_wf_to_ynab_transactions(wf_txns, "acct-123")
|
||||
classification = _classify_for_import(ynab_ready, [])
|
||||
_preview_import(classification)
|
||||
print("test_preview: PASS")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_wf_to_ynab_millis()
|
||||
test_build_import_ids()
|
||||
test_format_amount_dollars()
|
||||
test_convert_wf_to_ynab_transactions()
|
||||
test_classify_for_import()
|
||||
test_preview()
|
||||
print("\nALL TESTS PASSED")
|
||||
|
||||
Reference in New Issue
Block a user