Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve testing for api error response #216

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/client_reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(&params).await
{
assert_eq!("Bad Request: chat not found".to_string(), description);
if let Err(Error::Api(error)) = dbg!(api.send_message(&params).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");
}
}
}
16 changes: 6 additions & 10 deletions src/client_ureq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(&params)
{
assert_eq!("Bad Request: chat not found".to_string(), description);
if let Err(Error::Api(error)) = dbg!(api.send_message(&params)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think dbg should be removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its neat to have: when something fails the dbg output will be on the output too which helps to understand the full return value. At first, I thought to only have it on the else case, but it might also be useful when some assert fails.

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");
}
}

Expand Down