From: Christian Heller Date: Sun, 11 Jan 2026 18:13:55 +0000 (+0100) Subject: Add testing of ledger_raw template. X-Git-Url: https://plomlompom.com/repos/do_day?a=commitdiff_plain;h=HEAD;p=ledgplom Add testing of ledger_raw template. --- 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.html b/src/tests/empty.html deleted file mode 100644 index b173595..0000000 --- a/src/tests/empty.html +++ /dev/null @@ -1,94 +0,0 @@ - - - - - - - - - -
- - - - - - - - - -
- -
- -
- -
- [#]
- [b]
- [e] -
 
- -
- - 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.ledger_structured b/src/tests/empty.ledger_structured new file mode 100644 index 0000000..b173595 --- /dev/null +++ b/src/tests/empty.ledger_structured @@ -0,0 +1,94 @@ + + + + + + + + + +
+ + + + + + + + + +
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
 
+ +
+ + diff --git a/src/tests/full.html b/src/tests/full.html deleted file mode 100644 index 8f3b3b2..0000000 --- a/src/tests/full.html +++ /dev/null @@ -1,260 +0,0 @@ - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- -
- -
- [#]
- [b]
- [e] -
- -
- -
- -
- [#]
- [b]
- [e] -
2001-01-01 testfoo
10.00€foo
-10.00€bar
 
- -
- -
- -
- [#]
- [b]
- [e] -
2001-01-02 test
-10.00€barbar
10.00€baz
 
 
- -
- -
- -
- [#]
- [b]
- [e] -
2001-01-02 test
20.00€bar
-20.00€bazbaz
 
- -
- -
- -
- [#]
- [b]
- [e] -
2001-01-01 test
10.00€foo
-10.00€bar
 
- -
- - 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.ledger_structured b/src/tests/full.ledger_structured new file mode 100644 index 0000000..ee98fa9 --- /dev/null +++ b/src/tests/full.ledger_structured @@ -0,0 +1,260 @@ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-01 testfoo
10.00€foo
-10.00€bar
 
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-02 test
-10.00€barbar
10.00€baz
 
 
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-02 test
20.00€bar
-20.00€bazbaz
 
+ +
+ +
+ +
+ [#]
+ [b]
+ [e] +
2001-01-01 test
10.00€foo
-10.00€bar
 
+ +
+ +