Skip to content

test(plugin): test matrices for generate_base_ccy_prices, split_expenses, zerosum, check_average_cost - #1012

Merged
robcohen merged 1 commit into
mainfrom
feat/numeric-plugin-test-matrices
May 6, 2026
Merged

robcohen merged 1 commit into
mainfrom
feat/numeric-plugin-test-matrices

Conversation

@robcohen

@robcohen robcohen commented May 6, 2026

Copy link
Copy Markdown
Member

test(plugin): test matrices for generate_base_ccy_prices, split_expenses, zerosum, check_average_cost

Phase 1 of #992 plan (closes the remaining 4 of 11 Tier 1 plugins
in #1002). All four plugins had only one trivial integration test
each — usually with weak count assertions — and now have full
matrices with strict equality checks plus property tests for the
two with clean algebraic invariants.

Coverage

generate_base_ccy_prices (1 → 5 tests, +1 proptest)

Plugin reads existing prices and emits derived (C, base) entries
when chains (C, X) + (X, base) exist. Matrix:

  • no config → passthrough (was: lone test with weak > 2 count)
  • ETH→EUR + EUR→USD → exactly one ETH→USD = 2200, count + amount
    • currency all asserted strictly
  • target price already exists → no duplicate
  • prices already in base currency → no derivation

Proptest prop_generate_base_ccy_prices_multiplies_chain_exactly
pins the multiplicative invariant: derived rate = (C→X) * (X→base)
for any cents-based pair of rates.

split_expenses (1 → 4 tests, +1 proptest)

Plugin splits any Expenses:* posting (whose account doesn't
already include a member name) into N proportional sub-postings
with __automatic__: True metadata, plus emits Open directives
for the new sub-accounts.

Matrix:

  • no config → passthrough
  • 2 members, $100 → $50 each (was: weak >= 2 posting count;
    now strict on amounts, currencies, metadata, and emitted Opens)
  • account already contains a member name → unchanged, no
    automatic added
  • non-Expenses posting (Income/Assets) → unchanged

Proptest prop_split_expenses_sum_preserves_total pins:
sum(splits) = (amount/N) * N for random (amount, member_count).

zerosum (1 → 4 tests)

Plugin matches paired postings in zero-sum accounts within a
configurable date window and moves them to a "matched" account.

Matrix:

  • missing config → 1 error (existing test, kept)
  • pair within 30-day window → both halves move to matched account,
    none remain in zerosum
  • pair outside the window → unmatched, both remain in zerosum
  • single unmatched posting → remains in zerosum

(No proptest — the matching/window/account-substitution logic is
discrete enough that the example matrix is the right shape.)

check_average_cost (1 → 5 tests)

Plugin warns when a reducing posting's cost differs from the
account's running average by more than tolerance. Critical
constraint: only checked for accounts opened with NONE booking
(the lot-management-by-author case from #907).

Added make_open_with_booking helper. Matrix:

  • correct sale at average cost → silent
  • sale below average by 33% (default tolerance is 1%) → 1 warning
    mentioning the account and commodity
  • STRICT-booking account → skipped entirely (issue [Compat]: Plugin check_average_cost shows warning #907 false-
    positive guard)
  • account with booking: None → also skipped (only NONE-booking
    is checked)
  • custom tolerance: 40% deviation within a 50% tolerance → silent

Verified

  • 124 → 143 plugin integration tests (+19 new + 2 proptests)
  • scripts/check-plugin-test-quality.sh: OK
  • scripts/test-plugin-test-quality.sh: 28/28
  • cargo clippy -p rustledger-plugin --all-features --all-targets:
    clean

Tightened from existing weak shapes

Two existing tests had weak count assertions in the multi-line form
that PAT_A's missing multi-line variant doesn't catch (#1011):

  • test_generate_base_ccy_prices_creates_derived_price:
    assert!(price_count > 2) → strict count + amount
  • test_split_expenses_divides_by_members: assert!(...len() >= 2)
    → strict count + amounts + metadata

Both replaced by stronger assertions in the new matrices.

#1002 progress

After this PR: all 11 Tier 1 plugins covered (capital_gains
family in #1007, transaction-emitter family in #1008, this
numeric/emits family completes the set).

Closes part of #1002.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

…ses, zerosum, check_average_cost

Phase 1 of #992 plan (closes the remaining 4 of 11 Tier 1 plugins
in #1002). All four plugins had only one trivial integration test
each — usually with weak count assertions — and now have full
matrices with strict equality checks plus property tests for the
two with clean algebraic invariants.

## Coverage

### `generate_base_ccy_prices` (1 → 5 tests, +1 proptest)

Plugin reads existing prices and emits derived `(C, base)` entries
when chains `(C, X)` + `(X, base)` exist. Matrix:

- no config → passthrough (was: lone test with weak `> 2` count)
- ETH→EUR + EUR→USD → exactly one ETH→USD = 2200, count + amount
  + currency all asserted strictly
- target price already exists → no duplicate
- prices already in base currency → no derivation

Proptest `prop_generate_base_ccy_prices_multiplies_chain_exactly`
pins the multiplicative invariant: derived rate = `(C→X) * (X→base)`
for any cents-based pair of rates.

### `split_expenses` (1 → 4 tests, +1 proptest)

Plugin splits any Expenses:* posting (whose account doesn't
already include a member name) into N proportional sub-postings
with `__automatic__: True` metadata, plus emits Open directives
for the new sub-accounts.

Matrix:

- no config → passthrough
- 2 members, $100 → $50 each (was: weak `>= 2` posting count;
  now strict on amounts, currencies, metadata, and emitted Opens)
- account already contains a member name → unchanged, no
  __automatic__ added
- non-Expenses posting (Income/Assets) → unchanged

Proptest `prop_split_expenses_sum_preserves_total` pins:
sum(splits) = (amount/N) * N for random (amount, member_count).

### `zerosum` (1 → 4 tests)

Plugin matches paired postings in zero-sum accounts within a
configurable date window and moves them to a "matched" account.

Matrix:

- missing config → 1 error (existing test, kept)
- pair within 30-day window → both halves move to matched account,
  none remain in zerosum
- pair outside the window → unmatched, both remain in zerosum
- single unmatched posting → remains in zerosum

(No proptest — the matching/window/account-substitution logic is
discrete enough that the example matrix is the right shape.)

### `check_average_cost` (1 → 5 tests)

Plugin warns when a reducing posting's cost differs from the
account's running average by more than `tolerance`. Critical
constraint: only checked for accounts opened with `NONE` booking
(the lot-management-by-author case from #907).

Added `make_open_with_booking` helper. Matrix:

- correct sale at average cost → silent
- sale below average by 33% (default tolerance is 1%) → 1 warning
  mentioning the account and commodity
- STRICT-booking account → skipped entirely (issue #907 false-
  positive guard)
- account with `booking: None` → also skipped (only NONE-booking
  is checked)
- custom tolerance: 40% deviation within a 50% tolerance → silent

## Verified

- 124 → 143 plugin integration tests (+19 new + 2 proptests)
- `scripts/check-plugin-test-quality.sh`: OK
- `scripts/test-plugin-test-quality.sh`: 28/28
- `cargo clippy -p rustledger-plugin --all-features --all-targets`:
  clean

## Tightened from existing weak shapes

Two existing tests had weak count assertions in the multi-line form
that PAT_A's missing multi-line variant doesn't catch (#1011):

- `test_generate_base_ccy_prices_creates_derived_price`:
  `assert!(price_count > 2)` → strict count + amount
- `test_split_expenses_divides_by_members`: `assert!(...len() >= 2)`
  → strict count + amounts + metadata

Both replaced by stronger assertions in the new matrices.

## #1002 progress

After this PR: **all 11 Tier 1 plugins covered** (capital_gains
family in #1007, transaction-emitter family in #1008, this
numeric/emits family completes the set).

Closes part of #1002.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 6, 2026 15:04
@robcohen
robcohen requested a review from a team as a code owner May 6, 2026 15:04
@codecov

codecov Bot commented May 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 30ms ± 0.3ms 22.1 MB
beancount 878ms ± 8.2ms 50.2 MB
Comparison 29.3x faster 2.3x less

vs main branch (2026-05-06)

Metric main this PR Change
Time 30.9ms 30ms -2.9% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 88%
Full AST Match 99%
Total Files 786
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen merged commit 91065f7 into main May 6, 2026
35 checks passed
@robcohen
robcohen deleted the feat/numeric-plugin-test-matrices branch May 6, 2026 15:12
robcohen added a commit that referenced this pull request May 6, 2026
…tions (#1013)

Closes #1011.

## What

`scripts/check-plugin-test-quality.sh` had multi-line variants for
shapes B (`assert_ne!`), C (`!is_empty()`), and D (`!= 0`) added in
#1005. Shape A — `assert!(x.len() >= N)` / precomputed-count — only
had its single-line PAT_A. So a multi-line write of the original
#992 bug shape:

    assert!(
        txn_count >= 3,
        "forecast should expand to at least 3 transactions"
    );

slipped past the lint. PR #1008 caught one such instance by hand;
PR #1012 caught two more.

## Fix

Added `ML_PAT_A` mirroring single-line `PAT_A` with the same
count-shaped LHS restriction (`COUNT_LHS`) and the negative
lookbehind for `prop_assert!`/`debug_assert!` that the other
multi-line patterns already use:

    ML_PAT_A='(?<!\w)assert!\(\s*'"$COUNT_LHS"'\s*(?:>|>=)\s*\d+'

Wired into the existing `find_multiline_in_rs` call.

## Knock-on: tightened one existing test

The new pattern caught a real instance in
`test_check_drained_adds_balance_assertions_on_close`:

    assert!(
        balance_count > 0,
        "should insert balance assertions after close"
    );

The plugin emits exactly one balance assertion per (closed account,
currency); test setup has one Close on Assets:Bank with USD only,
so we expect 1. Tightened to `assert_eq!(.count(), 1, ...)`.

## Self-test

Extended `scripts/test-plugin-test-quality.sh` with 4 new cases
(28 → 32 total):
- multi-line `assert!(\n    x.len() >= 1,\n    "msg"\n)`
- multi-line `assert!(\n    price_count >= 1\n)` (the actual #992 shape)
- multi-line `assert!(\n    x.count() > 0\n)`
- false-positive guard: multi-line `prop_assert!(\n    x.len() >= 1\n)`
  must NOT fire

## Verified

- 143/143 plugin tests pass (one pre-existing test tightened, no new
  test functions)
- `scripts/check-plugin-test-quality.sh`: OK (clean on current main)
- `scripts/test-plugin-test-quality.sh`: 32/32
- `cargo clippy -p rustledger-plugin --all-features --all-targets`:
  clean

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant