* Cast enums in `is_in` and `is_not_in` (#1527) * Restore original tests, add tests for generated SQL
This commit is contained in:
parent
4b5fd9388e
commit
1abc47b5f1
@ -62,7 +62,8 @@ macro_rules! bind_vec_func {
|
|||||||
V: Into<Value>,
|
V: Into<Value>,
|
||||||
I: IntoIterator<Item = V>,
|
I: IntoIterator<Item = V>,
|
||||||
{
|
{
|
||||||
Expr::col((self.entity_name(), *self)).$func(v)
|
let v_with_enum_cast = v.into_iter().map(|v| self.save_as(Expr::val(v)));
|
||||||
|
Expr::col((self.entity_name(), *self)).$func(v_with_enum_cast)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ use sea_orm::{
|
|||||||
entity::prelude::*,
|
entity::prelude::*,
|
||||||
entity::*,
|
entity::*,
|
||||||
sea_query::{BinOper, Expr},
|
sea_query::{BinOper, Expr},
|
||||||
ActiveEnum as ActiveEnumTrait, DatabaseConnection,
|
ActiveEnum as ActiveEnumTrait, DatabaseConnection, QueryTrait,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[sea_orm_macros::test]
|
#[sea_orm_macros::test]
|
||||||
@ -98,6 +98,7 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
.await?
|
.await?
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
model,
|
model,
|
||||||
Entity::find()
|
Entity::find()
|
||||||
@ -109,6 +110,24 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
.await?
|
.await?
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
|
// Equivalent to the above.
|
||||||
|
let select_with_tea_in = Entity::find().filter(Column::Tea.is_in([Tea::EverydayTea]));
|
||||||
|
assert_eq!(
|
||||||
|
select_with_tea_in
|
||||||
|
.build(sea_orm::DatabaseBackend::Postgres)
|
||||||
|
.to_string(),
|
||||||
|
[
|
||||||
|
"SELECT \"active_enum\".\"id\",",
|
||||||
|
"\"active_enum\".\"category\",",
|
||||||
|
"\"active_enum\".\"color\",",
|
||||||
|
"CAST(\"active_enum\".\"tea\" AS text)",
|
||||||
|
"FROM \"active_enum\"",
|
||||||
|
"WHERE \"active_enum\".\"tea\" IN (CAST('EverydayTea' AS tea))",
|
||||||
|
]
|
||||||
|
.join(" ")
|
||||||
|
);
|
||||||
|
assert_eq!(model, select_with_tea_in.one(db).await?.unwrap());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
model,
|
model,
|
||||||
Entity::find()
|
Entity::find()
|
||||||
@ -121,6 +140,26 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
.await?
|
.await?
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
|
// Equivalent to the above.
|
||||||
|
let select_with_tea_not_in = Entity::find()
|
||||||
|
.filter(Column::Tea.is_not_null())
|
||||||
|
.filter(Column::Tea.is_not_in([Tea::BreakfastTea]));
|
||||||
|
assert_eq!(
|
||||||
|
select_with_tea_not_in
|
||||||
|
.build(sea_orm::DatabaseBackend::Postgres)
|
||||||
|
.to_string(),
|
||||||
|
[
|
||||||
|
"SELECT \"active_enum\".\"id\",",
|
||||||
|
"\"active_enum\".\"category\",",
|
||||||
|
"\"active_enum\".\"color\",",
|
||||||
|
"CAST(\"active_enum\".\"tea\" AS text)",
|
||||||
|
"FROM \"active_enum\"",
|
||||||
|
"WHERE \"active_enum\".\"tea\" IS NOT NULL",
|
||||||
|
"AND \"active_enum\".\"tea\" NOT IN (CAST('BreakfastTea' AS tea))",
|
||||||
|
]
|
||||||
|
.join(" ")
|
||||||
|
);
|
||||||
|
assert_eq!(model, select_with_tea_not_in.one(db).await?.unwrap());
|
||||||
|
|
||||||
let res = model.delete(db).await?;
|
let res = model.delete(db).await?;
|
||||||
|
|
||||||
|
@ -113,6 +113,15 @@ pub async fn insert_teas(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
.await?
|
.await?
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
|
// Equivalent to the above.
|
||||||
|
assert_eq!(
|
||||||
|
model,
|
||||||
|
Entity::find()
|
||||||
|
.filter(Column::Id.is_in([Tea::EverydayTea]))
|
||||||
|
.one(db)
|
||||||
|
.await?
|
||||||
|
.unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
let res = model.delete(db).await?;
|
let res = model.delete(db).await?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user