no-mistakes-postgres-engine 0.109.130

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::*;
use crate::test_support::FsSources;
use std::path::PathBuf;

mod current_database;
mod declared_returns;
mod discriminant;
mod edges;
mod errors;
mod expressions;
mod findings;
mod fixture_function_contracts;
mod function_body;
mod model;
mod names;
mod not_null;
mod order;
mod parse;
mod projection_cache;
mod relation;
mod resolve;
mod trigger_writes;
mod view_definition;
mod view_relation;

fn load_fixture(name: &str) -> Result<SchemaCatalog, String> {
    let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/postgres/catalog");
    SchemaCatalog::load(&root, name, &FsSources).map_err(|error| error.to_string())
}

#[test]
fn expressions_ignore_formatting_but_not_source_bindings() {
    assert!(expression_matches(
        "LOWER( category_text )",
        "lower(category_text)",
        false
    ));
    assert!(!expression_matches(
        "lower(input.category_text)",
        "lower(category_text)",
        false
    ));
    assert!(expression_matches(
        "lower(input.category_text)",
        "lower(category_text)",
        true
    ));
}
#[test]
fn expression_normalization_preserves_structural_parentheses() {
    assert_ne!(
        normalize_expression("(left_value + right_value) * multiplier"),
        normalize_expression("left_value + (right_value * multiplier)")
    );
}
#[test]
fn rejects_outside_catalog_paths() {
    let root = Path::new("/repo");
    assert!(catalog_path(root, "").is_err());
    assert!(catalog_path(root, ".").is_err());
    assert!(catalog_path(root, "../schema.json").is_err());
    assert!(catalog_path(root, "/schema.json").is_err());
    assert_eq!(
        catalog_path(root, "db/schema.json").unwrap(),
        root.join("db/schema.json")
    );
    assert_eq!(
        catalog_path(root, "./db/schema.json").unwrap(),
        root.join("db/schema.json")
    );
}

#[test]
fn reports_catalog_read_parse_and_version_failures() {
    let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/postgres/catalog");

    let malformed = SchemaCatalog::load(&root, "invalid.json", &FsSources).unwrap_err();
    assert!(malformed.to_string().contains("not valid JSON"));
    let wrong_version = SchemaCatalog::load(&root, "wrong-version.json", &FsSources).unwrap_err();
    assert!(wrong_version.to_string().contains("formatVersion 2"));
    let missing = SchemaCatalog::load(&root, "missing.json", &FsSources).unwrap_err();
    assert!(missing
        .to_string()
        .contains("failed to read schemaCatalogPath"));
}

mod view_queries;

#[test]
fn row_security_resolves_through_the_catalog_relation() {
    let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(
        "../../test-cases/rules/postgres-lock-written-rows-only/fixture/statement-lock-edges",
    );
    let catalog =
        crate::SchemaCatalog::load(&root, "schema.json", &crate::test_support::FsSources).unwrap();
    let security: &crate::CatalogRowSecurity =
        catalog.relation_row_security("lock_edges.secured").unwrap();
    assert!(security.enabled && security.forced);
    assert_eq!(security.policies["visible"], crate::PolicyCommand::Select);
    assert_eq!(security.policies["writable"], crate::PolicyCommand::Update);
    assert!(!catalog.relation_row_security("scores").unwrap().enabled);
    assert!(catalog.relation_row_security("other.secured").is_none());
}