use scythe_codegen::{generate_with_backend, get_backend};
use scythe_core::analyzer::analyze;
use scythe_core::catalog::Catalog;
use scythe_core::dialect::SqlDialect;
use scythe_core::parser::parse_query_with_dialect;
const SCHEMA: [&str; 2] = [
"CREATE TYPE widget_status AS ENUM ('active', 'inactive');",
"CREATE TABLE widgets (id SERIAL PRIMARY KEY, \"my col\" TEXT, status widget_status NOT NULL);",
];
const QUERY: &str = "-- @name GetWidget\n-- @returns :one\nSELECT id, \"my col\", status FROM widgets WHERE id = $1;";
fn generate_query_fn() -> String {
let backend = get_backend("rust-sqlx", "postgresql").expect("backend must support postgresql");
let catalog = Catalog::from_ddl_with_dialect(&SCHEMA, &SqlDialect::PostgreSQL).expect("schema must parse");
let parsed = parse_query_with_dialect(QUERY, &SqlDialect::PostgreSQL).expect("query must parse");
let analyzed = analyze(&catalog, &parsed).expect("query must analyze");
let code = generate_with_backend(&analyzed, &*backend).expect("codegen must succeed");
code.query_fn.expect("expected a query function")
}
#[test]
fn rust_sqlx_query_fn_aliases_a_mangled_column() {
let query_fn = generate_query_fn();
assert!(
query_fn.contains("\\\"my col\\\" AS \\\"my_col\\\""),
"expected the SQL to alias the non-identifier column to the field sqlx's \
query_as! macro must report -- `\"my col\" AS \"my_col\"` -- but got:\n{query_fn}"
);
}
#[test]
fn rust_sqlx_query_fn_enum_alias_is_not_double_escaped() {
let query_fn = generate_query_fn();
assert!(
query_fn.contains("status AS \\\"status: WidgetStatus\\\""),
"expected a single-escaped enum alias -- `status AS \"status: WidgetStatus\"` -- \
but got:\n{query_fn}"
);
}