Skip to content

Commit 69571f1

Browse files
eitsupimax-sixty
andauthored
test(docs): add tests for examples in website hero and repo README (#2710)
Co-authored-by: Maximilian Roos <[email protected]>
1 parent 1b2d86d commit 69571f1

File tree

4 files changed

+78
-42
lines changed

4 files changed

+78
-42
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ filter country == "USA" # Each line transforms the previou
3131
aggregate { # `aggregate` reduces each column to a value
3232
max salary,
3333
min salary,
34-
count, # Trailing commas are allowed
34+
count s"*", # Trailing commas are allowed
3535
}
3636
```
3737

prql-compiler/tests/docs/main.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

prql-compiler/tests/website/main.rs

-40
This file was deleted.

web/website/content/_index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ hero_section:
2323
aggregate {
2424
average total,
2525
sum_income = sum income,
26-
ct = count,
26+
ct = count s"*",
2727
}
2828
)
2929
sort {-sum_income}

0 commit comments

Comments
 (0)