-
Notifications
You must be signed in to change notification settings - Fork 228
test(docs): add tests for examples in website hero and repo README #2710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
b45755d
52380c5
5443c96
a593227
f587948
87a97b8
8c27fdd
74a27dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,75 @@ | ||||
use prql_compiler::Options; | ||||
use regex::Regex; | ||||
use serde_yaml::Value; | ||||
use std::fs; | ||||
|
||||
const WEBSITE_TOPPAGE: &str = "../web/website/content/_index.md"; | ||||
|
||||
fn compile(prql: &str) -> Result<String, prql_compiler::ErrorMessages> { | ||||
prql_compiler::compile(prql, &Options::default().no_signature()) | ||||
} | ||||
|
||||
fn sql_normalize(sql: &str) -> String { | ||||
let re = Regex::new(r"\n\s+").unwrap(); | ||||
re.replace_all(sql, " ").trim().to_string() | ||||
} | ||||
|
||||
fn website_contents() -> Value { | ||||
let contents = fs::read_to_string(WEBSITE_TOPPAGE) | ||||
.unwrap() | ||||
.replace("---", ""); | ||||
|
||||
serde_yaml::from_str::<Value>(&contents).unwrap() | ||||
} | ||||
|
||||
fn website_examples() -> Vec<Value> { | ||||
let value = website_contents(); | ||||
|
||||
value | ||||
.get("showcase_section") | ||||
.unwrap() | ||||
.get("examples") | ||||
.unwrap() | ||||
.as_sequence() | ||||
.unwrap() | ||||
.to_vec() | ||||
} | ||||
|
||||
fn website_hero_example() -> String { | ||||
let value = website_contents(); | ||||
|
||||
value | ||||
.get("hero_section") | ||||
.unwrap() | ||||
.get("prql_example") | ||||
.unwrap() | ||||
.as_str() | ||||
.unwrap() | ||||
.to_string() | ||||
} | ||||
|
||||
#[test] | ||||
fn test_readme_examples() { | ||||
let contents = fs::read_to_string("../README.md").unwrap(); | ||||
max-sixty marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
let re = Regex::new(r"(?s)```(elm|prql)\n(?P<prql>.+?)\n```").unwrap(); | ||||
eitsupi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
assert_ne!(re.find_iter(&contents).count(), 0); | ||||
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clever regex-ing! In a way this is similar to prql/web/book/tests/snapshot.rs Line 152 in 65706a1
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a comment with a ref There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was initially thinking of code block analysis using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I def think writing another parser wouldn't be great! This code is a very good balance IMO |
||||
for cap in re.captures_iter(&contents) { | ||||
let prql = &cap["prql"]; | ||||
compile(prql).unwrap(); | ||||
} | ||||
} | ||||
|
||||
#[test] | ||||
fn test_website_examples() { | ||||
for example in website_examples() { | ||||
let prql = example.get("prql").unwrap().as_str().unwrap(); | ||||
let sql = example.get("sql").unwrap().as_str().unwrap(); | ||||
assert_eq!(sql_normalize(&compile(prql).unwrap()), sql_normalize(sql)); | ||||
} | ||||
} | ||||
|
||||
#[test] | ||||
fn test_website_hero_example() { | ||||
let prql = website_hero_example(); | ||||
compile(&prql).unwrap(); | ||||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW Rust unfortunately has poor performance with making lots of test binaries. So it would be faster if we could have this as a file within this test https://github.com/PRQL/prql/blob/65706a115a84997c608eaeda38b1aef1240fcec3/web/book/tests/snapshot.rs (which would change to a module, like this file is)
See the link at
prql/prql-compiler/tests/integration/README.md
Lines 54 to 55 in f4eb8c7
(but we can merge this and make the change / I'll make the change, I don't want to slow you down!)