diff --git a/Cargo.toml b/Cargo.toml index 4889f558..84b7dfd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,5 +84,9 @@ required-features = [ "mysql" ] name = "mysql-types-chrono" required-features = [ "mysql", "chrono", "macros" ] +[[test]] +name = "derives" +required-features = [ "macros" ] + [profile.release] lto = true diff --git a/tests/derives.rs b/tests/derives.rs index e9b36e0a..9c7aa411 100644 --- a/tests/derives.rs +++ b/tests/derives.rs @@ -1,27 +1,60 @@ +use sqlx::decode::Decode; +use sqlx::encode::Encode; + +#[derive(PartialEq, Debug, Encode, Decode)] +struct Foo(i32); + #[test] -#[cfg(feature = "macros")] -fn encode() { - use sqlx::encode::Encode; - - #[derive(Encode)] - struct Foo(i32); - - #[cfg(feature = "postgres")] - let _: Box> = Box::new(Foo(1)); - #[cfg(feature = "mysql")] - let _: Box> = Box::new(Foo(1)); +#[cfg(feature = "mysql")] +fn encode_mysql() { + encode_with_db::(); } #[test] -#[cfg(feature = "macros")] -fn decode() { - use sqlx::decode::Decode; - - #[derive(Decode)] - struct Foo(i32); - - #[cfg(feature = "postgres")] - >::decode_null().ok(); - #[cfg(feature = "mysql")] - >::decode_null().ok(); +#[cfg(feature = "postgres")] +fn encode_postgres() { + encode_with_db::(); +} + +#[allow(dead_code)] +fn encode_with_db() +where + Foo: Encode, + i32: Encode, +{ + let example = Foo(0x1122_3344); + + let mut encoded = Vec::new(); + let mut encoded_orig = Vec::new(); + + Encode::::encode(&example, &mut encoded); + Encode::::encode(&example.0, &mut encoded_orig); + + assert_eq!(encoded, encoded_orig); +} + +#[test] +#[cfg(feature = "mysql")] +fn decode_mysql() { + decode_with_db::(); +} + +#[test] +#[cfg(feature = "postgres")] +fn decode_postgres() { + decode_with_db::(); +} + +#[allow(dead_code)] +fn decode_with_db() +where + Foo: Decode + Encode, +{ + let example = Foo(0x1122_3344); + + let mut encoded = Vec::new(); + Encode::::encode(&example, &mut encoded); + + let decoded = Foo::decode(&encoded).unwrap(); + assert_eq!(example, decoded); }