Skip to content

Commit

Permalink
Merge pull request #18 from rodneylab/test__update_graphql_snapshot_t…
Browse files Browse the repository at this point in the history
…esting

test: ☑️ improve snapshot testing
  • Loading branch information
rodneylab authored Nov 9, 2024
2 parents 0a36fbe + f513908 commit 142b956
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 15 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ tracing-opentelemetry = "0.27.0"
tracing-subscriber = { version = "0.3.18", features = ["std", "env-filter"] }

[dev-dependencies]
futures = "0.3.31"
http-body-util = "0.1.2"
insta = { version = "1.41.1", features = ["json"] }
insta = { version = "1.41.1", features = ["glob", "json", "redactions"] }
mime = "0.3.17"
serde_json = "1.0"
tower = { version = "0.5.1", features = ['util'] }
5 changes: 5 additions & 0 deletions src/model/snapshot_inputs/create_draft.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"operationName": "CreateDraftMutation",
"variables": {},
"query": "mutation CreateDraftMutation { createDraft(title: \"Draft title\", body: \"Draft body text\") { id title } }"
}
5 changes: 5 additions & 0 deletions src/model/snapshot_inputs/hello.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"operationName": "HelloQuery",
"variables": {},
"query": "query HelloQuery { hello }"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: src/model/tests.rs
expression: body_json
input_file: src/model/snapshot_inputs/create_draft.json
snapshot_kind: text
---
{
"data": {
"createDraft": {
"id": 1,
"title": "Draft title"
}
},
"extensions": {
"traceId": "[traceId]"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: src/model/tests.rs
expression: body_json
input_file: src/model/snapshot_inputs/hello.json
snapshot_kind: text
---
{
"data": {
"hello": "Hello everybody!"
},
"extensions": {
"traceId": "[traceId]"
}
}

This file was deleted.

35 changes: 27 additions & 8 deletions src/model/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{path::Path, str};

use axum::{
body::Body,
http::{header, Method, Request, StatusCode},
};
use futures::executor::block_on;
use http_body_util::BodyExt;
use serde_json::{json, Value};
use tower::{Service, ServiceExt};
Expand Down Expand Up @@ -102,14 +105,17 @@ mod helpers {
}
}

#[tokio::test]
async fn snapshot_hello_query() {
async fn snapshot_graqphql_query_async<P: AsRef<Path>>(path: P) {
// arrange
let app = helpers::get_app().await;
let json_request_body: Value = json!({
"operationName":"HelloQuery",
"variables":{},
"query":"query HelloQuery { hello }"
let json_request_body: Value = serde_json::from_slice(
&std::fs::read(&path).expect("file should exist and have read permissions set"),
)
.unwrap_or_else(|_| {
panic!(
"File `{}`, should contain valid JSON",
path.as_ref().display()
)
});

// act
Expand All @@ -126,8 +132,21 @@ async fn snapshot_hello_query() {
.unwrap();

// assert
let body = response.into_body().collect().await.unwrap().to_bytes();
insta::assert_debug_snapshot!(body);
let body_bytes = response.into_body().collect().await.unwrap().to_bytes();
let body = str::from_utf8(&body_bytes).unwrap();
let body_json: Value = serde_json::from_str(body).unwrap();
insta::assert_json_snapshot!(body_json, {".extensions.traceId" => "[traceId]"});
assert_eq!(
body_json["extensions"]["traceId"],
"00000000000000000000000000000000"
);
}

#[tokio::test]
async fn snapshot_graphql_queries() {
insta::glob!("snapshot_inputs/*.json", |path| {
block_on(snapshot_graqphql_query_async(path));
});
}

#[tokio::test]
Expand Down

0 comments on commit 142b956

Please sign in to comment.