More tests
This commit is contained in:
parent
e177a338c4
commit
5b0720065f
@ -18,7 +18,7 @@ mod tests {
|
|||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn active_enum_1() {
|
fn active_enum_string() {
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Category {
|
pub enum Category {
|
||||||
Big,
|
Big,
|
||||||
@ -100,84 +100,78 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn active_enum_2() {
|
fn active_enum_derive_signed_integers() {
|
||||||
#[derive(Debug, PartialEq)]
|
macro_rules! test_int {
|
||||||
pub enum Category {
|
($ident: ident, $rs_type: expr, $db_type: expr, $col_def: ident) => {
|
||||||
Big,
|
#[derive(Debug, PartialEq, DeriveActiveEnum)]
|
||||||
Small,
|
#[sea_orm(rs_type = $rs_type, db_type = $db_type)]
|
||||||
}
|
pub enum $ident {
|
||||||
|
#[sea_orm(num_value = 1)]
|
||||||
impl ActiveEnum for Category {
|
Big,
|
||||||
type Value = i32; // FIXME: only support i32 for now
|
#[sea_orm(num_value = 0)]
|
||||||
|
Small,
|
||||||
fn to_value(&self) -> Self::Value {
|
#[sea_orm(num_value = -10)]
|
||||||
match self {
|
Negative,
|
||||||
Self::Big => 1,
|
|
||||||
Self::Small => 0,
|
|
||||||
}
|
}
|
||||||
.to_owned()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn try_from_value(v: &Self::Value) -> Result<Self, DbErr> {
|
assert_eq!($ident::Big.to_value(), 1);
|
||||||
match v {
|
assert_eq!($ident::Small.to_value(), 0);
|
||||||
1 => Ok(Self::Big),
|
assert_eq!($ident::Negative.to_value(), -10);
|
||||||
0 => Ok(Self::Small),
|
|
||||||
_ => Err(DbErr::Query(format!(
|
assert_eq!($ident::try_from_value(&1).ok(), Some($ident::Big));
|
||||||
"unexpected value for Category enum: {}",
|
assert_eq!($ident::try_from_value(&0).ok(), Some($ident::Small));
|
||||||
v
|
assert_eq!($ident::try_from_value(&-10).ok(), Some($ident::Negative));
|
||||||
))),
|
assert_eq!(
|
||||||
|
$ident::try_from_value(&2).err(),
|
||||||
|
Some(DbErr::Query(format!(
|
||||||
|
"unexpected value for {} enum: 2",
|
||||||
|
stringify!($ident)
|
||||||
|
)))
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!($ident::db_type(), ColumnType::$col_def.def());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test_int!(I8, "i8", "TinyInteger", TinyInteger);
|
||||||
|
test_int!(I16, "i16", "SmallInteger", SmallInteger);
|
||||||
|
test_int!(I32, "i32", "Integer", Integer);
|
||||||
|
test_int!(I64, "i64", "BigInteger", BigInteger);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn active_enum_derive_unsigned_integers() {
|
||||||
|
macro_rules! test_uint {
|
||||||
|
($ident: ident, $rs_type: expr, $db_type: expr, $col_def: ident) => {
|
||||||
|
#[derive(Debug, PartialEq, DeriveActiveEnum)]
|
||||||
|
#[sea_orm(rs_type = $rs_type, db_type = $db_type)]
|
||||||
|
pub enum $ident {
|
||||||
|
#[sea_orm(num_value = 1)]
|
||||||
|
Big,
|
||||||
|
#[sea_orm(num_value = 0)]
|
||||||
|
Small,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn db_type() -> ColumnDef {
|
assert_eq!($ident::Big.to_value(), 1);
|
||||||
ColumnType::Integer.def()
|
assert_eq!($ident::Small.to_value(), 0);
|
||||||
}
|
|
||||||
|
assert_eq!($ident::try_from_value(&1).ok(), Some($ident::Big));
|
||||||
|
assert_eq!($ident::try_from_value(&0).ok(), Some($ident::Small));
|
||||||
|
assert_eq!(
|
||||||
|
$ident::try_from_value(&2).err(),
|
||||||
|
Some(DbErr::Query(format!(
|
||||||
|
"unexpected value for {} enum: 2",
|
||||||
|
stringify!($ident)
|
||||||
|
)))
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!($ident::db_type(), ColumnType::$col_def.def());
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, DeriveActiveEnum)]
|
test_uint!(U8, "u8", "TinyInteger", TinyInteger);
|
||||||
#[sea_orm(rs_type = "i32", db_type = "Integer")]
|
test_uint!(U16, "u16", "SmallInteger", SmallInteger);
|
||||||
pub enum DeriveCategory {
|
test_uint!(U32, "u32", "Integer", Integer);
|
||||||
#[sea_orm(num_value = 1)]
|
test_uint!(U64, "u64", "BigInteger", BigInteger);
|
||||||
Big,
|
|
||||||
#[sea_orm(num_value = 0)]
|
|
||||||
Small,
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(Category::Big.to_value(), 1);
|
|
||||||
assert_eq!(Category::Small.to_value(), 0);
|
|
||||||
assert_eq!(DeriveCategory::Big.to_value(), 1);
|
|
||||||
assert_eq!(DeriveCategory::Small.to_value(), 0);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Category::try_from_value(&2).err(),
|
|
||||||
Some(DbErr::Query(
|
|
||||||
"unexpected value for Category enum: 2".to_owned()
|
|
||||||
))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
Category::try_from_value(&1).ok(),
|
|
||||||
Some(Category::Big)
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
Category::try_from_value(&0).ok(),
|
|
||||||
Some(Category::Small)
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
DeriveCategory::try_from_value(&2).err(),
|
|
||||||
Some(DbErr::Query(
|
|
||||||
"unexpected value for DeriveCategory enum: 2".to_owned()
|
|
||||||
))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
DeriveCategory::try_from_value(&1).ok(),
|
|
||||||
Some(DeriveCategory::Big)
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
DeriveCategory::try_from_value(&0).ok(),
|
|
||||||
Some(DeriveCategory::Small)
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(Category::db_type(), ColumnType::Integer.def());
|
|
||||||
assert_eq!(DeriveCategory::db_type(), ColumnType::Integer.def());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user