2020-10-24 18:51:55 +02:00
|
|
|
use std::{
|
|
|
|
fmt::{Debug, Display},
|
2022-03-14 19:05:15 +01:00
|
|
|
fs,
|
2020-10-24 18:51:55 +02:00
|
|
|
sync::Arc,
|
|
|
|
};
|
2022-04-13 13:56:54 +02:00
|
|
|
use teloxide::{
|
|
|
|
dispatching::dialogue::{Serializer, SqliteStorage, SqliteStorageError, Storage},
|
|
|
|
types::ChatId,
|
|
|
|
};
|
2020-10-24 18:51:55 +02:00
|
|
|
|
2021-03-05 22:50:11 +01:00
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
2020-10-24 18:51:55 +02:00
|
|
|
async fn test_sqlite_json() {
|
2022-03-14 19:05:15 +01:00
|
|
|
fs::create_dir("./test_db1").unwrap();
|
|
|
|
let storage = SqliteStorage::open(
|
|
|
|
"./test_db1/test_db1.sqlite",
|
|
|
|
teloxide::dispatching::dialogue::serializer::Json,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
test_sqlite(storage).await;
|
2022-03-14 19:05:15 +01:00
|
|
|
fs::remove_dir_all("./test_db1").unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 22:50:11 +01:00
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
2020-10-24 18:51:55 +02:00
|
|
|
async fn test_sqlite_bincode() {
|
2022-03-14 19:05:15 +01:00
|
|
|
fs::create_dir("./test_db2").unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
let storage = SqliteStorage::open(
|
2022-03-14 19:05:15 +01:00
|
|
|
"./test_db2/test_db2.sqlite",
|
2020-10-24 18:51:55 +02:00
|
|
|
teloxide::dispatching::dialogue::serializer::Bincode,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
test_sqlite(storage).await;
|
2022-03-14 19:05:15 +01:00
|
|
|
fs::remove_dir_all("./test_db2").unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 22:50:11 +01:00
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
2020-10-24 18:51:55 +02:00
|
|
|
async fn test_sqlite_cbor() {
|
2022-03-14 19:05:15 +01:00
|
|
|
fs::create_dir("./test_db3").unwrap();
|
|
|
|
let storage = SqliteStorage::open(
|
|
|
|
"./test_db3/test_db3.sqlite",
|
|
|
|
teloxide::dispatching::dialogue::serializer::Cbor,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
test_sqlite(storage).await;
|
2022-03-14 19:05:15 +01:00
|
|
|
fs::remove_dir_all("./test_db3").unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Dialogue = String;
|
|
|
|
|
2021-03-28 04:20:35 +02:00
|
|
|
macro_rules! test_dialogues {
|
|
|
|
($storage:expr, $_0:expr, $_1:expr, $_2:expr) => {
|
2022-04-13 13:56:54 +02:00
|
|
|
assert_eq!(Arc::clone(&$storage).get_dialogue(ChatId(1)).await.unwrap(), $_0);
|
|
|
|
assert_eq!(Arc::clone(&$storage).get_dialogue(ChatId(11)).await.unwrap(), $_1);
|
|
|
|
assert_eq!(Arc::clone(&$storage).get_dialogue(ChatId(256)).await.unwrap(), $_2);
|
2021-03-28 04:20:35 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:51:55 +02:00
|
|
|
async fn test_sqlite<S>(storage: Arc<SqliteStorage<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);
|
2020-10-24 18:51:55 +02:00
|
|
|
|
2022-04-13 13:56:54 +02:00
|
|
|
Arc::clone(&storage).update_dialogue(ChatId(1), "ABC".to_owned()).await.unwrap();
|
|
|
|
Arc::clone(&storage).update_dialogue(ChatId(11), "DEF".to_owned()).await.unwrap();
|
|
|
|
Arc::clone(&storage).update_dialogue(ChatId(256), "GHI".to_owned()).await.unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
|
2021-03-28 04:20:35 +02:00
|
|
|
test_dialogues!(
|
|
|
|
storage,
|
|
|
|
Some("ABC".to_owned()),
|
|
|
|
Some("DEF".to_owned()),
|
|
|
|
Some("GHI".to_owned())
|
|
|
|
);
|
2020-10-24 18:51:55 +02:00
|
|
|
|
2022-04-13 13:56:54 +02:00
|
|
|
Arc::clone(&storage).remove_dialogue(ChatId(1)).await.unwrap();
|
|
|
|
Arc::clone(&storage).remove_dialogue(ChatId(11)).await.unwrap();
|
|
|
|
Arc::clone(&storage).remove_dialogue(ChatId(256)).await.unwrap();
|
2020-10-24 18:51:55 +02:00
|
|
|
|
2021-03-28 04:20:35 +02:00
|
|
|
test_dialogues!(storage, None, None, None);
|
2021-05-08 13:21:24 +02:00
|
|
|
|
|
|
|
// Check that a try to remove a non-existing dialogue results in an error.
|
2021-05-08 15:00:38 +02:00
|
|
|
assert!(matches!(
|
2022-04-13 13:56:54 +02:00
|
|
|
Arc::clone(&storage).remove_dialogue(ChatId(1)).await.unwrap_err(),
|
2021-05-08 15:00:38 +02:00
|
|
|
SqliteStorageError::DialogueNotFound
|
|
|
|
));
|
2020-10-24 18:51:55 +02:00
|
|
|
}
|