|
| 1 | +use prql_compiler::Options; |
| 2 | +use regex::Regex; |
| 3 | +use serde_yaml::Value; |
| 4 | +use std::fs; |
| 5 | + |
| 6 | +const WEBSITE_TOPPAGE: &str = "../web/website/content/_index.md"; |
| 7 | + |
| 8 | +fn compile(prql: &str) -> Result<String, prql_compiler::ErrorMessages> { |
| 9 | + prql_compiler::compile(prql, &Options::default().no_signature()) |
| 10 | +} |
| 11 | + |
| 12 | +fn sql_normalize(sql: &str) -> String { |
| 13 | + let re = Regex::new(r"\n\s+").unwrap(); |
| 14 | + re.replace_all(sql, " ").trim().to_string() |
| 15 | +} |
| 16 | + |
| 17 | +fn website_contents() -> Value { |
| 18 | + let contents = fs::read_to_string(WEBSITE_TOPPAGE) |
| 19 | + .unwrap() |
| 20 | + .replace("---", ""); |
| 21 | + |
| 22 | + serde_yaml::from_str::<Value>(&contents).unwrap() |
| 23 | +} |
| 24 | + |
| 25 | +fn website_examples() -> Vec<Value> { |
| 26 | + let value = website_contents(); |
| 27 | + |
| 28 | + value |
| 29 | + .get("showcase_section") |
| 30 | + .unwrap() |
| 31 | + .get("examples") |
| 32 | + .unwrap() |
| 33 | + .as_sequence() |
| 34 | + .unwrap() |
| 35 | + .to_vec() |
| 36 | +} |
| 37 | + |
| 38 | +fn website_hero_example() -> String { |
| 39 | + let value = website_contents(); |
| 40 | + |
| 41 | + value |
| 42 | + .get("hero_section") |
| 43 | + .unwrap() |
| 44 | + .get("prql_example") |
| 45 | + .unwrap() |
| 46 | + .as_str() |
| 47 | + .unwrap() |
| 48 | + .to_string() |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn test_readme_examples() { |
| 53 | + let contents = fs::read_to_string("../README.md").unwrap(); |
| 54 | + // Similar to code at https://github.com/PRQL/prql/blob/65706a115a84997c608eaeda38b1aef1240fcec3/web/book/tests/snapshot.rs#L152, but specialized for the Readme. |
| 55 | + let re = Regex::new(r"(?s)```(elm|prql)\n(?P<prql>.+?)\n```").unwrap(); |
| 56 | + assert_ne!(re.find_iter(&contents).count(), 0); |
| 57 | + for cap in re.captures_iter(&contents) { |
| 58 | + let prql = &cap["prql"]; |
| 59 | + compile(prql).unwrap(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn test_website_examples() { |
| 65 | + for example in website_examples() { |
| 66 | + let prql = example.get("prql").unwrap().as_str().unwrap(); |
| 67 | + let sql = example.get("sql").unwrap().as_str().unwrap(); |
| 68 | + assert_eq!(sql_normalize(&compile(prql).unwrap()), sql_normalize(sql)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_website_hero_example() { |
| 74 | + let prql = website_hero_example(); |
| 75 | + compile(&prql).unwrap(); |
| 76 | +} |
0 commit comments