Skip to content

Commit 112234d

Browse files
committed
move tests to postgres test module
1 parent 586ac7c commit 112234d

File tree

3 files changed

+66
-111
lines changed

3 files changed

+66
-111
lines changed

src/parser.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -938,32 +938,30 @@ impl Parser {
938938
let exists_keyword = self.parse_keyword("EXISTS");
939939

940940
if !if_keyword && not_keyword {
941-
return Err(
942-
ParserError::ParserError("Expected a table name found: keyword NOT".to_string())
943-
)
941+
return Err(ParserError::ParserError(
942+
"Expected a table name found: keyword NOT".to_string(),
943+
));
944944
}
945945

946946
if if_keyword && !not_keyword && !exists_keyword {
947-
return Err(
948-
ParserError::ParserError("Expected keyword NOT found: table name".to_string())
949-
)
947+
return Err(ParserError::ParserError(
948+
"Expected keyword NOT found: table name".to_string(),
949+
));
950950
}
951951

952952
if if_keyword && !not_keyword {
953-
return Err(
954-
ParserError::ParserError("Expected keyword NOT found: keyword EXISTS".to_string())
955-
)
953+
return Err(ParserError::ParserError(
954+
"Expected keyword NOT found: keyword EXISTS".to_string(),
955+
));
956956
}
957957

958958
if if_keyword && not_keyword && !exists_keyword {
959-
return Err(
960-
ParserError::ParserError("Expected keyword EXISTS found: table name".to_string())
961-
)
959+
return Err(ParserError::ParserError(
960+
"Expected keyword EXISTS found: table name".to_string(),
961+
));
962962
}
963963

964-
let if_not_exists = if_keyword
965-
&& not_keyword
966-
&& exists_keyword;
964+
let if_not_exists = if_keyword && not_keyword && exists_keyword;
967965
let table_name = self.parse_object_name()?;
968966
// parse optional column list (schema)
969967
let (columns, constraints) = self.parse_columns()?;

tests/sqlparser_common.rs

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -996,102 +996,6 @@ fn parse_create_table() {
996996
.contains("Expected column option, found: GARBAGE"));
997997
}
998998

999-
#[test]
1000-
fn parse_create_table_if_not_exists() {
1001-
let sql = "CREATE TABLE IF NOT EXISTS uk_cities (\
1002-
name VARCHAR(100) NOT NULL,\
1003-
lat DOUBLE NULL,\
1004-
lng DOUBLE)";
1005-
let ast = one_statement_parses_to(
1006-
sql,
1007-
"CREATE TABLE IF NOT EXISTS uk_cities (\
1008-
name character varying(100) NOT NULL, \
1009-
lat double NULL, \
1010-
lng double)",
1011-
);
1012-
match ast {
1013-
Statement::CreateTable {
1014-
name,
1015-
columns,
1016-
constraints,
1017-
with_options,
1018-
if_not_exists: true,
1019-
external: false,
1020-
file_format: None,
1021-
location: None,
1022-
} => {
1023-
assert_eq!("uk_cities", name.to_string());
1024-
assert_eq!(
1025-
columns,
1026-
vec![
1027-
ColumnDef {
1028-
name: "name".into(),
1029-
data_type: DataType::Varchar(Some(100)),
1030-
collation: None,
1031-
options: vec![ColumnOptionDef {
1032-
name: None,
1033-
option: ColumnOption::NotNull
1034-
}],
1035-
},
1036-
ColumnDef {
1037-
name: "lat".into(),
1038-
data_type: DataType::Double,
1039-
collation: None,
1040-
options: vec![ColumnOptionDef {
1041-
name: None,
1042-
option: ColumnOption::Null
1043-
}],
1044-
},
1045-
ColumnDef {
1046-
name: "lng".into(),
1047-
data_type: DataType::Double,
1048-
collation: None,
1049-
options: vec![],
1050-
}
1051-
]
1052-
);
1053-
assert!(constraints.is_empty());
1054-
assert_eq!(with_options, vec![]);
1055-
}
1056-
_ => unreachable!(),
1057-
}
1058-
}
1059-
1060-
#[test]
1061-
fn parse_bad_if_not_exists() {
1062-
let res = parse_sql_statements("CREATE TABLE NOT EXISTS uk_cities ()");
1063-
assert_eq!(
1064-
ParserError::ParserError(
1065-
"Expected a table name found: keyword NOT".to_string()
1066-
),
1067-
res.unwrap_err()
1068-
);
1069-
1070-
let res = parse_sql_statements("CREATE TABLE IF EXISTS uk_cities ()");
1071-
assert_eq!(
1072-
ParserError::ParserError(
1073-
"Expected keyword NOT found: keyword EXISTS".to_string()
1074-
),
1075-
res.unwrap_err()
1076-
);
1077-
1078-
let res = parse_sql_statements("CREATE TABLE IF uk_cities ()");
1079-
assert_eq!(
1080-
ParserError::ParserError(
1081-
"Expected keyword NOT found: table name".to_string()
1082-
),
1083-
res.unwrap_err()
1084-
);
1085-
1086-
let res = parse_sql_statements("CREATE TABLE IF NOT uk_cities ()");
1087-
assert_eq!(
1088-
ParserError::ParserError(
1089-
"Expected keyword EXISTS found: table name".to_string()
1090-
),
1091-
res.unwrap_err()
1092-
);
1093-
}
1094-
1095999
#[test]
10961000
fn parse_create_table_with_options() {
10971001
let sql = "CREATE TABLE t (c int) WITH (foo = 'bar', a = 123)";

tests/sqlparser_postgres.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,59 @@ fn parse_create_table_with_inherit() {
226226
pg().verified_stmt(sql);
227227
}
228228

229+
#[test]
230+
fn parse_create_table_if_not_exists() {
231+
let sql = "CREATE TABLE IF NOT EXISTS uk_cities ()";
232+
let ast = pg().one_statement_parses_to(
233+
sql,
234+
"CREATE TABLE IF NOT EXISTS uk_cities ()",
235+
);
236+
match ast {
237+
Statement::CreateTable {
238+
name,
239+
columns: _columns,
240+
constraints,
241+
with_options,
242+
if_not_exists: true,
243+
external: false,
244+
file_format: None,
245+
location: None,
246+
} => {
247+
assert_eq!("uk_cities", name.to_string());
248+
assert!(constraints.is_empty());
249+
assert_eq!(with_options, vec![]);
250+
}
251+
_ => unreachable!(),
252+
}
253+
}
254+
255+
#[test]
256+
fn parse_bad_if_not_exists() {
257+
let res = pg().parse_sql_statements("CREATE TABLE NOT EXISTS uk_cities ()");
258+
assert_eq!(
259+
ParserError::ParserError("Expected a table name found: keyword NOT".to_string()),
260+
res.unwrap_err()
261+
);
262+
263+
let res = pg().parse_sql_statements("CREATE TABLE IF EXISTS uk_cities ()");
264+
assert_eq!(
265+
ParserError::ParserError("Expected keyword NOT found: keyword EXISTS".to_string()),
266+
res.unwrap_err()
267+
);
268+
269+
let res = pg().parse_sql_statements("CREATE TABLE IF uk_cities ()");
270+
assert_eq!(
271+
ParserError::ParserError("Expected keyword NOT found: table name".to_string()),
272+
res.unwrap_err()
273+
);
274+
275+
let res = pg().parse_sql_statements("CREATE TABLE IF NOT uk_cities ()");
276+
assert_eq!(
277+
ParserError::ParserError("Expected keyword EXISTS found: table name".to_string()),
278+
res.unwrap_err()
279+
);
280+
}
281+
229282
#[test]
230283
fn parse_copy_example() {
231284
let sql = r#"COPY public.actor (actor_id, first_name, last_name, last_update, value) FROM stdin;

0 commit comments

Comments
 (0)