mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-20 13:59:00 +01:00
Fix the tests and examples
This commit is contained in:
parent
01b7b91bda
commit
9b75378572
9 changed files with 54 additions and 45 deletions
|
@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Do not return a dialogue from `Storage::{remove_dialogue, update_dialogue}`.
|
- Do not return a dialogue from `Storage::{remove_dialogue, update_dialogue}`.
|
||||||
|
- Require `D: ToOwned<Owned = D>` in `dialogues_repl` and `InMemStorage`.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ use crate::dialogue::states::{
|
||||||
use derive_more::From;
|
use derive_more::From;
|
||||||
use teloxide::macros::Transition;
|
use teloxide::macros::Transition;
|
||||||
|
|
||||||
#[derive(Transition, From)]
|
#[derive(Transition, Clone, From)]
|
||||||
pub enum Dialogue {
|
pub enum Dialogue {
|
||||||
Start(StartState),
|
Start(StartState),
|
||||||
ReceiveFullName(ReceiveFullNameState),
|
ReceiveFullName(ReceiveFullNameState),
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::dialogue::{states::receive_location::ReceiveLocationState, Dialogue};
|
use crate::dialogue::{states::receive_location::ReceiveLocationState, Dialogue};
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
#[derive(Generic)]
|
#[derive(Clone, Generic)]
|
||||||
pub struct ReceiveAgeState {
|
pub struct ReceiveAgeState {
|
||||||
pub full_name: String,
|
pub full_name: String,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::dialogue::{states::receive_age::ReceiveAgeState, Dialogue};
|
use crate::dialogue::{states::receive_age::ReceiveAgeState, Dialogue};
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
#[derive(Generic)]
|
#[derive(Clone, Generic)]
|
||||||
pub struct ReceiveFullNameState;
|
pub struct ReceiveFullNameState;
|
||||||
|
|
||||||
#[teloxide(subtransition)]
|
#[teloxide(subtransition)]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::dialogue::Dialogue;
|
use crate::dialogue::Dialogue;
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
#[derive(Generic)]
|
#[derive(Clone, Generic)]
|
||||||
pub struct ReceiveLocationState {
|
pub struct ReceiveLocationState {
|
||||||
pub full_name: String,
|
pub full_name: String,
|
||||||
pub age: u8,
|
pub age: u8,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::dialogue::{states::ReceiveFullNameState, Dialogue};
|
use crate::dialogue::{states::ReceiveFullNameState, Dialogue};
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct StartState;
|
pub struct StartState;
|
||||||
|
|
||||||
#[teloxide(subtransition)]
|
#[teloxide(subtransition)]
|
||||||
|
|
|
@ -35,8 +35,11 @@
|
||||||
//!
|
//!
|
||||||
//! use teloxide::{dispatching::dialogue::Transition, prelude::*, teloxide, RequestError};
|
//! use teloxide::{dispatching::dialogue::Transition, prelude::*, teloxide, RequestError};
|
||||||
//!
|
//!
|
||||||
|
//! #[derive(Clone)]
|
||||||
//! struct _1State;
|
//! struct _1State;
|
||||||
|
//! #[derive(Clone)]
|
||||||
//! struct _2State;
|
//! struct _2State;
|
||||||
|
//! #[derive(Clone)]
|
||||||
//! struct _3State;
|
//! struct _3State;
|
||||||
//!
|
//!
|
||||||
//! type Out = TransitionOut<D, RequestError>;
|
//! type Out = TransitionOut<D, RequestError>;
|
||||||
|
@ -56,7 +59,7 @@
|
||||||
//! todo!()
|
//! todo!()
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! #[derive(Transition)]
|
//! #[derive(Clone, Transition)]
|
||||||
//! enum D {
|
//! enum D {
|
||||||
//! _1(_1State),
|
//! _1(_1State),
|
||||||
//! _2(_2State),
|
//! _2(_2State),
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
future::Future,
|
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
use teloxide::dispatching::dialogue::{RedisStorage, Serializer, Storage};
|
use teloxide::dispatching::dialogue::{RedisStorage, Serializer, Storage};
|
||||||
|
@ -40,32 +39,35 @@ async fn test_redis_cbor() {
|
||||||
|
|
||||||
type Dialogue = String;
|
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>>)
|
async fn test_redis<S>(storage: Arc<RedisStorage<S>>)
|
||||||
where
|
where
|
||||||
S: Send + Sync + Serializer<Dialogue> + 'static,
|
S: Send + Sync + Serializer<Dialogue> + 'static,
|
||||||
<S as Serializer<Dialogue>>::Error: Debug + Display,
|
<S as Serializer<Dialogue>>::Error: Debug + Display,
|
||||||
{
|
{
|
||||||
check_dialogue(None, Arc::clone(&storage).update_dialogue(1, "ABC".to_owned())).await;
|
test_dialogues!(storage, None, None, None);
|
||||||
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
|
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;
|
test_dialogues!(
|
||||||
check_dialogue("GHI", Arc::clone(&storage).update_dialogue(256, "MNO".to_owned())).await;
|
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;
|
test_dialogues!(storage, None, None, None);
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
future::Future,
|
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
use teloxide::dispatching::dialogue::{Serializer, SqliteStorage, Storage};
|
use teloxide::dispatching::dialogue::{Serializer, SqliteStorage, Storage};
|
||||||
|
@ -36,32 +35,35 @@ async fn test_sqlite_cbor() {
|
||||||
|
|
||||||
type Dialogue = String;
|
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>>)
|
async fn test_sqlite<S>(storage: Arc<SqliteStorage<S>>)
|
||||||
where
|
where
|
||||||
S: Send + Sync + Serializer<Dialogue> + 'static,
|
S: Send + Sync + Serializer<Dialogue> + 'static,
|
||||||
<S as Serializer<Dialogue>>::Error: Debug + Display,
|
<S as Serializer<Dialogue>>::Error: Debug + Display,
|
||||||
{
|
{
|
||||||
check_dialogue(None, Arc::clone(&storage).update_dialogue(1, "ABC".to_owned())).await;
|
test_dialogues!(storage, None, None, None);
|
||||||
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
|
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;
|
test_dialogues!(
|
||||||
check_dialogue("GHI", Arc::clone(&storage).update_dialogue(256, "MNO".to_owned())).await;
|
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;
|
test_dialogues!(storage, None, None, None);
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue