From f0b7681cb3a3668329bfee196b5b00fdd734ff94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=8B=D1=80=D1=86=D0=B5=D0=B2=20=D0=92=D0=B0=D0=B4?= =?UTF-8?q?=D0=B8=D0=BC=20=D0=98=D0=B3=D0=BE=D1=80=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sun, 21 Jan 2024 11:38:26 +0300 Subject: [PATCH] Add PostgresStorage tests --- crates/teloxide/Cargo.toml | 4 ++ crates/teloxide/tests/postgres.rs | 91 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 crates/teloxide/tests/postgres.rs diff --git a/crates/teloxide/Cargo.toml b/crates/teloxide/Cargo.toml index f19ea988..c17e1eb0 100644 --- a/crates/teloxide/Cargo.toml +++ b/crates/teloxide/Cargo.toml @@ -152,6 +152,10 @@ name = "sqlite" path = "tests/sqlite.rs" required-features = ["sqlite-storage-nativetls", "cbor-serializer", "bincode-serializer"] +[[test]] +name = "postgres" +path = "tests/postgres.rs" +required-features = ["postgres-storage-nativetls", "cbor-serializer", "bincode-serializer"] [[example]] name = "admin" diff --git a/crates/teloxide/tests/postgres.rs b/crates/teloxide/tests/postgres.rs new file mode 100644 index 00000000..cbcab064 --- /dev/null +++ b/crates/teloxide/tests/postgres.rs @@ -0,0 +1,91 @@ +use std::{ + fmt::{Debug, Display}, + sync::Arc, +}; +use teloxide::{ + dispatching::dialogue::{PostgresStorage, PostgresStorageError, Serializer, Storage}, + types::ChatId, +}; + +#[tokio::test] +#[cfg_attr(not(CI_POSTGRES), ignore)] +async fn test_postgres_json() { + let storage = PostgresStorage::open( + "postgres://teloxide:rewrite_it_in_rust@localhost:5432/test_postgres_json", + 4, + teloxide::dispatching::dialogue::serializer::Json, + ) + .await + .unwrap(); + + test_postgres(storage).await; +} + +#[tokio::test] +#[cfg_attr(not(CI_POSTGRES), ignore)] +async fn test_postgres_bincode() { + let storage = PostgresStorage::open( + "postgres://teloxide:rewrite_it_in_rust@localhost:5432/test_postgres_bincode", + 4, + teloxide::dispatching::dialogue::serializer::Bincode, + ) + .await + .unwrap(); + + test_postgres(storage).await; +} + +#[tokio::test] +#[cfg_attr(not(CI_POSTGRES), ignore)] +async fn test_postgres_cbor() { + let storage = PostgresStorage::open( + "postgres://teloxide:rewrite_it_in_rust@localhost:5432/test_postgres_cbor", + 4, + teloxide::dispatching::dialogue::serializer::Cbor, + ) + .await + .unwrap(); + + test_postgres(storage).await; +} + +type Dialogue = String; + +macro_rules! test_dialogues { + ($storage:expr, $_0:expr, $_1:expr, $_2:expr) => { + 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); + }; +} + +async fn test_postgres(storage: Arc>) +where + S: Send + Sync + Serializer + 'static, + >::Error: Debug + Display, +{ + test_dialogues!(storage, None, None, None); + + 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(); + + test_dialogues!( + storage, + Some("ABC".to_owned()), + Some("DEF".to_owned()), + Some("GHI".to_owned()) + ); + + 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(); + + test_dialogues!(storage, None, None, None); + + // Check that a try to remove a non-existing dialogue results in an error. + assert!(matches!( + Arc::clone(&storage).remove_dialogue(ChatId(1)).await.unwrap_err(), + PostgresStorageError::DialogueNotFound + )); +}