test(dpp): improve coverage for data contract serialization and index validation#3438
Conversation
… validation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis pull request adds comprehensive unit test coverage across three modules: credit conversion and serialization behavior, data contract index operations and field matching logic, and data contract serialization format handling. All changes are test-only additions with no modifications to public APIs or functional code. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~18 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
⏳ Review in progress (commit ec0d015) |
There was a problem hiding this comment.
🧹 Nitpick comments (4)
packages/rs-dpp/src/balances/credits.rs (1)
444-457: Consider addingi64::MINcase forSignedCredits::to_unsigned().You already test signed serialization with
i64::MIN; adding the conversion edge here would fully lock downunsigned_abs()behavior.Optional test addition
#[test] fn signed_credits_to_unsigned_zero() { let sc: SignedCredits = 0; assert_eq!(sc.to_unsigned(), 0); } + +#[test] +fn signed_credits_to_unsigned_min_i64() { + let sc: SignedCredits = i64::MIN; + assert_eq!(sc.to_unsigned(), (i64::MAX as u64) + 1); +}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/rs-dpp/src/balances/credits.rs` around lines 444 - 457, Add a test case covering the i64::MIN edge for SignedCredits::to_unsigned(): create a SignedCredits value set to i64::MIN and assert that to_unsigned() returns 9223372036854775808u64 (2^63), so the test verifies the method handles the i64::MIN overflow edge correctly; update the existing signed_credits_to_unsigned_returns_abs test (or add a new test) to include this SignedCredits::MIN assertion referencing SignedCredits::to_unsigned().packages/rs-dpp/src/data_contract/document_type/index/mod.rs (2)
1436-1465: Consider consolidating with existing error tests.These tests duplicate coverage from lines 1056-1077 (
test_index_property_try_from_empty_map_error,test_index_property_try_from_multiple_entries_error,test_index_property_try_from_invalid_sort_order_error) but add error message verification.Rather than having separate tests, consider enhancing the existing tests to include the error message assertions. This reduces duplication while preserving the improved coverage.
Example consolidation for empty map test
#[test] fn test_index_property_try_from_empty_map_error() { let map: BTreeMap<String, String> = BTreeMap::new(); let result = IndexProperty::try_from(map); assert!(result.is_err()); + let err_msg = format!("{}", result.unwrap_err()); + assert!(err_msg.contains("empty")); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/rs-dpp/src/data_contract/document_type/index/mod.rs` around lines 1436 - 1465, Remove the three duplicate tests (test_index_property_try_from_unknown_direction, test_index_property_try_from_empty_map, test_index_property_try_from_three_entries_error) and instead add the error-message assertions into the existing tests test_index_property_try_from_invalid_sort_order_error, test_index_property_try_from_empty_map_error, and test_index_property_try_from_multiple_entries_error respectively; locate the IndexProperty::try_from invocation in each existing test, capture the Err with format!("{}", result.unwrap_err()), and assert the appropriate substring ("up" for invalid direction, "empty" for empty map, "more than one" for multiple entries) so coverage is preserved without duplicating test cases.
1262-1277: Test name and comment may cause confusion between "null" and "missing".The test name
test_objects_are_conflicting_both_null_values_not_conflictingand comment mention "null values," but the test actually verifies behavior for missing properties (keys not present in the map). Inplatform_value, a missing key and a key withValue::Nullmay behave differently depending onValue::get_optional_from_mapsemantics.Consider renaming to
test_objects_are_conflicting_both_missing_properties_not_conflictingfor clarity, or add a separate test that explicitly usesValue::Nullto verify both scenarios.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/rs-dpp/src/data_contract/document_type/index/mod.rs` around lines 1262 - 1277, The test named test_objects_are_conflicting_both_null_values_not_conflicting is misleading because it checks missing properties, not Value::Null; update the test to either rename it to test_objects_are_conflicting_both_missing_properties_not_conflicting (update the function name and any comments) to reflect that it uses absent keys, or add a new test that constructs ValueMap entries with Value::Null and asserts objects_are_conflicting behavior for nulls; locate the test and related helpers (objects_are_conflicting, make_index, ValueMap, Value::Null) and ensure both missing-key and explicit-Value::Null cases are covered and clearly named.packages/rs-dpp/src/data_contract/serialized_version/mod.rs (1)
913-1120: Optional: Consider extracting test fixture builders.The conversion tests contain repeated
DataContractV0andDataContractV1struct initialization (lines 917-925, 940-948, 961-969, 986-1003, 1016-1033, 1046-1063, 1079-1087, 1102-1110). You could reduce duplication with parameterized helper functions:♻️ Optional refactor
fn make_data_contract_v0(id: Identifier, owner_id: Identifier, version: u32) -> DataContractV0 { DataContractV0 { id, config: DataContractConfig::V0(DataContractConfigV0::default()), version, owner_id, schema_defs: None, document_types: BTreeMap::new(), metadata: None, } } fn make_data_contract_v1(id: Identifier, owner_id: Identifier, version: u32, config: DataContractConfig) -> DataContractV1 { DataContractV1 { id, config, version, owner_id, schema_defs: None, document_types: BTreeMap::new(), created_at: None, updated_at: None, created_at_block_height: None, updated_at_block_height: None, created_at_epoch: None, updated_at_epoch: None, groups: BTreeMap::new(), tokens: BTreeMap::new(), keywords: vec![], description: None, } }Then use:
let v0 = make_data_contract_v0( Identifier::from([10u8; 32]), Identifier::from([20u8; 32]), 1 );However, the current explicit approach aids test readability, so this refactor is purely optional.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/rs-dpp/src/data_contract/serialized_version/mod.rs` around lines 913 - 1120, Tests repeat verbose DataContractV0/DataContractV1 construction; extract small fixture builders to reduce duplication: add helper functions like make_data_contract_v0(id: Identifier, owner_id: Identifier, version: u32) -> DataContractV0 and make_data_contract_v1(id: Identifier, owner_id: Identifier, version: u32, config: DataContractConfig) -> DataContractV1 and replace repeated inline structs in tests (e.g., try_from_platform_versioned_data_contract_v0_*, try_from_platform_versioned_data_contract_v1_*, try_from_platform_versioned_data_contract_*) with calls to these helpers to keep the same field defaults (config default, None fields, empty BTreeMap/vec) while improving readability and avoiding duplication.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/rs-dpp/src/balances/credits.rs`:
- Around line 444-457: Add a test case covering the i64::MIN edge for
SignedCredits::to_unsigned(): create a SignedCredits value set to i64::MIN and
assert that to_unsigned() returns 9223372036854775808u64 (2^63), so the test
verifies the method handles the i64::MIN overflow edge correctly; update the
existing signed_credits_to_unsigned_returns_abs test (or add a new test) to
include this SignedCredits::MIN assertion referencing
SignedCredits::to_unsigned().
In `@packages/rs-dpp/src/data_contract/document_type/index/mod.rs`:
- Around line 1436-1465: Remove the three duplicate tests
(test_index_property_try_from_unknown_direction,
test_index_property_try_from_empty_map,
test_index_property_try_from_three_entries_error) and instead add the
error-message assertions into the existing tests
test_index_property_try_from_invalid_sort_order_error,
test_index_property_try_from_empty_map_error, and
test_index_property_try_from_multiple_entries_error respectively; locate the
IndexProperty::try_from invocation in each existing test, capture the Err with
format!("{}", result.unwrap_err()), and assert the appropriate substring ("up"
for invalid direction, "empty" for empty map, "more than one" for multiple
entries) so coverage is preserved without duplicating test cases.
- Around line 1262-1277: The test named
test_objects_are_conflicting_both_null_values_not_conflicting is misleading
because it checks missing properties, not Value::Null; update the test to either
rename it to
test_objects_are_conflicting_both_missing_properties_not_conflicting (update the
function name and any comments) to reflect that it uses absent keys, or add a
new test that constructs ValueMap entries with Value::Null and asserts
objects_are_conflicting behavior for nulls; locate the test and related helpers
(objects_are_conflicting, make_index, ValueMap, Value::Null) and ensure both
missing-key and explicit-Value::Null cases are covered and clearly named.
In `@packages/rs-dpp/src/data_contract/serialized_version/mod.rs`:
- Around line 913-1120: Tests repeat verbose DataContractV0/DataContractV1
construction; extract small fixture builders to reduce duplication: add helper
functions like make_data_contract_v0(id: Identifier, owner_id: Identifier,
version: u32) -> DataContractV0 and make_data_contract_v1(id: Identifier,
owner_id: Identifier, version: u32, config: DataContractConfig) ->
DataContractV1 and replace repeated inline structs in tests (e.g.,
try_from_platform_versioned_data_contract_v0_*,
try_from_platform_versioned_data_contract_v1_*,
try_from_platform_versioned_data_contract_*) with calls to these helpers to keep
the same field defaults (config default, None fields, empty BTreeMap/vec) while
improving readability and avoiding duplication.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 20149d65-b118-4971-a5a3-9107d90bac3c
📒 Files selected for processing (3)
packages/rs-dpp/src/balances/credits.rspackages/rs-dpp/src/data_contract/document_type/index/mod.rspackages/rs-dpp/src/data_contract/serialized_version/mod.rs
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## v3.1-dev #3438 +/- ##
============================================
+ Coverage 80.66% 80.74% +0.08%
============================================
Files 2852 2852
Lines 285371 286158 +787
============================================
+ Hits 230190 231071 +881
+ Misses 55181 55087 -94
🚀 New features to boost your workflow:
|
Summary
Adds 100 tests across 3 files in rs-dpp covering real logic.
serialized_version/mod.rsfirst_mismatch()for V0/V1,TryFromPlatformVersioned, accessors, Displaydocument_type/index/mod.rsobjects_are_conflicting(),ContestedIndexFieldMatch::matches(), Ord, IndexProperty errorsbalances/credits.rsto_signed()overflow,from_vec_bytes/to_vec_bytesround-trips,CreditOperation::mergeTest plan
cargo test -p dpp --libpassescargo fmt --allclean🤖 Generated with Claude Code
Summary by CodeRabbit