forked from SeaQL/sea-orm
/
sql_err_tests.rs
62 lines (54 loc) · 1.8 KB
/
sql_err_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
pub mod common;
pub use common::{bakery_chain::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use rust_decimal_macros::dec;
pub use sea_orm::{entity::*, error::*, tests_cfg, DatabaseConnection, EntityName, ExecResult};
use uuid::Uuid;
#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() {
let ctx = TestContext::new("bakery_chain_sql_err_tests").await;
create_tables(&ctx.db).await.unwrap();
test_error(&ctx.db).await;
ctx.delete().await;
}
pub async fn test_error(db: &DatabaseConnection) {
let mud_cake = cake::ActiveModel {
name: Set("Moldy Cake".to_owned()),
price: Set(dec!(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(None),
..Default::default()
};
let cake = mud_cake.save(db).await.expect("could not insert cake");
// if compiling without sqlx, this assignment will complain,
// but the whole test is useless in that case anyway.
#[allow(unused_variables)]
let error: DbErr = cake
.into_active_model()
.insert(db)
.await
.expect_err("inserting should fail due to duplicate primary key");
assert_eq!(error.sql_err(), Some(SqlErr::UniqueConstraintViolation));
let fk_cake = cake::ActiveModel {
name: Set("fk error Cake".to_owned()),
price: Set(dec!(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(Some(1000)),
..Default::default()
};
let fk_error = fk_cake
.insert(db)
.await
.expect_err("create foreign key should fail with non-primary key");
assert_eq!(
fk_error.sql_err(),
Some(SqlErr::ForeignKeyConstraintViolation)
);
}