Skip to content
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

Add additional test for PL/pgSQL parsing #32

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
91 changes: 91 additions & 0 deletions tests/data/plpgsql_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[
{
"PLpgSQL_function": {
"action": {
"PLpgSQL_stmt_block": {
"body": [
{
"PLpgSQL_stmt_execsql": {
"into": true,
"lineno": 5,
"sqlstmt": {
"PLpgSQL_expr": {
"query": "SELECT details FROM t WHERE col = input"
}
},
"target": {
"PLpgSQL_row": {
"fields": [
{
"name": "result",
"varno": 2
}
],
"lineno": 5,
"refname": "(unnamed row)"
}
}
}
},
{
"PLpgSQL_stmt_return": {
"expr": {
"PLpgSQL_expr": {
"query": "result"
}
},
"lineno": 6
}
}
],
"lineno": 4
}
},
"datums": [
{
"PLpgSQL_var": {
"datatype": {
"PLpgSQL_type": {
"typname": "UNKNOWN"
}
},
"refname": "input"
Copy link
Member Author

Choose a reason for hiding this comment

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

It's surprising that it couldn't find the data type for input, since it's clearly listed in the function arguments.

Copy link
Member

Choose a reason for hiding this comment

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

I think that might be a limitation of the libpg_query logic here - we could investigate and see if we can fix that, if helpful.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not strictly needed for what we're looking at internally; it was just surprising.

}
},
{
"PLpgSQL_var": {
"datatype": {
"PLpgSQL_type": {
"typname": "UNKNOWN"
}
},
"refname": "found"
}
},
{
"PLpgSQL_var": {
"datatype": {
"PLpgSQL_type": {
"typname": "jsonb"
}
},
"lineno": 3,
"refname": "result"
}
},
{
"PLpgSQL_row": {
"fields": [
{
"name": "result",
"varno": 2
}
],
"lineno": 5,
"refname": "(unnamed row)"
}
}
]
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"query": "v_version IS NULL"
}
},
"lineno": 1,
"lineno": 3,
"then_body": [
{
"PLpgSQL_stmt_return": {
Expand All @@ -20,7 +20,7 @@
"query": "v_name"
}
},
"lineno": 1
"lineno": 4
}
}
]
Expand All @@ -33,11 +33,11 @@
"query": "v_name || '/' || v_version"
}
},
"lineno": 1
"lineno": 6
}
}
],
"lineno": 1
"lineno": 2
}
},
"datums": [
Expand Down
47 changes: 37 additions & 10 deletions tests/parse_plpgsql_tests.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
#[macro_use]
mod support;
use support::*;

Check warning on line 3 in tests/parse_plpgsql_tests.rs

View workflow job for this annotation

GitHub Actions / ci (stable)

unused import: `support::*`

Check warning on line 3 in tests/parse_plpgsql_tests.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

unused import: `support::*`

#[test]
fn it_can_parse_a_simple_function() {
let result = pg_query::parse_plpgsql(
" \
CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name varchar, v_version varchar) \
RETURNS varchar AS $$ \
BEGIN \
IF v_version IS NULL THEN \
RETURN v_name; \
END IF; \
RETURN v_name || '/' || v_version; \
"
CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name varchar, v_version varchar)
RETURNS varchar AS $$
BEGIN
IF v_version IS NULL THEN
RETURN v_name;
END IF;
RETURN v_name || '/' || v_version;
END; \
$$ LANGUAGE plpgsql;",
$$ LANGUAGE plpgsql;
",
);
assert!(result.is_ok());
let result = result.unwrap();
let expected = include_str!("data/plpgsql_simple.json");
let actual = serde_json::to_string_pretty(&result).unwrap();
assert_eq!(expected, &actual);
}

#[test]
fn it_can_parse_a_query_function() {
let result = pg_query::parse_plpgsql(
"
CREATE OR REPLACE FUNCTION fn(input integer) RETURNS jsonb LANGUAGE plpgsql STABLE AS
'
DECLARE
result jsonb;
BEGIN
SELECT details FROM t INTO result WHERE col = input;
RETURN result;
END;
';
",
);
assert!(result.is_ok());
let result = result.unwrap();
let expected = include_str!("data/simple_plpgsql.json");
let expected = include_str!("data/plpgsql_query.json");
let actual = serde_json::to_string_pretty(&result).unwrap();
assert_eq!(expected, actual);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ macro_rules! assert_debug_eq {
};
}

macro_rules! assert_eq {
($left:expr, $right:expr) => {
if let Ok(_diff) = std::env::var("DIFF") {
pretty_assertions::assert_eq!($left, $right);
} else {
std::assert_eq!($left, $right);
}
};
}

pub fn assert_vec_matches<T: PartialEq>(a: &Vec<T>, b: &Vec<T>) {
let matching = a.iter().zip(b.iter()).filter(|&(a, b)| a == b).count();
assert!(matching == a.len() && matching == b.len())
Expand Down
Loading