From 5c956c92cce5d71e4be8aa5acddfaea065c48adc Mon Sep 17 00:00:00 2001 From: Rodney Lab Date: Fri, 8 Nov 2024 18:10:55 +0000 Subject: [PATCH] =?UTF-8?q?test:=20=E2=98=91=EF=B8=8F=20add=20snapshot=20t?= =?UTF-8?q?esting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .ignore | 3 ++ Cargo.lock | 44 +++++++++++++++++++ Cargo.toml | 9 ++++ ...l__model__tests__snapshot_hello_query.snap | 6 +++ src/model/tests.rs | 28 ++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 .ignore create mode 100644 src/model/snapshots/axum_graphql__model__tests__snapshot_hello_query.snap diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..3186de5 --- /dev/null +++ b/.ignore @@ -0,0 +1,3 @@ +# files listed here will be ignored by cargo watch, it also ignores .gitignore files. Other tools migth use this file. Add files to .gitignore instead, if cargo watch should ignore them and they don't need version control. +**/*.snap +**/*.snap.new diff --git a/Cargo.lock b/Cargo.lock index 4f7cc03..6dba656 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -337,6 +337,7 @@ dependencies = [ "axum", "dotenvy", "http-body-util", + "insta", "metrics", "metrics-exporter-prometheus", "mime", @@ -520,6 +521,18 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -705,6 +718,12 @@ dependencies = [ "serde", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -1339,6 +1358,19 @@ dependencies = [ "serde", ] +[[package]] +name = "insta" +version = "1.41.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "serde", + "similar", +] + [[package]] name = "ipnet" version = "2.10.1" @@ -1435,6 +1467,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -2381,6 +2419,12 @@ dependencies = [ "rand_core", ] +[[package]] +name = "similar" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" + [[package]] name = "sketches-ddsketch" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 045b9fa..c233a14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,14 @@ repository = "https://github.com/rodneylab/axum-graphql" rust-version = "1.70.0" description = "Rust GraphQL demo/test API written in Rust, using Axum for routing, async-graphql and SQLx." +# Faster snapshot runs +# See: https://docs.rs/insta/latest/insta/#optional-faster-runs +[profile.dev.package.insta] +opt-level = 3 + +[profile.dev.package.similar] +opt-level = 3 + # Faster cargo check and cargo build # See: https://github.com/launchbadge/sqlx#compile-time-verification [profile.dev.package.sqlx-macros] @@ -42,6 +50,7 @@ tracing-subscriber = { version = "0.3.18", features = ["std", "env-filter"] } [dev-dependencies] http-body-util = "0.1.2" +insta = { version = "1.41.1", features = ["json"] } mime = "0.3.17" serde_json = "1.0" tower = { version = "0.5.1", features = ['util'] } diff --git a/src/model/snapshots/axum_graphql__model__tests__snapshot_hello_query.snap b/src/model/snapshots/axum_graphql__model__tests__snapshot_hello_query.snap new file mode 100644 index 0000000..c1e6799 --- /dev/null +++ b/src/model/snapshots/axum_graphql__model__tests__snapshot_hello_query.snap @@ -0,0 +1,6 @@ +--- +source: src/model/tests.rs +expression: body +snapshot_kind: text +--- +b"{\"data\":{\"hello\":\"Hello everybody!\"},\"extensions\":{\"traceId\":\"00000000000000000000000000000000\"}}" diff --git a/src/model/tests.rs b/src/model/tests.rs index ab724a7..5f8044e 100644 --- a/src/model/tests.rs +++ b/src/model/tests.rs @@ -102,6 +102,34 @@ mod helpers { } } +#[tokio::test] +async fn snapshot_hello_query() { + // arrange + let app = helpers::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 + let body = response.into_body().collect().await.unwrap().to_bytes(); + insta::assert_debug_snapshot!(body); +} + #[tokio::test] async fn graphql_endpoint_responds_to_hello_query() { // arrange