diff --git a/sqlx-mysql/src/type_info.rs b/sqlx-mysql/src/type_info.rs index 046a439f..78f24334 100644 --- a/sqlx-mysql/src/type_info.rs +++ b/sqlx-mysql/src/type_info.rs @@ -117,7 +117,38 @@ impl TypeInfo for MySqlTypeInfo { self.id.is_null() } - fn name(&self) -> &str { + fn name(&self) -> &'static str { self.name() } } + +#[cfg(test)] +impl MySqlTypeInfo { + pub(crate) const TINYINT_1: Self = + Self { id: MySqlTypeId::TINYINT, max_size: 1, flags: ColumnFlags::empty(), charset: 0 }; + + pub(crate) const BIGINT: Self = + Self { id: MySqlTypeId::BIGINT, max_size: 0, flags: ColumnFlags::empty(), charset: 0 }; + + pub(crate) const BINARY: Self = Self { + id: MySqlTypeId::CHAR, + max_size: 0, + flags: ColumnFlags::BINARY_COLLATION, + charset: 0, + }; +} + +#[cfg(test)] +mod tests { + use super::MySqlTypeInfo; + + #[test] + fn should_identify_boolean() { + assert_eq!(MySqlTypeInfo::TINYINT_1.name(), "BOOLEAN"); + } + + #[test] + fn should_identify_binary() { + assert_eq!(MySqlTypeInfo::BINARY.name(), "BINARY"); + } +} diff --git a/sqlx-mysql/src/types/bool.rs b/sqlx-mysql/src/types/bool.rs index c96f920e..4b860b2a 100644 --- a/sqlx-mysql/src/types/bool.rs +++ b/sqlx-mysql/src/types/bool.rs @@ -27,3 +27,31 @@ impl<'r> Decode<'r, MySql> for bool { Ok(raw.decode::()? != 0) } } + +#[cfg(test)] +mod tests { + use bytes::Bytes; + use sqlx_core::Result; + + use crate::{MySqlRawValue, MySqlRawValueFormat, MySqlTypeInfo}; + + #[test] + fn decode_bool_from_tinyint() -> Result<()> { + let bytes = Bytes::from_static(b"\x01"); + let val: bool = MySqlRawValue::binary(&bytes, &MySqlTypeInfo::TINYINT_1).decode()?; + + assert_eq!(val, true); + + Ok(()) + } + + #[test] + fn decode_bool_from_bigint() -> Result<()> { + let bytes = Bytes::from_static(b"\x01\x00\x00\x00\x00\x00\x00\x00"); + let val: bool = MySqlRawValue::binary(&bytes, &MySqlTypeInfo::BIGINT).decode()?; + + assert_eq!(val, true); + + Ok(()) + } +} diff --git a/sqlx-mysql/src/types/str.rs b/sqlx-mysql/src/types/str.rs index a986152f..f7678a70 100644 --- a/sqlx-mysql/src/types/str.rs +++ b/sqlx-mysql/src/types/str.rs @@ -2,8 +2,7 @@ use std::str::from_utf8_unchecked; use bytes::Bytes; use bytestring::ByteString; -use sqlx_core::{decode, encode, Type}; -use sqlx_core::{Decode, Encode}; +use sqlx_core::{decode, encode, Decode, Encode, Type}; use crate::io::MySqlWriteExt; use crate::type_info::MySqlTypeInfo;