-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
returning_tests.rs
187 lines (163 loc) Β· 5.27 KB
/
returning_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![allow(unused_imports, dead_code)]
pub mod common;
pub use common::{bakery_chain::*, setup::*, TestContext};
use sea_orm::{entity::prelude::*, IntoActiveModel, Set};
pub use sea_query::{Expr, Query};
use serde_json::json;
#[sea_orm_macros::test]
async fn main() -> Result<(), DbErr> {
use bakery::*;
let ctx = TestContext::new("returning_tests").await;
let db = &ctx.db;
let builder = db.get_database_backend();
let mut insert = Query::insert();
insert
.into_table(Entity)
.columns([Column::Name, Column::ProfitMargin])
.values_panic(["Bakery Shop".into(), 0.5.into()]);
let mut update = Query::update();
update
.table(Entity)
.values([
(Column::Name, "Bakery Shop".into()),
(Column::ProfitMargin, 0.5.into()),
])
.and_where(Column::Id.eq(1));
let columns = [Column::Id, Column::Name, Column::ProfitMargin];
let returning =
Query::returning().exprs(columns.into_iter().map(|c| c.into_returning_expr(builder)));
create_tables(db).await?;
if db.support_returning() {
insert.returning(returning.clone());
let insert_res = db
.query_one(builder.build(&insert))
.await?
.expect("Insert failed with query_one");
let _id: i32 = insert_res.try_get("", "id")?;
let _name: String = insert_res.try_get("", "name")?;
let _profit_margin: f64 = insert_res.try_get("", "profit_margin")?;
update.returning(returning.clone());
let update_res = db
.query_one(builder.build(&update))
.await?
.expect("Update filed with query_one");
let _id: i32 = update_res.try_get("", "id")?;
let _name: String = update_res.try_get("", "name")?;
let _profit_margin: f64 = update_res.try_get("", "profit_margin")?;
} else {
let insert_res = db.execute(builder.build(&insert)).await?;
assert!(insert_res.rows_affected() > 0);
let update_res = db.execute(builder.build(&update)).await?;
assert!(update_res.rows_affected() > 0);
}
ctx.delete().await;
Ok(())
}
#[sea_orm_macros::test]
#[cfg_attr(
any(
feature = "sqlx-mysql",
all(
feature = "sqlx-sqlite",
not(feature = "sqlite-use-returning-for-3_35")
)
),
should_panic(expected = "Database backend doesn't support RETURNING")
)]
async fn update_many() {
pub use common::{features::*, TestContext};
use edit_log::*;
let run = || async {
let ctx = TestContext::new("returning_tests_update_many").await;
let db = &ctx.db;
create_tables(db).await?;
Entity::insert(
Model {
id: 1,
action: "before_save".into(),
values: json!({ "id": "unique-id-001" }),
}
.into_active_model(),
)
.exec(db)
.await?;
Entity::insert(
Model {
id: 2,
action: "before_save".into(),
values: json!({ "id": "unique-id-002" }),
}
.into_active_model(),
)
.exec(db)
.await?;
Entity::insert(
Model {
id: 3,
action: "before_save".into(),
values: json!({ "id": "unique-id-003" }),
}
.into_active_model(),
)
.exec(db)
.await?;
assert_eq!(
Entity::find().all(db).await?,
[
Model {
id: 1,
action: "before_save".into(),
values: json!({ "id": "unique-id-001" }),
},
Model {
id: 2,
action: "before_save".into(),
values: json!({ "id": "unique-id-002" }),
},
Model {
id: 3,
action: "before_save".into(),
values: json!({ "id": "unique-id-003" }),
},
]
);
// Update many with returning
assert_eq!(
Entity::update_many()
.col_expr(
Column::Values,
Expr::value(json!({ "remarks": "save log" }))
)
.filter(Column::Action.eq("before_save"))
.exec_with_returning(db)
.await?,
[
Model {
id: 1,
action: "before_save".into(),
values: json!({ "remarks": "save log" }),
},
Model {
id: 2,
action: "before_save".into(),
values: json!({ "remarks": "save log" }),
},
Model {
id: 3,
action: "before_save".into(),
values: json!({ "remarks": "save log" }),
},
]
);
// No-op
assert_eq!(
Entity::update_many()
.filter(Column::Action.eq("before_save"))
.exec_with_returning(db)
.await?,
[]
);
Result::<(), DbErr>::Ok(())
};
run().await.unwrap();
}