no-mistakes-postgres-engine 0.109.131

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

#[test]
fn relation_uses_exact_names_then_a_unique_tail() {
    let catalog = SchemaCatalog::from_json(
        r#"{
            "formatVersion": 2, "coverage": "complete",
            "tables": {
                "public.orders": { "columns": { "id": { "dataType": "integer" } } },
                "public.events": { "columns": { "id": { "dataType": "integer" } } },
                "other.events": { "columns": { "id": { "dataType": "integer" } } }
            }
        }"#,
    )
    .unwrap();
    assert_eq!(
        catalog
            .relation("public.orders")
            .map(|table| table.name.as_str()),
        Some("public.orders")
    );
    assert_eq!(
        catalog.relation("orders").map(|table| table.name.as_str()),
        Some("public.orders")
    );
    assert!(catalog.relation("schema.events").is_none());
    // A name that spells another schema is not the bare name of a qualified key.
    assert!(catalog.relation("schema.orders").is_none());
    assert!(catalog.relation("missing").is_none());
    assert_eq!(catalog.column_line("public.orders", "id"), 1);
}

#[test]
fn an_explicit_foreign_schema_is_never_the_catalogs_bare_table() {
    let json = |schema: &str| {
        format!(
            r#"{{"formatVersion": 2, "coverage": "complete"{schema},
                "tables": {{"accounts": {{"columns": {{"id": {{"dataType": "integer"}}}}}}}}}}"#
        )
    };
    let own = SchemaCatalog::from_json(&json(r#", "schema": "public""#)).unwrap();
    for spelling in [
        "accounts",
        "public.accounts",
        "\"public\".accounts",
        "PUBLIC.accounts",
    ] {
        assert!(own.relation(spelling).is_some(), "{spelling}");
    }
    assert!(own.relation("audit.accounts").is_none());
    assert!(own.relation("\"Public\".accounts").is_none());
    // Names are split quote-aware: a quoted schema or table that contains a dot is one part.
    let quoted = SchemaCatalog::from_json(
        r#"{"formatVersion": 2, "coverage": "complete", "schema": "Catalog.Test",
            "tables": {"\"audit.log\"": {"columns": {"id": {"dataType": "integer"}}}}}"#,
    )
    .unwrap();
    for spelling in [
        "\"audit.log\"",
        "\"Catalog.Test\".\"audit.log\"",
        "\"Catalog.Test\".\"AUDIT.LOG\"",
    ] {
        assert_eq!(
            quoted.relation(spelling).is_some(),
            !spelling.contains("AUDIT"),
            "{spelling}"
        );
    }
    assert!(quoted.relation("public.\"audit.log\"").is_none());
    // A catalog that names no schema cannot tell, and keeps matching the bare key.
    let anonymous = SchemaCatalog::from_json(&json("")).unwrap();
    assert!(anonymous.relation("audit.accounts").is_some());
}

#[test]
fn relation_identity_needs_explicit_schema_evidence_for_bare_names() {
    let json = |schema: &str| {
        format!(
            r#"{{"formatVersion": 2, "coverage": "complete"{schema},
                "tables": {{"accounts": {{"columns": {{"id": {{"dataType": "integer"}}}}}}}}}}"#
        )
    };
    let explicit = SchemaCatalog::from_json(&json(r#", "schema": "public""#)).unwrap();
    assert!(explicit.same_relation("public.accounts", "accounts"));
    assert!(!explicit.same_relation("audit.accounts", "accounts"));

    let anonymous = SchemaCatalog::from_json(&json("")).unwrap();
    assert!(anonymous.same_relation("accounts", "accounts"));
    assert!(!anonymous.same_relation("public.accounts", "accounts"));
}

#[test]
fn fallback_index_preserves_exact_precedence_ambiguity_and_quoted_components() {
    let catalog = super::load_fixture("relation-index.json").unwrap();
    for (name, expected) in [
        ("accounts", Some("accounts")),
        ("public.accounts", Some("public.accounts")),
        ("audit.accounts", Some("audit.accounts")),
        ("orders", Some("public.orders")),
        ("events", None),
        ("other.accounts", None),
        ("public.events", Some("public.events")),
        ("\"Order.Items\"", Some("\"Sales.Zone\".\"Order.Items\"")),
        ("\"MixedCase\"", Some("public.\"MixedCase\"")),
        ("mixedcase", None),
    ] {
        assert_eq!(
            catalog.relation(name).map(|table| table.name.as_str()),
            expected,
            "{name}"
        );
    }
    // A qualified fallback can select the bare key even though the unqualified tail
    // is ambiguous; exact qualified keys still take precedence.
    let mut catalog = catalog;
    catalog.schema = Some("other".to_owned());
    assert_eq!(catalog.relation("other.accounts").unwrap().name, "accounts");
}

#[test]
fn indexed_catalog_preserves_unknown_search_path_and_load_failures() {
    let catalog = super::load_fixture("relation-index.json").unwrap();
    assert!(!catalog.hides_selected_relation(&["$user".to_owned()], "orders"));
    assert!(super::load_fixture("empty.json")
        .unwrap_err()
        .contains("is empty"));
    assert!(super::load_fixture("../outside.json").is_err());
}

#[test]
fn exact_only_catalog_access_does_not_build_fallback_indexes() {
    let catalog = super::load_fixture("relation-index.json").unwrap();
    assert!(catalog.relation_fallback.get().is_none());
    assert!(catalog.relation("public.orders").is_some());
    assert!(catalog.relation("foreign.orders").is_none());
    assert!(catalog.relation_fallback.get().is_none());
    assert!(catalog.relation("orders").is_some());
    assert!(catalog.relation_fallback.get().is_some());
    assert_eq!(
        catalog.clone().relation("orders").unwrap().name,
        "public.orders"
    );
}

#[test]
fn catalog_locations_counts_column_zero_and_indented_keys() {
    let source = "{\n\"formatVersion\": 2,\n\"coverage\": \"complete\",\n\"tables\": {\n\"comments\": {\n\"columns\": {\n\"target_id\": { \"dataType\": \"uuid\" },\n  \"target_type\": { \"dataType\": \"text\" }\n}\n},\n  \"bookmarks\": {\n    \"columns\": {}\n  }\n}\n}\n";
    let parsed = jsonc_parser::parse_to_ast(
        source,
        &jsonc_parser::CollectOptions::default(),
        &jsonc_parser::ParseOptions::default(),
    )
    .unwrap();
    let locations =
        crate::catalog::locations::catalog_locations(parsed.value.as_ref().unwrap(), source);
    assert_eq!(locations.tables.get("comments"), Some(&5));
    assert_eq!(locations.tables.get("bookmarks"), Some(&11));
    assert_eq!(
        locations
            .columns
            .get(&("comments".to_string(), "target_id".to_string())),
        Some(&7)
    );
    assert_eq!(
        locations
            .columns
            .get(&("comments".to_string(), "target_type".to_string())),
        Some(&8)
    );
}