|
| 1 | +use crate::analysis_route::main::analysis_route::analysis_route; |
| 2 | +use crate::analysis_route::main::native_gap::native_gap; |
| 3 | +use crate::analysis_route::main::skipped_analysis::skipped_analysis; |
| 4 | +use crate::analysis_route::models::{AnalysisRoute, LocatedGap, ParserGap}; |
| 5 | +use crate::analysis_route::tests::test_types::NativeGapTestCase; |
| 6 | +use crate::analysis_route::types::NativeDialect; |
| 7 | +use crate::assembly::project::models::SyntaxCheck; |
| 8 | + |
| 9 | +/// A test dialect whose parser refuses the word PIVOT and an unfilled `@@@` placeholder. |
| 10 | +struct PivotlessDialect; |
| 11 | + |
| 12 | +impl NativeDialect for PivotlessDialect { |
| 13 | + fn name(&self) -> &'static str { |
| 14 | + "postgres" |
| 15 | + } |
| 16 | + |
| 17 | + fn read_whole(&self, sql: &str) -> Result<(), ParserGap> { |
| 18 | + let refused = ["PIVOT", "@@@"] |
| 19 | + .iter() |
| 20 | + .filter_map(|word| sql.find(word).map(|offset| (offset, *word))) |
| 21 | + .min(); |
| 22 | + match refused { |
| 23 | + Some((offset, word)) => Err(ParserGap { |
| 24 | + construct: word.to_owned(), |
| 25 | + offset, |
| 26 | + }), |
| 27 | + None => Ok(()), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn given_any_dialect_when_routing_analysis_then_it_stays_on_the_legacy_path() { |
| 34 | + for dialect in [ |
| 35 | + Some("postgres"), |
| 36 | + Some("snowflake"), |
| 37 | + Some("duckdb"), |
| 38 | + Some("sqlite"), |
| 39 | + Some("bigquery"), |
| 40 | + Some("databricks"), |
| 41 | + Some("clickhouse"), |
| 42 | + Some("tsql"), |
| 43 | + None, |
| 44 | + ] { |
| 45 | + assert!( |
| 46 | + matches!(analysis_route(dialect), AnalysisRoute::Legacy), |
| 47 | + "{dialect:?}" |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn given_model_sql_when_probing_the_native_parser_then_its_gap_is_located_after_placeholders() { |
| 54 | + let test_cases = [ |
| 55 | + NativeGapTestCase { |
| 56 | + description: "SQL the parser reads whole has no gap", |
| 57 | + sql: "SELECT 1 AS a", |
| 58 | + placeholders: &[], |
| 59 | + expected_gap: None, |
| 60 | + }, |
| 61 | + NativeGapTestCase { |
| 62 | + description: "a placeholder is filled before the parser reads the SQL", |
| 63 | + sql: "SELECT * FROM t WHERE region = @@@region", |
| 64 | + placeholders: &[("region", "'eu'")], |
| 65 | + expected_gap: None, |
| 66 | + }, |
| 67 | + NativeGapTestCase { |
| 68 | + description: "the gap's line and column count from 1, in characters", |
| 69 | + sql: "SELECT 'é' AS a\nFROM t\n PIVOT (sum(x) FOR y IN (1))", |
| 70 | + placeholders: &[], |
| 71 | + expected_gap: Some(LocatedGap { |
| 72 | + construct: "PIVOT".to_owned(), |
| 73 | + line: 3, |
| 74 | + column: 3, |
| 75 | + }), |
| 76 | + }, |
| 77 | + ]; |
| 78 | + for test_case in test_cases { |
| 79 | + let check = SyntaxCheck { |
| 80 | + sql: test_case.sql.to_owned(), |
| 81 | + placeholders: test_case |
| 82 | + .placeholders |
| 83 | + .iter() |
| 84 | + .map(|(name, value)| ((*name).to_owned(), (*value).to_owned())) |
| 85 | + .collect(), |
| 86 | + }; |
| 87 | + assert_eq!( |
| 88 | + native_gap(&PivotlessDialect, &check), |
| 89 | + Ok(test_case.expected_gap), |
| 90 | + "{}", |
| 91 | + test_case.description |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[test] |
| 97 | +fn given_a_located_gap_when_skipping_analysis_then_p026_names_the_model_and_the_gap() { |
| 98 | + let gap = LocatedGap { |
| 99 | + construct: "PIVOT".to_owned(), |
| 100 | + line: 3, |
| 101 | + column: 3, |
| 102 | + }; |
| 103 | + assert_eq!( |
| 104 | + skipped_analysis("orders_pivot", "postgres", &gap), |
| 105 | + ( |
| 106 | + "P026", |
| 107 | + "SQL analysis skipped for model 'orders_pivot': the postgres parser cannot read PIVOT \ |
| 108 | + at line 3, column 3" |
| 109 | + .to_owned() |
| 110 | + ) |
| 111 | + ); |
| 112 | +} |
0 commit comments