Skip to content

Commit

Permalink
feat: err handler coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Mar 11, 2024
1 parent d649a51 commit bba51f6
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/analyzer/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub enum Err {
InvalidDefaultValue { raw_value: String, raw_type: String },
#[display(fmt = "Default null in non-nullable: the default value cannot be null in non-nullable field")]
DefaultNullInNonNullable,
#[display(fmt = "Data type exceeded: the defualt value exceeds the maximum value of '{}'", raw_type)]
#[display(fmt = "Data type exceeded: the default value exceeds the maximum value of '{}'", raw_type)]
DataTypeExceeded { raw_type: String },
#[display(fmt = "Table group not found")]
TableGroupNotFound,
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ impl IndexedRef {
let composition_cols = BTreeSet::from_iter(compositions.iter().map(|s| s.to_string.clone()));
let indexes_cols = BTreeSet::from_iter(def_item.cols.iter().filter_map(|s| {
match s {
IndexesColumnType::String(s) => Some(s.clone()),
IndexesColumnType::String(s) => Some(s.to_string.clone()),
_ => None
}
}));
Expand Down
35 changes: 20 additions & 15 deletions src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
})
.cloned()
.collect();
let ident_strings: Vec<_> = idents.iter().map(|s| s.to_string.clone()).collect();

for ident in &def.cols {
if let IndexesColumnType::String(col_name) = ident {
if table.cols.iter().find(|col| &col.name.to_string == col_name).is_none() {
throw_err(Err::ColumnNotFound, &(0..0), input)?; // FIXME:
if table.cols.iter().find(|col| col.name.to_string == col_name.to_string).is_none() {
throw_err(Err::ColumnNotFound, &col_name.span_range, input)?;
}
}
}
Expand All @@ -165,29 +166,29 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
throw_err(Err::DuplicatedPrimaryKey, &def.span_range, input)?;
}

tmp_table_indexer.pk_list.extend(idents.clone())
tmp_table_indexer.pk_list.extend(ident_strings.clone())
} else if settings.is_unique {
if tmp_table_indexer.unique_list.iter().any(|uniq_item| idents.iter().all(|id| uniq_item.contains(id))) {
if tmp_table_indexer.unique_list.iter().any(|uniq_item| idents.iter().all(|id| uniq_item.contains(&id.to_string))) {
throw_err(Err::DuplicatedUniqueKey, &def.span_range, input)?;
}

tmp_table_indexer.unique_list.push(idents.clone().into_iter().collect())
tmp_table_indexer.unique_list.push(ident_strings.clone().into_iter().collect())
}

if settings.r#type.is_some() {
if tmp_table_indexer.indexed_list.iter().any(|(idx_item, idx_type)| idx_item == &idents && idx_type == &settings.r#type) {
if tmp_table_indexer.indexed_list.iter().any(|(idx_item, idx_type)| idx_item == &ident_strings && idx_type == &settings.r#type) {
throw_err(Err::DuplicatedIndexKey, &def.span_range, input)?;
}

tmp_table_indexer.indexed_list.push((idents, settings.r#type.clone()));
tmp_table_indexer.indexed_list.push((ident_strings, settings.r#type.clone()));
}
}
None => {
if tmp_table_indexer.indexed_list.iter().any(|(idx_item, _)| idx_item == &idents) {
if tmp_table_indexer.indexed_list.iter().any(|(idx_item, _)| idx_item == &ident_strings) {
throw_err(Err::DuplicatedIndexKey, &def.span_range, input)?;
}

tmp_table_indexer.indexed_list.push((idents, None))
tmp_table_indexer.indexed_list.push((ident_strings, None))
},
};
}
Expand Down Expand Up @@ -299,6 +300,8 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
match default_value {
Value::Enum(_) => (),
Value::String(val) => {
let err = Err::InvalidDefaultValue { raw_value: val.clone(), raw_type: col.r#type.raw.clone() };

if !matches!(
type_name,
ColumnTypeName::Bit
Expand All @@ -307,25 +310,27 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
| ColumnTypeName::VarChar
| ColumnTypeName::Enum(_)
) {
throw_err(Err::InvalidDefaultValue { raw_value: val.clone(), raw_type: col.r#type.raw.clone() }, &span_range, input)?;
throw_err(err.clone(), &span_range, input)?;
}

// validate fixed and variable length data type
match type_name {
ColumnTypeName::Bit
| ColumnTypeName::Char
if matches!(col.r#type.args[0], Value::Integer(len) if val.len() as i64 != len) => {
panic!("defualt value does not match with the specified fixed length")
throw_err(err.clone(), &span_range, input)?;
}
ColumnTypeName::Varbit
| ColumnTypeName::VarChar
if matches!(col.r#type.args[0], Value::Integer(cap) if val.len() as i64 > cap) => {
panic!("defualt value exceeds the specified variable length")
throw_err(err.clone(), &span_range, input)?;
}
_ => ()
};
},
Value::Integer(val) => {
let err = Err::DataTypeExceeded { raw_type: col.r#type.raw.clone() };

if !matches!(
type_name,
ColumnTypeName::SmallSerial
Expand All @@ -341,15 +346,15 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
match type_name {
ColumnTypeName::SmallInt
if (val > i16::MAX as i64) || (val < i16::MIN as i64) => {
panic!("defualt value exceeds the maximum value of the specified type")
throw_err(err.clone(), &span_range, input)?;
}
ColumnTypeName::Integer
if (val > i32::MAX as i64) || (val < i32::MIN as i64) => {
panic!("defualt value exceeds the maximum value of the specified type")
throw_err(err.clone(), &span_range, input)?;
}
ColumnTypeName::BigInt
if val.overflowing_add(1).1 || val.overflowing_sub(1).1 => {
panic!("defualt value exceeds the maximum value of the specified type")
throw_err(err.clone(), &span_range, input)?;
}
_ => ()
};
Expand Down
6 changes: 3 additions & 3 deletions src/ast/indexes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use super::{Attribute, SpanRange};
use super::*;

#[derive(Debug, Clone, Default)]
pub struct IndexesBlock {
Expand Down Expand Up @@ -39,8 +39,8 @@ pub struct IndexesSettings {

#[derive(Debug, Clone)]
pub enum IndexesColumnType {
String(String),
Expr(String),
String(Ident),
Expr(Literal),
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down
10 changes: 7 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,18 +732,22 @@ fn parse_indexes_ident(pair: Pair<Rule>) -> ParserResult<IndexesColumnType> {
match p1.as_rule() {
Rule::ident => {
let value = parse_ident(p1)?;
Ok(IndexesColumnType::String(value.to_string))
Ok(IndexesColumnType::String(value))
}
Rule::backquoted_quoted_string => {
let p2 = p1
.clone()
.into_inner()
.next()
.ok_or_else(|| unreachable!("something went wrong at indexes_ident"))?;

match p2.as_rule() {
Rule::backquoted_quoted_value => {
let value = p2.as_str().to_string();
Ok(IndexesColumnType::Expr(value))
Ok(IndexesColumnType::Expr(Literal {
span_range: s2r(p1.as_span()),
raw: p1.as_str().to_owned(),
value: Value::String(p2.as_str().to_owned())
}))
}
_ => throw_rules(&[Rule::backquoted_quoted_value], p2)?,
}
Expand Down
12 changes: 10 additions & 2 deletions tests/out/comment.in.ron
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ SchemaBlock {
span_range: 223..238,
cols: [
String(
"id",
Ident {
span_range: 223..225,
raw: "id",
to_string: "id",
},
),
],
settings: Some(
Expand Down Expand Up @@ -256,7 +260,11 @@ SchemaBlock {
span_range: 250..270,
cols: [
String(
"created_at",
Ident {
span_range: 250..260,
raw: "created_at",
to_string: "created_at",
},
),
],
settings: None,
Expand Down
24 changes: 20 additions & 4 deletions tests/out/composite_pk.in.ron
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ SchemaBlock {
span_range: 211..218,
cols: [
String(
"id",
Ident {
span_range: 211..213,
raw: "id",
to_string: "id",
},
),
],
settings: Some(
Expand Down Expand Up @@ -505,13 +509,25 @@ SchemaBlock {
span_range: 437..465,
cols: [
String(
"id",
Ident {
span_range: 438..440,
raw: "id",
to_string: "id",
},
),
String(
"full_name",
Ident {
span_range: 442..451,
raw: "full_name",
to_string: "full_name",
},
),
String(
"gender",
Ident {
span_range: 453..459,
raw: "gender",
to_string: "gender",
},
),
],
settings: Some(
Expand Down
18 changes: 15 additions & 3 deletions tests/out/general_schema.in.ron
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,18 @@ SchemaBlock {
span_range: 559..605,
cols: [
String(
"merchant_id",
Ident {
span_range: 560..571,
raw: "merchant_id",
to_string: "merchant_id",
},
),
String(
"status",
Ident {
span_range: 573..579,
raw: "status",
to_string: "status",
},
),
],
settings: Some(
Expand Down Expand Up @@ -635,7 +643,11 @@ SchemaBlock {
span_range: 608..631,
cols: [
String(
"id",
Ident {
span_range: 608..610,
raw: "id",
to_string: "id",
},
),
],
settings: Some(
Expand Down
Loading

0 comments on commit bba51f6

Please sign in to comment.