Skip to content

feat: use high level macros in most tests#46

Merged
patrickleet merged 2 commits into
mainfrom
chore/test-cleanup
May 28, 2026
Merged

feat: use high level macros in most tests#46
patrickleet merged 2 commits into
mainfrom
chore/test-cleanup

Conversation

@patrickleet
Copy link
Copy Markdown
Collaborator

@patrickleet patrickleet commented May 28, 2026

Summary by CodeRabbit

  • Chores
    • Internal test-suite and metadata refactor: migrate to the new attribute-driven event/read-model annotations and update test call sites to the fallible event-handler API.
    • No public API, behavior, or user-facing functionality changed.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 15468a58-474e-4f69-90b7-a0f04f076f81

📥 Commits

Reviewing files that changed from the base of the PR and between 78a2e1f and c0a94c8.

📒 Files selected for processing (3)
  • tests/distributed_read_model/checkout_saga_service/models/saga.rs
  • tests/distributed_read_model/main.rs
  • tests/distributed_read_model/seat_inventory_service/models/seat.rs
✅ Files skipped from review due to trivial changes (1)
  • tests/distributed_read_model/checkout_saga_service/models/saga.rs

📝 Walkthrough

Walkthrough

This PR migrates test aggregates and read-model definitions across the sourced-rust test suite from #[digest(...)]/impl_aggregate! macro patterns to #[sourced(entity)]/#[event(...)] attribute patterns, and from #[readmodel(...)] attributes to dedicated #[table(...)] and #[id(...)] annotations. Method signatures are updated to accept owned IDs and return Result types, with all test invocations adjusted to unwrap fallible results. No public API changes; only internal test code was modified.

Changes

Event-Sourcing and Read-Model Attribute Refactoring

Layer / File(s) Summary
Core Library Test Aggregate and Read-Model Updates
src/commit_builder/mod.rs, src/outbox/commit.rs, src/snapshot/repository.rs
Test-only aggregates (TestAggregate, Dummy) switch to #[sourced(entity)] with #[event(...)] handlers, read-models gain #[table(...)]/#[id] attributes, and multiple test call sites unwrap result-returning handler calls.
Async Repository Aggregates and Test View Migration
tests/async_repository/main.rs
Aggregates (AlphaAggregate, BetaAggregate, SnapshotCounter) use new event/sourcing wiring; TestView read-model attributes updated to #[table(...)]/#[id]; tests pass owned string IDs and unwrap handler results.
Read-model attribute migrations (many tests)
tests/*/read_models/*, tests/*/views.rs
Systematic migration of #[readmodel(...)]#[table(...)] + #[id(...)] across many test read-models, preserving composite primary keys and relationship metadata.
Game aggregates and movement handlers
tests/blob_game/aggregate.rs
BlobGame switches from #[digest(...)] to #[event(...)] for initialization and movement handlers and adds #[sourced(entity)]; previous aggregate! wiring removed.
Postgres & SQLite repository tests
tests/postgres_repository/main.rs, tests/sqlite_repository/main.rs
Counter/CounterProjection aggregates refactored to #[sourced(...)] with #[event(...)] handlers that accept owned string IDs and return Result; read-models move to #[table(...)]/#[id]; tests updated to use .into() and .unwrap().
Saga & domain aggregates
tests/sagas/order/{inventory,order_aggregate,payment,saga}.rs
Saga and domain aggregates add #[sourced(entity)] and replace #[digest(...)] with #[event(...)] handlers (guards preserved); explicit sourced_rust::aggregate! blocks removed.
Snapshots and sourced snapshot fixtures
tests/snapshots/*, tests/sourced_snapshot/*, tests/todos/*
Snapshot and todo aggregates migrated to #[sourced(...)]/#[event(...)]; manual replay/impl_aggregate! wiring removed; tests updated to commit between fallible handler calls where appropriate.
Distributed read-model projection simplification
tests/distributed_read_model/main.rs
ProjectionCheckpoint aggregate and replay logic removed; projection now marks messages processed and commits read-model changes directly; trait bounds and imports simplified.
Misc small attribute updates
tests/distributed_read_model/checkout_saga_service/models/saga.rs, tests/distributed_read_model/seat_inventory_service/models/seat.rs
A few aggregates updated to include explicit aggregate_type = "..." in their #[sourced(...)] annotations and small import adjustments.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • patrickleet/sourced_rust#14: Event-record and digest API refactor that made event recording fallible and drives the .unwrap() updates in tests.
  • patrickleet/sourced_rust#35: Relational read-model derive infra enabling #[table]/#[id] attribute styles used in this PR.
  • patrickleet/sourced_rust#36: Relationship-includes changes that touch overlapping read-model schema files adjusted alongside this attribute migration.

Poem

🐰 From digests old to events new I hop and cheer,
I add a #[sourced] and make the handlers clear,
Tables wear #[table], ids don their #[id] crown,
Results unwrap like carrots shared around the town,
A tiny rabbit nods—tests pass, no panic here.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.59% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: use high level macros in most tests' accurately describes the main change throughout the PR: migrating test code from low-level approaches (digest/impl_aggregate!/manual replay) to high-level macro-based approaches (#[sourced]/​#[event] attributes).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/test-cleanup

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/sagas/order/inventory.rs`:
- Around line 49-53: The reserve method is unsafe for duplicate order_ids:
reserve(&mut self, order_id: String, quantity: u32) currently always subtracts
quantity from available and adds to reserved then inserts/overwrites
reservations, corrupting state on repeats; fix by checking reservations for an
existing entry first and either (a) early-return/block duplicates (if
reservations.contains_key(&order_id) return) or (b) make update idempotent by
computing let prev = self.reservations.get(&order_id).cloned().unwrap_or(0); let
delta = quantity.saturating_sub(prev); apply self.available -= delta;
self.reserved += delta; then insert the new quantity — choose one approach and
update the guard on the #[event("StockReserved", when =
self.can_reserve(quantity))] path accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: def2ad9a-0022-4a26-bbda-db26ec19c657

📥 Commits

Reviewing files that changed from the base of the PR and between 2409f89 and 78a2e1f.

📒 Files selected for processing (29)
  • src/commit_builder/mod.rs
  • src/outbox/commit.rs
  • src/snapshot/repository.rs
  • tests/async_repository/main.rs
  • tests/blob_game/aggregate.rs
  • tests/bomberman/views.rs
  • tests/distributed_read_model/main.rs
  • tests/distributed_read_model/read_models/checkout_step_view.rs
  • tests/distributed_read_model/read_models/checkout_view.rs
  • tests/distributed_read_model/read_models/seat_view.rs
  • tests/distributed_read_model_board/read_models/board_view.rs
  • tests/distributed_read_model_board/read_models/card_view.rs
  • tests/persistent_repository_conformance/read_models.rs
  • tests/postgres_repository/main.rs
  • tests/read_model_commit_bridge/main.rs
  • tests/read_model_metadata/main.rs
  • tests/read_model_relationship_includes/main.rs
  • tests/read_model_schema_bootstrap/main.rs
  • tests/read_model_session/main.rs
  • tests/read_models/aggregate.rs
  • tests/sagas/order/inventory.rs
  • tests/sagas/order/order_aggregate.rs
  • tests/sagas/order/payment.rs
  • tests/sagas/order/saga.rs
  • tests/snapshots/aggregate.rs
  • tests/snapshots/main.rs
  • tests/sourced_snapshot/aggregates.rs
  • tests/sqlite_repository/main.rs
  • tests/todos/aggregate.rs

Comment on lines +49 to 53
#[event("StockReserved", when = self.can_reserve(quantity))]
pub fn reserve(&mut self, order_id: String, quantity: u32) {
self.available -= quantity;
self.reserved += quantity;
self.reservations.insert(order_id, quantity);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Reject duplicate reservations for the same order.

Line 53 overwrites any existing entry for order_id, but Lines 51-52 still apply the full delta, so a second StockReserved for the same order corrupts available and reserved. Either block duplicate order IDs in the guard or make the map update additive/idempotent.

Suggested fix
-    #[event("StockReserved", when = self.can_reserve(quantity))]
+    #[event(
+        "StockReserved",
+        when = self.can_reserve(quantity) && !self.reservations.contains_key(&order_id)
+    )]
     pub fn reserve(&mut self, order_id: String, quantity: u32) {
         self.available -= quantity;
         self.reserved += quantity;
         self.reservations.insert(order_id, quantity);
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/sagas/order/inventory.rs` around lines 49 - 53, The reserve method is
unsafe for duplicate order_ids: reserve(&mut self, order_id: String, quantity:
u32) currently always subtracts quantity from available and adds to reserved
then inserts/overwrites reservations, corrupting state on repeats; fix by
checking reservations for an existing entry first and either (a)
early-return/block duplicates (if reservations.contains_key(&order_id) return)
or (b) make update idempotent by computing let prev =
self.reservations.get(&order_id).cloned().unwrap_or(0); let delta =
quantity.saturating_sub(prev); apply self.available -= delta; self.reserved +=
delta; then insert the new quantity — choose one approach and update the guard
on the #[event("StockReserved", when = self.can_reserve(quantity))] path
accordingly.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

Actionable comments posted: 0

@patrickleet patrickleet merged commit c4656dc into main May 28, 2026
4 checks passed
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