Skip to content

test(drive): improve document module test coverage - #3333

Merged
QuantumExplorer merged 2 commits into
v3.1-devfrom
test/drive-document-coverage
Mar 16, 2026
Merged

QuantumExplorer merged 2 commits into
v3.1-devfrom
test/drive-document-coverage

Conversation

@QuantumExplorer

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

Improve test coverage for the packages/rs-drive/src/drive/document/ module by targeting code paths with 0% coverage from drive-only tests.

What was done?

Added 17 new unit tests targeting genuinely uncovered code paths in the drive document module. Coverage analysis was performed using cargo llvm-cov to identify specific files and functions with the lowest coverage.

Delete module (3 tests):

  • test_delete_document_for_contract_id - exercises the delete_document_for_contract_id public API (was 0% covered)
  • test_delete_document_for_contract_id_estimated_costs - exercises the estimation (apply=false) path
  • test_delete_document_keeps_history_returns_error - covers the InvalidDeletionOfDocumentThatKeepsHistory error branch

Insert module (4 tests):

  • test_add_document_by_contract_id - exercises the add_document public API (was 0% covered)
  • test_add_document_with_history - covers the documents_keep_history() branch in add_document_to_primary_storage (was ~50% covered)
  • test_add_document_with_history_estimated_costs - covers the estimation path for history-keeping documents
  • test_add_document_for_contract_with_non_unique_batch - covers the document_is_unique_for_document_type_in_batch=false branch

Query module (6 tests):

  • test_query_documents_dry_run - covers the dry_run early return path
  • test_query_documents_with_epoch_fee - covers the epoch fee calculation branch
  • test_query_documents_without_epoch_zero_cost - covers the no-epoch zero-cost path
  • test_query_documents_with_flags - exercises query_documents_with_flags (was 0% covered)
  • test_query_documents_with_flags_dry_run - dry_run path for flags query
  • test_query_documents_with_flags_with_epoch - epoch fee calculation for flags query

Update module (4 tests):

  • test_add_update_multiple_documents_operations - exercises batch update operations (was 0% covered)
  • test_add_update_multiple_documents_operations_empty - covers empty documents guard
  • test_update_document_for_contract_immutable_error - covers UpdatingReadOnlyImmutableDocument error
  • test_update_document_with_serialization_for_contract - exercises the serialization update API (was 0% covered)
  • test_update_document_for_contract_id - exercises the contract_id update API (was 0% covered)

How Has This Been Tested?

All 17 new tests pass:

cargo test -p drive --features server

Full drive test suite passes with all existing tests (1374+ unit tests, 30 query tests, 3 history query tests).

Breaking Changes

None. This PR adds tests only.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

Add 17 new tests targeting genuinely uncovered code paths in the
drive document module, focusing on public API endpoints and error
branches that had 0% coverage from drive-only tests:

Delete module:
- delete_document_for_contract_id (stateful + estimated costs)
- delete of history-keeping documents returns correct error

Insert module:
- add_document via contract_id API
- add_document_for_contract with history-keeping documents (apply + estimated)
- add_document_for_contract_apply_and_add_to_operations non-unique batch path

Query module:
- query_documents dry_run, with epoch fee, without epoch
- query_documents_with_flags (stateful, dry_run, with epoch)

Update module:
- add_update_multiple_documents_operations (non-empty + empty)
- update immutable document type returns error
- update_document_with_serialization_for_contract
- update_document_for_contract_id

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@github-actions github-actions Bot added this to the v3.1.0 milestone Mar 16, 2026
@coderabbitai

coderabbitai Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@QuantumExplorer has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 10 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f601d332-692f-4ad2-bd6c-b7462be979f2

📥 Commits

Reviewing files that changed from the base of the PR and between 8954eeb and b215d6d.

📒 Files selected for processing (4)
  • packages/rs-drive/src/drive/document/delete/mod.rs
  • packages/rs-drive/src/drive/document/insert/mod.rs
  • packages/rs-drive/src/drive/document/query/mod.rs
  • packages/rs-drive/src/drive/document/update/mod.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch test/drive-document-coverage
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@codecov

codecov Bot commented Mar 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.26778% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.02%. Comparing base (6a60c5b) to head (b215d6d).
⚠️ Report is 6 commits behind head on v3.1-dev.

Files with missing lines Patch % Lines
packages/rs-drive/src/drive/document/insert/mod.rs 98.85% 3 Missing ⚠️
packages/rs-drive/src/drive/document/update/mod.rs 99.25% 2 Missing ⚠️
packages/rs-drive/src/drive/document/delete/mod.rs 99.51% 1 Missing ⚠️
packages/rs-drive/src/drive/document/query/mod.rs 99.54% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           v3.1-dev    #3333      +/-   ##
============================================
+ Coverage     73.22%   74.02%   +0.80%     
============================================
  Files          3291     3296       +5     
  Lines        271882   276291    +4409     
============================================
+ Hits         199084   204532    +5448     
+ Misses        72798    71759    -1039     
Components Coverage Δ
dpp 60.41% <ø> (+<0.01%) ⬆️
drive 80.19% <99.26%> (+1.88%) ⬆️
drive-abci 85.76% <ø> (+0.08%) ⬆️
sdk 31.25% <ø> (ø)
dapi-client 79.06% <ø> (ø)
platform-version ∅ <ø> (∅)
platform-value 58.46% <ø> (ø)
platform-wallet 60.40% <ø> (ø)
drive-proof-verifier ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@QuantumExplorer QuantumExplorer left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Audit Review

Good coverage additions targeting previously untested API variants (contract-id-based APIs, estimation paths, dry-run, with-flags queries). Error-path tests are particularly well done. The main pattern issue across the PR is tests that perform write operations but never read back the data to verify correctness.

Good practices observed

  • Strong error-variant matching with matches!() and .expect_err()
  • Covers alternative API surfaces (*_for_contract_id variants) not just the primary APIs
  • Covers estimation/dry-run paths consistently
  • No test duplication with existing tests
  • Consistent style with existing test code

Production code observation

In add_document/v0/mod.rs, the drive_operations variable created at line 29 for contract fetch is shadowed by a second drive_operations at line 51. The contract fetch operations are discarded and not included in fee calculation. This is pre-existing, not introduced by this PR.

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Code Review

Test-only PR adding 17 unit tests to drive/document. All tests compile and exercise real code paths, but several only assert 'no error' or 'fee > 0' without verifying actual state changes. The dry-run query tests are effectively vacuous because they query an empty contract. No blocking issues — these are coverage-improving tests that would benefit from stronger assertions.

🟡 4 suggestion(s)

🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `packages/rs-drive/src/drive/document/update/mod.rs`:
- [SUGGESTION] line 2234: Update tests only assert fee, never verify the document was actually updated
  Both `test_update_document_with_serialization_for_contract` (line 2234) and `test_update_document_for_contract_id` (line 2312) set displayName from "Alice" to "Bob", call the update API, then only assert `processing_fee > 0`. Neither test queries the document afterward to confirm the stored value changed. A serialization bug that charged fees but wrote stale data would pass both tests. Adding a read-back assertion (e.g., query the profile and check displayName == "Bob") would make these tests catch real regressions.
- [SUGGESTION] lines 2058-2071: Multi-update test only counts the outer vector, not the batch contents
  `test_add_update_multiple_documents_operations` creates 2 documents, calls `add_update_multiple_documents_operations`, and asserts `drive_operations.len() == 1`. This correctly verifies batching (multiple docs → one operation), but doesn't inspect the operation variant or confirm both documents are represented inside it. A regression that emitted a wrong variant or silently dropped one document would still pass. Destructure the emitted `DriveOperation` and assert the variant and number of nested update operations.

In `packages/rs-drive/src/drive/document/query/mod.rs`:
- [SUGGESTION] lines 526-543: Dry-run query tests are vacuous — they query an empty contract
  `test_query_documents_dry_run` and `test_query_documents_with_flags_dry_run` never insert any documents before querying. `setup_dashpay()` only applies the contract, so asserting 0 results / 0 skipped / 0 cost is the same outcome you'd get from a non-dry-run query on an empty document type. These tests don't actually verify that the dry_run flag short-circuits storage access — they only prove that querying an empty type returns nothing. Insert at least one document before querying so the dry-run path diverges from the normal path (dry run should still return 0 results, but the non-dry-run would return 1).

In `packages/rs-drive/src/drive/document/insert/mod.rs`:
- [SUGGESTION] lines 1011-1099: History insert test doesn't verify any history-specific behavior
  `test_add_document_with_history` inserts two documents under a history-enabled contract, asserts `storage_fee > 0` for the first, and commits the transaction. It never queries back to confirm the documents are retrievable or that the history tree structure was created. Since the stated goal is to cover the history code path, this test currently proves only that the calls returned `Ok` — a bug where history storage silently fell back to non-history storage would pass. Consider querying the inserted documents or comparing the storage structure against a non-history contract.

- Add fetch-back verification to insert, update, and delete tests
- Verify document content after operations (not just fees)
- Insert documents before calling update operations
- Replace .unwrap() with .expect() for better diagnostics
- Remove unused variable

Co-Authored-By: Claude Opus 4.6 <[email protected]>

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Code Review

Test-only PR adding 17 unit tests to drive/document. All tests compile and exercise real code paths, but several only assert 'no error' or 'fee > 0' without verifying actual state changes. The dry-run query tests are effectively vacuous because they query an empty contract. No blocking issues — these are coverage-improving tests that would benefit from stronger assertions.

🟡 4 suggestion(s) | 💬 1 nitpick(s)

🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `packages/rs-drive/src/drive/document/update/mod.rs`:
- [SUGGESTION] line 2234: Update tests only assert fee, never verify the document was actually updated
  Both `test_update_document_with_serialization_for_contract` and `test_update_document_for_contract_id` set displayName from "Alice" to "Bob", call the update API, then only assert `processing_fee > 0`. Neither test queries the document afterward to confirm the stored value changed. A serialization bug that charged fees but wrote stale data would pass both tests. Adding a read-back assertion (e.g., query the profile and check displayName == "Bob") would make these tests catch real regressions.
- [SUGGESTION] lines 2058-2071: Multi-update test only counts the outer vector, not the batch contents
  `test_add_update_multiple_documents_operations` creates 2 documents, calls `add_update_multiple_documents_operations`, and asserts `drive_operations.len() == 1`. This correctly verifies batching (multiple docs to one operation), but doesn't inspect the operation variant or confirm both documents are represented inside it. A regression that emitted a wrong variant or silently dropped one document would still pass.

In `packages/rs-drive/src/drive/document/query/mod.rs`:
- [SUGGESTION] lines 526-543: Dry-run query tests are vacuous — they query an empty contract
  `test_query_documents_dry_run` and `test_query_documents_with_flags_dry_run` never insert any documents before querying. `setup_dashpay()` only applies the contract, so asserting 0 results / 0 skipped / 0 cost is the same outcome you'd get from a non-dry-run query on an empty document type. These tests don't actually verify that the dry_run flag short-circuits storage access — they only prove that querying an empty type returns nothing. Insert at least one document before querying so the dry-run path diverges from the normal path (dry run should still return 0 results, but the non-dry-run would return 1).

In `packages/rs-drive/src/drive/document/insert/mod.rs`:
- [SUGGESTION] lines 1011-1099: History insert test doesn't verify any history-specific behavior
  `test_add_document_with_history` inserts two documents under a history-enabled contract, asserts `storage_fee > 0` for the first, and commits the transaction. It never queries back to confirm the documents are retrievable or that the history tree structure was created. A bug where history storage silently fell back to non-history storage would pass. Consider querying the inserted documents or comparing the storage structure against a non-history contract.

@QuantumExplorer
QuantumExplorer merged commit 99fe9a9 into v3.1-dev Mar 16, 2026
17 checks passed
@QuantumExplorer
QuantumExplorer deleted the test/drive-document-coverage branch March 16, 2026 05:42
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.

2 participants