Skip to content

Commit

Permalink
Merge pull request #9 from rodneylab/test__update_model_unit_tests
Browse files Browse the repository at this point in the history
test: ☑️ update model unit tests
  • Loading branch information
rodneylab authored Nov 1, 2024
2 parents 5729716 + cd0ff1e commit 9e345a9
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions src/model/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,7 @@ pub struct InputError {
received: String,
}

#[derive(SimpleObject)]
#[derive(Debug, PartialEq, SimpleObject)]
pub struct DeleteDraftResponse {
/// Deleted post
post: Option<Post>,
Expand All @@ -32,7 +32,7 @@ pub struct DeleteDraftResponse {
error: Option<InputError>,
}

#[derive(SimpleObject)]
#[derive(Debug, PartialEq, SimpleObject)]
pub struct PublishResponse {
/// Published post
post: Option<Post>,
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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
}
);
}
}

0 comments on commit 9e345a9

Please sign in to comment.