#[path = "testutils/mod.rs"]
mod testutils;
use graphlite::Value;
use std::sync::OnceLock;
use testutils::test_fixture::TestFixture;
static SHARED_FIXTURE: OnceLock<TestFixture> = OnceLock::new();
fn get_shared_fixture() -> &'static TestFixture {
SHARED_FIXTURE.get_or_init(|| TestFixture::empty().expect("Failed to create shared fixture"))
}
#[test]
fn test_schema_ddl_operations() {
let fixture = get_shared_fixture();
let test_schema = "business_schema";
fixture.assert_query_succeeds(&format!("CREATE SCHEMA IF NOT EXISTS {}", test_schema));
fixture.assert_query_succeeds("CREATE SCHEMA new_schema");
fixture.assert_query_fails("CREATE SCHEMA new_schema", "already exists");
fixture.assert_query_succeeds("CREATE SCHEMA IF NOT EXISTS new_schema");
fixture.assert_query_succeeds("DROP SCHEMA IF EXISTS new_schema");
fixture.assert_query_succeeds("DROP SCHEMA IF EXISTS nonexistent_schema");
fixture.assert_query_fails("DROP SCHEMA nonexistent_schema_2", "not found");
}
#[test]
fn test_drop_schema_basic() {
let fixture = get_shared_fixture();
let test_schema = "drop_test_schema";
fixture.assert_query_succeeds(&format!("CREATE SCHEMA IF NOT EXISTS {}", test_schema));
fixture.assert_query_succeeds(&format!("DROP SCHEMA {}", test_schema));
fixture.assert_query_fails("DROP SCHEMA nonexistent_schema", "not found");
}
#[test]
fn test_drop_schema_if_exists() {
let fixture = get_shared_fixture();
let test_schema = "if_exists_test_schema";
fixture.assert_query_succeeds(&format!("CREATE SCHEMA IF NOT EXISTS {}", test_schema));
fixture.assert_query_succeeds(&format!("DROP SCHEMA IF EXISTS {}", test_schema));
fixture.assert_query_succeeds("DROP SCHEMA IF EXISTS nonexistent_schema");
}
#[test]
fn test_ddl_error_cases() {
let fixture = get_shared_fixture();
match fixture.query("CREATE SCHEMA \"invalid name with spaces\"") {
Ok(_) => panic!("Expected schema name with spaces to fail"),
Err(e) => {
assert!(
e.contains("Parse error") || e.contains("Invalid schema name"),
"Expected parse error or validation error, got: {}",
e
);
}
}
match fixture.query("CREATE SCHEMA \"\"") {
Ok(_) => panic!("Expected empty schema name to fail"),
Err(e) => {
assert!(
e.contains("Parse error")
|| e.contains("Invalid")
|| e.contains("Validation error"),
"Expected parse error or validation error, got: {}",
e
);
}
}
fixture.assert_query_fails("CREATE GRAPH invalid_schema/graph_name", "not found");
match fixture.query("CREATE GRAPH \"\"") {
Ok(_) => panic!("Expected empty graph name to fail"),
Err(e) => {
assert!(
e.contains("Parse error")
|| e.contains("Invalid")
|| e.contains("Validation error"),
"Expected parse error or validation error, got: {}",
e
);
}
}
fixture.assert_query_fails("DROP SCHEMA non_existent_schema_12345", "not found");
fixture.assert_query_fails(
"DROP GRAPH non_existent_schema/non_existent_graph",
"not found",
);
}
#[test]
fn test_session_schema_management() {
let fixture = get_shared_fixture();
let test_schema = "session_schema";
fixture.assert_query_succeeds(&format!("CREATE SCHEMA {}", test_schema));
fixture.assert_query_succeeds(&format!("SESSION SET SCHEMA {}", test_schema));
fixture.assert_query_succeeds(&format!("CREATE GRAPH {}/session_test_graph", test_schema));
fixture.assert_query_succeeds(&format!(
"SESSION SET GRAPH {}/session_test_graph",
test_schema
));
fixture.assert_query_succeeds("INSERT (n:SessionTest {name: 'Schema Test'})");
fixture.assert_first_value(
"MATCH (n:SessionTest) RETURN count(n) as count",
"count",
Value::Number(1.0),
);
fixture.assert_query_succeeds(&format!("DROP SCHEMA {} CASCADE", test_schema));
}