tests: add tests for APIResult
This commit is contained in:
parent
d45e7a8443
commit
547b659a97
4 changed files with 126 additions and 2 deletions
|
@ -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"
|
||||
|
|
|
@ -3,12 +3,19 @@ use kxio::{net::Response, print::Printer};
|
|||
|
||||
use crate::{e, s};
|
||||
|
||||
pub(crate) struct APIResult<T> {
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct APIResult<T>
|
||||
where
|
||||
T: for<'a> serde::Deserialize<'a>,
|
||||
{
|
||||
pub(crate) text: String,
|
||||
pub(crate) result: Result<T, kxio::net::Error>,
|
||||
}
|
||||
|
||||
impl<T: for<'a> serde::Deserialize<'a>> APIResult<T> {
|
||||
impl<T> APIResult<T>
|
||||
where
|
||||
T: for<'a> serde::Deserialize<'a>,
|
||||
{
|
||||
pub async fn new(response: kxio::net::Result<Response>, prt: &Printer) -> Self {
|
||||
match response {
|
||||
Ok(response) => {
|
||||
|
|
114
src/tests/api_result.rs
Normal file
114
src/tests/api_result.rs
Normal file
|
@ -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<TestData> = 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<TestData> = 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<TestData> = 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<TestData> = 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<TestData> =
|
||||
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<TestData> =
|
||||
APIResult::new(Err(message.clone().into()), &printers.0).await;
|
||||
|
||||
let_assert!(Err(result) = result.result);
|
||||
assert_peq!(s!(result), message);
|
||||
}
|
|
@ -4,11 +4,13 @@ use std::collections::HashMap;
|
|||
// type TestResult = Result<(), Box<dyn std::error::Error>>;
|
||||
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue