Add tests for sqlite storage

This commit is contained in:
Sergey Levitin 2020-10-24 19:51:55 +03:00
parent 7ba0a5b5a4
commit 5e1fe064cd
4 changed files with 76 additions and 3 deletions

View file

@ -39,9 +39,9 @@ jobs:
include:
- rust: stable
features: "--features \"redis-storage cbor-serializer bincode-serializer frunk-\""
features: "--features \"redis-storage sqlite-storage cbor-serializer bincode-serializer frunk-\""
- rust: beta
features: "--features \"redis-storage cbor-serializer bincode-serializer frunk-\""
features: "--features \"redis-storage sqlite-storage cbor-serializer bincode-serializer frunk-\""
- rust: nightly
features: "--all-features"

3
.gitignore vendored
View file

@ -3,4 +3,5 @@
Cargo.lock
.idea/
.vscode/
examples/*/target
examples/*/target
test_db*.sqlite

View file

@ -81,3 +81,8 @@ all-features = true
name = "redis"
path = "tests/redis.rs"
required-features = ["redis-storage", "cbor-serializer", "bincode-serializer"]
[[test]]
name = "sqlite"
path = "tests/sqlite.rs"
required-features = ["sqlite-storage", "cbor-serializer", "bincode-serializer"]

67
tests/sqlite.rs Normal file
View file

@ -0,0 +1,67 @@
use std::{
fmt::{Debug, Display},
future::Future,
sync::Arc,
};
use teloxide::dispatching::dialogue::{Serializer, SqliteStorage, Storage};
#[tokio::test(threaded_scheduler)]
async fn test_sqlite_json() {
let storage =
SqliteStorage::open("./test_db1.sqlite", teloxide::dispatching::dialogue::serializer::JSON)
.await
.unwrap();
test_sqlite(storage).await;
}
#[tokio::test(threaded_scheduler)]
async fn test_sqlite_bincode() {
let storage = SqliteStorage::open(
"./test_db2.sqlite",
teloxide::dispatching::dialogue::serializer::Bincode,
)
.await
.unwrap();
test_sqlite(storage).await;
}
#[tokio::test(threaded_scheduler)]
async fn test_sqlite_cbor() {
let storage =
SqliteStorage::open("./test_db3.sqlite", teloxide::dispatching::dialogue::serializer::CBOR)
.await
.unwrap();
test_sqlite(storage).await;
}
type Dialogue = String;
async fn test_sqlite<S>(storage: Arc<SqliteStorage<S>>)
where
S: Send + Sync + Serializer<Dialogue> + 'static,
<S as Serializer<Dialogue>>::Error: Debug + Display,
{
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;
// 1 - ABC, 11 - DEF, 256 - GHI
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;
// 1 - GKL, 11 - DEF, 256 - MNO
check_dialogue("JKL", Arc::clone(&storage).remove_dialogue(1)).await;
check_dialogue("DEF", Arc::clone(&storage).remove_dialogue(11)).await;
check_dialogue("MNO", Arc::clone(&storage).remove_dialogue(256)).await;
}
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())
}