-
-
Notifications
You must be signed in to change notification settings - Fork 521
/
json_struct_tests.rs
97 lines (82 loc) · 2.22 KB
/
json_struct_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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(())
}