dodil/erp-brick-purchasing

DODIL ERP brick: purchasing

tests/test_statement_budget.py

"""Statement budgets: every statement is a round trip (100-300 ms on the live pg wire), so a request's statements must not grow with the number of
unrelated documents. Counted at the engine, one request at a time (erp_platform.testing.count_statements)."""
import pytest
from conftest import CLERK, PAYAPPROVER, api, bill, confirmed, must, receive, supplier_price, world
from erp_platform.testing import count_statements
from test_stories_pay import approved_bill, propose


@pytest.fixture
def counted(engine):
    return lambda: count_statements(engine)


def _show(label, c):
    print(f"BUDGET {label}: statements={c.statements} round_trips={c.round_trips}")


def _bills(first, n):
    for k in range(first, first + n):
        approved_bill(number=f"INV-{k}", note=f"DN-{k}")


def test_proposing_and_cancelling_a_run_do_not_grow_with_the_number_of_bills(counted):
    world()
    _bills(1, 2)
    with counted() as few_propose:
        run = must(propose())
    with counted() as few_cancel:
        must(api("POST", f"/payment-runs/{run['run_id']}/cancel", PAYAPPROVER, {"reason": "again"}))
    _bills(3, 18)
    with counted() as many_propose:
        run = must(propose())
    with counted() as many_cancel:
        must(api("POST", f"/payment-runs/{run['run_id']}/cancel", PAYAPPROVER, {"reason": "again"}))
    _show("propose (2 bills)", few_propose)
    _show("propose (20 bills)", many_propose)
    _show("cancel (2 bills)", few_cancel)
    _show("cancel (20 bills)", many_cancel)
    assert few_propose.statements <= 31 and many_propose.statements == few_propose.statements, (few_propose.statements, many_propose.statements)      # was 31 and 49 for 2 and 8 bills
    assert many_cancel.statements <= few_cancel.statements and few_cancel.statements <= 12, (few_cancel.statements, many_cancel.statements)       # was 16


def test_approving_a_bill_does_not_grow_with_the_number_of_other_bills(counted):
    world()
    _bills(1, 2)
    supplier_price()
    o = confirmed(lines=[{"product_id": "COLA-075", "quantity": "10"}])
    receive(o, quantity="10", note="DN-A")
    b = bill(o, number="INV-A", who=CLERK, quantity="10", unit_price="18.0000")[1]
    with counted() as few:
        must(api("POST", f"/bills/{b['bill_id']}/approve", PAYAPPROVER, {}))
    _bills(3, 18)
    o = confirmed(lines=[{"product_id": "COLA-075", "quantity": "10"}])
    receive(o, quantity="10", note="DN-B")
    b = bill(o, number="INV-B", who=CLERK, quantity="10", unit_price="18.0000")[1]
    with counted() as many:
        must(api("POST", f"/bills/{b['bill_id']}/approve", PAYAPPROVER, {}))
    _show("approve bill (2 others)", few)
    _show("approve bill (20 others)", many)
    # the settings, account map, supplier, order and receipts are each read once per request (request_memo): was 143, then 59
    assert many.statements == few.statements and few.statements <= 56, (few.statements, many.statements)