Implemented connection tests

This commit is contained in:
S-Y-rat 2020-07-31 21:37:35 +03:00
parent c09eca1e39
commit 8413b6b2b7
4 changed files with 54 additions and 19 deletions

View file

@ -160,6 +160,6 @@ pub use transition::{
pub use storage::{RedisStorage, RedisStorageError}; pub use storage::{RedisStorage, RedisStorageError};
#[cfg(feature = "sqlite-storage")] #[cfg(feature = "sqlite-storage")]
pub use storage::{SqliteStorage, SqliteStorageError}; pub use storage::{SqliteStorage, SqliteStorageLocation, SqliteStorageError};
pub use storage::{serializer, InMemStorage, Serializer, Storage}; pub use storage::{serializer, InMemStorage, Serializer, Storage};

View file

@ -17,7 +17,7 @@ pub use serializer::Serializer;
use std::sync::Arc; use std::sync::Arc;
#[cfg(feature = "sqlite-storage")] #[cfg(feature = "sqlite-storage")]
pub use sqlite_storage::{SqliteStorage, SqliteStorageError}; pub use sqlite_storage::{SqliteStorage, SqliteStorageLocation, SqliteStorageError};
/// A storage of dialogues. /// A storage of dialogues.
/// ///

View file

@ -1,16 +1,13 @@
use super::{serializer::Serializer, Storage}; // use super::{serializer::Serializer, Storage};
// use futures::future::BoxFuture;
use std::{ use std::{
convert::Infallible, convert::Infallible,
fmt::{Debug, Display}, fmt::{Debug, Display},
sync::Arc,
}; };
use sqlx::{SqliteConnection, Connection, sqlite::SqliteError}; use sqlx::sqlite::SqlitePool;
use serde::{de::DeserializeOwned, Serialize}; // use serde::{de::DeserializeOwned, Serialize};
use thiserror::Error; use thiserror::Error;
use tokio::{ // use tokio::task::block_in_place;
sync::Mutex,
task::block_in_place,
};
pub enum SqliteStorageLocation { pub enum SqliteStorageLocation {
InMemory, InMemory,
@ -26,11 +23,11 @@ where
#[error("parsing/serializing error: {0}")] #[error("parsing/serializing error: {0}")]
SerdeError(SE), SerdeError(SE),
#[error("error from Sqlite: {0}")] #[error("error from Sqlite: {0}")]
SqliteError(#[from] SqliteError), SqliteError(Box<dyn std::error::Error>),
} }
pub struct SqliteStorage<S> { pub struct SqliteStorage<S> {
conn: Mutex<SqliteConnection>, conn: SqlitePool,
serializer: S, serializer: S,
} }
@ -38,14 +35,39 @@ impl <S> SqliteStorage<S> {
pub async fn open( pub async fn open(
path: SqliteStorageLocation, path: SqliteStorageLocation,
serializer: S, serializer: S,
) -> Result<Arc<Self>, Box<dyn std::error::Error>>{ ) -> Result<Self, SqliteStorageError<Infallible>>{
let url = match path { let url = match path {
SqliteStorageLocation::InMemory => String::from("sqlite::memory:"), SqliteStorageLocation::InMemory => String::from("sqlite::memory:"),
SqliteStorageLocation::Path(p) => p, SqliteStorageLocation::Path(p) => p,
}; };
Ok(Arc::new(Self { Ok(Self {
conn: Mutex::new(SqliteConnection::connect(&url[..]).await?), conn: SqlitePool::connect(&url[..]).await
.expect("Impossible sqlite error"),
serializer, serializer,
})) })
} }
} }
// impl<S, D> Storage<D> for SqliteStorage<S>
// where
// S: Send + Sync + Serializer<D> + 'static,
// D: Send + Serialize + DeserializeOwned + 'static,
// <S as Serializer<D>>::Error: Debug + Display,
// {
// type Error = SqliteStorageError<<S as Serializer<D>>::Error>;
// fn remove_dialogue(
// self: Arc<Self>,
// chat_id: i64,
// ) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
// Box::pin(async move {
// todo!()
// });
// }
// fn update_dialogue(
// self: Arc<Self>,
// chat_id: i64,
// dialogue: D
// ) { todo!() }
// }

View file

@ -1,6 +1,19 @@
use teloxide::dispatching::dialogue::SqliteStorage; use teloxide::dispatching::dialogue::{
serializer::{Bincode, CBOR, JSON},
SqliteStorage, SqliteStorageLocation::InMemory
};
#[tokio::test] #[tokio::test]
async fn test_sqlite() { async fn test_sqlite_json() {
todo!() let _storage = SqliteStorage::open(InMemory, JSON).await.unwrap();
} }
#[tokio::test]
async fn test_sqlite_cbor() {
let _storage = SqliteStorage::open(InMemory, CBOR).await.unwrap();
}
#[tokio::test]
async fn test_sqlite_bincode() {
let _storage = SqliteStorage::open(InMemory, Bincode).await.unwrap();
}