-
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.
Browse files
Browse the repository at this point in the history
[#110] parser:ddl 테스트코드 작성
- Loading branch information
Showing
7 changed files
with
1,101 additions
and
101 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
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,100 @@ | ||
#![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(); | ||
fn test_handle_create_database_query() { | ||
struct TestCase { | ||
name: String, | ||
input: Vec<Token>, | ||
expected: SQLStatement, | ||
want_error: bool, | ||
} | ||
|
||
let mut parser = Parser::with_string(text).unwrap(); | ||
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 test_db".into(), | ||
input: vec![Token::Identifier("test_db".to_owned())], | ||
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, | ||
}, | ||
TestCase { | ||
name: "오류: 빈 토큰".into(), | ||
input: vec![], | ||
expected: Default::default(), | ||
want_error: true, | ||
}, | ||
TestCase { | ||
name: "오류: CREATE DATABASE IF NOT EXISTS".into(), | ||
input: vec![Token::If, Token::Not, Token::Exists], | ||
expected: Default::default(), | ||
want_error: true, | ||
}, | ||
TestCase { | ||
name: "CREATE DATABASE IF NOT EXISTS DELETE;".into(), | ||
input: vec![ | ||
Token::If, | ||
Token::Not, | ||
Token::Exists, | ||
Token::Delete, | ||
Token::SemiColon, | ||
], | ||
expected: Default::default(), | ||
want_error: true, | ||
}, | ||
TestCase { | ||
name: "CREATE DATABASE test_db DELETE".into(), | ||
input: vec![Token::Identifier("test_db".to_owned()), Token::Delete], | ||
expected: Default::default(), | ||
want_error: true, | ||
}, | ||
]; | ||
|
||
let expected = CreateDatabaseQuery::builder() | ||
.set_name("test_db".to_owned()) | ||
.set_if_not_exists(true) | ||
.build(); | ||
for t in test_cases { | ||
let mut parser = Parser::new(t.input); | ||
|
||
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 got = parser.handle_create_database_query(); | ||
|
||
let expected = CreateDatabaseQuery::builder() | ||
.set_name("test_db".to_owned()) | ||
.set_if_not_exists(false) | ||
.build(); | ||
assert_eq!( | ||
got.is_err(), | ||
t.want_error, | ||
"TC: {} Error: {:?}", | ||
t.name, | ||
got.err() | ||
); | ||
|
||
assert_eq!( | ||
parser.parse(ParserContext::default()).unwrap(), | ||
vec![expected], | ||
); | ||
if let Ok(alias) = got { | ||
assert_eq!(alias, t.expected, "TC: {}", t.name); | ||
} | ||
} | ||
} |
Oops, something went wrong.