From: Christian Heller Date: Tue, 13 Jan 2026 20:40:16 +0000 (+0100) Subject: Extend template testing to /balance, improve its template code. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bdb.prefix%7D%7D/static/process?a=commitdiff_plain;ds=sidebyside;p=ledgplom Extend template testing to /balance, improve its template code. --- diff --git a/src/ledgplom/http.py b/src/ledgplom/http.py index 1938525..3306a8d 100644 --- a/src/ledgplom/http.py +++ b/src/ledgplom/http.py @@ -16,6 +16,7 @@ _SERVER_HOST = '127.0.0.1' _SERVER_PORT = 8084 _TOK_RAW = 'raw' _TOK_STRUCTURED = 'structured' +PAGENAME_BALANCE = 'balance' _PAGENAME_EDIT_RAW = f'{_PREFIX_EDIT}{_TOK_RAW}' _PAGENAME_EDIT_STRUCTURED = f'{_PREFIX_EDIT}{_TOK_STRUCTURED}' PAGENAME_LEDGER_RAW = f'{_PREFIX_LEDGER}{_TOK_RAW}' @@ -111,7 +112,7 @@ class _Handler(PlomHttpHandler): ### start = time_ns() ctx = {'unsaved_changes': self.server.ledger.tainted, 'path': self.path} - if self.pagename == 'balance': + if self.pagename == PAGENAME_BALANCE: self.get_balance(ctx) elif self.pagename.startswith(_PREFIX_EDIT): self.get_edit(ctx, self.pagename == _PAGENAME_EDIT_RAW) @@ -133,7 +134,7 @@ class _Handler(PlomHttpHandler): ctx['valid'] = self.server.ledger.blocks_valid_up_incl(id_) ctx['block'] = self.server.ledger.blocks[id_] ctx['path_up_incl'] = f'{self.path_toks[1]}?up_incl=' - self._send_rendered('balance', ctx) + self._send_rendered(PAGENAME_BALANCE, ctx) def get_edit(self, ctx, raw: bool) -> None: 'Display edit form for individual Booking.' diff --git a/src/ledgplom/testing.py b/src/ledgplom/testing.py index 844128d..8e7a53a 100644 --- a/src/ledgplom/testing.py +++ b/src/ledgplom/testing.py @@ -2,13 +2,14 @@ # built-ins from sys import exit as sys_exit from pathlib import Path -from typing import Optional +from typing import Any, Optional # requirements.txt from jinja2 import (Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader) # ourselves -from ledgplom.http import (PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED, - PATH_TEMPLATES) +from ledgplom.http import ( + PAGENAME_BALANCE, PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED, + PATH_TEMPLATES) from ledgplom.ledger import Ledger @@ -27,10 +28,11 @@ def run_tests() -> None: 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)} + templates = {item: jinja.get_template(f'{item}.tmpl') for item in ( + PAGENAME_BALANCE, PAGENAME_LEDGER_RAW, PAGENAME_LEDGER_STRUCTURED)} for path in [p for p in _PATH_TESTS.iterdir() if p.parts[-1].endswith(_EXT_DAT)]: + ledger = Ledger(path) for key, template in templates.items(): test_path = Path(str(path)[:-len(_EXT_DAT)] + f'.{key}') if not test_path.exists(): @@ -38,10 +40,20 @@ def run_tests() -> None: with test_path.open('r', encoding='utf8') as f: lines_expected = [line.rstrip('\n') for line in f.readlines()] - ledger = Ledger(path) - ctx = { - 'blocks': ledger.blocks, - 'has_redundant_empty_lines': ledger.has_redundant_empty_lines} + ctx: dict[str, Any] = {} + if key == PAGENAME_BALANCE: + id_ = len(ledger.blocks) - 1 + ctx['roots'] = sorted([ac for + ac in ledger.calc_accounts().values() + if not ac.parent], + key=lambda r: r.basename) + ctx['valid'] = ledger.blocks_valid_up_incl(id_) + ctx['block'] = ledger.blocks[id_] + ctx['path_up_incl'] = f'{PAGENAME_BALANCE}?up_incl=' + else: + ctx['blocks'] = ledger.blocks + ctx['has_redundant_empty_lines'] =\ + ledger.has_redundant_empty_lines lines_rendered = template.render(**ctx).split('\n') msg_prefix = f'test for {test_path}:' for idx0, line in enumerate(lines_rendered): diff --git a/src/templates/balance.tmpl b/src/templates/balance.tmpl index f9eece8..1812ce9 100644 --- a/src/templates/balance.tmpl +++ b/src/templates/balance.tmpl @@ -9,7 +9,7 @@ td.money table { float: left; } summary::marker { - {{ macros.css_noninput_monospace() }} +{{ macros.css_noninput_monospace() -}} } summary { list-style-type: "[…]"; @@ -24,62 +24,65 @@ span.indent { -{% macro account_with_children(block_id, account, indent) %} -{% macro tr_money_balance(amount, currency) %} - - {{amount}} - {{ macros.currency_short(currency) }} - +{% macro account_with_children(block_id, account, name_indent) %} + +{%- macro _(code_indent=1) %} +{{- " " * 4 * code_indent -}} +{% endmacro %} + +{%- macro tr_money_balance(amount, currency, i=4) %} +{{_(i)}} +{{_(i)}} + {{- amount -}} + +{{_(i)}} + {{- macros.currency_short(currency) -}} + +{{_(i)}} {% endmacro %} -{% if account.get_wealth_at(block_id).moneys|length > 0 %} - - - {% if account.get_wealth_at(block_id).moneys|length == 1 %} - - {% for currency, amount in account.get_wealth_at(block_id).moneys.items() %} - {{tr_money_balance(amount, currency)}} - {% endfor %} -
- {% else %} -
- - - {% for currency, amount in account.get_wealth_at(block_id).moneys.items() %} - {% if 1 == loop.index %} - {{tr_money_balance(amount, currency)}} - {% endif %} - {% endfor %} -
-
- - {% for currency, amount in account.get_wealth_at(block_id).moneys.items() %} - {% if 1 < loop.index %} - {{tr_money_balance(amount, currency)}} - {% endif %} - {% endfor %} -
-
- {% endif %} - - - {% - for i in range(indent) %} {% - endfor - %}{% - if account.parent - %}:{% - endif - %}{{account.basename}}{% - if account.children - %}:{% - endif - %} - - {{account.desc}} - - {% for child in account.children_euro_sorted_at(block_id) %} - {{account_with_children(block_id, child, indent=indent+1)}} - {% endfor %} + +{%- if account.get_wealth_at(block_id).moneys | length > 0 %} +{{_()}} +{{_()}} +{##}{% if account.get_wealth_at(block_id).moneys | length == 1 %} +{{_()}} +{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} + {{- tr_money_balance(amt, cur) -}} +{######}{% endfor %} +{{_()}}
+{##}{% else %} +{{_()}}
+{{_()}} +{{_()}} +{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} + {{- tr_money_balance(amt, cur, i=6) + if 1 == loop.index -}} +{######}{% endfor %} +{{_()}}
+{{_()}}
+{{_()}} +{######}{% for cur, amt in account.get_wealth_at(block_id).moneys.items() %} + {{- tr_money_balance(amt, cur, 5) if 1 < loop.index -}} +{######}{% endfor %} +{{_()}}
+{{_()}}
+{##}{% endif %} +{{_()}} +{{_()}} +{{_()}} {##} +{######}{% for n in range(name_indent) -%} +  {# -#} +{######}{% endfor -%} + + {{- ":" if account.parent }} + {{- account.basename }} + {{- ":" if account.children }} +{{_()}} +{{_()}} {{ account.desc }} +{{_()}} +{##}{% for child in account.children_euro_sorted_at(block_id) %} + {{- account_with_children(block_id, child, name_indent=name_indent+1) -}} +{##}{% endfor %} {% endif %} {% endmacro %} @@ -87,14 +90,18 @@ span.indent { {% block content %}

-{{macros.conditional_block_nav(path_up_incl,'prev',block)}} -{{macros.conditional_block_nav(path_up_incl,'next',block)}} -| -balance after booking {{block.id_}} ({{block.booking.date}}: {{block.booking.target}}) + {{ macros.conditional_block_nav(path_up_incl, 'prev', block) -}}{##} + {{ macros.conditional_block_nav(path_up_incl, 'next', block) }}{##} + | + balance after {# -#} + {# -#} + booking {{block.id_}} {# -#} + ({{ block.booking.date }}: {{ block.booking.target }}){# -#} +

{% for root in roots %} - {{account_with_children(block.id_,root,indent=0)}} +{{ account_with_children(block.id_, root, name_indent=0) -}} {% endfor %}
{% endblock %} diff --git a/src/tests/empty.balance b/src/tests/empty.balance new file mode 100644 index 0000000..cfd8da5 --- /dev/null +++ b/src/tests/empty.balance @@ -0,0 +1,93 @@ + + + + + + + + + +

+ prev + next + | + balance after booking 0 (: ) +

+ +
+ + diff --git a/src/tests/full.balance b/src/tests/full.balance new file mode 100644 index 0000000..c348be1 --- /dev/null +++ b/src/tests/full.balance @@ -0,0 +1,221 @@ + + + + + + + + + +

+ prev + next + | + balance after booking 4 (2001-01-01: test) +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + +
-10€
+
+ + + + + +
-1USD
+
+
+ bar: +
+ + + + + +
-10€
+
+  :x: +
+ + + + + +
-10€
+
+   :y +
+ + + + + +
-1USD
+
+  :z +
+ + + + + +
-10€
+
+ baz +
+
+ + + + + + +
20€
+
+ + + + + +
1USD
+
+
+ foo: +
+
+ + + + + + +
10€
+
+ + + + + +
1USD
+
+
+  :x +
+ + diff --git a/src/tests/full.dat b/src/tests/full.dat index 2866ede..1f7f85a 100644 --- a/src/tests/full.dat +++ b/src/tests/full.dat @@ -12,6 +12,7 @@ baz -20 € ; baz 2001-01-01 test - foo 10 € - bar -10 € - + foo:x 10 € + foo:x 1 USD + bar:x:y -10 € + bar:z -1 USD diff --git a/src/tests/full.ledger_raw b/src/tests/full.ledger_raw index 50bf269..7134672 100644 --- a/src/tests/full.ledger_raw +++ b/src/tests/full.ledger_raw @@ -163,7 +163,7 @@ Detected redundant empty lines in gaps, - + - + [#]
[b]
[e] 2001-01-01 test  - foo 10 €  - bar -10 €  + foo:x 10 €  + foo:x 1 USD  + bar:x:y -10 €  + bar:z -1 USD    diff --git a/src/tests/full.ledger_structured b/src/tests/full.ledger_structured index d572547..f83b1a6 100644 --- a/src/tests/full.ledger_structured +++ b/src/tests/full.ledger_structured @@ -215,7 +215,7 @@ Detected redundant empty lines in gaps, - + - + [#]
[b]
[e] @@ -243,13 +243,25 @@ Detected redundant empty lines in gaps, 10.00 € - foo + foo:x + + + + 1.00 + USD + foo:x -10.00 € - bar + bar:x:y + + + + -1.00 + USD + bar:z