Test illegal type casts

next
Sayan Nandan 2 years ago
parent 8f77c1d73a
commit c5d7f5f6f6
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -193,6 +193,14 @@ static LUT: [(&str, FullTag); 14] = [
("list", FullTag::LIST),
];
pub static TY_BOOL: &str = LUT[0].0;
pub static TY_UINT: [&str; 4] = [LUT[1].0, LUT[2].0, LUT[3].0, LUT[4].0];
pub static TY_SINT: [&str; 4] = [LUT[5].0, LUT[6].0, LUT[7].0, LUT[8].0];
pub static TY_FLOAT: [&str; 2] = [LUT[9].0, LUT[10].0];
pub static TY_BINARY: &str = LUT[11].0;
pub static TY_STRING: &str = LUT[12].0;
pub static TY_LIST: &str = LUT[13].0;
#[derive(Debug, PartialEq, Clone)]
pub struct Field {
layers: VInline<1, Layer>,

@ -25,16 +25,20 @@
*/
mod plan {
use crate::engine::{
core::{
model::{
alt::{AlterAction, AlterPlan},
Field, Layer,
use crate::{
engine::{
core::{
model::{
self,
alt::{AlterAction, AlterPlan},
Field, Layer,
},
tests::model::create,
},
tests::model::create,
error::{DatabaseError, DatabaseResult},
ql::{ast::parse_ast_node_full, tests::lex_insecure},
},
error::{DatabaseError, DatabaseResult},
ql::{ast::parse_ast_node_full, tests::lex_insecure},
vecfuse,
};
fn with_plan(model: &str, plan: &str, f: impl Fn(AlterPlan)) -> DatabaseResult<()> {
let model = create(model)?;
@ -175,4 +179,124 @@ mod plan {
DatabaseError::DdlModelAlterFieldNotFound
);
}
fn bad_type_cast(orig_ty: &str, new_ty: &str) {
let create = format!("create model mymodel(username: string, silly_field: {orig_ty})");
let alter = format!("alter model mymodel update silly_field {{ type: {new_ty} }}");
assert_eq!(
with_plan(&create, &alter, |_| {}).expect_err(&format!(
"found no error in transformation: {orig_ty} -> {new_ty}"
)),
DatabaseError::DdlModelAlterBadTypedef,
"failed to match error in transformation: {orig_ty} -> {new_ty}",
)
}
fn enumerated_bad_type_casts<O, N>(orig_ty: O, new_ty: N)
where
O: IntoIterator<Item = &'static str>,
N: IntoIterator<Item = &'static str> + Clone,
{
for orig in orig_ty {
let new_ty = new_ty.clone();
for new in new_ty {
bad_type_cast(orig, new);
}
}
}
#[test]
fn illegal_bool_direct_cast() {
enumerated_bad_type_casts(
["bool"],
vecfuse![
model::TY_UINT,
model::TY_SINT,
model::TY_BINARY,
model::TY_STRING,
model::TY_LIST
],
);
}
#[test]
fn illegal_uint_direct_cast() {
enumerated_bad_type_casts(
model::TY_UINT,
vecfuse![
model::TY_BOOL,
model::TY_SINT,
model::TY_FLOAT,
model::TY_BINARY,
model::TY_STRING,
model::TY_LIST
],
);
}
#[test]
fn illegal_sint_direct_cast() {
enumerated_bad_type_casts(
model::TY_SINT,
vecfuse![
model::TY_BOOL,
model::TY_UINT,
model::TY_FLOAT,
model::TY_BINARY,
model::TY_STRING,
model::TY_LIST
],
);
}
#[test]
fn illegal_float_direct_cast() {
enumerated_bad_type_casts(
model::TY_FLOAT,
vecfuse![
model::TY_BOOL,
model::TY_UINT,
model::TY_SINT,
model::TY_BINARY,
model::TY_STRING,
model::TY_LIST
],
);
}
#[test]
fn illegal_binary_direct_cast() {
enumerated_bad_type_casts(
[model::TY_BINARY],
vecfuse![
model::TY_BOOL,
model::TY_UINT,
model::TY_SINT,
model::TY_FLOAT,
model::TY_STRING,
model::TY_LIST
],
);
}
#[test]
fn illegal_string_direct_cast() {
enumerated_bad_type_casts(
[model::TY_STRING],
vecfuse![
model::TY_BOOL,
model::TY_UINT,
model::TY_SINT,
model::TY_FLOAT,
model::TY_BINARY,
model::TY_LIST
],
);
}
#[test]
fn illegal_list_direct_cast() {
enumerated_bad_type_casts(
["list { type: string }"],
vecfuse![
model::TY_BOOL,
model::TY_UINT,
model::TY_SINT,
model::TY_FLOAT,
model::TY_BINARY,
model::TY_STRING
],
);
}
}

@ -55,3 +55,34 @@ pub fn random_string_checked(rng: &mut impl Rng, l: usize, ck: impl Fn(&str) ->
}
}
}
pub trait VecFuse<T> {
fn fuse_append(self, arr: &mut Vec<T>);
}
impl<T> VecFuse<T> for T {
fn fuse_append(self, arr: &mut Vec<T>) {
arr.push(self);
}
}
impl<T, const N: usize> VecFuse<T> for [T; N] {
fn fuse_append(self, arr: &mut Vec<T>) {
arr.extend(self)
}
}
impl<T> VecFuse<T> for Vec<T> {
fn fuse_append(self, arr: &mut Vec<T>) {
arr.extend(self)
}
}
#[macro_export]
macro_rules! vecfuse {
($($expr:expr),* $(,)?) => {{
let mut v = Vec::new();
$(<_ as crate::util::test_utils::VecFuse<_>>::fuse_append($expr, &mut v);)*
v
}};
}

Loading…
Cancel
Save