Skip to content

Commit

Permalink
added tests and a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
takaebato committed Dec 17, 2023
1 parent 374985d commit f2ad35e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
44 changes: 44 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,50 @@ pub fn all_dialects() -> TestedDialects {
}
}

static ALL_DIALECT_NAMES: &[&str] = &[
"generic",
"mysql",
"postgresql",
"hive",
"sqlite",
"snowflake",
"redshift",
"mssql",
"clickhouse",
"bigquery",
"ansi",
"duckdb",
];

pub fn partition_all_dialects_by_inclusion(
dialect_names: Vec<&str>,
) -> (TestedDialects, TestedDialects) {
for dialect_name in &dialect_names {
assert!(
ALL_DIALECT_NAMES.contains(dialect_name),
"Unknown dialect: {}",
dialect_name
);
}

let (included_dialect_names, excluded_dialect_names) = ALL_DIALECT_NAMES
.iter()
.partition(|&dialect_name| dialect_names.contains(dialect_name));

let build_tested_dialects = |names: Vec<&str>| TestedDialects {
dialects: names
.iter()
.map(|&name| dialect_from_str(name).unwrap())
.collect(),
options: None,
};

(
build_tested_dialects(included_dialect_names),
build_tested_dialects(excluded_dialect_names),
)
}

pub fn assert_eq_vec<T: ToString>(expected: &[&str], actual: &[T]) {
assert_eq!(
expected,
Expand Down
33 changes: 26 additions & 7 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use test_utils::*;
use sqlparser::ast::SelectItem::UnnamedExpr;
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, SQLiteDialect};
use sqlparser::parser::ParserOptions;
use sqlparser::parser::{ParserError, ParserOptions};
use sqlparser::tokenizer::Token;

#[test]
Expand Down Expand Up @@ -433,12 +433,31 @@ fn invalid_empty_list() {

#[test]
fn parse_start_transaction_with_modifier() {
sqlite_and_generic().verified_stmt("BEGIN DEFERRED TRANSACTION");
sqlite_and_generic().verified_stmt("BEGIN IMMEDIATE TRANSACTION");
sqlite_and_generic().verified_stmt("BEGIN EXCLUSIVE TRANSACTION");
sqlite_and_generic().one_statement_parses_to("BEGIN DEFERRED", "BEGIN DEFERRED TRANSACTION");
sqlite_and_generic().one_statement_parses_to("BEGIN IMMEDIATE", "BEGIN IMMEDIATE TRANSACTION");
sqlite_and_generic().one_statement_parses_to("BEGIN EXCLUSIVE", "BEGIN EXCLUSIVE TRANSACTION");
let (supported_dialects, unsupported_dialects) =
partition_all_dialects_by_inclusion(vec!["generic", "sqlite"]);

supported_dialects.verified_stmt("BEGIN DEFERRED TRANSACTION");
supported_dialects.verified_stmt("BEGIN IMMEDIATE TRANSACTION");
supported_dialects.verified_stmt("BEGIN EXCLUSIVE TRANSACTION");
supported_dialects.one_statement_parses_to("BEGIN DEFERRED", "BEGIN DEFERRED TRANSACTION");
supported_dialects.one_statement_parses_to("BEGIN IMMEDIATE", "BEGIN IMMEDIATE TRANSACTION");
supported_dialects.one_statement_parses_to("BEGIN EXCLUSIVE", "BEGIN EXCLUSIVE TRANSACTION");

let res = unsupported_dialects.parse_sql_statements("BEGIN DEFERRED");
assert_eq!(
ParserError::ParserError("Expected end of statement, found: DEFERRED".to_string()),
res.unwrap_err(),
);
let res = unsupported_dialects.parse_sql_statements("BEGIN IMMEDIATE");
assert_eq!(
ParserError::ParserError("Expected end of statement, found: IMMEDIATE".to_string()),
res.unwrap_err(),
);
let res = unsupported_dialects.parse_sql_statements("BEGIN EXCLUSIVE");
assert_eq!(
ParserError::ParserError("Expected end of statement, found: EXCLUSIVE".to_string()),
res.unwrap_err(),
);
}

fn sqlite() -> TestedDialects {
Expand Down

0 comments on commit f2ad35e

Please sign in to comment.