Skip to content

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

Merged
merged 8 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions prql-compiler/tests/docs/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use prql_compiler::Options;
Copy link
Member

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

We follow the advice in
<https://matklad.github.io/2021/02/27/delete-cargo-integration-tests.html>.

(but we can merge this and make the change / I'll make the change, I don't want to slow you down!)

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();
let re = Regex::new(r"(?s)```(elm|prql)\n(?P<prql>.+?)\n```").unwrap();
assert_ne!(re.find_iter(&contents).count(), 0);
Comment on lines +55 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever regex-ing!

In a way this is similar to

fn collect_book_examples() -> Result<Vec<Example>> {
, but easier to have this separate given they're in different places

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment with a ref

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially thinking of code block analysis using pulldown-cmark, but changed because it seemed like overkill. (And extracting code blocks with pulldown-cmark was too difficult for me!)

Copy link
Member

Choose a reason for hiding this comment

The 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();
}
40 changes: 0 additions & 40 deletions prql-compiler/tests/website/main.rs

This file was deleted.