2020-07-01 16:41:00 +02:00
|
|
|
use std::{
|
|
|
|
fmt::{Debug, Display},
|
|
|
|
future::Future,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2020-07-01 18:31:30 +02:00
|
|
|
use teloxide::dispatching::dialogue::{
|
|
|
|
serializer::{Bincode, CBOR, JSON},
|
|
|
|
RedisStorage, Serializer, Storage,
|
2020-07-01 16:41:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_redis_json() {
|
2020-07-26 19:47:02 +02:00
|
|
|
let storage = RedisStorage::open("redis://127.0.0.1:7777", JSON).await.unwrap();
|
2020-07-04 16:54:47 +02:00
|
|
|
test_redis(storage).await;
|
2020-07-01 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_redis_bincode() {
|
2020-07-26 19:47:02 +02:00
|
|
|
let storage = RedisStorage::open("redis://127.0.0.1:7778", Bincode).await.unwrap();
|
2020-07-04 16:54:47 +02:00
|
|
|
test_redis(storage).await;
|
2020-07-01 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_redis_cbor() {
|
2020-07-26 19:47:02 +02:00
|
|
|
let storage = RedisStorage::open("redis://127.0.0.1:7779", CBOR).await.unwrap();
|
2020-07-04 16:54:47 +02:00
|
|
|
test_redis(storage).await;
|
2020-07-01 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Dialogue = String;
|
|
|
|
|
2020-07-04 16:54:47 +02:00
|
|
|
async fn test_redis<S>(storage: Arc<RedisStorage<S>>)
|
2020-07-01 16:41:00 +02:00
|
|
|
where
|
|
|
|
S: Send + Sync + Serializer<Dialogue> + 'static,
|
|
|
|
<S as Serializer<Dialogue>>::Error: Debug + Display,
|
|
|
|
{
|
2020-07-26 19:47:02 +02:00
|
|
|
check_dialogue(None, Arc::clone(&storage).update_dialogue(1, "ABC".to_owned())).await;
|
|
|
|
check_dialogue(None, Arc::clone(&storage).update_dialogue(11, "DEF".to_owned())).await;
|
|
|
|
check_dialogue(None, Arc::clone(&storage).update_dialogue(256, "GHI".to_owned())).await;
|
2020-07-01 16:41:00 +02:00
|
|
|
|
2020-07-04 16:54:47 +02:00
|
|
|
// 1 - ABC, 11 - DEF, 256 - GHI
|
|
|
|
|
2020-07-26 19:47:02 +02:00
|
|
|
check_dialogue("ABC", Arc::clone(&storage).update_dialogue(1, "JKL".to_owned())).await;
|
|
|
|
check_dialogue("GHI", Arc::clone(&storage).update_dialogue(256, "MNO".to_owned())).await;
|
2020-07-01 16:41:00 +02:00
|
|
|
|
2020-07-04 16:54:47 +02:00
|
|
|
// 1 - GKL, 11 - DEF, 256 - MNO
|
|
|
|
|
2020-07-01 18:31:30 +02:00
|
|
|
check_dialogue("JKL", Arc::clone(&storage).remove_dialogue(1)).await;
|
2020-07-04 16:54:47 +02:00
|
|
|
check_dialogue("DEF", Arc::clone(&storage).remove_dialogue(11)).await;
|
|
|
|
check_dialogue("MNO", Arc::clone(&storage).remove_dialogue(256)).await;
|
2020-07-01 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn check_dialogue<E>(
|
|
|
|
expected: impl Into<Option<&str>>,
|
|
|
|
actual: impl Future<Output = Result<Option<Dialogue>, E>>,
|
|
|
|
) where
|
|
|
|
E: Debug,
|
|
|
|
{
|
|
|
|
assert_eq!(expected.into().map(ToOwned::to_owned), actual.await.unwrap())
|
|
|
|
}
|