Skip to content

Commit

Permalink
test(mysql): add error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Feb 3, 2023
1 parent a1a3e4f commit b98e986
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ name = "mysql-macros"
path = "tests/mysql/macros.rs"
required-features = ["mysql", "macros"]

[[test]]
name = "mysql-error"
path = "tests/mysql/error.rs"
required-features = ["mysql"]

[[test]]
name = "mysql-test-attr"
path = "tests/mysql/test-attr.rs"
Expand Down
80 changes: 80 additions & 0 deletions tests/mysql/error.rs
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(())
}
16 changes: 16 additions & 0 deletions tests/mysql/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@ CREATE TABLE tweet
text TEXT NOT NULL,
owner_id BIGINT
);

CREATE TABLE tweet_reply
(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
tweet_id BIGINT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
text TEXT NOT NULL,
owner_id BIGINT,
CONSTRAINT tweet_id_fk FOREIGN KEY (tweet_id) REFERENCES tweet(id)
);

CREATE TABLE products (
product_no INTEGER,
name TEXT,
price NUMERIC CHECK (price > 0)
);
2 changes: 1 addition & 1 deletion tests/postgres/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn it_fails_with_foreign_key_violation() -> anyhow::Result<()> {
let mut tx = conn.begin().await?;

let res: Result<_, sqlx::Error> =
sqlx::query("INSERT INTO tweet_reply (tweet, text) VALUES (1, 'Reply!');")
sqlx::query("INSERT INTO tweet_reply (tweet_id, text) VALUES (1, 'Reply!');")
.execute(&mut tx)
.await;
let err = res.unwrap_err();
Expand Down
2 changes: 1 addition & 1 deletion tests/postgres/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CREATE TABLE tweet
CREATE TABLE tweet_reply
(
id BIGSERIAL PRIMARY KEY,
tweet BIGINT REFERENCES tweet(id),
tweet_id BIGINT NOT NULL REFERENCES tweet(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
text TEXT NOT NULL,
owner_id BIGINT
Expand Down

0 comments on commit b98e986

Please sign in to comment.