use aurora_db::error::ErrorCode;
use aurora_db::{Aurora, AuroraConfig};
use serde_json::json;
use tempfile::TempDir;
#[tokio::test]
async fn test_required_variable_validation() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let config = AuroraConfig {
db_path,
enable_write_buffering: false,
durability_mode: aurora_db::types::DurabilityMode::Synchronous,
..Default::default()
};
let db = Aurora::with_config(config).await.unwrap();
db.new_collection(
"users",
vec![
(
"name".to_string(),
aurora_db::types::FieldType::SCALAR_STRING,
false,
),
(
"email".to_string(),
aurora_db::types::FieldType::SCALAR_STRING,
false,
),
],
)
.await
.unwrap();
let query_with_required = r#"
query GetUser($name: String!) {
users(where: { name: { eq: $name } }) {
name
email
}
}
"#;
let result = db.execute((query_with_required, json!({}))).await;
assert!(
result.is_err(),
"Query should fail when required variable is missing"
);
let err = result.unwrap_err();
assert_eq!(err.code, ErrorCode::UndefinedVariable);
assert!(
err.message.contains("name"),
"Error should mention the missing variable name"
);
assert!(
err.message.contains("String!"),
"Error should show the required type"
);
let result = db
.execute((query_with_required, json!({ "name": "Alice" })))
.await;
assert!(
result.is_ok(),
"Query should succeed when required variable is provided"
);
let query_with_optional = r#"
query GetUsers($limit: Int) {
users(limit: $limit) {
name
}
}
"#;
let result = db.execute((query_with_optional, json!({}))).await;
assert!(
result.is_ok(),
"Query should succeed when optional variable is missing"
);
let query_with_default = r#"
query GetUsers($limit: Int = 10) {
users(limit: $limit) {
name
}
}
"#;
let result = db.execute((query_with_default, json!({}))).await;
assert!(
result.is_ok(),
"Query should succeed when variable with default is missing"
);
}
#[tokio::test]
async fn test_required_variable_in_mutation() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let config = AuroraConfig {
db_path,
enable_write_buffering: false,
durability_mode: aurora_db::types::DurabilityMode::Synchronous,
..Default::default()
};
let db = Aurora::with_config(config).await.unwrap();
db.new_collection(
"users",
vec![
(
"name".to_string(),
aurora_db::types::FieldType::SCALAR_STRING,
false,
),
(
"email".to_string(),
aurora_db::types::FieldType::SCALAR_STRING,
false,
),
],
)
.await
.unwrap();
let mutation = r#"
mutation CreateUser($name: String!, $email: String!) {
insertInto(collection: "users", data: {
name: $name,
email: $email
}) {
id
name
}
}
"#;
let result = db.execute((mutation, json!({}))).await;
assert!(
result.is_err(),
"Mutation should fail when required variables are missing"
);
let err = result.unwrap_err();
assert_eq!(err.code, ErrorCode::UndefinedVariable);
let result = db.execute((mutation, json!({ "name": "Alice" }))).await;
assert!(
result.is_err(),
"Mutation should fail when one required variable is missing"
);
let err = result.unwrap_err();
assert_eq!(err.code, ErrorCode::UndefinedVariable);
assert!(
err.message.contains("email"),
"Error should mention the missing 'email' variable"
);
let result = db
.execute((
mutation,
json!({
"name": "Alice",
"email": "alice@example.com"
}),
))
.await;
assert!(
result.is_ok(),
"Mutation should succeed when all required variables are provided"
);
}
#[tokio::test]
async fn test_boolean_variable_type_validation() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let config = AuroraConfig {
db_path,
enable_write_buffering: false,
durability_mode: aurora_db::types::DurabilityMode::Synchronous,
..Default::default()
};
let db = Aurora::with_config(config).await.unwrap();
db.new_collection(
"users",
vec![
(
"name".to_string(),
aurora_db::types::FieldType::SCALAR_STRING,
false,
),
(
"active".to_string(),
aurora_db::types::FieldType::SCALAR_BOOL,
false,
),
],
)
.await
.unwrap();
let query = r#"
query GetActiveUsers($isActive: Boolean!) {
users(where: { active: { eq: $isActive } }) {
name
}
}
"#;
let result = db.execute((query, json!({ "isActive": "true" }))).await;
if result.is_err() {
let err = result.unwrap_err();
assert_eq!(
err.code,
ErrorCode::TypeError,
"Should fail with TypeError when wrong type is provided"
);
}
let result = db.execute((query, json!({ "isActive": true }))).await;
assert!(
result.is_ok(),
"Query should succeed with correct boolean type"
);
}