use super::super::embedded_call_facts_with_demand;
use crate::statements::{extract_sql_statement_facts, SqlFactSite, SqlLocationDemand};
use crate::{extract_embedded_sql_from_source, EmbeddedSqlOptions};
use std::path::PathBuf;
fn fixture(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../test-cases/check-runner/postgres-parse-failure-locations/fixture")
.join(name)
}
#[test]
fn parse_failure_locations_rebase_through_the_existing_embedded_provenance() {
let path = fixture("failures.ts");
let source = std::fs::read_to_string(&path).unwrap();
let embedded = extract_embedded_sql_from_source(
&path,
&source,
&EmbeddedSqlOptions::configured("db", &["query".into()]),
);
let facts = embedded_call_facts_with_demand(&embedded, true, false, SqlLocationDemand::Full);
assert_eq!(facts.len(), 1);
assert_eq!(
facts[0]
.parse_failures
.iter()
.map(|failure| failure.line)
.collect::<Vec<_>>(),
[4, 7]
);
let locations = facts[0].variant_locations.as_ref().unwrap();
for (index, line) in [4, 7].into_iter().enumerate() {
assert_eq!(
locations
.position(SqlFactSite::ParseFailure(index))
.unwrap()
.source_line,
line
);
}
}
#[test]
fn parse_failure_locations_preserve_columns_and_composed_lexical_origins() {
for (name, column) in [("lexical-same-line.sql", 11), ("lexical-unicode.sql", 13)] {
let facts = extract_sql_statement_facts(&std::fs::read_to_string(fixture(name)).unwrap());
assert_eq!(
facts
.parse_failures
.iter()
.map(|failure| (failure.line, failure.column))
.collect::<Vec<_>>(),
[(1, column)]
);
}
let path = fixture("lexical-composed.ts");
let source = std::fs::read_to_string(&path).unwrap();
let embedded = extract_embedded_sql_from_source(
&path,
&source,
&EmbeddedSqlOptions::configured("db", &["query".into()]),
);
let facts = embedded_call_facts_with_demand(&embedded, true, false, SqlLocationDemand::Full);
assert_eq!(
facts[0]
.parse_failures
.iter()
.map(|failure| failure.line)
.collect::<Vec<_>>(),
[3]
);
let at = facts[0]
.variant_locations
.as_ref()
.unwrap()
.position(SqlFactSite::ParseFailure(0))
.unwrap();
assert_eq!((at.sql_line, at.sql_column, at.source_line), (1, 11, 3));
}