forked from SeaQL/sea-orm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparallel_tests.rs
118 lines (103 loc) · 3.45 KB
/
parallel_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
pub mod common;
pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel, Set};
#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("features_parallel_tests").await;
create_tables(&ctx.db).await?;
crud_in_parallel(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn crud_in_parallel(db: &DatabaseConnection) -> Result<(), DbErr> {
let metadata = [
metadata::Model {
uuid: Uuid::new_v4(),
ty: "Type".to_owned(),
key: "markup".to_owned(),
value: "1.18".to_owned(),
bytes: vec![1, 2, 3],
date: Some(Date::from_ymd_opt(2021, 9, 27).unwrap()),
time: Some(Time::from_hms_opt(11, 32, 55).unwrap()),
},
metadata::Model {
uuid: Uuid::new_v4(),
ty: "Type".to_owned(),
key: "exchange_rate".to_owned(),
value: "0.78".to_owned(),
bytes: vec![1, 2, 3],
date: Some(Date::from_ymd_opt(2021, 9, 27).unwrap()),
time: Some(Time::from_hms_opt(11, 32, 55).unwrap()),
},
metadata::Model {
uuid: Uuid::new_v4(),
ty: "Type".to_owned(),
key: "service_charge".to_owned(),
value: "1.1".to_owned(),
bytes: vec![1, 2, 3],
date: None,
time: None,
},
];
let _insert_res = futures::try_join!(
metadata[0].clone().into_active_model().insert(db),
metadata[1].clone().into_active_model().insert(db),
metadata[2].clone().into_active_model().insert(db),
)?;
let find_res = futures::try_join!(
Metadata::find_by_id(metadata[0].uuid).one(db),
Metadata::find_by_id(metadata[1].uuid).one(db),
Metadata::find_by_id(metadata[2].uuid).one(db),
)?;
assert_eq!(
metadata,
[
find_res.0.clone().unwrap(),
find_res.1.clone().unwrap(),
find_res.2.clone().unwrap(),
]
);
let mut active_models = (
find_res.0.unwrap().into_active_model(),
find_res.1.unwrap().into_active_model(),
find_res.2.unwrap().into_active_model(),
);
active_models.0.bytes = Set(vec![0]);
active_models.1.bytes = Set(vec![1]);
active_models.2.bytes = Set(vec![2]);
let _update_res = futures::try_join!(
active_models.0.clone().update(db),
active_models.1.clone().update(db),
active_models.2.clone().update(db),
)?;
let find_res = futures::try_join!(
Metadata::find_by_id(metadata[0].uuid).one(db),
Metadata::find_by_id(metadata[1].uuid).one(db),
Metadata::find_by_id(metadata[2].uuid).one(db),
)?;
assert_eq!(
[
active_models.0.bytes.clone().unwrap(),
active_models.1.bytes.clone().unwrap(),
active_models.2.bytes.clone().unwrap(),
],
[
find_res.0.clone().unwrap().bytes,
find_res.1.clone().unwrap().bytes,
find_res.2.clone().unwrap().bytes,
]
);
let _delete_res = futures::try_join!(
active_models.0.delete(db),
active_models.1.delete(db),
active_models.2.delete(db),
)?;
assert_eq!(Metadata::find().all(db).await?, []);
Ok(())
}