mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 14:34:19 +00:00
add proper encode/decode test
This commit is contained in:
parent
60ef862713
commit
1e9e816fab
2 changed files with 59 additions and 22 deletions
|
@ -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
|
||||
|
|
|
@ -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<dyn Encode<sqlx::Postgres>> = Box::new(Foo(1));
|
||||
#[cfg(feature = "mysql")]
|
||||
let _: Box<dyn Encode<sqlx::MySql>> = Box::new(Foo(1));
|
||||
#[cfg(feature = "mysql")]
|
||||
fn encode_mysql() {
|
||||
encode_with_db::<sqlx::MySql>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn decode() {
|
||||
use sqlx::decode::Decode;
|
||||
|
||||
#[derive(Decode)]
|
||||
struct Foo(i32);
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
<Foo as Decode<sqlx::Postgres>>::decode_null().ok();
|
||||
#[cfg(feature = "mysql")]
|
||||
<Foo as Decode<sqlx::MySql>>::decode_null().ok();
|
||||
#[cfg(feature = "postgres")]
|
||||
fn encode_postgres() {
|
||||
encode_with_db::<sqlx::Postgres>();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn encode_with_db<DB: sqlx::Database>()
|
||||
where
|
||||
Foo: Encode<DB>,
|
||||
i32: Encode<DB>,
|
||||
{
|
||||
let example = Foo(0x1122_3344);
|
||||
|
||||
let mut encoded = Vec::new();
|
||||
let mut encoded_orig = Vec::new();
|
||||
|
||||
Encode::<DB>::encode(&example, &mut encoded);
|
||||
Encode::<DB>::encode(&example.0, &mut encoded_orig);
|
||||
|
||||
assert_eq!(encoded, encoded_orig);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "mysql")]
|
||||
fn decode_mysql() {
|
||||
decode_with_db::<sqlx::MySql>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "postgres")]
|
||||
fn decode_postgres() {
|
||||
decode_with_db::<sqlx::Postgres>();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn decode_with_db<DB: sqlx::Database>()
|
||||
where
|
||||
Foo: Decode<DB> + Encode<DB>,
|
||||
{
|
||||
let example = Foo(0x1122_3344);
|
||||
|
||||
let mut encoded = Vec::new();
|
||||
Encode::<DB>::encode(&example, &mut encoded);
|
||||
|
||||
let decoded = Foo::decode(&encoded).unwrap();
|
||||
assert_eq!(example, decoded);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue