Repository: diesel-rs/diesel
File: diesel/src/pg/types/uuid.rs (Rust)

Code before:
impl FromSql<Uuid, Pg> for uuid::Uuid {
    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
        uuid::Uuid::from_slice(value.as_bytes()).map_err(Into::into)
    }
}

#[cfg(all(feature = "postgres_backend", feature = "uuid"))]
impl ToSql<Uuid, Pg> for uuid::Uuid {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
        out.write_all(self.as_bytes())
            .map(|_| IsNull::No)
            .map_err(Into::into)
    }
}

#[test]
fn uuid_to_sql() {
    use crate::query_builder::bind_collector::ByteWrapper;

    let mut buffer = Vec::new();
    let test_uuid = uuid::Uuid::from_fields(0xFFFF_FFFF, 0xFFFF, 0xFFFF, b"abcdef12").unwrap();
    let mut bytes = Output::test(ByteWrapper(&mut buffer));
    ToSql::<Uuid, Pg>::to_sql(&test_uuid, &mut bytes).unwrap();
    assert_eq!(buffer, 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(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(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_nullable_sql(None);
    assert_eq!(
        uuid.unwrap_err().to_string(),
        "Unexpected null for non-null column"
    );
}

Review comment (suggestion):
Is there any value in continuing to test the `from_fields` method? Looks like that's still supported

Code after:
impl FromSql<Uuid, Pg> for uuid::Uuid {
    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
        uuid::Uuid::from_slice(value.as_bytes()).map_err(Into::into)
    }
}

#[cfg(all(feature = "postgres_backend", feature = "uuid"))]
impl ToSql<Uuid, Pg> for uuid::Uuid {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
        out.write_all(self.as_bytes())
            .map(|_| IsNull::No)
            .map_err(Into::into)
    }
}

#[test]
fn uuid_to_sql() {
    use crate::query_builder::bind_collector::ByteWrapper;

    let mut buffer = Vec::new();
    let bytes = [
        0xFF_u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
        0x31, 0x32,
    ];

    let test_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
    let mut bytes = Output::test(ByteWrapper(&mut buffer));
    ToSql::<Uuid, Pg>::to_sql(&test_uuid, &mut bytes).unwrap();
    assert_eq!(buffer, test_uuid.into_bytes());
}

#[test]
fn some_uuid_from_sql() {
    let bytes = [
        0xFF_u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
        0x31, 0x32,
    ];
    let input_uuid = uuid::Uuid::from_slice(&bytes).unwrap();
    let output_uuid =
        FromSql::<Uuid, Pg>::from_sql(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(PgValue::for_test(b"boom"));
    assert_eq!(
        uuid.unwrap_err().to_string(),
        "invalid length: expected 16 bytes, found 4"
    );
}