Skip to content

Commit

Permalink
test(quaint): add explicit regression test for issue prisma/prisma#24072
Browse files Browse the repository at this point in the history
, via new macro "assert_connector_error"
  • Loading branch information
jkomyno committed Sep 23, 2024
1 parent 7034e0a commit a7d4f00
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ macro_rules! match_connector_result {
};
}

#[macro_export]
macro_rules! assert_connector_error {
($runner:expr, $q:expr, $code:expr, $( $($matcher:pat_param)|+ $( if $pred:expr )? => $msg:expr ),*) => {
use query_tests_setup::*;
use query_tests_setup::ConnectorVersion::*;

let connector = $runner.connector_version();

let mut results = match &connector {
$(
$( $matcher )|+ $( if $pred )? => $msg.to_string()
),*
};

if results.len() == 0 {
panic!("No assertion failure defined for connector {connector}.");
}

$runner.query($q).await?.assert_failure($code, Some(results));
};
}

#[macro_export]
macro_rules! is_one_of {
($result:expr, $potential_results:expr) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ mod one2one_opt {
runner,
"mutation { deleteOneParent(where: { id: 1 }) { id }}",
2003,
"Foreign key constraint violated: `Child_parent_id_fkey (index)`"
"Foreign key constraint violated"
);

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod prisma_21901;
mod prisma_22007;
mod prisma_22298;
mod prisma_22971;
mod prisma_24072;
mod prisma_5952;
mod prisma_6173;
mod prisma_7010;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use indoc::indoc;
use query_engine_tests::*;

#[test_suite(schema(schema))]
mod prisma_24072 {
fn schema() -> String {
let schema = indoc! {
r#"model Parent {
#id(id, Int, @id)
child Child?
}
model Child {
#id(id, Int, @id)
parent_id Int? @default(2) @unique
parent Parent? @relation(fields: [parent_id], references: [id], onDelete: SetDefault)
}"#
};

schema.to_owned()
}

// Deleting the parent without cascading to the child should fail with an explicitly named constraint violation,
// without any "(not available)" names.
#[connector_test]
async fn test_24072(runner: Runner) -> TestResult<()> {
insta::assert_snapshot!(
run_query!(&runner, r#"mutation { createOneParent(data: { id: 1, child: { create: { id: 1 }}}) { id }}"#),
@r###"{"data":{"createOneParent":{"id":1}}}"###
);

assert_connector_error!(
&runner,
"mutation { deleteOneParent(where: { id: 1 }) { id }}",
2003,
CockroachDb(_) | Postgres(_) | SqlServer(_) | Vitess(_) => "Foreign key constraint violated: `Child_parent_id_fkey (index)`",
MySql(_) => "Foreign key constraint violated: `parent_id`",
Sqlite(_) => "Foreign key constraint violated: `foreign key`",
_ => "Foreign key constraint violated"
);

Ok(())
}
}

0 comments on commit a7d4f00

Please sign in to comment.