Fix the tests and examples

This commit is contained in:
Temirkhan Myrzamadi 2021-03-28 08:20:35 +06:00
parent 01b7b91bda
commit 9b75378572
9 changed files with 54 additions and 45 deletions

View file

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Do not return a dialogue from `Storage::{remove_dialogue, update_dialogue}`.
- Require `D: ToOwned<Owned = D>` in `dialogues_repl` and `InMemStorage`.
### Fixed

View file

@ -6,7 +6,7 @@ use crate::dialogue::states::{
use derive_more::From;
use teloxide::macros::Transition;
#[derive(Transition, From)]
#[derive(Transition, Clone, From)]
pub enum Dialogue {
Start(StartState),
ReceiveFullName(ReceiveFullNameState),

View file

@ -1,7 +1,7 @@
use crate::dialogue::{states::receive_location::ReceiveLocationState, Dialogue};
use teloxide::prelude::*;
#[derive(Generic)]
#[derive(Clone, Generic)]
pub struct ReceiveAgeState {
pub full_name: String,
}

View file

@ -1,7 +1,7 @@
use crate::dialogue::{states::receive_age::ReceiveAgeState, Dialogue};
use teloxide::prelude::*;
#[derive(Generic)]
#[derive(Clone, Generic)]
pub struct ReceiveFullNameState;
#[teloxide(subtransition)]

View file

@ -1,7 +1,7 @@
use crate::dialogue::Dialogue;
use teloxide::prelude::*;
#[derive(Generic)]
#[derive(Clone, Generic)]
pub struct ReceiveLocationState {
pub full_name: String,
pub age: u8,

View file

@ -1,6 +1,7 @@
use crate::dialogue::{states::ReceiveFullNameState, Dialogue};
use teloxide::prelude::*;
#[derive(Clone)]
pub struct StartState;
#[teloxide(subtransition)]

View file

@ -35,8 +35,11 @@
//!
//! use teloxide::{dispatching::dialogue::Transition, prelude::*, teloxide, RequestError};
//!
//! #[derive(Clone)]
//! struct _1State;
//! #[derive(Clone)]
//! struct _2State;
//! #[derive(Clone)]
//! struct _3State;
//!
//! type Out = TransitionOut<D, RequestError>;
@ -56,7 +59,7 @@
//! todo!()
//! }
//!
//! #[derive(Transition)]
//! #[derive(Clone, Transition)]
//! enum D {
//! _1(_1State),
//! _2(_2State),

View file

@ -1,6 +1,5 @@
use std::{
fmt::{Debug, Display},
future::Future,
sync::Arc,
};
use teloxide::dispatching::dialogue::{RedisStorage, Serializer, Storage};
@ -40,32 +39,35 @@ async fn test_redis_cbor() {
type Dialogue = String;
macro_rules! test_dialogues {
($storage:expr, $_0:expr, $_1:expr, $_2:expr) => {
assert_eq!(Arc::clone(&$storage).get_dialogue(1).await.unwrap(), $_0);
assert_eq!(Arc::clone(&$storage).get_dialogue(11).await.unwrap(), $_1);
assert_eq!(Arc::clone(&$storage).get_dialogue(256).await.unwrap(), $_2);
};
}
async fn test_redis<S>(storage: Arc<RedisStorage<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;
test_dialogues!(storage, None, None, None);
// 1 - ABC, 11 - DEF, 256 - GHI
Arc::clone(&storage).update_dialogue(1, "ABC".to_owned()).await.unwrap();
Arc::clone(&storage).update_dialogue(11, "DEF".to_owned()).await.unwrap();
Arc::clone(&storage).update_dialogue(256, "GHI".to_owned()).await.unwrap();
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;
test_dialogues!(
storage,
Some("ABC".to_owned()),
Some("DEF".to_owned()),
Some("GHI".to_owned())
);
// 1 - GKL, 11 - DEF, 256 - MNO
Arc::clone(&storage).remove_dialogue(1).await.unwrap();
Arc::clone(&storage).remove_dialogue(11).await.unwrap();
Arc::clone(&storage).remove_dialogue(256).await.unwrap();
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())
test_dialogues!(storage, None, None, None);
}

View file

@ -1,6 +1,5 @@
use std::{
fmt::{Debug, Display},
future::Future,
sync::Arc,
};
use teloxide::dispatching::dialogue::{Serializer, SqliteStorage, Storage};
@ -36,32 +35,35 @@ async fn test_sqlite_cbor() {
type Dialogue = String;
macro_rules! test_dialogues {
($storage:expr, $_0:expr, $_1:expr, $_2:expr) => {
assert_eq!(Arc::clone(&$storage).get_dialogue(1).await.unwrap(), $_0);
assert_eq!(Arc::clone(&$storage).get_dialogue(11).await.unwrap(), $_1);
assert_eq!(Arc::clone(&$storage).get_dialogue(256).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,
{
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;
test_dialogues!(storage, None, None, None);
// 1 - ABC, 11 - DEF, 256 - GHI
Arc::clone(&storage).update_dialogue(1, "ABC".to_owned()).await.unwrap();
Arc::clone(&storage).update_dialogue(11, "DEF".to_owned()).await.unwrap();
Arc::clone(&storage).update_dialogue(256, "GHI".to_owned()).await.unwrap();
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;
test_dialogues!(
storage,
Some("ABC".to_owned()),
Some("DEF".to_owned()),
Some("GHI".to_owned())
);
// 1 - GKL, 11 - DEF, 256 - MNO
Arc::clone(&storage).remove_dialogue(1).await.unwrap();
Arc::clone(&storage).remove_dialogue(11).await.unwrap();
Arc::clone(&storage).remove_dialogue(256).await.unwrap();
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())
test_dialogues!(storage, None, None, None);
}