diff --git a/scrapper/Cargo.toml b/scrapper/Cargo.toml index e835b0a..fb03414 100644 --- a/scrapper/Cargo.toml +++ b/scrapper/Cargo.toml @@ -20,3 +20,4 @@ serde_json = "1.0.134" [dev-dependencies] rand = "0.8.5" +serial_test = "3.2.0" \ No newline at end of file diff --git a/scrapper/src/redis.rs b/scrapper/src/redis.rs index 4d30dda..cf83fa4 100644 --- a/scrapper/src/redis.rs +++ b/scrapper/src/redis.rs @@ -64,15 +64,26 @@ mod tests { use super::*; use rand::distributions::{Alphanumeric, DistString}; use redis::RedisResult; + use serial_test::serial; const REDIS_CONNECTION_STRING: &str = "redis://localhost:6379"; + /// Cleans up the database + async fn cleanup(redis_service: &mut RedisService) { + redis::cmd("flushall") + .exec_async(&mut redis_service.multiplexed_connection) + .await + .expect("failed to clean db") + } + #[tokio::test] + #[serial] async fn test_redis_service_new() { let _ = RedisService::new(REDIS_CONNECTION_STRING, "a").await; } #[tokio::test] + #[serial] async fn test_redis_service_is_post_seen_false() { // Setup let random_stream_name = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); @@ -85,9 +96,11 @@ mod tests { // Assert assert_eq!(result, false); + cleanup(&mut service).await; } #[tokio::test] + #[serial] async fn test_redis_service_is_post_seen_true() { // Setup let random_stream_name = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); @@ -101,9 +114,11 @@ mod tests { // Assert assert_eq!(result, true); + cleanup(&mut service).await; } #[tokio::test] + #[serial] async fn test_redis_service_publish() { // Setup let random_stream_name = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); @@ -121,12 +136,13 @@ mod tests { let result = service.publish(&post).await; let stream_length: RedisResult = redis::cmd("XLEN") - .arg(&random_stream_name) + .arg(&format!("posts:{}", random_stream_name)) .query_async(&mut service.multiplexed_connection) .await; // Assert assert_eq!(result, true); - assert_eq!(stream_length, Ok(0)); + assert_eq!(stream_length, Ok(1)); + cleanup(&mut service).await; } }