no-mistakes-postgres-engine 0.109.125

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::*;

#[test]
fn appended_helper_is_a_hole_between_the_callers_own_fragments() {
    let facts = extract("pending.ts");
    assert_eq!(facts.calls.len(), 15);
    assert_eq!(call(&facts, 0).kind, EmbeddedSqlKind::Dynamic);
    assert_eq!(
        call(&facts, 0).sql_text.as_deref(),
        Some("SELECT l.id FROM links l WHERE l.account_id = $1 AND ")
    );
    assert_eq!(call(&facts, 0).recovered_placeholder_positions, [(1, 47)]);
    let pending = pending_for(&facts, 0).expect("pending");
    assert_eq!(pending.template.kind, EmbeddedSqlKind::Composed);
    let template = pending.template.variants[0].sql_text.as_str();
    let holes = sentinel::find_all(template);
    assert_eq!(holes.len(), 1);
    assert!(template[holes[0].0.end..].ends_with(" ORDER BY l.id"));
    let (index, site) = &pending.sites[0];
    assert_eq!(*index, holes[0].1);
    assert_eq!(site.local, "ownerVisible");
    assert!(site.value_used);
    assert_eq!(site.line, 8);
    let SiteArg::Literal(literal) = &site.args[0] else {
        panic!("string literal argument");
    };
    assert_eq!(literal.text, "l.parent_id");
    // The literal is written on the call line; later splicing must use this line.
    assert_eq!(literal.line, 8);
    assert!(literal.origin > 0);
}

#[test]
fn an_insert_prefix_survives_for_the_insert_fail_closed_policy() {
    let facts = extract("pending.ts");
    assert_eq!(call(&facts, 1).kind, EmbeddedSqlKind::Dynamic);
    assert_eq!(
        call(&facts, 1).sql_text.as_deref(),
        Some("INSERT INTO links (id) VALUES (1) ")
    );
    assert!(pending_for(&facts, 1).is_some());
}

#[test]
fn an_executor_argument_can_be_the_helper_call_itself() {
    let facts = extract("pending.ts");
    assert_eq!(call(&facts, 2).sql_text, None);
    let pending = pending_for(&facts, 2).expect("pending");
    assert_eq!(
        pending.template.sql_text,
        Some(sentinel::encode(pending.sites[0].0))
    );
}

#[test]
fn argument_kinds_are_recorded_for_the_projection() {
    let facts = extract("pending.ts");
    let args = |index| pending_for(&facts, index).map(|pending| pending.sites[0].1.args.clone());
    assert_eq!(args(3), Some(vec![SiteArg::Scalar]));
    assert_eq!(args(5), Some(vec![SiteArg::Builder, SiteArg::Scalar]));
    let template = pending_for(&facts, 5)
        .and_then(|pending| {
            pending.template.sql_text.clone().or_else(|| {
                pending
                    .template
                    .variants
                    .first()
                    .map(|variant| variant.sql_text.clone())
            })
        })
        .expect("template");
    assert!(template.starts_with("SELECT 1 FROM p WHERE true"));
    assert!(sentinel::contains(&template));
}

#[test]
fn unexecuted_or_interpolated_helpers_never_become_holes() {
    let facts = extract("pending.ts");
    // A conditional append is not composed into the call's own text; only its
    // complete alternatives hold the hole.
    let pending = pending_for(&facts, 4).expect("pending");
    assert_eq!(pending.template.variants.len(), 2);
    assert_eq!(
        pending.template.sql_text.as_deref(),
        Some("SELECT 1 WHERE ")
    );
    assert!(call(&facts, 4).variants.is_empty());
    assert_eq!(call(&facts, 4).sql_text.as_deref(), Some("SELECT 1 WHERE "));
    // An interpolated call is a bound value, not SQL text.
    assert!(pending_for(&facts, 6).is_none());
    // Ten holes from the original calls, plus the bare-template argument.
    assert_eq!(facts.pending_imported.len(), 11);
}

#[test]
fn argument_shapes_decide_whether_a_hole_is_recorded() {
    let facts = extract("pending.ts");
    let args = pending_for(&facts, 10).map(|pending| {
        pending.sites[0]
            .1
            .args
            .iter()
            .map(|arg| match arg {
                SiteArg::Literal(literal) => SiteArg::Literal(literal.text.clone().into()),
                other => other.clone(),
            })
            .collect::<Vec<_>>()
    });
    assert_eq!(
        args,
        Some(vec![
            SiteArg::Literal("l.template".into()),
            SiteArg::Scalar,
            SiteArg::Bool(true),
            SiteArg::Null,
        ])
    );
    // A no-substitution template is the same literal as a quoted string.
    let backtick = pending_for(&facts, 11).map(|pending| {
        pending.sites[0]
            .1
            .args
            .iter()
            .map(|arg| match arg {
                SiteArg::Literal(literal) => SiteArg::Literal(literal.text.clone().into()),
                other => other.clone(),
            })
            .collect::<Vec<_>>()
    });
    assert_eq!(backtick, Some(vec![SiteArg::Literal("l.template".into())]));
    // An object literal of classifiable values is recorded as such.
    let object = pending_for(&facts, 13).map(|pending| pending.sites[0].1.args.clone());
    assert_eq!(
        object,
        Some(vec![SiteArg::Object(vec![("a".into(), SiteArg::Scalar)])])
    );
    // Spread and two-builder arguments cannot be reasoned about.
    for index in [12, 14] {
        assert!(pending_for(&facts, index).is_none(), "{index}");
        assert_eq!(
            call(&facts, index).sql_text.as_deref(),
            Some("SELECT 1 WHERE ")
        );
    }
}

#[test]
fn aliased_or_alternative_helper_results_are_never_completed() {
    let facts = extract("pending.ts");
    // A stored helper result aliases a mutable builder.
    assert!(pending_for(&facts, 7).is_none());
    // Alternatives that include a hole publish no partial version set until
    // every hole is filled.
    let pending = pending_for(&facts, 9).expect("pending");
    assert_eq!(pending.template.variants.len(), 2);
    assert!(call(&facts, 9).variants.is_empty());
    assert_eq!(call(&facts, 9).sql_text, None);
}

#[test]
fn a_builder_fragment_that_held_a_hole_keeps_only_its_leading_statement() {
    let facts = extract("pending.ts");
    assert_eq!(
        call(&facts, 8).sql_text.as_deref(),
        Some("SELECT 2 SELECT 1 WHERE ")
    );
    assert!(facts
        .fragments
        .iter()
        .any(|fragment| fragment.sql_text.as_deref() == Some("SELECT 1 WHERE ")));
}

#[test]
fn a_trimmed_fragment_publishes_only_the_binds_that_remain() {
    let facts = extract("pending.ts");
    let trimmed = facts
        .fragments
        .iter()
        .find(|fragment| fragment.sql_text.as_deref() == Some("SELECT $1 WHERE "))
        .expect("trimmed fragment");
    // The suffix `AND ${id} = ${id}` is its own fragment. If the trimmed
    // fragment kept positions from before the cut, those binds would still be here.
    assert_eq!(trimmed.recovered_placeholder_positions, [(1, 8)]);
    assert!(facts
        .fragments
        .iter()
        .any(|fragment| { fragment.sql_text.as_deref() == Some(" AND $1 = $2") }));
}