Skip to content

Commit

Permalink
add tests && code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xring committed Apr 8, 2024
1 parent 8a9a27c commit 76c79b0
Show file tree
Hide file tree
Showing 47 changed files with 1,295 additions and 936 deletions.
16 changes: 16 additions & 0 deletions .github/ISSUE_TEMPLATE/other.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
name: Other
about: Questions, design proposals, tech debt, etc.
title: ''
labels: triage
assignees: ''

---

*Title*: *One line description*

*Description*:
>Describe the issue.
[optional *Relevant Links*:]
>Any extra documentation required to understand the issue.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
notebook.md
#**/zz_*.rs

/target

Expand Down
35 changes: 34 additions & 1 deletion src/base/algorithm_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nom::IResult;

use base::ParseSQLError;

/// ALGORITHM [=] {DEFAULT | INSTANT | INPLACE | COPY}
/// parse `ALGORITHM [=] {DEFAULT | INSTANT | INPLACE | COPY}`
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum AlgorithmType {
Instant, // alter table only
Expand Down Expand Up @@ -37,3 +37,36 @@ impl AlgorithmType {
)(i)
}
}

#[cfg(test)]
mod tests {
use base::algorithm_type::AlgorithmType;

#[test]
fn parse_algorithm_type() {
let str1 = "ALGORITHM INSTANT";
let res1 = AlgorithmType::parse(str1);
assert!(res1.is_ok());
assert_eq!(res1.unwrap().1, AlgorithmType::Instant);

let str2 = "ALGORITHM=DEFAULT";
let res2 = AlgorithmType::parse(str2);
assert!(res2.is_ok());
assert_eq!(res2.unwrap().1, AlgorithmType::Default);

let str3 = "ALGORITHM= INPLACE";
let res3 = AlgorithmType::parse(str3);
assert!(res3.is_ok());
assert_eq!(res3.unwrap().1, AlgorithmType::Inplace);

let str4 = "ALGORITHM =COPY";
let res4 = AlgorithmType::parse(str4);
assert!(res4.is_ok());
assert_eq!(res4.unwrap().1, AlgorithmType::Copy);

let str5 = "ALGORITHM = DEFAULT";
let res5 = AlgorithmType::parse(str5);
assert!(res5.is_ok());
assert_eq!(res5.unwrap().1, AlgorithmType::Default);
}
}
79 changes: 54 additions & 25 deletions src/base/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use nom::{
IResult,
};

use base::column::Column;
use base::error::ParseSQLErrorKind;
use base::Column;
use base::ParseSQLErrorKind;
use base::{CommonParser, DataType, Literal, ParseSQLError};

#[derive(Debug, Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
Expand Down Expand Up @@ -275,9 +275,9 @@ mod tests {
use super::*;

#[test]
fn it_parses_arithmetic_expressions() {
fn parses_arithmetic_expressions() {
use super::{
ArithmeticBase::{Column as ABColumn, Scalar},
ArithmeticBase::{Column as ArithmeticBaseColumn, Scalar},
ArithmeticOperator::*,
};

Expand Down Expand Up @@ -315,18 +315,33 @@ mod tests {
),
];
let expected_col_lit_ae = [
ArithmeticExpression::new(Add, ABColumn("foo".into()), Scalar(5.into()), None),
ArithmeticExpression::new(Add, ABColumn("foo".into()), Scalar(5.into()), None),
ArithmeticExpression::new(Add, Scalar(5.into()), ABColumn("foo".into()), None),
ArithmeticExpression::new(
Add,
ArithmeticBaseColumn("foo".into()),
Scalar(5.into()),
None,
),
ArithmeticExpression::new(
Add,
ArithmeticBaseColumn("foo".into()),
Scalar(5.into()),
None,
),
ArithmeticExpression::new(
Add,
Scalar(5.into()),
ArithmeticBaseColumn("foo".into()),
None,
),
ArithmeticExpression::new(
Multiply,
ABColumn("foo".into()),
ABColumn("bar".into()),
ArithmeticBaseColumn("foo".into()),
ArithmeticBaseColumn("bar".into()),
Some(String::from("foobar")),
),
ArithmeticExpression::new(
Subtract,
ABColumn(Column {
ArithmeticBaseColumn(Column {
name: String::from("max(foo)"),
alias: None,
table: None,
Expand All @@ -353,19 +368,29 @@ mod tests {
}

#[test]
fn it_displays_arithmetic_expressions() {
fn displays_arithmetic_expressions() {
use super::{
ArithmeticBase::{Column as ABColumn, Scalar},
ArithmeticBase::{Column as ArithmeticBaseColumn, Scalar},
ArithmeticOperator::*,
};

let expressions = [
ArithmeticExpression::new(Add, ABColumn("foo".into()), Scalar(5.into()), None),
ArithmeticExpression::new(Subtract, Scalar(5.into()), ABColumn("foo".into()), None),
ArithmeticExpression::new(
Add,
ArithmeticBaseColumn("foo".into()),
Scalar(5.into()),
None,
),
ArithmeticExpression::new(
Subtract,
Scalar(5.into()),
ArithmeticBaseColumn("foo".into()),
None,
),
ArithmeticExpression::new(
Multiply,
ABColumn("foo".into()),
ABColumn("bar".into()),
ArithmeticBaseColumn("foo".into()),
ArithmeticBaseColumn("bar".into()),
None,
),
ArithmeticExpression::new(Divide, Scalar(10.into()), Scalar(2.into()), None),
Expand All @@ -384,9 +409,9 @@ mod tests {
}

#[test]
fn it_parses_arithmetic_casts() {
fn parses_arithmetic_casts() {
use super::{
ArithmeticBase::{Column as ABColumn, Scalar},
ArithmeticBase::{Column as ArithmeticBaseColumn, Scalar},
ArithmeticOperator::*,
};

Expand All @@ -400,29 +425,33 @@ mod tests {
let expected = [
ArithmeticExpression::new(
Add,
ABColumn(Column::from("t.foo")),
ABColumn(Column::from("t.bar")),
ArithmeticBaseColumn(Column::from("t.foo")),
ArithmeticBaseColumn(Column::from("t.bar")),
None,
),
ArithmeticExpression::new(
Subtract,
Scalar(5.into()),
ArithmeticBaseColumn("foo".into()),
None,
),
ArithmeticExpression::new(Subtract, Scalar(5.into()), ABColumn("foo".into()), None),
ArithmeticExpression::new(
Subtract,
Scalar(5.into()),
ABColumn("foo".into()),
ArithmeticBaseColumn("foo".into()),
Some("5_minus_foo".into()),
),
];

for (i, e) in exprs.iter().enumerate() {
let res = ArithmeticExpression::parse(e);
assert!(res.is_ok(), "{} failed to parse", e);
println!("{:?}", res);
assert_eq!(res.unwrap().1, expected[i]);
}
}

#[test]
fn nested_arithmetic() {
fn parse_nested_arithmetic() {
let qs = [
"1 + 1",
"1 + 2 - 3",
Expand Down Expand Up @@ -483,7 +512,7 @@ mod tests {
}

#[test]
fn arithmetic_scalar() {
fn parse_arithmetic_scalar() {
let qs = "56";
let res = Arithmetic::parse(qs);
assert!(res.is_err());
Expand Down
46 changes: 46 additions & 0 deletions src/base/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use base::condition::ConditionExpression;
use base::error::ParseSQLError;
use base::Literal;

/// ```sql
/// CASE expression
/// WHEN {value1 | condition1} THEN result1
/// ...
/// ELSE resultN
/// END
/// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct CaseWhenExpression {
pub condition: ConditionExpression,
Expand Down Expand Up @@ -78,3 +85,42 @@ impl fmt::Display for ColumnOrLiteral {
Ok(())
}
}

#[cfg(test)]
mod tests {
use base::condition::ConditionBase::{Field, Literal};
use base::condition::ConditionExpression::{Base, ComparisonOp};
use base::condition::ConditionTree;
use base::Literal::Integer;
use base::Operator::Greater;
use base::{CaseWhenExpression, Column, ColumnOrLiteral};

#[test]
fn parse_case() {
let str = "CASE WHEN age > 10 THEN col_name ELSE 22 END;";
let res = CaseWhenExpression::parse(str);

let exp = CaseWhenExpression {
condition: ComparisonOp(ConditionTree {
operator: Greater,
left: Box::new(Base(Field(Column {
name: "age".to_string(),
alias: None,
table: None,
function: None,
}))),
right: Box::new(Base(Literal(Integer(10)))),
}),
then_expr: ColumnOrLiteral::Column(Column {
name: "col_name".to_string(),
alias: None,
table: None,
function: None,
}),
else_expr: Some(ColumnOrLiteral::Literal(Integer(22))),
};

assert!(res.is_ok());
assert_eq!(res.unwrap().1, exp);
}
}
7 changes: 0 additions & 7 deletions src/base/check_constraint.rs

This file was deleted.

Loading

0 comments on commit 76c79b0

Please sign in to comment.