forked from SeaQL/sea-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum_primary_key_tests.rs
129 lines (115 loc) Β· 2.99 KB
/
enum_primary_key_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
#![allow(unused_imports, dead_code)]
pub mod common;
pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{
entity::prelude::*,
entity::*,
sea_query::{BinOper, Expr},
ActiveEnum as ActiveEnumTrait, DatabaseConnection,
};
#[sea_orm_macros::test]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("enum_primary_key_tests").await;
create_tables(&ctx.db).await?;
insert_teas(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn insert_teas(db: &DatabaseConnection) -> Result<(), DbErr> {
use teas::*;
let model = Model {
id: Tea::EverydayTea,
category: None,
color: None,
};
assert_eq!(
model,
ActiveModel {
id: Set(Tea::EverydayTea),
category: Set(None),
color: Set(None),
}
.insert(db)
.await?
);
assert_eq!(model, Entity::find().one(db).await?.unwrap());
assert_eq!(
model,
Entity::find()
.filter(Column::Id.is_not_null())
.filter(Column::Category.is_null())
.filter(Column::Color.is_null())
.one(db)
.await?
.unwrap()
);
// UNIQUE constraint failed
assert!(ActiveModel {
id: Set(Tea::EverydayTea),
category: Set(Some(Category::Big)),
color: Set(Some(Color::Black)),
}
.insert(db)
.await
.is_err());
// UNIQUE constraint failed
assert!(Entity::insert(ActiveModel {
id: Set(Tea::EverydayTea),
category: Set(Some(Category::Big)),
color: Set(Some(Color::Black)),
})
.exec(db)
.await
.is_err());
let _ = ActiveModel {
category: Set(Some(Category::Big)),
color: Set(Some(Color::Black)),
..model.into_active_model()
}
.save(db)
.await?;
let model = Entity::find().one(db).await?.unwrap();
assert_eq!(
model,
Model {
id: Tea::EverydayTea,
category: Some(Category::Big),
color: Some(Color::Black),
}
);
assert_eq!(
model,
Entity::find()
.filter(Column::Id.eq(Tea::EverydayTea))
.filter(Column::Category.eq(Category::Big))
.filter(Column::Color.eq(Color::Black))
.one(db)
.await?
.unwrap()
);
assert_eq!(
model,
Entity::find()
.filter(
Expr::col(Column::Id)
.binary(BinOper::In, Expr::tuple([Tea::EverydayTea.as_enum()]))
)
.one(db)
.await?
.unwrap()
);
// Equivalent to the above.
assert_eq!(
model,
Entity::find()
.filter(Column::Id.is_in([Tea::EverydayTea]))
.one(db)
.await?
.unwrap()
);
let res = model.delete(db).await?;
assert_eq!(res.rows_affected, 1);
assert_eq!(Entity::find().one(db).await?, None);
Ok(())
}