sea-orm/tests/json_struct_tests.rs
Billy Chan d6831e5295
Bump version of sqlx, sea-query, sea-schema, time and uuid (#834)
* Bump version of sqlx, sea-query, sea-schema, time and uuid

* PostgreSQL `u32` was wrapped in `Oid` (launchbadge/sqlx#1602)

* Update test cases

* Fix clippy warnings

* cargo fmt

* Fix clippy warnings

* Bump sea-schema to ^0.9.2

* Update test cases

* Pin on sea-query minor version
2022-07-05 01:05:11 +08:00

98 lines
2.2 KiB
Rust

pub mod common;
pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use serde_json::json;
#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("json_struct_tests").await;
create_tables(&ctx.db).await?;
insert_json_struct_1(&ctx.db).await?;
insert_json_struct_2(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn insert_json_struct_1(db: &DatabaseConnection) -> Result<(), DbErr> {
use json_struct::*;
let model = Model {
id: 1,
json: json!({
"id": 1,
"name": "apple",
"price": 12.01,
"notes": "hand picked, organic",
}),
json_value: KeyValue {
id: 1,
name: "apple".into(),
price: 12.01,
notes: Some("hand picked, organic".into()),
},
json_value_opt: Some(KeyValue {
id: 1,
name: "apple".into(),
price: 12.01,
notes: Some("hand picked, organic".into()),
}),
};
let result = model.clone().into_active_model().insert(db).await?;
assert_eq!(result, model);
assert_eq!(
Entity::find()
.filter(Column::Id.eq(model.id))
.one(db)
.await?,
Some(model)
);
Ok(())
}
pub async fn insert_json_struct_2(db: &DatabaseConnection) -> Result<(), DbErr> {
use json_struct::*;
let model = Model {
id: 2,
json: json!({
"id": 2,
"name": "orange",
"price": 10.93,
"notes": "sweet & juicy",
}),
json_value: KeyValue {
id: 1,
name: "orange".into(),
price: 10.93,
notes: None,
},
json_value_opt: None,
};
let result = model.clone().into_active_model().insert(db).await?;
assert_eq!(result, model);
assert_eq!(
Entity::find()
.filter(Column::Id.eq(model.id))
.one(db)
.await?,
Some(model)
);
Ok(())
}