Skip to content

test: Unit test for storage helpers#376

Open
nayt9 wants to merge 1 commit intoCommitlabs-Org:masterfrom
nayt9:storage
Open

test: Unit test for storage helpers#376
nayt9 wants to merge 1 commit intoCommitlabs-Org:masterfrom
nayt9:storage

Conversation

@nayt9
Copy link

@nayt9 nayt9 commented Mar 24, 2026

Added comprehensive unit tests for the shared_utils storage helper module, focusing on initialization flag management and admin default paths. This PR significantly improves test coverage for critical storage operations used across all contracts.

Changes

Test Coverage Added

Initialization Flag Tests (10 tests)

  • test_is_initialized_returns_false_by_default - Verifies default uninitialized state
  • test_set_initialized_marks_contract_as_initialized - Tests initialization flag setting
  • test_set_initialized_is_idempotent - Ensures multiple calls are safe
  • test_require_initialized_succeeds_when_initialized - Validates guard function
  • test_require_initialized_panics_when_not_initialized - Tests panic behavior
  • test_require_not_initialized_succeeds_when_not_initialized - Validates inverse guard
  • test_require_not_initialized_panics_when_initialized - Tests double-init protection
  • test_initialization_flag_persists_across_calls - Verifies storage persistence
  • test_initialization_flag_uses_correct_storage_key - Validates key usage
  • test_storage_key_constants_are_unique - Ensures no key collisions

Admin Storage Tests (8 tests)

  • test_set_and_get_admin - Basic admin storage operations
  • test_admin_can_be_updated - Tests admin transfer functionality
  • test_get_admin_panics_when_not_initialized - Validates initialization requirement
  • test_get_admin_panics_when_admin_not_set - Tests missing admin handling
  • test_admin_persists_across_calls - Verifies storage persistence
  • test_admin_uses_correct_storage_key - Validates key usage
  • test_multiple_admins_in_different_contracts - Tests contract isolation
  • test_admin_can_be_set_before_initialization - Tests setup flexibility

Generic Storage Helper Tests (10 tests)

  • test_get_or_default_returns_default_when_key_not_exists - Default value handling
  • test_get_or_default_returns_stored_value_when_exists - Stored value retrieval
  • test_set_and_get_generic_value - Basic storage operations
  • test_get_returns_none_when_key_not_exists - Missing key handling
  • test_has_returns_false_when_key_not_exists - Key existence check (negative)
  • test_has_returns_true_when_key_exists - Key existence check (positive)
  • test_storage_with_different_types - Multi-type support (i128, u64, bool)
  • test_storage_key_isolation - Ensures keys don't interfere
  • test_storage_value_can_be_overwritten - Tests value updates
  • test_storage_keys_are_short_symbols - Validates symbol constraints

Integration Tests (3 tests)

  • test_typical_initialization_flow - End-to-end initialization workflow
  • test_cannot_reinitialize_after_initialization - Double-init protection
  • test_admin_can_be_set_before_initialization - Flexible setup order

Bug Fix

Fixed duplicate import issue in lib.rs that was causing compilation errors:

  • Removed duplicate pub use fees; statement
  • Removed unused wildcard re-exports that were causing warnings

Testing

Total of 31 new unit tests added covering:

  • ✅ Initialization flag lifecycle
  • ✅ Admin storage and retrieval
  • ✅ Generic storage helpers
  • ✅ Storage key isolation
  • ✅ Type safety across different data types
  • ✅ Panic conditions and error handling
  • ✅ Storage persistence across contract calls
  • ✅ Multi-contract isolation

Security Considerations

  • All tests validate proper panic behavior for unauthorized access
  • Tests confirm storage isolation between contracts
  • Initialization guards prevent double-initialization attacks
  • Admin retrieval requires initialization, preventing uninitialized state access

Files Changed

  • contracts/shared_utils/src/storage.rs - Added 31 comprehensive unit tests
  • contracts/shared_utils/src/lib.rs - Fixed duplicate import compilation error

closes #253

@drips-wave
Copy link

drips-wave bot commented Mar 24, 2026

@nayt9 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Unit tests: storage helpers \u2014 initialized flag, admin default paths

1 participant