Skip to content

Commit d566324

Browse files
committed
Adding mock support for the Database trait in AKD
Related to: facebook#332
1 parent 3a353e7 commit d566324

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

akd/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ once_cell = { version = "1" }
6262
ctor = "0.1"
6363
tokio-test = "0.4"
6464
tokio = { version = "1.21", features = ["rt", "sync", "time", "macros"] }
65+
mockall = "0.11"
6566

6667
# To enable the public-test feature in tests
6768
akd = { path = ".", features = ["public-tests"], default-features = false }

akd/src/storage/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub enum DbSetState {
4343

4444
/// Storable represents an _item_ which can be stored in the storage layer
4545
#[cfg(feature = "serde_serialization")]
46-
pub trait Storable: Clone + Serialize + DeserializeOwned + Sync {
46+
pub trait Storable: Clone + Serialize + DeserializeOwned + Sync + 'static {
4747
/// This particular storage will have a key type
4848
type StorageKey: Clone + Serialize + Eq + Hash + Send + Sync + std::fmt::Debug;
4949

@@ -68,7 +68,7 @@ pub trait Storable: Clone + Serialize + DeserializeOwned + Sync {
6868

6969
/// Storable represents an _item_ which can be stored in the storage layer
7070
#[cfg(not(feature = "serde_serialization"))]
71-
pub trait Storable: Clone + Sync {
71+
pub trait Storable: Clone + Sync + 'static {
7272
/// This particular storage will have a key type
7373
type StorageKey: Clone + Eq + Hash + Send + Sync + std::fmt::Debug;
7474

akd/src/tests.rs

+64-2
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,78 @@
88

99
//! Contains the tests for the high-level API (directory, auditor, client)
1010
11+
use std::collections::HashMap;
12+
1113
use crate::{
1214
auditor::audit_verify,
1315
client::{key_history_verify, lookup_verify},
1416
directory::{Directory, PublishCorruption},
1517
ecvrf::{HardCodedAkdVRF, VRFKeyStorage},
16-
errors::AkdError,
17-
storage::{manager::StorageManager, memory::AsyncInMemoryDatabase, types::DbRecord, Database},
18+
errors::{AkdError, StorageError},
19+
storage::{
20+
manager::StorageManager,
21+
memory::AsyncInMemoryDatabase,
22+
types::{DbRecord, KeyData, ValueState, ValueStateRetrievalFlag},
23+
Database, DbSetState, Storable,
24+
},
1825
AkdLabel, AkdValue, HistoryParams, HistoryVerificationParams, VerifyResult,
1926
};
2027

28+
#[derive(Clone)]
29+
pub struct LocalDatabase;
30+
31+
unsafe impl Send for LocalDatabase {}
32+
unsafe impl Sync for LocalDatabase {}
33+
34+
mockall::mock! {
35+
pub LocalDatabase {
36+
37+
}
38+
impl Clone for LocalDatabase {
39+
fn clone(&self) -> Self;
40+
}
41+
#[async_trait::async_trait]
42+
impl Database for LocalDatabase {
43+
/// Set a record in the database
44+
async fn set(&self, record: DbRecord) -> Result<(), StorageError>;
45+
46+
/// Set multiple records in the database with a minimal set of operations
47+
async fn batch_set(
48+
&self,
49+
records: Vec<DbRecord>,
50+
state: DbSetState,
51+
) -> Result<(), StorageError>;
52+
53+
/// Retrieve a stored record from the database
54+
async fn get<St: Storable>(&self, id: &St::StorageKey) -> Result<DbRecord, StorageError>;
55+
56+
/// Retrieve a batch of records by id from the database
57+
async fn batch_get<St: Storable>(
58+
&self,
59+
ids: &[St::StorageKey],
60+
) -> Result<Vec<DbRecord>, StorageError>;
61+
62+
/* User data searching */
63+
64+
/// Retrieve the user data for a given user
65+
async fn get_user_data(&self, username: &AkdLabel) -> Result<KeyData, StorageError>;
66+
67+
/// Retrieve a specific state for a given user
68+
async fn get_user_state(
69+
&self,
70+
username: &AkdLabel,
71+
flag: ValueStateRetrievalFlag,
72+
) -> Result<ValueState, StorageError>;
73+
74+
/// Retrieve the user -> state version mapping in bulk. This is the same as get_user_states but with less data retrieved from the storage layer
75+
async fn get_user_state_versions(
76+
&self,
77+
usernames: &[AkdLabel],
78+
flag: ValueStateRetrievalFlag,
79+
) -> Result<HashMap<AkdLabel, (u64, AkdValue)>, StorageError>;
80+
}
81+
}
82+
2183
// A simple test to ensure that the empty tree hashes to the correct value
2284
#[tokio::test]
2385
async fn test_empty_tree_root_hash() -> Result<(), AkdError> {

0 commit comments

Comments
 (0)