test(mysql): add some basic unit tests for types to experiment

This commit is contained in:
Ryan Leckey 2021-02-26 00:20:01 -08:00
parent 6b5f37dfb1
commit 730439fcf7
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9
3 changed files with 61 additions and 3 deletions

View File

@ -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");
}
}

View File

@ -27,3 +27,31 @@ impl<'r> Decode<'r, MySql> for bool {
Ok(raw.decode::<u8>()? != 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(())
}
}

View File

@ -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;