From 62bf14b9683fea49dc93b96d9587c4e1a8dcbb95 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sun, 11 Jan 2026 19:13:55 +0100 Subject: [PATCH] Add testing of ledger_raw template. --- src/ledgplom/http.py | 6 +- src/ledgplom/ledger.py | 4 +- src/ledgplom/testing.py | 53 ++--- src/templates/ledger_raw.tmpl | 10 +- src/templates/ledger_structured.tmpl | 6 +- src/tests/empty.ledger_raw | 86 ++++++++ .../{empty.html => empty.ledger_structured} | 0 src/tests/full.ledger_raw | 192 ++++++++++++++++++ .../{full.html => full.ledger_structured} | 24 +-- 9 files changed, 333 insertions(+), 48 deletions(-) create mode 100644 src/tests/empty.ledger_raw rename src/tests/{empty.html => empty.ledger_structured} (100%) create mode 100644 src/tests/full.ledger_raw rename src/tests/{full.html => full.ledger_structured} (93%) diff --git a/src/ledgplom/http.py b/src/ledgplom/http.py index 85d4aa3..1938525 100644 --- a/src/ledgplom/http.py +++ b/src/ledgplom/http.py @@ -18,7 +18,7 @@ _TOK_RAW = 'raw' _TOK_STRUCTURED = 'structured' _PAGENAME_EDIT_RAW = f'{_PREFIX_EDIT}{_TOK_RAW}' _PAGENAME_EDIT_STRUCTURED = f'{_PREFIX_EDIT}{_TOK_STRUCTURED}' -_PAGENAME_LEDGER_RAW = f'{_PREFIX_LEDGER}{_TOK_RAW}' +PAGENAME_LEDGER_RAW = f'{_PREFIX_LEDGER}{_TOK_RAW}' PAGENAME_LEDGER_STRUCTURED = f'{_PREFIX_LEDGER}{_TOK_STRUCTURED}' @@ -116,7 +116,7 @@ class _Handler(PlomHttpHandler): elif self.pagename.startswith(_PREFIX_EDIT): self.get_edit(ctx, self.pagename == _PAGENAME_EDIT_RAW) elif self.pagename.startswith(_PREFIX_LEDGER): - self.get_ledger(ctx, self.pagename == _PAGENAME_LEDGER_RAW) + self.get_ledger(ctx, self.pagename == PAGENAME_LEDGER_RAW) else: self.get_ledger(ctx, False) ### end = time_ns() @@ -193,4 +193,4 @@ class _Handler(PlomHttpHandler): ctx['has_redundant_empty_lines'] =\ self.server.ledger.has_redundant_empty_lines self._send_rendered( - _PAGENAME_LEDGER_RAW if raw else PAGENAME_LEDGER_STRUCTURED, ctx) + PAGENAME_LEDGER_RAW if raw else PAGENAME_LEDGER_STRUCTURED, ctx) diff --git a/src/ledgplom/ledger.py b/src/ledgplom/ledger.py index 5e6b00b..d7b13da 100644 --- a/src/ledgplom/ledger.py +++ b/src/ledgplom/ledger.py @@ -603,11 +603,9 @@ class Ledger: booking_lines += [_TransferLine.from_dat(dat_line)] else: # enter new gap -> ready to start next block if booking_lines: - ### i_block = i_block.next = DatBlock(_Booking(booking_lines)) i_block.next = DatBlock(_Booking(booking_lines)) i_block = i_block.next - ### booking_lines.clear() - booking_lines = [] # .clear() + booking_lines = [] i_block.gap.add([_GapLine.from_dat(dat_line)]) self.last_save_hash = self._hash_dat_lines() diff --git a/src/ledgplom/testing.py b/src/ledgplom/testing.py index 0bcb545..cf91c08 100644 --- a/src/ledgplom/testing.py +++ b/src/ledgplom/testing.py @@ -7,12 +7,12 @@ from typing import Optional from jinja2 import (Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) # ourselves -from ledgplom.http import PAGENAME_LEDGER_STRUCTURED, PATH_TEMPLATES +from ledgplom.http import (PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED, + PATH_TEMPLATES) from ledgplom.ledger import Ledger _EXT_DAT = '.dat' -_EXT_HTML = '.html' _PATH_TESTS = Path('tests') @@ -24,26 +24,33 @@ def run_tests() -> None: print(f'{msg_prefix} FAILED – {abort_msg}') sys_exit(1) - tmpl = JinjaEnv(loader=JinjaFSLoader(PATH_TEMPLATES), - autoescape=True, - trim_blocks=True, - ).get_template(f'{PAGENAME_LEDGER_STRUCTURED}.tmpl') + jinja = JinjaEnv(loader=JinjaFSLoader(PATH_TEMPLATES), + autoescape=True, + trim_blocks=True) + templates = {item: jinja.get_template(f'{item}.tmpl') + for item in (PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED)} for path in [p for p in _PATH_TESTS.iterdir() if p.parts[-1].endswith(_EXT_DAT)]: - with Path(str(path)[:-len(_EXT_DAT)] + _EXT_HTML - ).open('r', encoding='utf8') as f: - lines_expected = [line.rstrip('\n') for line in f.readlines()] - lines_rendered = tmpl.render(blocks=Ledger(path).blocks).split('\n') - msg_prefix = f'test for {path}:' - for idx0, line in enumerate(lines_rendered): - idx1 = idx0 + 1 - abort_msg = '' - if idx1 > len(lines_expected): - abort_msg = f'only {idx0} lines expected' - elif lines_expected[idx0] != line: - abort_msg = f'line differs, expected: [{lines_expected[idx0]}]' - if abort_msg: - fail(abort_msg, msg_prefix, idx1) - if len(lines_expected) > idx1: - fail(f'more lines expected line {idx1}', msg_prefix, None) - print(f'{msg_prefix} passed') + for key, template in templates.items(): + test_path = Path(str(path)[:-len(_EXT_DAT)] + f'.{key}') + if not test_path.exists(): + continue + with test_path.open('r', encoding='utf8') as f: + lines_expected = [line.rstrip('\n') + for line in f.readlines()] + lines_rendered = template.render(blocks=Ledger(path).blocks + ).split('\n') + msg_prefix = f'test for {test_path}:' + for idx0, line in enumerate(lines_rendered): + idx1 = idx0 + 1 + abort_msg = '' + if idx1 > len(lines_expected): + abort_msg = f'only {idx0} lines expected' + elif lines_expected[idx0] != line: + abort_msg = ('line differs, expected: ' + f'[{lines_expected[idx0]}]') + if abort_msg: + fail(abort_msg, msg_prefix, idx1) + if len(lines_expected) > idx1: + fail(f'more lines expected line {idx1}', msg_prefix, None) + print(f'{msg_prefix} passed') diff --git a/src/templates/ledger_raw.tmpl b/src/templates/ledger_raw.tmpl index 5f129fb..76f3a3f 100644 --- a/src/templates/ledger_raw.tmpl +++ b/src/templates/ledger_raw.tmpl @@ -16,11 +16,13 @@ table { {{ macros.ledger_empty_lines_fix(has_redundant_empty_lines) -}} {% for block in blocks %} - {{ macros.ledger_block_columns('raw', block) }} +{{ macros.ledger_block_columns('raw', block) -}} {##}{% for line in block.lines %} - - - + {# -#} + + {{-line.raw}} {# -#} + {# -#} + {##}{% endfor %} {% endfor %}
{{line.raw}} 
diff --git a/src/templates/ledger_structured.tmpl b/src/templates/ledger_structured.tmpl index aeae2ba..9ec839e 100644 --- a/src/templates/ledger_structured.tmpl +++ b/src/templates/ledger_structured.tmpl @@ -17,8 +17,8 @@ {{ macros.ledger_block_columns('structured', block) -}} {##}{% if block.booking %} - + {{- block.booking.date }} {{ block.booking.target -}} {{ block.booking.intro_line.comment }} @@ -31,7 +31,7 @@ {{- macros.currency_short(line.currency) -}} - + {{- line.account -}} {{ line.comment }} diff --git a/src/tests/empty.ledger_raw b/src/tests/empty.ledger_raw new file mode 100644 index 0000000..6c66658 --- /dev/null +++ b/src/tests/empty.ledger_raw @@ -0,0 +1,86 @@ + + + + + + + + + +
+ + + + + + + +
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
 
+
+ + diff --git a/src/tests/empty.html b/src/tests/empty.ledger_structured similarity index 100% rename from src/tests/empty.html rename to src/tests/empty.ledger_structured diff --git a/src/tests/full.ledger_raw b/src/tests/full.ledger_raw new file mode 100644 index 0000000..09d2075 --- /dev/null +++ b/src/tests/full.ledger_raw @@ -0,0 +1,192 @@ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-01 test ; foo 
foo 10 € 
bar -10 € 
 
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-02 test 
bar -10 € ; bar 
baz 10 € 
 
 
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-02 test 
bar 20 € 
baz -20 € ; baz 
 
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-01 test 
foo 10 € 
bar -10 € 
 
+
+ + diff --git a/src/tests/full.html b/src/tests/full.ledger_structured similarity index 93% rename from src/tests/full.html rename to src/tests/full.ledger_structured index 8f3b3b2..ee98fa9 100644 --- a/src/tests/full.html +++ b/src/tests/full.ledger_structured @@ -108,19 +108,19 @@ td.currency { - 2001-01-01 test + 2001-01-01 test foo 10.00 € - foo + foo -10.00 € - bar + bar @@ -149,19 +149,19 @@ td.currency { - 2001-01-02 test + 2001-01-02 test -10.00 € - bar + bar bar 10.00 € - baz + baz @@ -193,19 +193,19 @@ td.currency { - 2001-01-02 test + 2001-01-02 test 20.00 € - bar + bar -20.00 € - baz + baz baz @@ -235,19 +235,19 @@ td.currency { - 2001-01-01 test + 2001-01-01 test 10.00 € - foo + foo -10.00 € - bar + bar -- 2.30.2