Skip to content

Commit f5341dc

Browse files
committed
feat(native): validate SQL test targets and scenario sources natively behind the preview engine
1 parent 7cc9233 commit f5341dc

20 files changed

Lines changed: 587 additions & 8 deletions

File tree

‎crates/sqlbuild-attachments/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pub mod cursor_intrinsics;
88
pub mod seeds;
99
pub mod sql_lexing;
1010
pub mod test_parameters;
11+
pub mod test_targets;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod scenario_source_violation;
2+
pub mod unknown_test_target;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Python's `_validate_scenario_source_references`.
2+
3+
use std::collections::HashSet;
4+
5+
use crate::test_targets::models::ScenarioCteSources;
6+
7+
/// Python's error for the first source a scenario may not read; check CTEs are read first.
8+
#[must_use]
9+
pub fn scenario_source_violation(
10+
file_label: &str,
11+
ctes: &[ScenarioCteSources],
12+
known_sources: &[String],
13+
) -> Option<String> {
14+
for cte in ctes.iter().filter(|cte| cte.check) {
15+
if let Some(source) = cte.sources.first() {
16+
return Some(format!(
17+
"SQL scenario file {file_label} CTE '{}' must not reference project source \
18+
'{source}' with __source(); source-backed scenario data is only allowed \
19+
in helper and fixture CTEs",
20+
cte.name
21+
));
22+
}
23+
}
24+
let known: HashSet<&str> = known_sources.iter().map(String::as_str).collect();
25+
for cte in ctes.iter().filter(|cte| !cte.check) {
26+
if let Some(source) = cte
27+
.sources
28+
.iter()
29+
.find(|source| !known.contains(source.as_str()))
30+
{
31+
return Some(format!(
32+
"SQL scenario file {file_label} references unknown source '{source}'"
33+
));
34+
}
35+
}
36+
None
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Python's `validate_test_ctes` for model SQL tests.
2+
3+
use std::collections::HashSet;
4+
5+
use crate::test_targets::models::TargetGroup;
6+
7+
/// Python's error for the first unknown target in group order, or None when all are known.
8+
#[must_use]
9+
pub fn unknown_test_target(file_label: &str, groups: &[TargetGroup]) -> Option<String> {
10+
for group in groups {
11+
let known: HashSet<&str> = group.known.iter().map(String::as_str).collect();
12+
if let Some(name) = group
13+
.names
14+
.iter()
15+
.find(|name| !known.contains(name.as_str()))
16+
{
17+
return Some(format!(
18+
"SQL test file {file_label} {} '{name}'",
19+
group.phrase
20+
));
21+
}
22+
}
23+
None
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod main;
2+
pub mod models;
3+
4+
#[cfg(test)]
5+
mod tests;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Targets a SQL test or scenario names, checked against the project's resources.
2+
3+
/// One group of names a model SQL test targets and the phrase Python reports for an unknown one.
4+
#[derive(Clone, Debug, PartialEq, Eq)]
5+
pub struct TargetGroup {
6+
/// For example `mocks unknown model`.
7+
pub phrase: String,
8+
pub names: Vec<String>,
9+
pub known: Vec<String>,
10+
}
11+
12+
/// One scenario CTE and the `__source("name")` references in its body.
13+
#[derive(Clone, Debug, PartialEq, Eq)]
14+
pub struct ScenarioCteSources {
15+
pub name: String,
16+
/// Expected-result and assertion CTEs must not read project sources at all.
17+
pub check: bool,
18+
pub sources: Vec<String>,
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[path = "tests/helpers.rs"]
2+
mod helpers;
3+
#[path = "tests/test_scenario_source_violation.rs"]
4+
mod scenario_source_violation;
5+
#[path = "tests/test_types.rs"]
6+
mod test_types;
7+
#[path = "tests/test_unknown_test_target.rs"]
8+
mod unknown_test_target;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::test_targets::models::{ScenarioCteSources, TargetGroup};
2+
3+
pub(super) fn cte(name: &str, check: bool, sources: &[&str]) -> ScenarioCteSources {
4+
ScenarioCteSources {
5+
name: name.to_owned(),
6+
check,
7+
sources: sources.iter().map(|source| (*source).to_owned()).collect(),
8+
}
9+
}
10+
11+
pub(super) fn group(phrase: &str, names: &[&str], known: &[&str]) -> TargetGroup {
12+
TargetGroup {
13+
phrase: phrase.to_owned(),
14+
names: names.iter().map(|name| (*name).to_owned()).collect(),
15+
known: known.iter().map(|name| (*name).to_owned()).collect(),
16+
}
17+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::test_targets::main::scenario_source_violation::scenario_source_violation;
2+
use crate::test_targets::tests::helpers::cte;
3+
use crate::test_targets::tests::test_types::ScenarioSourceTestCase;
4+
5+
#[test]
6+
fn given_scenario_sources_when_validating_then_python_first_error_is_returned() {
7+
let test_cases = [
8+
ScenarioSourceTestCase {
9+
description: "known sources in fixtures pass",
10+
ctes: vec![
11+
cte("__source__raw", false, &["raw"]),
12+
cte("__expected__orders", true, &[]),
13+
],
14+
expected_error: None,
15+
},
16+
ScenarioSourceTestCase {
17+
description: "a check CTE reading a source is reported before unknown fixture sources",
18+
ctes: vec![
19+
cte("helper", false, &["missing"]),
20+
cte("__expected__orders", true, &["raw"]),
21+
],
22+
expected_error: Some(
23+
"SQL scenario file tests/scenarios/orders.sql CTE '__expected__orders' must not reference project source 'raw' with __source(); source-backed scenario data is only allowed in helper and fixture CTEs",
24+
),
25+
},
26+
ScenarioSourceTestCase {
27+
description: "an unknown source in a helper CTE",
28+
ctes: vec![cte("helper", false, &["raw", "missing"])],
29+
expected_error: Some(
30+
"SQL scenario file tests/scenarios/orders.sql references unknown source 'missing'",
31+
),
32+
},
33+
];
34+
35+
for test_case in test_cases {
36+
assert_eq!(
37+
scenario_source_violation(
38+
"tests/scenarios/orders.sql",
39+
&test_case.ctes,
40+
&["raw".to_owned()]
41+
)
42+
.as_deref(),
43+
test_case.expected_error,
44+
"{}",
45+
test_case.description
46+
);
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::test_targets::models::{ScenarioCteSources, TargetGroup};
2+
3+
pub(super) struct UnknownTargetTestCase {
4+
pub(super) description: &'static str,
5+
pub(super) groups: Vec<TargetGroup>,
6+
pub(super) expected_error: Option<&'static str>,
7+
}
8+
9+
pub(super) struct ScenarioSourceTestCase {
10+
pub(super) description: &'static str,
11+
pub(super) ctes: Vec<ScenarioCteSources>,
12+
pub(super) expected_error: Option<&'static str>,
13+
}

0 commit comments

Comments
 (0)