From aa93591009f2b8495185fd54e1f5c53f6f8f96dc Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Tue, 17 Sep 2024 15:55:01 +0200 Subject: [PATCH] test: improve testing for api error response Using the destructuring might mistakenly result in a wrong output. This could be misleading. For example an error code of 500 would result in the panic message while its still an API Error. This moves the assertions to `assert_eq` and only destructures the `Error::Api` --- src/client_reqwest.rs | 16 ++++++---------- src/client_ureq.rs | 16 ++++++---------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/client_reqwest.rs b/src/client_reqwest.rs index c8a892c..812a9bd 100644 --- a/src/client_reqwest.rs +++ b/src/client_reqwest.rs @@ -145,7 +145,6 @@ impl AsyncTelegramApi for AsyncApi { mod async_tests { use super::*; use crate::api_params::SendMessageParams; - use crate::response::ErrorResponse; #[tokio::test] async fn async_send_message_success() { @@ -186,16 +185,13 @@ mod async_tests { .await; let api = AsyncApi::new_url(server.url()); - if let Err(Error::Api(ErrorResponse { - ok: false, - description, - error_code: 400, - parameters: None, - })) = api.send_message(¶ms).await - { - assert_eq!("Bad Request: chat not found".to_string(), description); + if let Err(Error::Api(error)) = dbg!(api.send_message(¶ms).await) { + assert_eq!(error.description, "Bad Request: chat not found"); + assert_eq!(error.error_code, 400); + assert_eq!(error.parameters, None); + assert!(!error.ok); } else { - panic!("Error was expected but there is none"); + panic!("API Error expected"); } } } diff --git a/src/client_ureq.rs b/src/client_ureq.rs index 8dbc4b5..14ebc18 100644 --- a/src/client_ureq.rs +++ b/src/client_ureq.rs @@ -216,7 +216,6 @@ mod tests { use crate::objects::ChatPermissions; use crate::objects::InlineQueryResultVenue; use crate::objects::InputPollOption; - use crate::response::ErrorResponse; #[test] fn new_sets_correct_url() { @@ -289,16 +288,13 @@ mod tests { .create(); let api = Api::new_url(server.url()); - if let Err(Error::Api(ErrorResponse { - ok: false, - description, - error_code: 400, - parameters: None, - })) = api.send_message(¶ms) - { - assert_eq!("Bad Request: chat not found".to_string(), description); + if let Err(Error::Api(error)) = dbg!(api.send_message(¶ms)) { + assert_eq!(error.description, "Bad Request: chat not found"); + assert_eq!(error.error_code, 400); + assert_eq!(error.parameters, None); + assert!(!error.ok); } else { - panic!("Error was expected but there is none"); + panic!("API Error expected"); } }