#![cfg(any(feature = "sqlite-storage-nativetls", feature = "sqlite-storage-rustls"))]
use std::{
collections::HashSet,
fmt::{Debug, Display},
fs,
sync::Arc,
};
use teloxide::{
dispatching::dialogue::{
DialogueKey, Serializer, SqliteStorage, SqliteStorageError, Storage, StorageKeys,
},
types::{BusinessConnectionId, ChatId},
};
#[tokio::test(flavor = "multi_thread")]
async fn test_sqlite_json() {
fs::create_dir("./test_db1").unwrap();
let storage = SqliteStorage::open(
"./test_db1/test_db1.sqlite",
teloxide::dispatching::dialogue::serializer::Json,
)
.await
.unwrap();
test_sqlite(storage).await;
fs::remove_dir_all("./test_db1").unwrap();
}
#[tokio::test(flavor = "multi_thread")]
async fn test_sqlite_bincode() {
fs::create_dir("./test_db2").unwrap();
let storage = SqliteStorage::open(
"./test_db2/test_db2.sqlite",
teloxide::dispatching::dialogue::serializer::Bincode,
)
.await
.unwrap();
test_sqlite(storage).await;
fs::remove_dir_all("./test_db2").unwrap();
}
#[tokio::test(flavor = "multi_thread")]
async fn test_sqlite_cbor() {
fs::create_dir("./test_db3").unwrap();
let storage = SqliteStorage::open(
"./test_db3/test_db3.sqlite",
teloxide::dispatching::dialogue::serializer::Cbor,
)
.await
.unwrap();
test_sqlite(storage).await;
fs::remove_dir_all("./test_db3").unwrap();
}
type Dialogue = String;
#[tokio::test]
async fn legacy_sqlite_rows_remain_in_the_ordinary_chat_namespace() {
use sqlx::SqlitePool;
use teloxide::dispatching::dialogue::serializer::Json;
let dir = "./test_db_legacy";
let path = "./test_db_legacy/dialogues.sqlite";
fs::create_dir_all(dir).unwrap();
let pool = SqlitePool::connect(&format!("sqlite:{path}?mode=rwc")).await.unwrap();
sqlx::query(
"CREATE TABLE teloxide_dialogues (chat_id BIGINT PRIMARY KEY, dialogue BLOB NOT NULL)",
)
.execute(&pool)
.await
.unwrap();
sqlx::query("INSERT INTO teloxide_dialogues VALUES (42, ?)")
.bind(b"7".as_slice())
.execute(&pool)
.await
.unwrap();
pool.close().await;
let ordinary = DialogueKey::from(ChatId(42));
let business = DialogueKey::business(ChatId(42), BusinessConnectionId("c:42".into()));
let storage = SqliteStorage::open(path, Json).await.unwrap();
assert_eq!(storage.clone().keys().await.unwrap(), HashSet::from([ordinary.clone()]));
assert_eq!(
Storage::<u32>::get_dialogue(storage.clone(), business.clone()).await.unwrap(),
None
);
storage.update_dialogue(business.clone(), 9u32).await.unwrap();
let storage = SqliteStorage::open(path, Json).await.unwrap();
assert_eq!(Storage::<u32>::get_dialogue(storage.clone(), ordinary).await.unwrap(), Some(7));
assert_eq!(Storage::<u32>::get_dialogue(storage, business).await.unwrap(), Some(9));
fs::remove_dir_all(dir).unwrap();
}
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_sqlite<S>(storage: Arc<SqliteStorage<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(),
SqliteStorageError::DialogueNotFound
));
}