MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / test_error

Function test_error

tests/sql_err_tests.rs:19–66  ·  view source on GitHub ↗
(db: &DatabaseConnection)

Source from the content-addressed store, hash-verified

17}
18
19pub async fn test_error(db: &DatabaseConnection) {
20 let mud_cake = cake::ActiveModel {
21 name: Set("Moldy Cake".to_owned()),
22 price: Set(rust_dec(10.25)),
23 gluten_free: Set(false),
24 serial: Set(Uuid::new_v4()),
25 bakery_id: Set(None),
26 ..Default::default()
27 };
28
29 let cake = mud_cake.save(db).await.expect("could not insert cake");
30
31 // if compiling without sqlx, this assignment will complain,
32 // but the whole test is useless in that case anyway.
33 #[allow(unused_variables)]
34 let error: DbErr = cake
35 .into_active_model()
36 .insert(db)
37 .await
38 .expect_err("inserting should fail due to duplicate primary key");
39
40 assert!(matches!(
41 error.sql_err(),
42 Some(SqlErr::UniqueConstraintViolation(_))
43 ));
44
45 let fk_cake = cake::ActiveModel {
46 name: Set("fk error Cake".to_owned()),
47 price: Set(rust_dec(10.25)),
48 gluten_free: Set(false),
49 serial: Set(Uuid::new_v4()),
50 bakery_id: Set(Some(1000)),
51 ..Default::default()
52 };
53
54 let fk_error = fk_cake
55 .insert(db)
56 .await
57 .expect_err("create foreign key should fail with non-primary key");
58
59 assert!(matches!(
60 fk_error.sql_err(),
61 Some(SqlErr::ForeignKeyConstraintViolation(_))
62 ));
63
64 let invalid_error = DbErr::Custom("random error".to_string());
65 assert_eq!(invalid_error.sql_err(), None)
66}

Callers 1

mainFunction · 0.85

Calls 4

rust_decFunction · 0.85
saveMethod · 0.80
into_active_modelMethod · 0.80
insertMethod · 0.45

Tested by

no test coverage detected