-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
dyn_table_name_tests.rs
76 lines (64 loc) 路 2.4 KB
/
dyn_table_name_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
#![allow(unused_imports, dead_code)]
pub mod common;
pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{
entity::prelude::*, DatabaseConnection, Delete, IntoActiveModel, Iterable, QueryTrait, Set,
Update,
};
use sea_query::{Expr, Query};
#[sea_orm_macros::test]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("dyn_table_name_tests").await;
create_tables(&ctx.db).await?;
dyn_table_name_lazy_static(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn dyn_table_name_lazy_static(db: &DatabaseConnection) -> Result<(), DbErr> {
use dyn_table_name_lazy_static::*;
for i in 1..=2 {
let entity = Entity {
table_name: TableName::from_str_truncate(format!("dyn_table_name_lazy_static_{}", i)),
};
let model = Model {
id: 1,
name: "1st Row".into(),
};
// Prepare insert statement
let mut insert = Entity::insert(model.clone().into_active_model());
// Reset the table name of insert statement
insert.query().into_table(entity.table_ref());
// Execute the insert statement
assert_eq!(insert.exec(db).await?.last_insert_id, 1);
// Prepare select statement
let mut select = Entity::find();
// Override the select statement
*QueryTrait::query(&mut select) = Query::select()
.exprs(Column::iter().map(|col| col.select_as(Expr::col(col))))
.from(entity.table_ref())
.to_owned();
// Execute the select statement
assert_eq!(select.clone().one(db).await?, Some(model.clone()));
// Prepare update statement
let update = Update::many(entity).set(ActiveModel {
name: Set("1st Row (edited)".into()),
..model.clone().into_active_model()
});
// Execute the update statement
assert_eq!(update.exec(db).await?.rows_affected, 1);
assert_eq!(
select.clone().one(db).await?,
Some(Model {
id: 1,
name: "1st Row (edited)".into(),
})
);
// Prepare delete statement
let delete = Delete::many(entity).filter(Expr::col(Column::Id).eq(1));
// Execute the delete statement
assert_eq!(delete.exec(db).await?.rows_affected, 1);
assert_eq!(select.one(db).await?, None);
}
Ok(())
}