add redis tests

This commit is contained in:
Denis-Cosmin NUTIU 2024-12-25 12:28:19 +02:00
parent 2d8bd3e6ea
commit 44b1c180e9
4 changed files with 79 additions and 4 deletions

View file

@ -5,4 +5,4 @@ services:
container_name: bluesky-bot-local-redis container_name: bluesky-bot-local-redis
image: redis/redis-stack-server:6.2.6-v17 image: redis/redis-stack-server:6.2.6-v17
ports: ports:
- "6370:6370" - "6379:6379"

View file

@ -17,3 +17,6 @@ redis = { version = "0.27.6", features = ["tokio-comp"] }
md5 = "0.7.0" md5 = "0.7.0"
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.216", features = ["derive"] }
serde_json = "1.0.134" serde_json = "1.0.134"
[dev-dependencies]
rand = "0.8.5"

View file

@ -57,7 +57,7 @@ async fn main() -> Result<(), anyhow::Error> {
// Redis setup // Redis setup
let mut redis_service = 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 // Scheduler setup
let mut scheduler = AsyncScheduler::new(); let mut scheduler = AsyncScheduler::new();

View file

@ -10,13 +10,13 @@ pub struct RedisService {
impl RedisService { impl RedisService {
/// Creates a new RedisService instance. /// 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 client = redis::Client::open(connection_string).unwrap();
let con = client.get_multiplexed_async_connection().await.unwrap(); let con = client.get_multiplexed_async_connection().await.unwrap();
RedisService { RedisService {
multiplexed_connection: con, multiplexed_connection: con,
stream_name, stream_name: stream_name.to_string(),
} }
} }
@ -58,3 +58,75 @@ impl RedisService {
true 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<i32> = 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));
}
}