#![cfg(test)]
use super::parse_schema;
fn expect_rejected(schema: &str, expected_name: &str) {
let err =
parse_schema(schema).expect_err("touch-flag collision must be rejected at parse time");
let message = err.to_string();
assert!(
message.contains("generated identifier"),
"expected a touch-flag collision diagnostic, got: {message}"
);
assert!(
message.contains(expected_name),
"diagnostic must name `{expected_name}`, got: {message}"
);
}
#[test]
fn nullable_field_rejected_alongside_its_own_touch_flag_name() {
expect_rejected(
r#"
model Widget {
id Int @id
weight Int?
weightIsSet Boolean
}
"#,
"weightIsSet",
);
}
#[test]
fn touch_flag_named_field_beside_non_nullable_field_of_same_base_name_is_accepted() {
let schema = r#"
model Widget {
id Int @id
weight Int
weightIsSet Boolean
}
"#;
assert!(
parse_schema(schema).is_ok(),
"`weight` is non-nullable, so it generates no `weightIsSet` touch flag at all — a field \
literally named `weightIsSet` does not collide with anything and must be accepted"
);
}
#[test]
fn touch_flag_named_field_with_no_matching_base_field_is_accepted() {
let schema = r#"
model Widget {
id Int @id
weightIsSet Boolean
}
"#;
assert!(
parse_schema(schema).is_ok(),
"`weightIsSet` has no sibling nullable `weight` field to collide with and must be accepted"
);
}
#[test]
fn nullable_primary_key_field_beside_its_touch_flag_name_is_accepted() {
let schema = r#"
model Widget {
id Int? @id
idIsSet Boolean
}
"#;
assert!(
parse_schema(schema).is_ok(),
"`id` is the primary key, excluded from `Update{{Model}}Input` entirely, so it generates \
no `idIsSet` touch flag and must be accepted"
);
}
#[test]
fn nullable_relation_field_beside_its_touch_flag_name_is_accepted() {
let schema = r#"
model Author {
id Int @id
name String
}
model Post {
id Int @id
title String
authorId Int?
author Author? @relation(fields:[authorId],references:[id])
authorIsSet Boolean
}
"#;
assert!(
parse_schema(schema).is_ok(),
"`author` is a relation field, dropped from `UpdatePostInput` entirely, so it generates \
no `authorIsSet` touch flag and must be accepted"
);
}