tests for redisclient

This commit is contained in:
Per Stark
2024-10-02 11:17:42 +02:00
parent 8e7e762130
commit 7d13c06a51
8 changed files with 446 additions and 214 deletions

View File

@@ -1,3 +1,4 @@
use axum::async_trait;
use redis::AsyncCommands;
use thiserror::Error;
use uuid::Uuid;
@@ -16,6 +17,32 @@ pub enum RedisError {
// Add more error variants as needed.
}
/// Defines the behavior for Redis client operations.
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait RedisClientTrait: Send + Sync {
/// Establishes a new multiplexed asynchronous connection to Redis.
async fn get_connection(&self) -> Result<redis::aio::MultiplexedConnection, RedisError>;
/// Stores `FileInfo` in Redis using SHA256 as the key.
async fn set_file_info(&self, sha256: &str, file_info: &FileInfo) -> Result<(), RedisError>;
/// Retrieves `FileInfo` from Redis using SHA256 as the key.
async fn get_file_info_by_sha(&self, sha256: &str) -> Result<Option<FileInfo>, RedisError>;
/// Deletes `FileInfo` from Redis using SHA256 as the key.
async fn delete_file_info(&self, sha256: &str) -> Result<(), RedisError>;
/// Sets a mapping from UUID to SHA256.
async fn set_sha_uuid_mapping(&self, uuid: &Uuid, sha256: &str) -> Result<(), RedisError>;
/// Retrieves the SHA256 hash associated with a given UUID.
async fn get_sha_by_uuid(&self, uuid: &Uuid) -> Result<Option<String>, RedisError>;
/// Deletes the UUID to SHA256 mapping from Redis.
async fn delete_sha_uuid_mapping(&self, uuid: &Uuid) -> Result<(), RedisError>;
}
/// Provides Redis-related operations for `FileInfo`.
pub struct RedisClient {
redis_url: String,
@@ -36,13 +63,15 @@ impl RedisClient {
redis_url: redis_url.to_string(),
}
}
}
#[async_trait]
impl RedisClientTrait for RedisClient {
/// Establishes a new multiplexed asynchronous connection to Redis.
///
/// # Returns
///
/// * `MultiplexedConnection` - The established connection.
pub async fn get_connection(&self) -> Result<redis::aio::MultiplexedConnection, RedisError> {
async fn get_connection(&self) -> Result<redis::aio::MultiplexedConnection, RedisError> {
let client = redis::Client::open(self.redis_url.clone())
.map_err(|e| RedisError::ConnectionError(e.to_string()))?;
let conn = client
@@ -55,14 +84,12 @@ impl RedisClient {
/// Stores `FileInfo` in Redis using SHA256 as the key.
///
/// # Arguments
///
/// * `sha256` - The SHA256 hash of the file.
/// * `file_info` - The `FileInfo` object to store.
///
/// # Returns
///
/// * `Result<(), RedisError>` - Empty result or an error.
pub async fn set_file_info(&self, sha256: &str, file_info: &FileInfo) -> Result<(), RedisError> {
async fn set_file_info(&self, sha256: &str, file_info: &FileInfo) -> Result<(), RedisError> {
let mut conn = self.get_connection().await?;
let key = format!("file_info:{}", sha256);
let value = serde_json::to_string(file_info)
@@ -75,13 +102,11 @@ impl RedisClient {
/// Retrieves `FileInfo` from Redis using SHA256 as the key.
///
/// # Arguments
///
/// * `sha256` - The SHA256 hash of the file.
///
/// # Returns
///
/// * `Result<Option<FileInfo>, RedisError>` - The `FileInfo` if found, otherwise `None`, or an error.
pub async fn get_file_info_by_sha(&self, sha256: &str) -> Result<Option<FileInfo>, RedisError> {
async fn get_file_info_by_sha(&self, sha256: &str) -> Result<Option<FileInfo>, RedisError> {
let mut conn = self.get_connection().await?;
let key = format!("file_info:{}", sha256);
let value: Option<String> = conn.get(key).await
@@ -98,13 +123,11 @@ impl RedisClient {
/// Deletes `FileInfo` from Redis using SHA256 as the key.
///
/// # Arguments
///
/// * `sha256` - The SHA256 hash of the file.
///
/// # Returns
///
/// * `Result<(), RedisError>` - Empty result or an error.
pub async fn delete_file_info(&self, sha256: &str) -> Result<(), RedisError> {
async fn delete_file_info(&self, sha256: &str) -> Result<(), RedisError> {
let mut conn = self.get_connection().await?;
let key = format!("file_info:{}", sha256);
conn.del(key).await
@@ -115,14 +138,12 @@ impl RedisClient {
/// Sets a mapping from UUID to SHA256.
///
/// # Arguments
///
/// * `uuid` - The UUID of the file.
/// * `sha256` - The SHA256 hash of the file.
///
/// # Returns
///
/// * `Result<(), RedisError>` - Empty result or an error.
pub async fn set_sha_uuid_mapping(&self, uuid: &Uuid, sha256: &str) -> Result<(), RedisError> {
async fn set_sha_uuid_mapping(&self, uuid: &Uuid, sha256: &str) -> Result<(), RedisError> {
let mut conn = self.get_connection().await?;
let key = format!("uuid_sha:{}", uuid);
conn.set(key, sha256).await
@@ -133,13 +154,11 @@ impl RedisClient {
/// Retrieves the SHA256 hash associated with a given UUID.
///
/// # Arguments
///
/// * `uuid` - The UUID of the file.
///
/// # Returns
///
/// * `Result<Option<String>, RedisError>` - The SHA256 hash if found, otherwise `None`, or an error.
pub async fn get_sha_by_uuid(&self, uuid: &Uuid) -> Result<Option<String>, RedisError> {
async fn get_sha_by_uuid(&self, uuid: &Uuid) -> Result<Option<String>, RedisError> {
let mut conn = self.get_connection().await?;
let key = format!("uuid_sha:{}", uuid);
let sha: Option<String> = conn.get(key).await
@@ -156,7 +175,7 @@ impl RedisClient {
/// # Returns
///
/// * `Result<(), RedisError>` - Empty result or an error.
pub async fn delete_sha_uuid_mapping(&self, uuid: &Uuid) -> Result<(), RedisError> {
async fn delete_sha_uuid_mapping(&self, uuid: &Uuid) -> Result<(), RedisError> {
let mut conn = self.get_connection().await?;
let key = format!("uuid_sha:{}", uuid);
conn.del(key).await
@@ -164,3 +183,155 @@ impl RedisClient {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use mockall::predicate::*;
use uuid::Uuid;
#[tokio::test]
async fn test_set_file_info() {
// Initialize the mock.
let mut mock_redis = MockRedisClientTrait::new();
let test_sha = "dummysha256hash".to_string();
let test_file_info = FileInfo {
uuid: Uuid::new_v4(),
sha256: test_sha.clone(),
path: "/path/to/file".to_string(),
mime_type: "text/plain".to_string(),
};
// Setup expectation for `set_file_info`.
mock_redis
.expect_set_file_info()
.with(eq(test_sha.clone()), eq(test_file_info.clone()))
.times(1)
.returning(|_, _| Ok(()) );
// Call `set_file_info` on the mock.
let set_result = mock_redis.set_file_info(&test_sha, &test_file_info).await;
assert!(set_result.is_ok());
}
#[tokio::test]
async fn test_get_file_info_by_sha() {
// Initialize the mock.
let mut mock_redis = MockRedisClientTrait::new();
let test_sha = "dummysha256hash".to_string();
let test_file_info = FileInfo {
uuid: Uuid::new_v4(),
sha256: test_sha.clone(),
path: "/path/to/file".to_string(),
mime_type: "text/plain".to_string(),
};
// Clone the FileInfo to use inside the closure.
let fi_clone = test_file_info.clone();
// Setup expectation for `get_file_info_by_sha`.
mock_redis
.expect_get_file_info_by_sha()
.with(eq(test_sha.clone()))
.times(1)
.returning(move |_: &str| {
// Return the cloned FileInfo.
let fi_inner = fi_clone.clone();
Ok(Some(fi_inner))
});
// Call `get_file_info_by_sha` on the mock.
let get_result = mock_redis.get_file_info_by_sha(&test_sha).await;
assert!(get_result.is_ok());
assert_eq!(get_result.unwrap(), Some(test_file_info));
}
#[tokio::test]
async fn test_delete_file_info() {
// Initialize the mock.
let mut mock_redis = MockRedisClientTrait::new();
let test_sha = "dummysha256hash".to_string();
// Setup expectation for `delete_file_info`.
mock_redis
.expect_delete_file_info()
.with(eq(test_sha.clone()))
.times(1)
.returning(|_: &str| Ok(()) );
// Call `delete_file_info` on the mock.
let delete_result = mock_redis.delete_file_info(&test_sha).await;
assert!(delete_result.is_ok());
}
#[tokio::test]
async fn test_set_sha_uuid_mapping() {
// Initialize the mock.
let mut mock_redis = MockRedisClientTrait::new();
let test_uuid = Uuid::new_v4();
let test_sha = "dummysha256hash".to_string();
// Setup expectation for `set_sha_uuid_mapping`.
mock_redis
.expect_set_sha_uuid_mapping()
.with(eq(test_uuid.clone()), eq(test_sha.clone()))
.times(1)
.returning(|_, _| Ok(()) );
// Call `set_sha_uuid_mapping` on the mock.
let set_result = mock_redis.set_sha_uuid_mapping(&test_uuid, &test_sha).await;
assert!(set_result.is_ok());
}
#[tokio::test]
async fn test_get_sha_by_uuid() {
// Initialize the mock.
let mut mock_redis = MockRedisClientTrait::new();
let test_uuid = Uuid::new_v4();
let test_sha = "dummysha256hash".to_string();
// Clone the SHA to use inside the closure.
let sha_clone = test_sha.clone();
// Setup expectation for `get_sha_by_uuid`.
mock_redis
.expect_get_sha_by_uuid()
.with(eq(test_uuid.clone()))
.times(1)
.returning(move |_: &Uuid| {
let sha_inner = sha_clone.clone();
Ok(Some(sha_inner))
});
// Call `get_sha_by_uuid` on the mock.
let get_result = mock_redis.get_sha_by_uuid(&test_uuid).await;
assert!(get_result.is_ok());
assert_eq!(get_result.unwrap(), Some(test_sha));
}
#[tokio::test]
async fn test_delete_sha_uuid_mapping() {
// Initialize the mock.
let mut mock_redis = MockRedisClientTrait::new();
let test_uuid = Uuid::new_v4();
// Setup expectation for `delete_sha_uuid_mapping`.
mock_redis
.expect_delete_sha_uuid_mapping()
.with(eq(test_uuid.clone()))
.times(1)
.returning(|_: &Uuid| Ok(()) );
// Call `delete_sha_uuid_mapping` on the mock.
let delete_result = mock_redis.delete_sha_uuid_mapping(&test_uuid).await;
assert!(delete_result.is_ok());
}
}