Skip to content

Commit

Permalink
Merge pull request #152 from myyrakle/test/#110
Browse files Browse the repository at this point in the history
[#110] parser:ddl 테스트코드 작성
  • Loading branch information
myyrakle authored Aug 14, 2024
2 parents 0bc585a + f4ba608 commit c6548f3
Show file tree
Hide file tree
Showing 7 changed files with 1,101 additions and 101 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
15 changes: 0 additions & 15 deletions src/parser/implements/ddl/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl Parser {

match current_token {
Token::RightParentheses => {
self.unget_next_token(current_token);
break;
}
_ => {
Expand All @@ -67,20 +66,6 @@ impl Parser {
}
}

// 닫는 괄호 체크
if !self.has_next_token() {
return Err(ParsingError::wrap("E1209 need more tokens"));
}

let current_token = self.get_next_token();

if Token::RightParentheses != current_token {
return Err(ParsingError::wrap(format!(
"E1210 expected ')'. but your input word is '{:?}'",
current_token
)));
}

if !self.has_next_token() {
return Ok(query_builder.build());
}
Expand Down
159 changes: 125 additions & 34 deletions src/parser/test/alter_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,137 @@
use crate::ast::ddl::alter_database::{
AlterDatabaseAction, AlterDatabaseQuery, AlterDatabaseRenameTo,
};
use crate::parser::context::ParserContext;
use crate::ast::SQLStatement;
use crate::lexer::tokens::Token;
use crate::parser::predule::Parser;

#[test]
pub fn alter_database_1() {
let text = r#"
ALTER DATABASE foo RENAME TO bar;
"#
.to_owned();
fn test_handle_alter_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: "ALTER DATABASE foo RENAME TO bar;".into(),
input: vec![
Token::Identifier("foo".to_owned()),
Token::Rename,
Token::To,
Token::Identifier("bar".to_owned()),
Token::SemiColon,
],
expected: AlterDatabaseQuery::builder()
.set_name("foo".to_owned())
.set_action(AlterDatabaseAction::RenameTo(AlterDatabaseRenameTo {
name: "bar".into(),
}))
.build()
.into(),
want_error: false,
},
TestCase {
name: "ALTER DATABASE foo RENAME TO bar".into(),
input: vec![
Token::Identifier("foo".to_owned()),
Token::Rename,
Token::To,
Token::Identifier("bar".to_owned()),
],
expected: AlterDatabaseQuery::builder()
.set_name("foo".to_owned())
.set_action(AlterDatabaseAction::RenameTo(AlterDatabaseRenameTo {
name: "bar".into(),
}))
.build()
.into(),
want_error: false,
},
TestCase {
name: "ALTER DATABASE foo".into(),
input: vec![Token::Identifier("foo".to_owned())],
expected: AlterDatabaseQuery::builder()
.set_name("foo".to_owned())
.build()
.into(),
want_error: false,
},
TestCase {
name: "오류: 빈 토큰".into(),
input: vec![],
expected: Default::default(),
want_error: true,
},
TestCase {
name: "오류: ALTER DATABASE DELETE".into(),
input: vec![Token::Delete],
expected: Default::default(),
want_error: true,
},
TestCase {
name: "오류: ALTER DATABASE foo RENAME".into(),
input: vec![Token::Identifier("foo".to_owned()), Token::Rename],
expected: Default::default(),
want_error: true,
},
TestCase {
name: "오류: ALTER DATABASE foo RENAME CREATE".into(),
input: vec![
Token::Identifier("foo".to_owned()),
Token::Rename,
Token::Create,
],
expected: Default::default(),
want_error: true,
},
TestCase {
name: "오류: ALTER DATABASE foo RENAME TO".into(),
input: vec![
Token::Identifier("foo".to_owned()),
Token::Rename,
Token::To,
],
expected: Default::default(),
want_error: true,
},
TestCase {
name: "오류: ALTER DATABASE foo RENAME TO ALTER".into(),
input: vec![
Token::Identifier("foo".to_owned()),
Token::Rename,
Token::To,
Token::Alter,
],
expected: Default::default(),
want_error: true,
},
TestCase {
name: "오류: ALTER DATABASE foo DELETE".into(),
input: vec![Token::Identifier("foo".to_owned()), Token::Delete],
expected: Default::default(),
want_error: true,
},
];

let expected = AlterDatabaseQuery::builder()
.set_name("foo".to_owned())
.set_action(AlterDatabaseAction::RenameTo(AlterDatabaseRenameTo {
name: "bar".into(),
}))
.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 alter_database_2() {
let text = r#"
ALTER DATABASE foo;
"#
.to_owned();

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

let expected = AlterDatabaseQuery::builder()
.set_name("foo".to_owned())
.build();
assert_eq!(
got.is_err(),
t.want_error,
"{}: want_error: {}, error: {:?}",
t.name,
t.want_error,
got.err()
);

assert_eq!(
parser.parse(ParserContext::default()).unwrap(),
vec![expected],
);
if let Ok(statements) = got {
assert_eq!(statements, t.expected.into(), "TC: {}", t.name);
}
}
}
122 changes: 89 additions & 33 deletions src/parser/test/create_database.rs
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);
}
}
}
Loading

0 comments on commit c6548f3

Please sign in to comment.