#![allow(clippy::unwrap_used)] use fraiseql_core::federation::{
mutation_executor::FederationMutationExecutor,
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"));
}
#[test]
fn test_mutation_authorization_error() {
let mock_adapter = common::mock_mutation_adapter();
let metadata = common::metadata_single_key("User", "id");
let variables = json!({
"id": "admin_user",
"role": "superadmin"
});
let runtime = tokio::runtime::Runtime::new().unwrap();
let executor = FederationMutationExecutor::new(mock_adapter, metadata);
let result =
runtime.block_on(executor.execute_local_mutation("User", "updateUser", &variables));
result.unwrap_or_else(|e| {
panic!("execute_local_mutation(User/updateUser) authorization check failed: {e}")
});
}
#[test]
fn test_mutation_duplicate_key_error() {
let mock_adapter = common::mock_mutation_adapter();
let metadata = common::metadata_single_key("User", "id");
let variables = json!({
"id": "existing_id",
"email": "test@example.com"
});
let runtime = tokio::runtime::Runtime::new().unwrap();
let executor = FederationMutationExecutor::new(mock_adapter, metadata);
let result =
runtime.block_on(executor.execute_local_mutation("User", "updateUser", &variables));
result.unwrap_or_else(|e| {
panic!("execute_local_mutation(User/updateUser) duplicate key check failed: {e}")
});
}
#[test]
fn test_mutation_latency_single_entity() {
use std::time::Instant;
let mock_adapter = common::mock_mutation_adapter();
let metadata = common::metadata_single_key("User", "id");
let variables = json!({
"id": "user1",
"name": "Updated User"
});
let runtime = tokio::runtime::Runtime::new().unwrap();
let executor = FederationMutationExecutor::new(mock_adapter, metadata);
let start = Instant::now();
let _result =
runtime.block_on(executor.execute_local_mutation("User", "updateUser", &variables));
let duration = start.elapsed();
assert!(duration.as_millis() < 10, "Single mutation took {:?}", duration);
}
#[test]
fn test_mutation_latency_batch_updates() {
use std::time::Instant;
let mock_adapter = common::mock_mutation_adapter();
let metadata = common::metadata_single_key("User", "id");
let runtime = tokio::runtime::Runtime::new().unwrap();
let executor = FederationMutationExecutor::new(mock_adapter, metadata);
let start = Instant::now();
for i in 0..10 {
let variables = json!({
"id": format!("user{}", i),
"name": format!("Updated User {}", i)
});
let _result =
runtime.block_on(executor.execute_local_mutation("User", "updateUser", &variables));
}
let duration = start.elapsed();
assert!(duration.as_millis() < 100, "Batch mutations took {:?}", duration);
}