//! The module to help with tests. use crate::{ transactional::{ Changes, Modifiable, }, Error as StorageError, Mappable, MerkleRoot, MerkleRootStorage, Result as StorageResult, StorageInspect, StorageMutate, }; /// The trait is used to provide a generic mocked implementation for all possible `StorageInspect`, /// `StorageMutate`, and `MerkleRootStorage` traits. pub trait MockStorageMethods { /// The mocked implementation fot the `StorageInspect::get` method. fn get( &self, key: &M::Key, ) -> StorageResult>>; /// The mocked implementation fot the `StorageInspect::contains_key` method. fn contains_key(&self, key: &M::Key) -> StorageResult; /// The mocked implementation fot the `StorageMutate::insert` method. fn insert( &mut self, key: &M::Key, value: &M::Value, ) -> StorageResult>; /// The mocked implementation fot the `StorageMutate::remove` method. fn remove( &mut self, key: &M::Key, ) -> StorageResult>; /// The mocked implementation fot the `MerkleRootStorage::root` method. fn root( &self, key: &Key, ) -> StorageResult; } /// The mocked storage is useful to test functionality build on top of the `StorageInspect`, /// `StorageMutate`, and `MerkleRootStorage` traits. #[derive(Default, Debug, Clone)] pub struct MockStorage { /// The mocked storage. pub storage: Storage, /// Additional data to be used in the tests. pub data: Data, } mockall::mock! { /// The basic mocked storage pub Basic {} impl MockStorageMethods for Basic { fn get( &self, key: &M::Key, ) -> StorageResult>>; fn contains_key(&self, key: &M::Key) -> StorageResult; fn insert( &mut self, key: &M::Key, value: &M::Value, ) -> StorageResult>; fn remove( &mut self, key: &M::Key, ) -> StorageResult>; fn root(&self, key: &Key) -> StorageResult; } impl Modifiable for Basic { fn commit_changes(&mut self, changes: Changes) -> StorageResult<()>; } } impl StorageInspect for MockStorage where M: Mappable + 'static, Storage: MockStorageMethods, { type Error = StorageError; fn get( &self, key: &M::Key, ) -> StorageResult>> { MockStorageMethods::get::(&self.storage, key) } fn contains_key(&self, key: &M::Key) -> StorageResult { MockStorageMethods::contains_key::(&self.storage, key) } } impl StorageMutate for MockStorage where M: Mappable + 'static, Storage: MockStorageMethods, { fn insert( &mut self, key: &M::Key, value: &M::Value, ) -> StorageResult> { MockStorageMethods::insert::(&mut self.storage, key, value) } fn remove(&mut self, key: &M::Key) -> StorageResult> { MockStorageMethods::remove::(&mut self.storage, key) } } impl MerkleRootStorage for MockStorage where Key: 'static, M: Mappable + 'static, Storage: MockStorageMethods, { fn root(&self, key: &Key) -> StorageResult { MockStorageMethods::root::(&self.storage, key) } }