-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#110] handle_create_database_query 테스트코드 재구성
- Loading branch information
Showing
2 changed files
with
61 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |