#![allow(clippy::unwrap_used, clippy::panic, clippy::print_stderr)] use fraiseql_core::federation::{
mutation_query_builder::{build_insert_query, build_update_query},
types::FederationMetadata,
};
use serde_json::json;
use super::common;
#[test]
fn test_mutation_entity_not_found() {
let metadata = common::metadata_single_key("User", "id");
let variables = json!({
"id": "nonexistent_user",
"name": "Updated"
});
let query = build_update_query("User", &variables, &metadata).unwrap();
assert!(
query.contains("WHERE \"id\" = 'nonexistent_user'"),
"Expected quoted column in: {query}"
);
}
#[test]
fn test_mutation_invalid_field_value() {
let metadata = FederationMetadata::default();
let invalid_variables = json!({
"id": "user1",
"metadata": { "nested": "object" } });
let result = build_insert_query("User", &invalid_variables, &metadata);
assert!(result.is_err(), "expected Err for nested object variable, got: {result:?}");
}
#[test]
fn test_mutation_missing_required_fields() {
let metadata = common::metadata_single_key("User", "id");
let missing_key = json!({
"name": "Updated Name"
});
let result = build_update_query("User", &missing_key, &metadata);
assert!(result.is_err(), "expected Err for missing key field, got: {result:?}");
assert!(result.unwrap_err().to_string().contains("missing"));
}
#[tokio::test]
async fn test_mutation_authorization_error() {
let metadata = common::metadata_single_key("User", "id");
let Some((_pg, executor)) =
common::pg_mutation_executor(metadata, &[("user", &["id text", "role text"])]).await
else {
eprintln!("SKIP test_mutation_authorization_error: no postgres");
return;
};
let variables = json!({
"id": "admin_user",
"role": "superadmin"
});
executor
.execute_local_mutation("User", "createUser", &variables)
.await
.unwrap_or_else(|e| panic!("seed createUser failed: {e}"));
let result = executor.execute_local_mutation("User", "updateUser", &variables).await;
result.unwrap_or_else(|e| {
panic!("execute_local_mutation(User/updateUser) authorization check failed: {e}")
});
}
#[tokio::test]
async fn test_mutation_duplicate_key_error() {
let metadata = common::metadata_single_key("User", "id");
let Some((_pg, executor)) =
common::pg_mutation_executor(metadata, &[("user", &["id text", "email text"])]).await
else {
eprintln!("SKIP test_mutation_duplicate_key_error: no postgres");
return;
};
let variables = json!({
"id": "existing_id",
"email": "test@example.com"
});
executor
.execute_local_mutation("User", "createUser", &variables)
.await
.unwrap_or_else(|e| panic!("seed createUser failed: {e}"));
let result = executor.execute_local_mutation("User", "updateUser", &variables).await;
result.unwrap_or_else(|e| {
panic!("execute_local_mutation(User/updateUser) duplicate key check failed: {e}")
});
}
#[tokio::test]
async fn test_mutation_latency_single_entity() {
let metadata = common::metadata_single_key("User", "id");
let Some((_pg, executor)) =
common::pg_mutation_executor(metadata, &[("user", &["id text", "name text"])]).await
else {
eprintln!("SKIP test_mutation_latency_single_entity: no postgres");
return;
};
let variables = json!({
"id": "user1",
"name": "Updated User"
});
executor
.execute_local_mutation("User", "createUser", &variables)
.await
.unwrap_or_else(|e| panic!("seed createUser failed: {e}"));
let result = executor.execute_local_mutation("User", "updateUser", &variables).await;
result.unwrap_or_else(|e| {
panic!("execute_local_mutation(User/updateUser) single entity failed: {e}")
});
}
#[tokio::test]
async fn test_mutation_latency_batch_updates() {
let metadata = common::metadata_single_key("User", "id");
let Some((_pg, executor)) =
common::pg_mutation_executor(metadata, &[("user", &["id text", "name text"])]).await
else {
eprintln!("SKIP test_mutation_latency_batch_updates: no postgres");
return;
};
for i in 0..10 {
let variables = json!({
"id": format!("user{}", i),
"name": format!("Updated User {}", i)
});
executor
.execute_local_mutation("User", "createUser", &variables)
.await
.unwrap_or_else(|e| panic!("seed createUser batch iteration {i} failed: {e}"));
let result = executor.execute_local_mutation("User", "updateUser", &variables).await;
result.unwrap_or_else(|e| {
panic!("execute_local_mutation(User/updateUser) batch iteration {i} failed: {e}")
});
}
}