Skip to content

Commit

Permalink
Factorize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Mar 15, 2024
1 parent 9b88152 commit 24043e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
57 changes: 28 additions & 29 deletions src/lexer/sql/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,21 @@ fn count_named_placeholders() {

#[test]
fn duplicate_column() {
expect_parser_err(
expect_parser_err_msg(
b"CREATE TABLE t (x TEXT, x TEXT)",
"duplicate column name: x",
);
}

#[test]
fn create_table_without_column() {
let r = parse(b"CREATE TABLE t ()");
let Error::ParserError(
expect_parser_err(
b"CREATE TABLE t ()",
ParserError::SyntaxError {
token_type: "RP",
found: None,
},
_,
) = r.unwrap_err()
else {
panic!("unexpected error type")
};
);
}

#[test]
Expand Down Expand Up @@ -147,44 +143,44 @@ fn extra_comments_between_statements() {

#[test]
fn insert_mismatch_count() {
expect_parser_err(b"INSERT INTO t (a, b) VALUES (1)", "1 values for 2 columns");
expect_parser_err_msg(b"INSERT INTO t (a, b) VALUES (1)", "1 values for 2 columns");
}

#[test]
fn insert_default_values() {
expect_parser_err(
expect_parser_err_msg(
b"INSERT INTO t (a) DEFAULT VALUES",
"0 values for 1 columns",
);
}

#[test]
fn create_view_mismatch_count() {
expect_parser_err(
expect_parser_err_msg(
b"CREATE VIEW v (c1, c2) AS SELECT 1",
"expected 2 columns for v but got 1",
);
}

#[test]
fn create_view_duplicate_column_name() {
expect_parser_err(
expect_parser_err_msg(
b"CREATE VIEW v (c1, c1) AS SELECT 1, 2",
"duplicate column name: c1",
);
}

#[test]
fn create_table_without_rowid_missing_pk() {
expect_parser_err(
expect_parser_err_msg(
b"CREATE TABLE t (c1) WITHOUT ROWID",
"PRIMARY KEY missing on table t",
);
}

#[test]
fn create_temporary_table_with_qualified_name() {
expect_parser_err(
expect_parser_err_msg(
b"CREATE TEMPORARY TABLE mem.x AS SELECT 1",
"temporary table name must be unqualified",
);
Expand All @@ -193,20 +189,20 @@ fn create_temporary_table_with_qualified_name() {

#[test]
fn create_table_with_only_generated_column() {
expect_parser_err(
expect_parser_err_msg(
b"CREATE TABLE test(data AS (1))",
"must have at least one non-generated column",
);
}

#[test]
fn create_strict_table_missing_datatype() {
expect_parser_err(b"CREATE TABLE t (c1) STRICT", "missing datatype for t.c1");
expect_parser_err_msg(b"CREATE TABLE t (c1) STRICT", "missing datatype for t.c1");
}

#[test]
fn create_strict_table_unknown_datatype() {
expect_parser_err(
expect_parser_err_msg(
b"CREATE TABLE t (c1 BOOL) STRICT",
"unknown datatype for t.c1: \"BOOL\"",
);
Expand All @@ -225,76 +221,79 @@ fn create_strict_table_generated_column() {

#[test]
fn selects_compound_mismatch_columns_count() {
expect_parser_err(
expect_parser_err_msg(
b"SELECT 1 UNION SELECT 1, 2",
"SELECTs to the left and right of UNION do not have the same number of result columns",
);
}

#[test]
fn delete_order_by_without_limit() {
expect_parser_err(
expect_parser_err_msg(
b"DELETE FROM t ORDER BY x",
"ORDER BY without LIMIT on DELETE",
);
}

#[test]
fn update_order_by_without_limit() {
expect_parser_err(
expect_parser_err_msg(
b"UPDATE t SET x = 1 ORDER BY x",
"ORDER BY without LIMIT on UPDATE",
);
}

#[test]
fn values_mismatch_columns_count() {
expect_parser_err(
expect_parser_err_msg(
b"INSERT INTO t VALUES (1), (1,2)",
"all VALUES must have the same number of terms",
);
}

#[test]
fn alter_add_column_primary_key() {
expect_parser_err(
expect_parser_err_msg(
b"ALTER TABLE t ADD COLUMN c PRIMARY KEY",
"Cannot add a PRIMARY KEY column",
);
}

#[test]
fn alter_add_column_unique() {
expect_parser_err(
expect_parser_err_msg(
b"ALTER TABLE t ADD COLUMN c UNIQUE",
"Cannot add a UNIQUE column",
);
}

#[test]
fn alter_rename_same() {
expect_parser_err(
expect_parser_err_msg(
b"ALTER TABLE t RENAME TO t",
"there is already another table or index with this name: t",
);
}

#[test]
fn natural_join_on() {
expect_parser_err(
expect_parser_err_msg(
b"SELECT x FROM t NATURAL JOIN t USING (x)",
"a NATURAL join may not have an ON or USING clause",
);
expect_parser_err(
expect_parser_err_msg(
b"SELECT x FROM t NATURAL JOIN t ON t.x = t.x",
"a NATURAL join may not have an ON or USING clause",
);
}

fn expect_parser_err(input: &[u8], error_msg: &str) {
fn expect_parser_err_msg(input: &[u8], error_msg: &str) {
expect_parser_err(input, ParserError::Custom(error_msg.to_owned()))
}
fn expect_parser_err(input: &[u8], err: ParserError) {
let r = parse(input);
if let Error::ParserError(ParserError::Custom(ref msg), _) = r.unwrap_err() {
assert_eq!(msg, error_msg);
if let Error::ParserError(e, _) = r.unwrap_err() {
assert_eq!(e, err);
} else {
panic!("unexpected error type")
};
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::dialect::Token;
use ast::{Cmd, ExplainKind, Name, Stmt};

/// Parser error
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum ParserError {
/// Stack overflow
StackOverflow,
Expand Down

0 comments on commit 24043e9

Please sign in to comment.