From 46f343cec8a47938eaf0a0f3db610719d0b5915b Mon Sep 17 00:00:00 2001 From: Luiz Carvalho Date: Wed, 21 Sep 2022 11:44:16 -0300 Subject: [PATCH] test(mssql): add error tests --- Cargo.toml | 5 +++ tests/mssql/error.rs | 80 +++++++++++++++++++++++++++++++++++++++++++ tests/mssql/setup.sql | 24 +++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 tests/mssql/error.rs diff --git a/Cargo.toml b/Cargo.toml index 536253470c..9aee09ab6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -324,6 +324,11 @@ name = "mssql-describe" path = "tests/mssql/describe.rs" required-features = ["mssql"] +[[test]] +name = "mssql-error" +path = "tests/mssql/error.rs" +required-features = ["mssql"] + [[test]] name = "mssql-macros" path = "tests/mssql/macros.rs" diff --git a/tests/mssql/error.rs b/tests/mssql/error.rs new file mode 100644 index 0000000000..e4cdb2677e --- /dev/null +++ b/tests/mssql/error.rs @@ -0,0 +1,80 @@ +use sqlx::{error::ErrorKind, mssql::Mssql, Connection, Executor, Transaction}; +use sqlx_test::new; + +async fn with_test_row(conn: &mut Transaction<'_, Mssql>) -> 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::().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, 'Foo', 0, 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::().await?; + let mut tx = conn.begin().await?; + + let res: Result<_, sqlx::Error> = + sqlx::query("INSERT INTO tweet_reply (id, tweet_id, text) VALUES (1, 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::().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::().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(()) +} diff --git a/tests/mssql/setup.sql b/tests/mssql/setup.sql index a033227b75..09328d0d1e 100644 --- a/tests/mssql/setup.sql +++ b/tests/mssql/setup.sql @@ -18,3 +18,27 @@ IF OBJECT_ID('tweet') IS NULL ); END; GO + +IF OBJECT_ID('tweet_reply') IS NULL + BEGIN + CREATE TABLE tweet_reply + ( + id BIGINT NOT NULL PRIMARY KEY, + tweet_id BIGINT NOT NULL, + text NVARCHAR(4000) NOT NULL, + owner_id BIGINT, + CONSTRAINT tweet_id_fk FOREIGN KEY (tweet_id) REFERENCES tweet(id) + ); + END; +GO + +IF OBJECT_ID('products') IS NULL + BEGIN + CREATE TABLE products + ( + product_no INTEGER, + name TEXT, + price NUMERIC CHECK (price > 0) + ); + END; +GO