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 GraphQL route tests #13

Merged
merged 1 commit into from
Nov 6, 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
173 changes: 3 additions & 170 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
mod post;

#[cfg(test)]
mod tests;

use async_graphql::{Context, EmptySubscription, Object, Schema};
use sqlx::SqlitePool;

Expand Down Expand Up @@ -83,173 +86,3 @@ impl MutationRoot {
publish_mutation(db_pool, id).await
}
}

#[cfg(test)]
mod tests {
use axum::{
body::Body,
http::{header, Method, Request, StatusCode},
Router,
};
use http_body_util::BodyExt;
use serde_json::{json, Value};
use sqlx::sqlite::SqlitePoolOptions;
use tower::util::ServiceExt;

use crate::main_app;

async fn get_app() -> Router {
let database_url = "sqlite://:memory:";
let app = main_app(database_url).await;

let db_pool = SqlitePoolOptions::new()
.max_connections(1)
.connect(database_url)
.await
.unwrap();

app.with_state(db_pool)
}

#[tokio::test]
async fn graphql_endpoint_responds_to_hello_query() {
// arrange
let app = get_app().await;
let json_request_body: Value = json!({
"operationName":"HelloQuery",
"variables":{},
"query":"query HelloQuery { hello }"
});

// act
let response = app
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(json_request_body.to_string()))
.unwrap(),
)
.await
.unwrap();

// assert
assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(
body,
json!({
"data": { "hello": "Hello everybody!" },
"extensions": {"traceId": "00000000000000000000000000000000"}
})
);
}

#[tokio::test]
async fn graphql_endpoint_responds_to_drafts_query() {
// arrange
let app = get_app().await;
let drafts_json_request_body: Value = json!({
"operationName":"DraftsQuery",
"variables":{},
"query":"query DraftsQuery { drafts { id title} }"
});

// act
let response = app
.clone()
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(drafts_json_request_body.to_string()))
.unwrap(),
)
.await
.unwrap();

// assert
assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(
body,
json!({
"data": { "drafts": [] },
"extensions": {"traceId": "00000000000000000000000000000000"}
})
);

// arrange
let create_draft_json_request_body: Value = json!({
"operationName":"CreateDraftMutation",
"variables":{},
"query": r#"mutation CreateDraftMutation {
createDraft(title: "Draft title", body: "Draft body text") {
id
title
}
}"#,
});

// act
let response = app
.clone()
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(create_draft_json_request_body.to_string()))
.unwrap(),
)
.await
.unwrap();

// assert
assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(
body,
json!({
"data": { "createDraft": { "id": 1, "title": "Draft title" } },
"extensions": {"traceId": "00000000000000000000000000000000"}
})
);

// arrange
// act
let response = app
.clone()
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(drafts_json_request_body.to_string()))
.unwrap(),
)
.await
.unwrap();

// assert
assert_eq!(response.status(), StatusCode::OK);

let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(
body,
json!({
"data": { "drafts": [ { "id": 1, "title": "Draft title" } ] },
"extensions": {"traceId": "00000000000000000000000000000000"}
})
);
}
}
11 changes: 6 additions & 5 deletions src/model/post.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use anyhow::Context;
use async_graphql::{Interface, SimpleObject, Union};
use serde::Deserialize;
use sqlx::SqlitePool;

#[derive(Debug, PartialEq, SimpleObject)]
#[derive(Debug, Deserialize, PartialEq, SimpleObject)]
pub struct Post {
id: i64,
title: String,
body: String,
published: bool,
pub id: i64,
pub title: String,
pub body: String,
pub published: bool,
}

#[derive(Debug, PartialEq, SimpleObject)]
Expand Down
Loading
Loading