Skip to content

test(plugin): proptests for implicit_prices, sell_gains, coherent_cost, check_average_cost (closes #1004) - #1042

Merged
robcohen merged 2 commits into
mainfrom
feat/issue-1004-numeric-plugin-proptests
May 9, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/issue-1004-numeric-plugin-proptests

Conversation

@robcohen

@robcohen robcohen commented May 8, 2026

Copy link
Copy Markdown
Member

Summary

Phase 4 of the plugin-testing-quality plan (#992). Adds property tests covering algebraic invariants for the four numeric plugins listed in #1004 that lacked any proptest coverage. The fifth plugin in the issue (unrealized) already had a proptest from #1007.

Properties pinned

Plugin Invariant Why it matters
implicit_prices N C @@ (N*P) Q → emitted Price = C @ P Q exactly EXACT regression test for #992 (off-by-factor-of-N bug)
sell_gains warning iff `(sale - cost) * units
coherent_cost error count = ` with_cost ∩ price_only
check_average_cost selling at computed weighted mean → 0 warnings Rounding/off-by-one in running totals would break this

For check_average_cost the test computes the expected mean using rust_decimal (same crate as the plugin) so any precision loss is identical on both sides — a real bug, not a precision artifact, would be required to fail.

Test count

  • native_plugins_test: 167 → 171 (4 new proptests, each 64 cases per run)
  • All proptests use proptest::collection::vec for posting-list inputs and Decimal::new(cents, 2) for prices to keep arithmetic well inside rust_decimal precision.

Test plan

  • cargo test -p rustledger-plugin — all 171 tests pass (proptests + examples)
  • cargo clippy -p rustledger-plugin --all-features --all-targets -- -D warnings — clean (after fixing two uninlined_format_args lints)
  • cargo fmt -p rustledger-plugin -- --check — clean

Closes #1004

🤖 Generated with Claude Code

…t, check_average_cost (closes #1004)

Phase 4 of the plugin-testing-quality plan from #992. Each numeric
plugin now has at least one property test pinning its core algebraic
invariant — the kind of bug that example tests routinely miss because
the input space is too large.

Plugins covered (4 new proptests, joining 5 existing):

- `implicit_prices`: per-unit price round-trips through the `@@` total
  form. For any (units N, per-unit P), input `N C @@ (N*P) Q` → emitted
  Price `C @ P Q` exactly. This is the EXACT regression test for the
  #992 bug, where `@@` total was emitted as a per-unit price (off by
  factor of N).

- `sell_gains`: warning emitted iff `(sale_price - cost_per) * |units|`
  is non-zero AND no Income/Expenses posting is present. Sweeps gain,
  loss, and zero-gain (sale at cost) cases via independent cost/sale
  cent generators, plus a boolean for the gain-account's presence.

- `coherent_cost`: error count equals `|currencies_with_cost ∩
  currencies_with_price_only|`. Generators produce up to 8 postings
  with currency_id ∈ 0..4 and posting kind ∈ {cost-only, price-only,
  cost+price}. The reference computation in the test mirrors the
  plugin's classification, catching any drift.

- `check_average_cost`: selling at the exact weighted-mean cost basis
  produces no warning. Generators sweep 1..=5 buys at varied (units,
  price) tuples. The test computes the expected mean using
  `rust_decimal` (same crate as the plugin), so any precision loss is
  identical on both sides — a rounding bug or off-by-one in the running
  totals would surface as an unexpected warning.

Total tests in `native_plugins_test`: 167 → 171.
All proptests run 64 cases per CI run (default). Clippy clean
(uninlined-format-args), fmt clean.

Closes #1004

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds property-based tests to rustledger-plugin’s native plugin test suite to pin key algebraic invariants for numeric plugins that previously lacked proptest coverage (Phase 4 of the plugin-testing-quality plan; closes #1004).

Changes:

  • Added a proptest for implicit_prices to ensure @@ total-price annotations emit the correct per-unit Price (regression coverage for #992).
  • Added a proptest for coherent_cost to ensure the number of emitted errors matches the set-intersection invariant of currencies used with cost vs price-only.
  • Added a proptest for sell_gains (warning iff non-zero gain and no Income posting) and for check_average_cost (sell at computed weighted mean produces no warnings).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 31.3ms ± 4.7ms 22.1 MB
beancount 846.3ms ± 4.3ms 50.1 MB
Comparison 27x faster 2.3x less

vs main branch (2026-05-08)

Metric main this PR Change
Time 31ms 31.3ms +1.0% ✅
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

@codecov

codecov Bot commented May 8, 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 8, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 74%
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

…e arithmetic comment)

Two Copilot inline findings, both real:

1. The implicit_prices proptest's doc comment claimed both `@` and `@@`
   forms were tested, but the test only constructed `@@` (via
   `make_transaction_with_cost_and_price_total`). The inline cost-stub
   comment also misnamed it "per-unit annotation" when `is_total: true`
   was set. Restructured to use `make_txn_with_price_annotation` with a
   generated `is_total` bool, exercising both forms in the same proptest
   (renamed: `prop_implicit_prices_emits_per_unit_for_both_annotation_forms`).
   The `@@` case remains the EXACT regression for #992; the `@` case
   pins the trivial-but-mutable identity path.

2. The check_average_cost proptest's comment described the mean
   computation as "sum cost in cents, then divide by total units, then
   divide by 100" — but the code uses `Decimal::new(price_cents, 2)`,
   which already represents the price in dollars (scale 2 ⇒ value =
   cents/100). There's no separate `/ 100` step. Rewrote the comment
   to accurately describe what the code does.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen merged commit 93469d8 into main May 9, 2026
43 checks passed
@robcohen
robcohen deleted the feat/issue-1004-numeric-plugin-proptests branch May 9, 2026 00:08
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.

Phase 4: property tests for numeric plugins

2 participants