From 44b1c180e9a5645342160784598b75f1ffbf0e69 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin NUTIU Date: Wed, 25 Dec 2024 12:28:19 +0200 Subject: [PATCH] add redis tests --- docker-compose.yaml | 2 +- scrapper/Cargo.toml | 3 ++ scrapper/src/main.rs | 2 +- scrapper/src/redis.rs | 76 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 5ac8a4e..e2ba075 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,4 +5,4 @@ services: container_name: bluesky-bot-local-redis image: redis/redis-stack-server:6.2.6-v17 ports: - - "6370:6370" \ No newline at end of file + - "6379:6379" \ No newline at end of file diff --git a/scrapper/Cargo.toml b/scrapper/Cargo.toml index 3581bce..e835b0a 100644 --- a/scrapper/Cargo.toml +++ b/scrapper/Cargo.toml @@ -17,3 +17,6 @@ redis = { version = "0.27.6", features = ["tokio-comp"] } md5 = "0.7.0" serde = { version = "1.0.216", features = ["derive"] } serde_json = "1.0.134" + +[dev-dependencies] +rand = "0.8.5" diff --git a/scrapper/src/main.rs b/scrapper/src/main.rs index be50bbf..42ead8a 100644 --- a/scrapper/src/main.rs +++ b/scrapper/src/main.rs @@ -57,7 +57,7 @@ async fn main() -> Result<(), anyhow::Error> { // Redis setup let mut redis_service = - RedisService::new(args.redis_connection_string, args.redis_stream_name).await; + RedisService::new(&args.redis_connection_string, &args.redis_stream_name).await; // Scheduler setup let mut scheduler = AsyncScheduler::new(); diff --git a/scrapper/src/redis.rs b/scrapper/src/redis.rs index 0f72a64..4d30dda 100644 --- a/scrapper/src/redis.rs +++ b/scrapper/src/redis.rs @@ -10,13 +10,13 @@ pub struct RedisService { impl RedisService { /// Creates a new RedisService instance. - pub async fn new(connection_string: String, stream_name: String) -> Self { + pub async fn new(connection_string: &str, stream_name: &str) -> Self { let client = redis::Client::open(connection_string).unwrap(); let con = client.get_multiplexed_async_connection().await.unwrap(); RedisService { multiplexed_connection: con, - stream_name, + stream_name: stream_name.to_string(), } } @@ -58,3 +58,75 @@ impl RedisService { true } } + +#[cfg(test)] +mod tests { + use super::*; + use rand::distributions::{Alphanumeric, DistString}; + use redis::RedisResult; + + const REDIS_CONNECTION_STRING: &str = "redis://localhost:6379"; + + #[tokio::test] + async fn test_redis_service_new() { + let _ = RedisService::new(REDIS_CONNECTION_STRING, "a").await; + } + + #[tokio::test] + async fn test_redis_service_is_post_seen_false() { + // Setup + let random_stream_name = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); + let random_post = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); + + let mut service = RedisService::new(REDIS_CONNECTION_STRING, &random_stream_name).await; + + // Test + let result = service.is_post_seen(&random_post).await; + + // Assert + assert_eq!(result, false); + } + + #[tokio::test] + async fn test_redis_service_is_post_seen_true() { + // Setup + let random_stream_name = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); + let random_post = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); + + let mut service = RedisService::new(REDIS_CONNECTION_STRING, &random_stream_name).await; + service.mark_post_seen(&random_post, 10).await; + + // Test + let result = service.is_post_seen(&random_post).await; + + // Assert + assert_eq!(result, true); + } + + #[tokio::test] + async fn test_redis_service_publish() { + // Setup + let random_stream_name = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); + + let mut service = RedisService::new(REDIS_CONNECTION_STRING, &random_stream_name).await; + + // Test + let post = NewsPost { + image: Some(String::from("i")), + title: Some(String::from("t")), + summary: Some(String::from("s")), + link: Some(String::from("l")), + author: Some(String::from("a")), + }; + let result = service.publish(&post).await; + + let stream_length: RedisResult = redis::cmd("XLEN") + .arg(&random_stream_name) + .query_async(&mut service.multiplexed_connection) + .await; + + // Assert + assert_eq!(result, true); + assert_eq!(stream_length, Ok(0)); + } +}