Skip to content

Commit

Permalink
[#142] group by 관련 테스트코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Jul 28, 2024
1 parent 40a6614 commit 8d10d86
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/parser/implements/dml/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ impl Parser {
}
}
_ => {
return Err(ParsingError::wrap(format!(
"E0304 expected 'FROM' clause. but your input word is '{:?}'",
// From이어야만 이전 루프를 탈출하기 때문에, From이 아닐 수 없음
unreachable!(
"expected 'FROM' clause. but your input word is '{:?}'",
current_token
)));
);
}
}

Expand Down
151 changes: 150 additions & 1 deletion src/parser/test/select.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#![cfg(test)]

use crate::ast::dml::expressions::binary::BinaryOperatorExpression;
use crate::ast::dml::expressions::call::CallExpression;
use crate::ast::dml::expressions::operators::BinaryOperator;
use crate::ast::dml::parts::_where::WhereClause;
use crate::ast::dml::parts::group_by::GroupByItem;
use crate::ast::dml::parts::join::{JoinClause, JoinType};
use crate::ast::dml::parts::order_by::{OrderByItem, OrderByNulls, OrderByType};
use crate::ast::dml::parts::select_item::{SelectItem, SelectWildCard};
use crate::ast::dml::select::SelectQuery;
use crate::ast::types::{SQLExpression, SelectColumn, TableName};
use crate::ast::types::{
AggregateFunction, BuiltInFunction, Function, SQLExpression, SelectColumn, TableName,
};
use crate::lexer::predule::OperatorToken;
use crate::lexer::tokens::Token;
use crate::parser::predule::Parser;
Expand Down Expand Up @@ -1078,6 +1081,81 @@ fn test_select_query() {
.build(),
want_error: false,
},
TestCase {
name: r#"
SELECT
p.content as post
FROM post as p
GROUP BY SELECT;
"#
.into(),
input: vec![
Token::Select,
Token::Identifier("p".into()),
Token::Period,
Token::Identifier("content".into()),
Token::As,
Token::Identifier("post".into()),
Token::From,
Token::Identifier("post".into()),
Token::As,
Token::Identifier("p".into()),
Token::Group,
Token::By,
Token::Select,
Token::SemiColon,
],
expected: Default::default(),
want_error: true,
},
TestCase {
name: r#"
SELECT
p.content as post
FROM post as p
GROUP BY p.content
LIMIT 5;
"#
.into(),
input: vec![
Token::Select,
Token::Identifier("p".into()),
Token::Period,
Token::Identifier("content".into()),
Token::As,
Token::Identifier("post".into()),
Token::From,
Token::Identifier("post".into()),
Token::As,
Token::Identifier("p".into()),
Token::Group,
Token::By,
Token::Identifier("p".into()),
Token::Period,
Token::Identifier("content".into()),
Token::Limit,
Token::Integer(5),
Token::SemiColon,
],
expected: SelectQuery::builder()
.add_select_item(
SelectItem::builder()
.set_item(SelectColumn::new(Some("p".into()), "content".into()).into())
.set_alias("post".into())
.build(),
)
.set_from_table(TableName {
database_name: None,
table_name: "post".into(),
})
.set_from_alias("p".into())
.add_group_by(GroupByItem {
item: SelectColumn::new(Some("p".into()), "content".into()),
})
.set_limit(5)
.build(),
want_error: false,
},
TestCase {
name: r#"
SELECT
Expand Down Expand Up @@ -1168,6 +1246,46 @@ fn test_select_query() {
expected: Default::default(),
want_error: true,
},
TestCase {
name: r#"
SELECT
COUNT(p.a)
FROM post as p
"#
.into(),
input: vec![
Token::Select,
Token::Identifier("count".into()),
Token::LeftParentheses,
Token::Identifier("p".into()),
Token::Period,
Token::Identifier("a".into()),
Token::RightParentheses,
Token::From,
Token::Identifier("post".into()),
Token::As,
Token::Identifier("p".into()),
],
expected: SelectQuery::builder()
.add_select_item(
SelectItem::builder()
.set_item(SQLExpression::FunctionCall(CallExpression {
function: Function::BuiltIn(BuiltInFunction::Aggregate(
AggregateFunction::Count,
)),
arguments: vec![SelectColumn::new(Some("p".into()), "a".into()).into()],
}))
.build(),
)
.set_has_aggregate(true)
.set_from_table(TableName {
database_name: None,
table_name: "post".into(),
})
.set_from_alias("p".into())
.build(),
want_error: false,
},
TestCase {
name: r#"
SELECT
Expand Down Expand Up @@ -1444,6 +1562,37 @@ fn test_select_query() {
.build(),
want_error: false,
},
TestCase {
name: r#"
집계함수가 사용된 컬럼이 group by에 있다면 오류
SELECT
COUNT(p.a)
FROM post as p
GROUP BY p.a
"#
.into(),
input: vec![
Token::Select,
Token::Identifier("count".into()),
Token::LeftParentheses,
Token::Identifier("p".into()),
Token::Period,
Token::Identifier("a".into()),
Token::RightParentheses,
Token::From,
Token::Identifier("post".into()),
Token::As,
Token::Identifier("p".into()),
Token::Group,
Token::By,
Token::Identifier("p".into()),
Token::Period,
Token::Identifier("a".into()),
],
expected: Default::default(),
want_error: true,
},
];

for t in test_cases {
Expand Down

0 comments on commit 8d10d86

Please sign in to comment.