forked from SeaQL/sea-orm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpi_tests.rs
61 lines (52 loc) · 1.65 KB
/
pi_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
pub mod common;
use common::{features::*, TestContext};
use pretty_assertions::assert_eq;
use rust_decimal_macros::dec;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use std::str::FromStr;
#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("pi_tests").await;
create_tables(&ctx.db).await?;
create_and_update_pi(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr> {
let pi = pi::Model {
id: 1,
decimal: dec!(3.1415926536),
big_decimal: BigDecimal::from_str("3.1415926536").unwrap(),
decimal_opt: None,
big_decimal_opt: None,
};
let res = pi.clone().into_active_model().insert(db).await?;
let model = Pi::find().one(db).await?;
assert_eq!(model, Some(res));
assert_eq!(model, Some(pi.clone()));
let res = pi::ActiveModel {
decimal_opt: Set(Some(dec!(3.1415926536))),
big_decimal_opt: Set(Some(BigDecimal::from_str("3.1415926536").unwrap())),
..pi.clone().into_active_model()
}
.update(db)
.await?;
let model = Pi::find().one(db).await?;
assert_eq!(model, Some(res));
assert_eq!(
model,
Some(pi::Model {
id: 1,
decimal: dec!(3.1415926536),
big_decimal: BigDecimal::from_str("3.1415926536").unwrap(),
decimal_opt: Some(dec!(3.1415926536)),
big_decimal_opt: Some(BigDecimal::from_str("3.1415926536").unwrap()),
})
);
Ok(())
}