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