#![warn(clippy::all)]
#[macro_use]
mod test_utils;
use test_utils::*;
use models_parser::ast::*;
use models_parser::dialect::{GenericDialect, SQLiteDialect};
use models_parser::tokenizer::Token;
#[test]
fn parse_create_table_without_rowid() {
let sql = "CREATE TABLE t (a INTEGER) WITHOUT ROWID";
match sqlite_and_generic().verified_stmt(sql) {
Statement::CreateTable(table) => {
assert!(table.without_rowid);
assert_eq!("t", table.name.to_string());
}
_ => unreachable!(),
}
}
#[test]
fn parse_create_virtual_table() {
let sql = "CREATE VIRTUAL TABLE IF NOT EXISTS t USING module_name (arg1, arg2)";
match sqlite_and_generic().verified_stmt(sql) {
Statement::CreateVirtualTable(CreateVirtualTable {
name,
if_not_exists: true,
module_name,
module_args,
}) => {
let args = vec![Ident::new("arg1"), Ident::new("arg2")];
assert_eq!("t", name.to_string());
assert_eq!("module_name", module_name.to_string());
assert_eq!(args, module_args);
}
_ => unreachable!(),
}
let sql = "CREATE VIRTUAL TABLE t USING module_name";
sqlite_and_generic().verified_stmt(sql);
}
#[test]
fn parse_create_table_auto_increment() {
let sql = "CREATE TABLE foo (bar INTEGER PRIMARY KEY AUTOINCREMENT)";
match sqlite_and_generic().verified_stmt(sql) {
Statement::CreateTable(table) => {
let name = table.name;
let columns = table.columns;
assert_eq!(name.to_string(), "foo");
assert_eq!(
vec![ColumnDef {
name: "bar".into(),
data_type: DataType::Int(None),
collation: None,
options: vec![
ColumnOptionDef {
name: None,
option: ColumnOption::Unique { is_primary: true }
},
ColumnOptionDef {
name: None,
option: ColumnOption::DialectSpecific(vec![Token::make_keyword(
"AUTOINCREMENT"
)])
}
],
}],
columns
);
}
_ => unreachable!(),
}
}
#[test]
fn parse_create_sqlite_quote() {
let sql = "CREATE TABLE `PRIMARY` (\"KEY\" INTEGER, [INDEX] INTEGER)";
match sqlite().verified_stmt(sql) {
Statement::CreateTable(table) => {
let columns = table.columns;
let name = table.name;
assert_eq!(name.to_string(), "`PRIMARY`");
assert_eq!(
vec![
ColumnDef {
name: Ident::with_quote('"', "KEY"),
data_type: DataType::Int(None),
collation: None,
options: vec![],
},
ColumnDef {
name: Ident::with_quote('[', "INDEX"),
data_type: DataType::Int(None),
collation: None,
options: vec![],
},
],
columns
);
}
_ => unreachable!(),
}
}
fn sqlite() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})],
}
}
fn sqlite_and_generic() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {}), Box::new(GenericDialect {})],
}
}