sea-orm/tests/collection_tests.rs
Billy Chan 4d8645ae8b
Support Vector of enum for Postgres (#1210)
* Support Vector of enum for Postgres

* Fix clippy

* Add tests for integer array

* Fix dependency

* Bump sea-query to 0.27.2

Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
2022-11-15 13:50:05 +08:00

128 lines
3.3 KiB
Rust

pub mod common;
pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
#[sea_orm_macros::test]
#[cfg(all(feature = "sqlx-postgres", feature = "postgres-array"))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("collection_tests").await;
create_tables(&ctx.db).await?;
insert_collection(&ctx.db).await?;
update_collection(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn insert_collection(db: &DatabaseConnection) -> Result<(), DbErr> {
use collection::*;
assert_eq!(
Model {
id: 1,
integers: vec![1, 2, 3],
integers_opt: Some(vec![1, 2, 3]),
teas: vec![Tea::BreakfastTea],
teas_opt: Some(vec![Tea::BreakfastTea]),
colors: vec![Color::Black],
colors_opt: Some(vec![Color::Black]),
}
.into_active_model()
.insert(db)
.await?,
Model {
id: 1,
integers: vec![1, 2, 3],
integers_opt: Some(vec![1, 2, 3]),
teas: vec![Tea::BreakfastTea],
teas_opt: Some(vec![Tea::BreakfastTea]),
colors: vec![Color::Black],
colors_opt: Some(vec![Color::Black]),
}
);
assert_eq!(
Model {
id: 2,
integers: vec![10, 9],
integers_opt: None,
teas: vec![Tea::BreakfastTea],
teas_opt: None,
colors: vec![Color::Black],
colors_opt: None,
}
.into_active_model()
.insert(db)
.await?,
Model {
id: 2,
integers: vec![10, 9],
integers_opt: None,
teas: vec![Tea::BreakfastTea],
teas_opt: None,
colors: vec![Color::Black],
colors_opt: None,
}
);
assert_eq!(
Model {
id: 3,
integers: vec![],
integers_opt: Some(vec![]),
teas: vec![],
teas_opt: Some(vec![]),
colors: vec![],
colors_opt: Some(vec![]),
}
.into_active_model()
.insert(db)
.await?,
Model {
id: 3,
integers: vec![],
integers_opt: Some(vec![]),
teas: vec![],
teas_opt: Some(vec![]),
colors: vec![],
colors_opt: Some(vec![]),
}
);
Ok(())
}
pub async fn update_collection(db: &DatabaseConnection) -> Result<(), DbErr> {
use collection::*;
let model = Entity::find_by_id(1).one(db).await?.unwrap();
ActiveModel {
integers: Set(vec![4, 5, 6]),
integers_opt: Set(Some(vec![4, 5, 6])),
teas: Set(vec![Tea::EverydayTea]),
teas_opt: Set(Some(vec![Tea::EverydayTea])),
colors: Set(vec![Color::White]),
colors_opt: Set(Some(vec![Color::White])),
..model.into_active_model()
}
.update(db)
.await?;
ActiveModel {
id: Unchanged(3),
integers: Set(vec![3, 1, 4]),
integers_opt: Set(None),
teas: Set(vec![Tea::EverydayTea]),
teas_opt: Set(None),
colors: Set(vec![Color::White]),
colors_opt: Set(None),
}
.update(db)
.await?;
Ok(())
}