#![cfg(any(feature = "postgres-storage-nativetls", feature = "postgres-storage-rustls"))]
use std::{
collections::HashSet,
fmt::{Debug, Display},
sync::Arc,
};
use teloxide::{
dispatching::dialogue::{
DialogueKey, PostgresStorage, PostgresStorageError, Serializer, Storage, StorageKeys,
},
types::{BusinessConnectionId, ChatId},
};
#[tokio::test]
#[cfg_attr(not(CI_POSTGRES), ignore)]
async fn test_postgres_json() {
let storage = PostgresStorage::open(
"postgres://teloxide:rewrite_it_in_rust@localhost:5432/test_postgres_json",
1,
teloxide::dispatching::dialogue::serializer::Json,
)
.await
.unwrap();
test_postgres(storage).await;
}
#[tokio::test]
#[cfg_attr(not(CI_POSTGRES), ignore)]
async fn test_postgres_bincode() {
let storage = PostgresStorage::open(
"postgres://teloxide:rewrite_it_in_rust@localhost:5432/test_postgres_bincode",
1,
teloxide::dispatching::dialogue::serializer::Bincode,
)
.await
.unwrap();
test_postgres(storage).await;
}
#[tokio::test]
#[cfg_attr(not(CI_POSTGRES), ignore)]
async fn test_postgres_cbor() {
let storage = PostgresStorage::open(
"postgres://teloxide:rewrite_it_in_rust@localhost:5432/test_postgres_cbor",
1,
teloxide::dispatching::dialogue::serializer::Cbor,
)
.await
.unwrap();
test_postgres(storage).await;
}
type Dialogue = String;
macro_rules! test_dialogues {
($storage:expr, $_0:expr, $_1:expr, $_2:expr) => {
assert_eq!(Arc::clone(&$storage).get_dialogue(ChatId(1).into()).await.unwrap(), $_0);
assert_eq!(Arc::clone(&$storage).get_dialogue(ChatId(11).into()).await.unwrap(), $_1);
assert_eq!(Arc::clone(&$storage).get_dialogue(ChatId(256).into()).await.unwrap(), $_2);
};
}
async fn test_postgres<S>(storage: Arc<PostgresStorage<S>>)
where
S: Send + Sync + Serializer<Dialogue> + 'static,
<S as Serializer<Dialogue>>::Error: Debug + Display,
{
test_dialogues!(storage, None, None, None);
assert!(storage.clone().keys().await.unwrap().is_empty());
storage.clone().update_dialogue(ChatId(1).into(), "ABC".to_owned()).await.unwrap();
storage.clone().update_dialogue(ChatId(11).into(), "DEF".to_owned()).await.unwrap();
storage.clone().update_dialogue(ChatId(256).into(), "GHI".to_owned()).await.unwrap();
let business = DialogueKey::business(ChatId(1), BusinessConnectionId("a:b".into()));
storage.clone().update_dialogue(business.clone(), "business".to_owned()).await.unwrap();
test_dialogues!(
storage,
Some("ABC".to_owned()),
Some("DEF".to_owned()),
Some("GHI".to_owned())
);
assert_eq!(
storage.clone().keys().await.unwrap(),
HashSet::from([ChatId(1).into(), ChatId(11).into(), ChatId(256).into(), business.clone()])
);
storage.clone().remove_dialogue(ChatId(1).into()).await.unwrap();
storage.clone().remove_dialogue(ChatId(11).into()).await.unwrap();
storage.clone().remove_dialogue(ChatId(256).into()).await.unwrap();
assert_eq!(
storage.clone().get_dialogue(business.clone()).await.unwrap(),
Some("business".to_owned())
);
storage.clone().remove_dialogue(business).await.unwrap();
test_dialogues!(storage, None, None, None);
assert!(storage.clone().keys().await.unwrap().is_empty());
assert!(matches!(
storage.remove_dialogue(ChatId(1).into()).await.unwrap_err(),
PostgresStorageError::DialogueNotFound
));
}