From cd0ff1e8eb65032acc10a090dd2a3818dbe3bdb8 Mon Sep 17 00:00:00 2001 From: Rodney Lab Date: Fri, 1 Nov 2024 15:52:27 +0000 Subject: [PATCH] =?UTF-8?q?test:=20=E2=98=91=EF=B8=8F=20update=20model=20u?= =?UTF-8?q?nit=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/post.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/src/model/post.rs b/src/model/post.rs index adf8e5a..d99e58b 100644 --- a/src/model/post.rs +++ b/src/model/post.rs @@ -10,7 +10,7 @@ pub struct Post { published: bool, } -#[derive(SimpleObject)] +#[derive(Debug, PartialEq, SimpleObject)] /// Extra information on the input error pub struct InputError { /// Field which had the invalid data @@ -23,7 +23,7 @@ pub struct InputError { received: String, } -#[derive(SimpleObject)] +#[derive(Debug, PartialEq, SimpleObject)] pub struct DeleteDraftResponse { /// Deleted post post: Option, @@ -32,7 +32,7 @@ pub struct DeleteDraftResponse { error: Option, } -#[derive(SimpleObject)] +#[derive(Debug, PartialEq, SimpleObject)] pub struct PublishResponse { /// Published post post: Option, @@ -233,7 +233,10 @@ mod tests { use crate::{ database::run_migrations, - model::post::{create_draft_mutation, publish_mutation}, + model::post::{ + create_draft_mutation, delete_draft_mutation, publish_mutation, DeleteDraftResponse, + InputError, + }, }; use super::{posts_query, Post}; @@ -327,4 +330,61 @@ mod tests { ); assert_eq!(chain.next().map(|val| format!("{val}")), None); } + + #[tokio::test] + async fn create_draft_mutation_returns_error_message_if_draft_does_not_exist() { + // arrange + let db_pool = get_db_pool().await; + let title = String::from("New Post Title"); + let body = String::from("# New Post\nNew post body"); + let Post { id, .. } = create_draft_mutation(&db_pool, &title, &body) + .await + .unwrap(); + let _ = publish_mutation(&db_pool, id).await; + + // act + let outcome = delete_draft_mutation(&db_pool, 999).await.unwrap(); + + // assert + assert_eq!( + outcome, + DeleteDraftResponse { + post: None, + error: Some(InputError { + field: String::from("id"), + message: String::from("Did not find draft post with id `999`"), + received: String::from("999") + }) + } + ); + } + + #[tokio::test] + async fn create_draft_mutation_returns_draft_on_valid_input() { + // arrange + let db_pool = get_db_pool().await; + let title = String::from("New Post Title"); + let body = String::from("# New Post\nNew post body"); + let Post { id, .. } = create_draft_mutation(&db_pool, &title, &body) + .await + .unwrap(); + //let _ = publish_mutation(&db_pool, id).await; + + // act + let outcome = delete_draft_mutation(&db_pool, id).await.unwrap(); + + // assert + assert_eq!( + outcome, + DeleteDraftResponse { + post: Some(Post { + id, + title, + body, + published: false + }), + error: None + } + ); + } }