Skip to content

Commit

Permalink
[#110] handle_create_database_query 테스트코드 재구성
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Aug 13, 2024
1 parent 0bc585a commit c854da3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 42 deletions.
10 changes: 6 additions & 4 deletions src/parser/implements/ddl/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ impl Parser {
}
_ => {
return Err(ParsingError::wrap(
"not supported command. possible commands: (create database)",
format!(
"not supported command. possible commands: (create database): {current_token:?}",
)
));
}
}
Expand Down Expand Up @@ -74,9 +76,9 @@ impl Parser {
query_builder = query_builder.set_name(identifier);
}
_ => {
return Err(ParsingError::wrap(
"not supported command. possible commands: (create database)",
));
return Err(ParsingError::wrap(format!(
"not supported command. possible commands: (drop database): {current_token:?}",
)));
}
}

Expand Down
93 changes: 55 additions & 38 deletions src/parser/test/create_database.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
#![cfg(test)]
use crate::ast::ddl::create_database::CreateDatabaseQuery;
use crate::parser::context::ParserContext;
use crate::ast::SQLStatement;
use crate::lexer::tokens::Token;
use crate::parser::predule::Parser;

#[test]
pub fn create_database_1() {
let text = r#"
CREATE DATABASE IF Not exists test_db;
"#
.to_owned();

let mut parser = Parser::with_string(text).unwrap();

let expected = CreateDatabaseQuery::builder()
.set_name("test_db".to_owned())
.set_if_not_exists(true)
.build();

assert_eq!(
parser.parse(ParserContext::default()).unwrap(),
vec![expected],
);
}

#[test]
pub fn create_database_2() {
let text = r#"
CREATE DATABASE test_db;
"#
.to_owned();

let mut parser = Parser::with_string(text).unwrap();

let expected = CreateDatabaseQuery::builder()
.set_name("test_db".to_owned())
.set_if_not_exists(false)
.build();

assert_eq!(
parser.parse(ParserContext::default()).unwrap(),
vec![expected],
);
fn test_handle_create_database_query() {
struct TestCase {
name: String,
input: Vec<Token>,
expected: SQLStatement,
want_error: bool,
}

let test_cases = vec![
TestCase {
name: "CREATE DATABASE test_db;".into(),
input: vec![Token::Identifier("test_db".to_owned()), Token::SemiColon],
expected: CreateDatabaseQuery::builder()
.set_name("test_db".to_owned())
.build()
.into(),
want_error: false,
},
TestCase {
name: "CREATE DATABASE IF NOT EXISTS test_db;".into(),
input: vec![
Token::If,
Token::Not,
Token::Exists,
Token::Identifier("test_db".to_owned()),
Token::SemiColon,
],
expected: CreateDatabaseQuery::builder()
.set_name("test_db".to_owned())
.set_if_not_exists(true)
.build()
.into(),
want_error: false,
},
];

for t in test_cases {
let mut parser = Parser::new(t.input);

let got = parser.handle_create_database_query();

assert_eq!(
got.is_err(),
t.want_error,
"TC: {} Error: {:?}",
t.name,
got.err()
);

if let Ok(alias) = got {
assert_eq!(alias, t.expected, "TC: {}", t.name);
}
}
}

0 comments on commit c854da3

Please sign in to comment.