-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
012faa7
commit d62dbd4
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use sqlx::{error::ErrorKind, mysql::MySql, Connection, Executor, Transaction}; | ||
use sqlx_test::new; | ||
|
||
async fn with_test_row(conn: &mut Transaction<'_, MySql>) -> anyhow::Result<()> { | ||
sqlx::query!("INSERT INTO tweet(id, text, owner_id) VALUES (1, 'Foo', 1)") | ||
.execute(conn) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
#[sqlx_macros::test] | ||
async fn it_fails_with_unique_violation() -> anyhow::Result<()> { | ||
let mut conn = new::<MySql>().await?; | ||
let mut tx = conn.begin().await?; | ||
with_test_row(&mut tx).await.unwrap(); | ||
|
||
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(), Some(ErrorKind::UniqueViolation)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[sqlx_macros::test] | ||
async fn it_fails_with_foreign_key_violation() -> anyhow::Result<()> { | ||
let mut conn = new::<MySql>().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(), Some(ErrorKind::ForeignKeyViolation)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[sqlx_macros::test] | ||
async fn it_fails_with_not_null_violation() -> anyhow::Result<()> { | ||
let mut conn = new::<MySql>().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(), Some(ErrorKind::NotNullViolation)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[sqlx_macros::test] | ||
async fn it_fails_with_check_violation() -> anyhow::Result<()> { | ||
let mut conn = new::<MySql>().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(), Some(ErrorKind::CheckViolation)); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters