hamelin_legacy 0.3.8

Legacy AST translation code for Hamelin (to be deprecated)
Documentation
use std::sync::Arc;

use hamelin_legacy::Compiler;

#[allow(dead_code)]
pub fn compare_dml(compiler: Arc<Compiler>, hamelin: String, sql: String) -> anyhow::Result<()> {
    let dml = compiler.compile_dml(hamelin)?;
    pretty_assertions::assert_eq!(
        sqlformat::format(
            dml.translation.sql.as_str(),
            &sqlformat::QueryParams::default(),
            &sqlformat::FormatOptions::default(),
        ),
        sqlformat::format(
            &sql,
            &sqlformat::QueryParams::default(),
            &sqlformat::FormatOptions::default(),
        ),
    );

    Ok(())
}

#[allow(dead_code)]
pub fn compare(compiler: Arc<Compiler>, hamelin: String, sql: String) -> anyhow::Result<()> {
    let query = compiler.compile_query(hamelin)?;

    pretty_assertions::assert_eq!(
        sqlformat::format(
            query.translation.sql.as_str(),
            &sqlformat::QueryParams::default(),
            &sqlformat::FormatOptions::default(),
        ),
        sqlformat::format(
            &sql,
            &sqlformat::QueryParams::default(),
            &sqlformat::FormatOptions::default(),
        ),
    );

    Ok(())
}

#[allow(dead_code)]
pub fn compare_expression(compiler: Arc<Compiler>, hamelin: String, sql: String) {
    pretty_assertions::assert_eq!(
        sqlformat::format(
            compiler
                .compile_query(hamelin)
                .unwrap()
                .translation
                .sql
                .as_str(),
            &sqlformat::QueryParams::default(),
            &sqlformat::FormatOptions::default(),
        ),
        sqlformat::format(
            &format!(r#"SELECT {} AS "result""#, sql),
            &sqlformat::QueryParams::default(),
            &sqlformat::FormatOptions::default(),
        ),
    );
}

pub fn error(compiler: Arc<Compiler>, hamelin: String) {
    assert!(compiler.compile_query(hamelin).is_err());
}

pub fn specific_error(compiler: Arc<Compiler>, hamelin: String, error: &str) {
    let res = compiler.compile_query(hamelin);

    match res {
        Ok(_) => panic!("Expected an error."),
        Err(e) => {
            let pretty = e
                .errors
                .iter()
                .map(|e| e.pretty.clone())
                .collect::<Vec<String>>()
                .join("\n");
            assert!(
                pretty.contains(error),
                "The error string:\n{}\ndid not contain\n{}",
                pretty,
                error
            )
        }
    }
}

pub fn specific_errors(compiler: Arc<Compiler>, hamelin: String, errors: Vec<&str>) {
    let res = compiler.compile_query(hamelin);

    match res {
        Ok(_) => panic!("Expected an error."),
        Err(e) => {
            assert_eq!(
                e.errors.len(),
                errors.len(),
                "Expected {} errors, got {}",
                errors.len(),
                e.errors.len()
            );
            e.errors
                .into_iter()
                .map(|e| e.pretty)
                .zip(errors.iter())
                .for_each(|(e, o)| {
                    assert!(
                        e.contains(o),
                        "The error string:\n{}\ndid not contain\n{}",
                        e,
                        o
                    )
                });
        }
    }
}

pub fn specific_error_dml(compiler: Arc<Compiler>, hamelin: String, error: &str) {
    let res = compiler.compile_dml(hamelin);

    match res {
        Ok(_) => panic!("Expected an error."),
        Err(e) => {
            let pretty = e
                .errors
                .iter()
                .map(|e| e.pretty.clone())
                .collect::<Vec<String>>()
                .join("\n");
            assert!(
                pretty.contains(error),
                "The error string:\n{}\ndid not contain\n{}",
                pretty,
                error
            )
        }
    }
}