blob: 5e5e86ec69569f0f7b2a500f950d5e351ef0dbc9 [file] [log] [blame]
use std::io::prelude::*;
use crate::deserialize::FromSqlRow;
use crate::deserialize::{self, FromSql};
use crate::expression::AsExpression;
use crate::pg::{Pg, PgValue};
use crate::serialize::{self, IsNull, Output, ToSql};
use crate::sql_types::Uuid;
#[derive(FromSqlRow, AsExpression)]
#[diesel(foreign_derive)]
#[sql_type = "Uuid"]
#[allow(dead_code)]
struct UuidProxy(uuid::Uuid);
impl FromSql<Uuid, Pg> for uuid::Uuid {
fn from_sql(bytes: Option<PgValue<'_>>) -> deserialize::Result<Self> {
let value = not_none!(bytes);
uuid::Uuid::from_slice(value.as_bytes()).map_err(Into::into)
}
}
impl ToSql<Uuid, Pg> for uuid::Uuid {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
out.write_all(self.as_bytes())
.map(|_| IsNull::No)
.map_err(Into::into)
}
}
#[test]
fn uuid_to_sql() {
let mut bytes = Output::test();
let test_uuid = uuid::Uuid::from_fields(0xFFFF_FFFF, 0xFFFF, 0xFFFF, b"abcdef12").unwrap();
ToSql::<Uuid, Pg>::to_sql(&test_uuid, &mut bytes).unwrap();
assert_eq!(bytes, test_uuid.as_bytes());
}
#[test]
fn some_uuid_from_sql() {
let input_uuid = uuid::Uuid::from_fields(0xFFFF_FFFF, 0xFFFF, 0xFFFF, b"abcdef12").unwrap();
let output_uuid =
FromSql::<Uuid, Pg>::from_sql(Some(PgValue::for_test(input_uuid.as_bytes()))).unwrap();
assert_eq!(input_uuid, output_uuid);
}
#[test]
fn bad_uuid_from_sql() {
let uuid = uuid::Uuid::from_sql(Some(PgValue::for_test(b"boom")));
assert_eq!(
uuid.unwrap_err().to_string(),
"invalid bytes length: expected 16, found 4"
);
}
#[test]
fn no_uuid_from_sql() {
let uuid = uuid::Uuid::from_sql(None);
assert_eq!(
uuid.unwrap_err().to_string(),
"Unexpected null for non-null column"
);
}