From 1e40df17179438030f1b10de5739dada137279a1 Mon Sep 17 00:00:00 2001 From: "Ifiok Jr." Date: Wed, 28 Aug 2024 14:36:10 +0100 Subject: [PATCH] test: improve coverage --- Cargo.lock | 1 + .../tests/compile/macros/absolute_file.rs | 5 ++++ .../tests/compile/macros/absolute_file.stderr | 5 ++++ crates/edgedb_codegen/tests/query.rs | 6 ++--- crates/edgedb_codegen_core/Cargo.toml | 4 +++ crates/edgedb_codegen_core/src/utils.rs | 27 +++++++++++++++++++ 6 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 crates/edgedb_codegen/tests/compile/macros/absolute_file.rs create mode 100644 crates/edgedb_codegen/tests/compile/macros/absolute_file.stderr diff --git a/Cargo.lock b/Cargo.lock index 4f690fe..fcf5ac3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -569,6 +569,7 @@ dependencies = [ name = "edgedb_codegen_core" version = "0.1.2" dependencies = [ + "assert2", "check_keyword", "edgedb-errors", "edgedb-protocol", diff --git a/crates/edgedb_codegen/tests/compile/macros/absolute_file.rs b/crates/edgedb_codegen/tests/compile/macros/absolute_file.rs new file mode 100644 index 0000000..b2fdf90 --- /dev/null +++ b/crates/edgedb_codegen/tests/compile/macros/absolute_file.rs @@ -0,0 +1,5 @@ +use edgedb_codegen_macros::edgedb_query_raw; + +fn main() { + edgedb_query_raw!(insert_user, file: "/absolute/path/to/queries/insert_user.edgeql"); +} diff --git a/crates/edgedb_codegen/tests/compile/macros/absolute_file.stderr b/crates/edgedb_codegen/tests/compile/macros/absolute_file.stderr new file mode 100644 index 0000000..c786a47 --- /dev/null +++ b/crates/edgedb_codegen/tests/compile/macros/absolute_file.stderr @@ -0,0 +1,5 @@ +error: absolute paths will only work on the current machine + --> tests/compile/macros/absolute_file.rs:4:39 + | +4 | edgedb_query_raw!(insert_user, file: "/absolute/path/to/queries/insert_user.edgeql"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/crates/edgedb_codegen/tests/query.rs b/crates/edgedb_codegen/tests/query.rs index 1e09db6..c3a43bc 100644 --- a/crates/edgedb_codegen/tests/query.rs +++ b/crates/edgedb_codegen/tests/query.rs @@ -15,9 +15,9 @@ edgedb_query!( #[tokio::test] pub async fn simple_query_with_input() -> Result<()> { let client = create_client().await?; - let input = simple::Input::builder() - .custom("This is a custom field") - .build(); + let input = simple::Input { + custom: String::from("This is a custom field"), + }; let output = simple::query(&client, &input).await?; insta::assert_ron_snapshot!(output, @r###" diff --git a/crates/edgedb_codegen_core/Cargo.toml b/crates/edgedb_codegen_core/Cargo.toml index 145bb9d..fa91954 100644 --- a/crates/edgedb_codegen_core/Cargo.toml +++ b/crates/edgedb_codegen_core/Cargo.toml @@ -27,6 +27,10 @@ thiserror = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread", "process"] } typed-builder = { workspace = true } +[dev-dependencies] +assert2 = { workspace = true } +tokio = { workspace = true, features = ["time", "test-util"] } + [features] with_bigint = [] with_bigdecimal = [] diff --git a/crates/edgedb_codegen_core/src/utils.rs b/crates/edgedb_codegen_core/src/utils.rs index 5225d0e..6673455 100644 --- a/crates/edgedb_codegen_core/src/utils.rs +++ b/crates/edgedb_codegen_core/src/utils.rs @@ -69,3 +69,30 @@ pub async fn rustfmt(source: &str) -> Result { pub fn prettify(source: &str) -> syn::Result { Ok(prettyplease::unparse(&syn::parse_str(source)?)) } + +#[cfg(test)] +mod tests { + use assert2::check; + + use super::*; + + #[tokio::test] + async fn can_format_file() -> Result<()> { + let content = "struct Foo { content: String, allowed: bool, times: u64 }"; + let formatted = rustfmt(content).await?; + + // formatting changes based on the version of rust used, so can't check for + // exact output + check!(formatted != content); + + Ok(()) + } + + #[tokio::test] + async fn error_when_formatting_invalid_rust() { + let content = "struct Foo { content: String, allowed: bool, times: u64,,,,, INVALID}"; + let result = rustfmt(content).await; + + check!(result.is_err(), "result should be an error"); + } +}