mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-14 00:07:05 +00:00
c09532864d
* feat(core): create error kind enum * feat(core): add error kind for postgres * feat(core): add error kind for sqlite * feat(core): add error kind for mysql * test(postgres): add error tests * test(sqlite): add error tests * test(mysql): add error tests * fix(tests): fix tests rebasing * refac(errors): add `ErrorKind::Other` variant
76 lines
2.1 KiB
Rust
76 lines
2.1 KiB
Rust
use sqlx::{error::ErrorKind, postgres::Postgres, Connection};
|
|
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(())
|
|
}
|