teloxide/tests/redis.rs

80 lines
2.2 KiB
Rust
Raw Normal View History

use std::{
fmt::{Debug, Display},
sync::Arc,
};
use teloxide::dispatching::dialogue::{RedisStorage, RedisStorageError, Serializer, Storage};
#[tokio::test]
async fn test_redis_json() {
let storage = RedisStorage::open(
"redis://127.0.0.1:7777",
teloxide::dispatching::dialogue::serializer::Json,
)
.await
.unwrap();
test_redis(storage).await;
}
#[tokio::test]
async fn test_redis_bincode() {
let storage = RedisStorage::open(
"redis://127.0.0.1:7778",
teloxide::dispatching::dialogue::serializer::Bincode,
)
.await
.unwrap();
test_redis(storage).await;
}
#[tokio::test]
async fn test_redis_cbor() {
let storage = RedisStorage::open(
"redis://127.0.0.1:7779",
teloxide::dispatching::dialogue::serializer::Cbor,
)
.await
.unwrap();
test_redis(storage).await;
}
type Dialogue = String;
2021-03-28 04:20:35 +02:00
macro_rules! test_dialogues {
($storage:expr, $_0:expr, $_1:expr, $_2:expr) => {
assert_eq!(Arc::clone(&$storage).get_dialogue(1).await.unwrap(), $_0);
assert_eq!(Arc::clone(&$storage).get_dialogue(11).await.unwrap(), $_1);
assert_eq!(Arc::clone(&$storage).get_dialogue(256).await.unwrap(), $_2);
};
}
async fn test_redis<S>(storage: Arc<RedisStorage<S>>)
where
S: Send + Sync + Serializer<Dialogue> + 'static,
<S as Serializer<Dialogue>>::Error: Debug + Display,
{
2021-03-28 04:20:35 +02:00
test_dialogues!(storage, None, None, None);
2021-03-28 04:20:35 +02:00
Arc::clone(&storage).update_dialogue(1, "ABC".to_owned()).await.unwrap();
Arc::clone(&storage).update_dialogue(11, "DEF".to_owned()).await.unwrap();
Arc::clone(&storage).update_dialogue(256, "GHI".to_owned()).await.unwrap();
2021-03-28 04:20:35 +02:00
test_dialogues!(
storage,
Some("ABC".to_owned()),
Some("DEF".to_owned()),
Some("GHI".to_owned())
);
2021-03-28 04:20:35 +02:00
Arc::clone(&storage).remove_dialogue(1).await.unwrap();
Arc::clone(&storage).remove_dialogue(11).await.unwrap();
Arc::clone(&storage).remove_dialogue(256).await.unwrap();
2021-03-28 04:20:35 +02:00
test_dialogues!(storage, None, None, None);
// Check that a try to remove a non-existing dialogue results in an error.
assert!(matches!(
Arc::clone(&storage).remove_dialogue(1).await.unwrap_err(),
2021-05-08 15:00:38 +02:00
RedisStorageError::DialogueNotFound
));
}