Skip to content

Commit

Permalink
test(mssql): add error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Sep 21, 2022
1 parent d62dbd4 commit 46f343c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
80 changes: 80 additions & 0 deletions tests/mssql/error.rs
Original file line number Diff line number Diff line change
@@ -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::<Mssql>().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::<Mssql>().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::<Mssql>().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::<Mssql>().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(())
}
24 changes: 24 additions & 0 deletions tests/mssql/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 46f343c

Please sign in to comment.