forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
102 lines (73 loc) · 2.78 KB
/
error.rs
File metadata and controls
102 lines (73 loc) · 2.78 KB
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
use sqlx::{error::ErrorKind, postgres::Postgres, Connection, Error};
use sqlx_test::new;
#[sqlx_macros::test]
async fn it_fails_with_unique_violation() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut tx = conn.begin().await?;
sqlx::query("INSERT INTO tweet(id, text, owner_id) VALUES (1, 'Foo', 1);")
.execute(&mut *tx)
.await?;
let res: Result<_, sqlx::Error> = sqlx::query("INSERT INTO tweet VALUES (1, NOW(), 'Foo', 1);")
.execute(&mut *tx)
.await;
let err = res.unwrap_err();
let err = err.into_database_error().unwrap();
assert_eq!(err.kind(), ErrorKind::UniqueViolation);
Ok(())
}
#[sqlx_macros::test]
async fn it_fails_with_foreign_key_violation() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut tx = conn.begin().await?;
let res: Result<_, sqlx::Error> =
sqlx::query("INSERT INTO tweet_reply (tweet_id, text) VALUES (1, 'Reply!');")
.execute(&mut *tx)
.await;
let err = res.unwrap_err();
let err = err.into_database_error().unwrap();
assert_eq!(err.kind(), ErrorKind::ForeignKeyViolation);
Ok(())
}
#[sqlx_macros::test]
async fn it_fails_with_not_null_violation() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut tx = conn.begin().await?;
let res: Result<_, sqlx::Error> = sqlx::query("INSERT INTO tweet (text) VALUES (null);")
.execute(&mut *tx)
.await;
let err = res.unwrap_err();
let err = err.into_database_error().unwrap();
assert_eq!(err.kind(), ErrorKind::NotNullViolation);
Ok(())
}
#[sqlx_macros::test]
async fn it_fails_with_check_violation() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut tx = conn.begin().await?;
let res: Result<_, sqlx::Error> =
sqlx::query("INSERT INTO products VALUES (1, 'Product 1', 0);")
.execute(&mut *tx)
.await;
let err = res.unwrap_err();
let err = err.into_database_error().unwrap();
assert_eq!(err.kind(), ErrorKind::CheckViolation);
Ok(())
}
#[sqlx_macros::test]
async fn it_fails_with_begin_failed() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let res = conn.begin_with("SELECT * FROM tweet").await;
let err = res.unwrap_err();
assert!(matches!(err, Error::BeginFailed), "{err:?}");
Ok(())
}
#[sqlx_macros::test]
async fn it_fails_with_invalid_save_point_statement() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut txn = conn.begin().await?;
let txn_conn = sqlx::Acquire::acquire(&mut txn).await?;
let res = txn_conn.begin_with("BEGIN").await;
let err = res.unwrap_err();
assert!(matches!(err, Error::InvalidSavePointStatement), "{err}");
Ok(())
}