From 6e3fa49ad5c6ec473be740c25b47e6236af4e93c Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Wed, 25 Mar 2020 18:26:57 -0700 Subject: [PATCH] add unit tests for `DatabaseError` downcasting --- sqlx-core/src/mysql/error.rs | 19 +++++++++++ sqlx-core/src/postgres/error.rs | 35 +++++++++++++++++++++ sqlx-core/src/postgres/protocol/mod.rs | 2 +- sqlx-core/src/postgres/protocol/response.rs | 2 +- sqlx-core/src/sqlite/error.rs | 18 +++++++++++ 5 files changed, 74 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/mysql/error.rs b/sqlx-core/src/mysql/error.rs index 8aa03c9a..31beb6bf 100644 --- a/sqlx-core/src/mysql/error.rs +++ b/sqlx-core/src/mysql/error.rs @@ -43,3 +43,22 @@ impl From for crate::Error { crate::Error::Database(Box::new(err)) } } + +#[test] +fn test_error_downcasting() { + let error = MySqlError(ErrPacket { + error_code: 0xABCD, + sql_state: None, + error_message: "".into() + }); + + let error = crate::Error::from(error); + + let db_err = match error { + crate::Error::Database(db_err) => db_err, + e => panic!("expected crate::Error::Database, got {:?}", e) + }; + + assert_eq!(db_err.downcast_ref::().0.error_code, 0xABCD); + assert_eq!(db_err.downcast::().0.error_code, 0xABCD); +} \ No newline at end of file diff --git a/sqlx-core/src/postgres/error.rs b/sqlx-core/src/postgres/error.rs index 91d6a45a..78475082 100644 --- a/sqlx-core/src/postgres/error.rs +++ b/sqlx-core/src/postgres/error.rs @@ -63,3 +63,38 @@ impl From for crate::Error { crate::Error::Database(Box::new(err)) } } + +#[test] +fn test_error_downcasting() { + use super::protocol::Severity; + + let error = PgError(Response { + severity: Severity::Panic, + code: "".into(), + message: "".into(), + detail: None, + hint: None, + position: None, + internal_position: None, + internal_query: None, + where_: None, + schema: None, + table: None, + column: None, + data_type: None, + constraint: None, + file: None, + line: None, + routine: None + }); + + let error = crate::Error::from(error); + + let db_err = match error { + crate::Error::Database(db_err) => db_err, + e => panic!("expected Error::Database, got {:?}", e) + }; + + assert_eq!(db_err.downcast_ref::().0.severity, Severity::Panic); + assert_eq!(db_err.downcast::().0.severity, Severity::Panic); +} \ No newline at end of file diff --git a/sqlx-core/src/postgres/protocol/mod.rs b/sqlx-core/src/postgres/protocol/mod.rs index 4626e9ca..8ba8268c 100644 --- a/sqlx-core/src/postgres/protocol/mod.rs +++ b/sqlx-core/src/postgres/protocol/mod.rs @@ -60,7 +60,7 @@ pub(crate) use message::Message; pub(crate) use notification_response::NotificationResponse; pub(crate) use parameter_description::ParameterDescription; pub(crate) use ready_for_query::ReadyForQuery; -pub(crate) use response::Response; +pub(crate) use response::{Response, Severity}; pub(crate) use row_description::{Field, RowDescription}; pub(crate) trait Write { diff --git a/sqlx-core/src/postgres/protocol/response.rs b/sqlx-core/src/postgres/protocol/response.rs index 6e31638e..bd876684 100644 --- a/sqlx-core/src/postgres/protocol/response.rs +++ b/sqlx-core/src/postgres/protocol/response.rs @@ -3,7 +3,7 @@ use std::str::{self, FromStr}; use crate::postgres::database::Postgres; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub(crate) enum Severity { Panic, Fatal, diff --git a/sqlx-core/src/sqlite/error.rs b/sqlx-core/src/sqlite/error.rs index e80fe4ed..d2b497e2 100644 --- a/sqlx-core/src/sqlite/error.rs +++ b/sqlx-core/src/sqlite/error.rs @@ -71,3 +71,21 @@ impl From for crate::Error { crate::Error::Database(Box::new(err)) } } + +#[test] +fn test_error_downcasting() { + let error = SqliteError { + code: "SQLITE_ERR_SOMETHING".into(), + message: "Some hypothetical error message".into() + }; + + let error = crate::Error::from(error); + + let db_err = match error { + crate::Error::Database(db_err) => db_err, + e => panic!("expected Error::Database, got {:?}", e) + }; + + assert_eq!(&db_err.downcast_ref::().code, "SQLITE_ERR_SOMETHING"); + assert_eq!(db_err.downcast::().code, "SQLITE_ERR_SOMETHING"); +} \ No newline at end of file