askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_bank_import_ofx.py · 7.5 KB · 177 lines
| 1 | """OFX import through the action (plan/bank-import-reconciliation-spec.md |
| 2 | section 3, v1.5). |
| 3 | |
| 4 | Parsing itself is covered in test_object_ofx.py; what matters here is that |
| 5 | OFX lands in the SAME canonical shape as CSV -- one landing zone, thin |
| 6 | importers -- and that the two things OFX carries and CSV usually does not |
| 7 | are actually put to work: FITID makes dedup exact, and a stated closing |
| 8 | balance plus a chained opening lets the statement check its own arithmetic. |
| 9 | """ |
| 10 | |
| 11 | import pathlib |
| 12 | |
| 13 | import object_execution |
| 14 | import object_records |
| 15 | import python_object_runtime |
| 16 | from conftest import stage_collection |
| 17 | |
| 18 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 19 | PACKAGES = REPO_ROOT / "packages" |
| 20 | BANKING_OBJECTS = PACKAGES / "app-banking" / "objects" |
| 21 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 22 | |
| 23 | ACCOUNT = "bank-1" |
| 24 | |
| 25 | |
| 26 | def setup_env(tmp_path, monkeypatch): |
| 27 | data_dir = tmp_path / "data" |
| 28 | for pkg, name in (("app-banking", "value_accounts"), |
| 29 | ("app-banking", "bank_import_profiles"), |
| 30 | ("app-banking", "bank_statement_imports"), |
| 31 | ("app-banking", "bank_lines"), |
| 32 | ("app-finance", "fin_accounts")): |
| 33 | stage_collection(data_dir, pkg, name) |
| 34 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 35 | object_records.create_collection_record( |
| 36 | "fin_accounts", {"id": "acct-cash", "name": "Cash", "account_type": "asset", |
| 37 | "owner_id": "dan"}, base_dir=data_dir) |
| 38 | object_records.create_collection_record( |
| 39 | "value_accounts", {"id": ACCOUNT, "name": "Checking", |
| 40 | "fin_account_id": "acct-cash", "owner_id": "dan"}, |
| 41 | base_dir=data_dir) |
| 42 | return data_dir |
| 43 | |
| 44 | |
| 45 | def ofx(transactions, *, closing="1475.00", start="20260701", end="20260731"): |
| 46 | body = "".join( |
| 47 | f"<STMTTRN><TRNTYPE>{t['type']}<DTPOSTED>{t['date']}<TRNAMT>{t['amount']}" |
| 48 | f"<FITID>{t['fitid']}<NAME>{t['name']}</STMTTRN>\n" |
| 49 | for t in transactions) |
| 50 | return f"""OFXHEADER:100 |
| 51 | DATA:OFXSGML |
| 52 | VERSION:102 |
| 53 | |
| 54 | <OFX><BANKMSGSRSV1><STMTTRNRS><STMTRS> |
| 55 | <CURDEF>USD |
| 56 | <BANKACCTFROM><BANKID>021000021<ACCTID>1234567890<ACCTTYPE>CHECKING</BANKACCTFROM> |
| 57 | <BANKTRANLIST><DTSTART>{start}<DTEND>{end} |
| 58 | {body}</BANKTRANLIST> |
| 59 | <LEDGERBAL><BALAMT>{closing}<DTASOF>{end}</LEDGERBAL> |
| 60 | </STMTRS></STMTTRNRS></BANKMSGSRSV1></OFX>""" |
| 61 | |
| 62 | |
| 63 | JULY = ofx([ |
| 64 | {"type": "CREDIT", "date": "20260702120000[-5:EST]", "amount": "1500.00", |
| 65 | "fitid": "TXN-A", "name": "ACME CORP PAYMENT"}, |
| 66 | {"type": "DEBIT", "date": "20260705", "amount": "-25.00", |
| 67 | "fitid": "TXN-B", "name": "SERVICE FEE"}, |
| 68 | ]) |
| 69 | |
| 70 | |
| 71 | def run(payload): |
| 72 | return object_execution.execute_object( |
| 73 | RUNTIME, |
| 74 | object_execution.ObjectExecutionRequest( |
| 75 | "action_import_ofx", method="POST", |
| 76 | payload={"_identity": {"user_id": "dan"}, **payload}), |
| 77 | roots=[BANKING_OBJECTS]).result |
| 78 | |
| 79 | |
| 80 | def lines(data_dir): |
| 81 | rows = object_records.read_collection_records("bank_lines", base_dir=data_dir) |
| 82 | rows.sort(key=lambda r: r["posted_on"]) |
| 83 | return rows |
| 84 | |
| 85 | |
| 86 | def imports(data_dir): |
| 87 | return object_records.read_collection_records( |
| 88 | "bank_statement_imports", base_dir=data_dir) |
| 89 | |
| 90 | |
| 91 | def test_ofx_lands_in_the_same_shape_as_csv(tmp_path, monkeypatch): |
| 92 | data_dir = setup_env(tmp_path, monkeypatch) |
| 93 | result = run({"bank_account_id": ACCOUNT, "content": JULY, |
| 94 | "opening_balance_cents": 0}) |
| 95 | assert result["status"] == 200 and result["imported"] == 2 |
| 96 | rows = lines(data_dir) |
| 97 | assert [int(r["amount_cents"]) for r in rows] == [150000, -2500] |
| 98 | assert [r["posted_on"] for r in rows] == ["2026-07-02", "2026-07-05"] |
| 99 | # FITID travels through as external_id -- the strong dedup key. |
| 100 | assert [r["external_id"] for r in rows] == ["TXN-A", "TXN-B"] |
| 101 | assert all(r["raw"] for r in rows) |
| 102 | imp = imports(data_dir)[0] |
| 103 | assert imp["source_format"] == "ofx" |
| 104 | assert imp["closing_balance_cents"] == "147500" # read from LEDGERBAL |
| 105 | assert imp["status"] == "accepted" # 0 + 147500 == 147500 |
| 106 | |
| 107 | |
| 108 | def test_fitid_dedups_even_when_the_bank_rewords_the_memo(tmp_path, monkeypatch): |
| 109 | """The reason OFX beats CSV: a content hash breaks when a pending |
| 110 | transaction posts under a tidied-up description; the bank's own id |
| 111 | does not.""" |
| 112 | data_dir = setup_env(tmp_path, monkeypatch) |
| 113 | run({"bank_account_id": ACCOUNT, "content": JULY, "opening_balance_cents": 0}) |
| 114 | reworded = ofx([ |
| 115 | {"type": "CREDIT", "date": "20260702120000[-5:EST]", "amount": "1500.00", |
| 116 | "fitid": "TXN-A", "name": "ACME CORP PAYMENT (POSTED)"}, |
| 117 | {"type": "DEBIT", "date": "20260709", "amount": "-142.35", |
| 118 | "fitid": "TXN-C", "name": "OFFICE SUPPLY"}, |
| 119 | ], closing="1332.65") |
| 120 | result = run({"bank_account_id": ACCOUNT, "content": reworded, |
| 121 | "opening_balance_cents": 0}) |
| 122 | assert result["duplicates"] == 1 and result["imported"] == 1 |
| 123 | assert len(lines(data_dir)) == 3 |
| 124 | |
| 125 | |
| 126 | def test_the_same_file_twice_is_a_no_op(tmp_path, monkeypatch): |
| 127 | data_dir = setup_env(tmp_path, monkeypatch) |
| 128 | first = run({"bank_account_id": ACCOUNT, "content": JULY, "opening_balance_cents": 0}) |
| 129 | again = run({"bank_account_id": ACCOUNT, "content": JULY, "opening_balance_cents": 0}) |
| 130 | assert again["already_imported"] is True |
| 131 | assert again["import_id"] == first["import_id"] |
| 132 | assert len(lines(data_dir)) == 2 |
| 133 | |
| 134 | |
| 135 | def test_a_chained_opening_makes_tie_out_work_and_says_it_was_derived(tmp_path, monkeypatch): |
| 136 | """OFX states no opening balance, so a lone file cannot tie out. Once a |
| 137 | prior statement exists its closing balance supplies one -- and the |
| 138 | import must record that the figure was derived, because it makes |
| 139 | continuity true by construction.""" |
| 140 | data_dir = setup_env(tmp_path, monkeypatch) |
| 141 | run({"bank_account_id": ACCOUNT, "content": JULY, "opening_balance_cents": 0}) |
| 142 | |
| 143 | august = ofx([{"type": "CREDIT", "date": "20260803", "amount": "500.00", |
| 144 | "fitid": "TXN-D", "name": "CUSTOMER PAYMENT"}], |
| 145 | closing="1975.00", start="20260801", end="20260831") |
| 146 | result = run({"bank_account_id": ACCOUNT, "content": august}) |
| 147 | assert result["opening_derived"] is True |
| 148 | assert result["checks"]["opening_balance"]["derived"] is True |
| 149 | assert result["checks"]["continuity"]["tautological"] is True |
| 150 | assert result["checks"]["tie_out"]["passed"] is True # 147500 + 50000 == 197500 |
| 151 | assert result["import_status"] == "accepted" |
| 152 | |
| 153 | |
| 154 | def test_a_chained_statement_that_does_not_add_up_is_flagged(tmp_path, monkeypatch): |
| 155 | data_dir = setup_env(tmp_path, monkeypatch) |
| 156 | run({"bank_account_id": ACCOUNT, "content": JULY, "opening_balance_cents": 0}) |
| 157 | wrong = ofx([{"type": "CREDIT", "date": "20260803", "amount": "500.00", |
| 158 | "fitid": "TXN-E", "name": "CUSTOMER PAYMENT"}], |
| 159 | closing="9999.00", start="20260801", end="20260831") |
| 160 | result = run({"bank_account_id": ACCOUNT, "content": wrong}) |
| 161 | assert result["import_status"] == "flagged" |
| 162 | assert "tie_out" in result["failed_checks"] |
| 163 | assert result["imported"] == 1 # evidence still lands |
| 164 | |
| 165 | |
| 166 | def test_refusals(tmp_path, monkeypatch): |
| 167 | setup_env(tmp_path, monkeypatch) |
| 168 | anon = object_execution.execute_object( |
| 169 | RUNTIME, |
| 170 | object_execution.ObjectExecutionRequest( |
| 171 | "action_import_ofx", method="POST", |
| 172 | payload={"bank_account_id": ACCOUNT, "content": JULY}), |
| 173 | roots=[BANKING_OBJECTS]).result |
| 174 | assert anon["status"] == 403 |
| 175 | assert run({"bank_account_id": "nope", "content": JULY})["status"] == 404 |
| 176 | assert run({"bank_account_id": ACCOUNT, "content": "not an ofx file"})["status"] == 422 |
| 177 | assert run({"bank_account_id": ACCOUNT})["status"] == 400 |