use super::{SqlIsolation, SqlIsolationChanges};
use crate::parse::PreparedSql;
#[test]
fn recovered_transaction_controls_without_locations_make_isolation_uncertain() {
let sql = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-cases/rules/postgres-lock-reason/fixture/isolation/sql/recovery.sql"
));
let statements: Vec<_> = crate::parse::parse_postgres_sql_with_function_sources(sql)
.into_iter()
.map(|statement| statement.statement)
.collect();
let changes =
SqlIsolationChanges::collect(&statements, None, PreparedSql::new(sql).tokens(), false);
assert_eq!(
changes.at(4, 1, Some(SqlIsolation::Serializable)),
SqlIsolation::Unknown
);
assert_eq!(changes.after_call(), Some(SqlIsolation::Unknown));
}
#[test]
fn savepoint_restore_tracks_active_isolation_and_released_names() {
let sql = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-cases/rules/postgres-lock-reason/fixture/isolation/facts/savepoint-lifecycle.sql"
));
let mut locations = Vec::new();
let prepared = PreparedSql::new(sql);
let statements = prepared
.parse_policy_with_locations(Some(&mut locations))
.unwrap();
let changes =
SqlIsolationChanges::collect(&statements, Some(&locations), prepared.tokens(), false);
assert_eq!(changes.at(6, 1, None), SqlIsolation::Serializable);
assert_eq!(changes.at(8, 1, None), SqlIsolation::Unknown);
assert_eq!(changes.after_call(), Some(SqlIsolation::Unknown));
}
#[test]
fn catalog_qualification_and_computed_values_preserve_isolation_uncertainty() {
let sql = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-cases/rules/postgres-lock-reason/fixture/review-imported-effects/sql/settings-mutations.sql"
));
let mut locations = Vec::new();
let prepared = PreparedSql::new(sql);
let statements = prepared
.parse_policy_with_locations(Some(&mut locations))
.unwrap();
let changes =
SqlIsolationChanges::collect(&statements, Some(&locations), prepared.tokens(), false);
for line in [34, 42, 66, 75, 91, 98] {
assert_eq!(
changes.at(line, 1, None),
SqlIsolation::Unknown,
"line {line}"
);
}
assert_eq!(changes.at(105, 1, None), SqlIsolation::Other);
}
#[test]
fn prepared_statement_execution_tracks_body_isolation_effects() {
let sql = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-cases/rules/postgres-lock-reason/fixture/review-imported-effects/sql/prepared-execute.sql"
));
let mut locations = Vec::new();
let prepared = PreparedSql::new(sql);
let statements = prepared
.parse_policy_with_locations(Some(&mut locations))
.unwrap();
let changes =
SqlIsolationChanges::collect(&statements, Some(&locations), prepared.tokens(), false);
for line in [7, 13, 34] {
assert_eq!(
changes.at(line, 1, None),
SqlIsolation::Other,
"line {line}"
);
}
for line in [19, 26, 41, 50, 57, 66] {
assert_eq!(
changes.at(line, 1, None),
SqlIsolation::Unknown,
"line {line}"
);
}
}