diff --git a/Cargo.toml b/Cargo.toml index 3406b12..6938415 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ tracing-subscriber = "0.3" [dev-dependencies] assert2 = "0.3" +http = "1.2" mutants = "0.0" pretty_assertions = "1.4" rstest = "0.23" diff --git a/src/api_result.rs b/src/api_result.rs index bf9ca53..dcedca1 100644 --- a/src/api_result.rs +++ b/src/api_result.rs @@ -3,12 +3,19 @@ use kxio::{net::Response, print::Printer}; use crate::{e, s}; -pub(crate) struct APIResult { +#[derive(Debug)] +pub(crate) struct APIResult +where + T: for<'a> serde::Deserialize<'a>, +{ pub(crate) text: String, pub(crate) result: Result, } -impl serde::Deserialize<'a>> APIResult { +impl APIResult +where + T: for<'a> serde::Deserialize<'a>, +{ pub async fn new(response: kxio::net::Result, prt: &Printer) -> Self { match response { Ok(response) => { diff --git a/src/tests/api_result.rs b/src/tests/api_result.rs new file mode 100644 index 0000000..a7581d6 --- /dev/null +++ b/src/tests/api_result.rs @@ -0,0 +1,114 @@ +use http::response::Builder; +use serde::Deserialize; +// +use super::*; + +use serde_json::json; + +use crate::api_result::APIResult; + +#[rstest::fixture] +fn printers() -> (kxio::print::Printer, kxio::print::TestPrint) { + let prt = kxio::print::test(); + let test_prt = prt.clone(); + let test_prt = test_prt.as_test().unwrap().clone(); + (prt, test_prt) +} + +#[derive(Deserialize, Debug, PartialEq, Eq)] +struct TestData { + foo: String, +} + +#[rstest::rstest] +#[tokio::test] +async fn test_result_ok_parsable_text(printers: (kxio::print::Printer, kxio::print::TestPrint)) { + #[derive(Deserialize, Debug, PartialEq, Eq)] + struct TestData { + foo: String, + } + let json_doc: String = s!(json!({"foo":"bar"})); + let response = Builder::default().body(json_doc.clone()).expect("response"); + let result: APIResult = APIResult::new(Ok(response.into()), &printers.0).await; + + assert_peq!(result.text, json_doc); +} + +#[rstest::rstest] +#[tokio::test] +async fn test_result_ok_parsable_result(printers: (kxio::print::Printer, kxio::print::TestPrint)) { + #[derive(Deserialize, Debug, PartialEq, Eq)] + struct TestData { + foo: String, + } + let json_doc: String = s!(json!({"foo":"bar"})); + let response = Builder::default().body(json_doc.clone()).expect("response"); + let result: APIResult = APIResult::new(Ok(response.into()), &printers.0).await; + + let_assert!(Ok(result) = result.result); + assert_peq!(result, TestData { foo: s!("bar") }); +} + +#[rstest::rstest] +#[tokio::test] +async fn test_result_ok_unparsable_text(printers: (kxio::print::Printer, kxio::print::TestPrint)) { + #[derive(Deserialize, Debug, PartialEq, Eq)] + struct TestData { + foo: String, + } + let invalid_doc: String = s!("foo-bar"); + let response = Builder::default() + .body(invalid_doc.clone()) + .expect("response"); + let result: APIResult = APIResult::new(Ok(response.into()), &printers.0).await; + + assert_peq!(result.text, invalid_doc); +} + +#[rstest::rstest] +#[tokio::test] +async fn test_result_ok_unparsable_result( + printers: (kxio::print::Printer, kxio::print::TestPrint), +) { + #[derive(Deserialize, Debug, PartialEq, Eq)] + struct TestData { + foo: String, + } + let invalid_doc: String = s!("foo-bar"); + let response = Builder::default() + .body(invalid_doc.clone()) + .expect("response"); + let result: APIResult = APIResult::new(Ok(response.into()), &printers.0).await; + + let_assert!(Err(result) = result.result); + assert_eq!(s!(result), s!("expected ident at line 1 column 2")); +} + +#[rstest::rstest] +#[tokio::test] +async fn test_result_error_text(printers: (kxio::print::Printer, kxio::print::TestPrint)) { + #[derive(Deserialize, Debug, PartialEq, Eq)] + struct TestData { + foo: String, + } + let message = s!("error message"); + let result: APIResult = + APIResult::new(Err(message.clone().into()), &printers.0).await; + + assert_peq!(result.text, s!("")); +} + +#[rstest::rstest] +#[tokio::test] +async fn test_result_error_result(printers: (kxio::print::Printer, kxio::print::TestPrint)) { + #[derive(Deserialize, Debug, PartialEq, Eq)] + struct TestData { + foo: String, + } + let message = s!("error message"); + let result: APIResult = + APIResult::new(Err(message.clone().into()), &printers.0).await; + + let_assert!(Err(result) = result.result); + assert_peq!(s!(result), message); +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs index b9bc9e9..6d1fc62 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -4,11 +4,13 @@ use std::collections::HashMap; // type TestResult = Result<(), Box>; use assert2::let_assert; +use pretty_assertions::assert_eq as assert_peq; use crate::{ config::AppConfig, f, init::run, nextcloud::NextcloudConfig, s, trello::TrelloConfig, Ctx, NAME, }; +mod api_result; mod config; pub(crate) mod given; mod init;