Skip to content

Commit 819e303

Browse files
authored
test: integration, compile all dialects and diff (#5344)
1 parent d518251 commit 819e303

File tree

30 files changed

+1701
-11
lines changed

30 files changed

+1701
-11
lines changed

.config/nextest.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ fail-fast = false
33
failure-output = "final"
44
slow-timeout = {period = "500ms", terminate-after = 4}
55

6+
[[profile.default.overrides]]
7+
# compileall does many passes over the same query, so it takes a bit longer
8+
filter = 'package(prqlc) & test(queries::compileall::)'
9+
slow-timeout = {period = "10s", terminate-after = 2}
10+
611
[[profile.default.overrides]]
712
filter = 'package(prqlc) & test(queries::results::)'
813
slow-timeout = {period = "10s", terminate-after = 4}

prqlc/prqlc/tests/integration/queries.rs

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{env, fs};
55
use insta::assert_debug_snapshot;
66
use insta::{assert_snapshot, with_settings};
77
use prqlc::sql::Dialect;
8+
use prqlc::sql::SupportLevel;
89
use prqlc::{Options, Target};
910
use test_each_file::test_each_path;
1011

@@ -50,6 +51,64 @@ mod compile {
5051
}
5152
}
5253

54+
fn should_run_query(dialect: Dialect, prql: &str) -> bool {
55+
let dialect_str = dialect.to_string().to_lowercase();
56+
57+
match dialect.support_level() {
58+
SupportLevel::Supported => !prql.contains(&format!("{dialect_str}:skip")),
59+
SupportLevel::Unsupported => prql.contains(&format!("{dialect_str}:test")),
60+
SupportLevel::Nascent => false,
61+
}
62+
}
63+
64+
mod compileall {
65+
use super::*;
66+
use similar::TextDiff;
67+
use strum::IntoEnumIterator;
68+
69+
test_each_path! { in "./prqlc/prqlc/tests/integration/queries" => run }
70+
71+
fn run(prql_path: &Path) {
72+
let test_name = prql_path.file_stem().unwrap().to_str().unwrap();
73+
let prql = fs::read_to_string(prql_path).unwrap();
74+
if prql.contains("generic:skip") {
75+
return;
76+
}
77+
78+
// first compile with the generic dialect
79+
let target = Target::Sql(Some(Dialect::Generic));
80+
let options = Options::default().no_signature().with_target(target);
81+
82+
let generic_sql = prqlc::compile(&prql, &options).unwrap();
83+
84+
// next compile with each dialect
85+
let mut diffsnap = "".to_owned();
86+
for dialect in Dialect::iter() {
87+
if !should_run_query(dialect, &prql) {
88+
continue;
89+
}
90+
91+
let dialect_target = Target::Sql(Some(dialect));
92+
let dialect_options = Options::default()
93+
.no_signature()
94+
.with_target(dialect_target);
95+
96+
let dialect_sql = prqlc::compile(&prql, &dialect_options).unwrap();
97+
98+
let diff = TextDiff::from_lines(&generic_sql, &dialect_sql);
99+
diffsnap = format!(
100+
"{diffsnap}\n{}",
101+
diff.unified_diff()
102+
.context_radius(10)
103+
.header("generic", &dialect.to_string())
104+
);
105+
}
106+
with_settings!({ input_file => prql_path }, {
107+
assert_snapshot!(test_name, diffsnap, &prql)
108+
});
109+
}
110+
}
111+
53112
mod fmt {
54113
use super::*;
55114

@@ -96,23 +155,12 @@ mod debug_lineage {
96155
mod results {
97156

98157
use itertools::Itertools;
99-
use prqlc::sql::SupportLevel;
100158

101159
use super::*;
102160
use crate::dbs::{batch_to_csv, runners};
103161

104162
test_each_path! { in "./prqlc/prqlc/tests/integration/queries" => run }
105163

106-
fn should_run_query(dialect: Dialect, prql: &str) -> bool {
107-
let dialect_str = dialect.to_string().to_lowercase();
108-
109-
match dialect.support_level() {
110-
SupportLevel::Supported => !prql.contains(&format!("{dialect_str}:skip")),
111-
SupportLevel::Unsupported => prql.contains(&format!("{dialect_str}:test")),
112-
SupportLevel::Nascent => false,
113-
}
114-
}
115-
116164
fn run(prql_path: &Path) {
117165
let test_name = prql_path.file_stem().unwrap().to_str().unwrap();
118166
let prql = fs::read_to_string(prql_path).unwrap();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: prqlc/prqlc/tests/integration/queries.rs
3+
expression: "# mysql:skip\n# clickhouse:skip\n# glaredb:skip (the string_agg function is not supported)\nfrom tracks\nfilter genre_id == 100\nderive empty_name = name == ''\naggregate {sum track_id, concat_array name, all empty_name, any empty_name}\n"
4+
input_file: prqlc/prqlc/tests/integration/queries/aggregation.prql
5+
---
6+
--- generic
7+
+++ sqlite
8+
@@ -1,9 +1,9 @@
9+
SELECT
10+
COALESCE(SUM(track_id), 0),
11+
- COALESCE(STRING_AGG(name, ''), ''),
12+
- COALESCE(BOOL_AND(name = ''), TRUE),
13+
- COALESCE(BOOL_OR(name = ''), FALSE)
14+
+ COALESCE(GROUP_CONCAT(name, ''), ''),
15+
+ COALESCE(MIN(name = '') > 0, TRUE),
16+
+ COALESCE(MAX(name = '') > 0, FALSE)
17+
FROM
18+
tracks
19+
WHERE
20+
genre_id = 100
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: prqlc/prqlc/tests/integration/queries.rs
3+
expression: "from invoices\nselect { customer_id, invoice_id, billing_country }\ntake 10..15\nappend (\n from invoices\n select { customer_id, invoice_id, billing_country }\n take 40..45\n)\nselect { billing_country, invoice_id }\n"
4+
input_file: prqlc/prqlc/tests/integration/queries/append_select.prql
5+
---
6+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
source: prqlc/prqlc/tests/integration/queries.rs
3+
expression: "from invoices\nselect { customer_id, invoice_id, total }\ntake 5\nappend (\n from invoice_items\n select { invoice_line_id, invoice_id, unit_price }\n take 5\n)\nselect { a = customer_id * 2, b = math.round 1 (invoice_id * total) }\n"
4+
input_file: prqlc/prqlc/tests/integration/queries/append_select_compute.prql
5+
---
6+
--- generic
7+
+++ glaredb
8+
@@ -23,13 +23,13 @@
9+
) AS table_2
10+
UNION
11+
ALL
12+
SELECT
13+
*
14+
FROM
15+
table_0
16+
)
17+
SELECT
18+
customer_id * 2 AS a,
19+
- ROUND(invoice_id * total, 1) AS b
20+
+ ROUND((invoice_id * total)::numeric, 1) AS b
21+
FROM
22+
table_1
23+
24+
25+
--- generic
26+
+++ postgres
27+
@@ -23,13 +23,13 @@
28+
) AS table_2
29+
UNION
30+
ALL
31+
SELECT
32+
*
33+
FROM
34+
table_0
35+
)
36+
SELECT
37+
customer_id * 2 AS a,
38+
- ROUND(invoice_id * total, 1) AS b
39+
+ ROUND((invoice_id * total)::numeric, 1) AS b
40+
FROM
41+
table_1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: prqlc/prqlc/tests/integration/queries.rs
3+
expression: "from invoices\nselect { customer_id, invoice_id, billing_country }\ntake 5\nappend (\n from employees\n select { employee_id, employee_id, country }\n take 5\n)\nappend (\n from invoice_items\n select { invoice_line_id, invoice_id, null }\n take 5\n)\nselect { billing_country, invoice_id }\n"
4+
input_file: prqlc/prqlc/tests/integration/queries/append_select_multiple_with_null.prql
5+
---
6+

0 commit comments

Comments
 (0)