#!/bin/bash set -uxo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" cd /workspace git config --global --add safe.directory /workspace mkdir -p /logs/verifier git checkout 789865de12b2162a72cb44a98268de713ab4163e -- tests/test_cams.py tests/test_demat_units.py || true git apply --verbose --reject - <<'EOF_R2E_TEST_PATCH' diff --git a/tests/test_cams.py b/tests/test_cams.py index becbfab..91539c3 100644 --- a/tests/test_cams.py +++ b/tests/test_cams.py @@ -97,6 +97,31 @@ class TestCAMSDetailed: assert s["isin"], f"JSON: scheme without ISIN: {s['scheme']!r}" assert s["amfi"], f"JSON: scheme without AMFI: {s['scheme']!r}" + def test_pdf_document_closed_after_parse(self, cams_file, cams_password, monkeypatch): + """`read_cas_pdf` must close the pypdfium2 document it opens. + + Leaving it open leaks the page / text-page child handles and + makes pdfium emit "objects still open" at interpreter / library + teardown. We spy on the opener and assert the document's raw + handle is cleared (closed) once parsing returns. + """ + import casparser.parsers as parsers + from casparser import read_cas_pdf + + opened = [] + orig_open = parsers._open_document + + def _spy(*args, **kwargs): + doc = orig_open(*args, **kwargs) + opened.append(doc) + return doc + + monkeypatch.setattr(parsers, "_open_document", _spy) + read_cas_pdf(cams_file, cams_password) + + assert opened, "_open_document was never called" + assert not opened[0].raw, "PdfDocument left open after read_cas_pdf" + class TestCAMSDetailedNew: """Multi-decade CAMS DETAILED statement (`CAMS_CAS_FILE_NEW`).""" diff --git a/tests/test_demat_units.py b/tests/test_demat_units.py index 579c2ad..08bfb3a 100644 --- a/tests/test_demat_units.py +++ b/tests/test_demat_units.py @@ -250,6 +250,34 @@ class TestCDSLHelpers: assert mf.nav == Decimal("25.0000") assert mf.value == Decimal("2500.00") # not 0 assert mf.total_cost is None + + def test_mf_holdings_wrapped_folio(self): + """A long folio wraps its `/` tail into the next + cell ("910121125" | "82/0"). It must be spliced back into the + full folio (91012112582/0), not truncated to its head — and the + tail must not be mistaken for the distribution-mode column, which + would shift the numerics. Regression for dropped folio tails.""" + block = _block( + _cell("SPGD - Motilal Oswal S&P 500 Index Fund", 22, 120), + _cell("INF247L01AG2", 192, 230), + _cell("910121125", 273, 300), # folio head + _cell("82/0", 305, 325), # folio tail (wrapped onto next cell) + _cell("DIRECT", 340, 380), # distribution mode + _cell("20037.345", 400, 430), # units + _cell("28.3293", 450, 480), # NAV + _cell("250504.20", 500, 530), # invested + _cell("567643.96", 550, 580), # value + ) + mf = cdsl_p._parse_mf_holdings_row(block, {}) + assert mf is not None + # Full folio reconstructed (no dash, matching the authoritative + # "Folio No :" block), not truncated to "910121125". + assert mf.folio == "91012112582/0" + # Numerics still align after the tail cell is consumed by folio. + assert mf.balance == Decimal("20037.345") + assert mf.nav == Decimal("28.3293") + assert mf.total_cost == Decimal("250504.20") + assert mf.value == Decimal("567643.96") # value is consistent with balance * nav assert abs(mf.balance * mf.nav - mf.value) <= Decimal("0.01") EOF_R2E_TEST_PATCH R2E_APPLY_RC=$? if [ "$R2E_APPLY_RC" -ne 0 ]; then echo "0.000000" > /logs/verifier/reward.txt printf '%s' '{"reward": 0.0, "resolved": false, "parse_status": "test_patch_apply_failed"}' > /logs/verifier/reward-details.json echo "R2E: test_patch failed to apply (rc=$R2E_APPLY_RC) — failing closed" >&2 exit 0 fi ( python -m pytest -v tests/test_cams.py tests/test_demat_units.py ) > /logs/verifier/test_output.log 2>&1 TEST_EXIT_CODE=$? cat /logs/verifier/test_output.log python3 "$SCRIPT_DIR/verifier.py" --log /logs/verifier/test_output.log --f2p "$SCRIPT_DIR/f2p.json" --p2p "$SCRIPT_DIR/p2p.json" --test-cmds 'python -m pytest -v tests/test_cams.py tests/test_demat_units.py' --exit-code "$TEST_EXIT_CODE" --out-dir /logs/verifier || { [ "$TEST_EXIT_CODE" -eq 0 ] && echo "1.0" > /logs/verifier/reward.txt || echo "0.0" > /logs/verifier/reward.txt; } git checkout 789865de12b2162a72cb44a98268de713ab4163e -- tests/test_cams.py tests/test_demat_units.py || true exit 0