use super::IdColumn;
type Blind = ();
fn round_trip<T: IdColumn<Blind>>(value: T) -> Result<T, T::Refusal> {
T::from_stored(value.to_stored())
}
#[test]
fn the_text_road_round_trips_every_shape_it_carries() {
assert_eq!(round_trip(-42_i64), Ok(-42));
assert_eq!(
round_trip(core::num::NonZeroU32::new(7).expect("seven is not zero")),
Ok(core::num::NonZeroU32::new(7).expect("seven is not zero")),
);
assert_eq!(
round_trip("2001:db8::1".parse::<core::net::Ipv6Addr>().expect("a v6")),
Ok("2001:db8::1".parse().expect("a v6")),
);
assert_eq!(
round_trip(
"[2001:db8::1]:443"
.parse::<core::net::SocketAddrV6>()
.expect("a v6 socket"),
),
Ok("[2001:db8::1]:443".parse().expect("a v6 socket")),
);
}
#[test]
fn the_stored_text_is_the_types_own_rendering() {
assert_eq!(<i64 as IdColumn<Blind>>::to_stored(&-42), "-42");
assert_eq!(
<core::net::Ipv4Addr as IdColumn<Blind>>::to_stored(&core::net::Ipv4Addr::new(10, 0, 0, 1)),
"10.0.0.1",
);
}
#[test]
fn the_text_road_refuses_a_spelling_the_type_does_not_admit() {
assert!(<i64 as IdColumn<Blind>>::from_stored("not a number".to_owned()).is_err());
assert!(
<core::num::NonZeroU32 as IdColumn<Blind>>::from_stored("0".to_owned()).is_err(),
"the invariant is re-established coming back, by the type's own door",
);
assert!(<core::net::IpAddr as IdColumn<Blind>>::from_stored(String::new()).is_err());
}
#[test]
fn the_uuid_road_is_the_value_itself() {
let raw = uuid::Uuid::from_u128(0x0192_38ab_cdef_0123_4567_89ab_cdef_0123);
let stored: uuid::Uuid = <uuid::Uuid as IdColumn<Blind>>::to_stored(&raw);
assert_eq!(stored, raw);
assert_eq!(round_trip(raw), Ok(raw));
assert_eq!(
stored.as_bytes().len(),
16,
"the stored form is the bytes and not the 36-character text",
);
}
#[test]
fn the_big_endian_blob_road_round_trips_the_wide_integers() {
assert_eq!(round_trip(i128::MIN), Ok(i128::MIN));
assert_eq!(round_trip(u128::MAX), Ok(u128::MAX));
assert_eq!(<i128 as IdColumn<Blind>>::to_stored(&-1), crate::BeI128(-1),);
assert_eq!(<u128 as IdColumn<Blind>>::to_stored(&1), crate::BeU128(1));
}
#[cfg(feature = "sqlite")]
#[test]
fn the_big_endian_blob_road_round_trips_u64_on_sqlite() {
assert_eq!(
<u64 as IdColumn<sqlx::Sqlite>>::from_stored(<u64 as IdColumn<sqlx::Sqlite>>::to_stored(&9)),
Ok(9),
);
assert_eq!(
<u64 as IdColumn<sqlx::Sqlite>>::from_stored(<u64 as IdColumn<sqlx::Sqlite>>::to_stored(
&u64::MAX
)),
Ok(u64::MAX),
);
let stored: crate::BeU64 = <u64 as IdColumn<sqlx::Sqlite>>::to_stored(&9);
assert_eq!(stored, crate::BeU64(9));
assert_eq!(
stored.to_blob().len(),
8,
"the stored form is eight bytes and not a decimal rendering",
);
}
#[cfg(feature = "postgres")]
#[test]
fn the_big_endian_blob_road_round_trips_u64_on_postgres() {
assert_eq!(
<u64 as IdColumn<sqlx::Postgres>>::from_stored(<u64 as IdColumn<sqlx::Postgres>>::to_stored(
&9
)),
Ok(9),
);
assert_eq!(
<u64 as IdColumn<sqlx::Postgres>>::from_stored(<u64 as IdColumn<sqlx::Postgres>>::to_stored(
&u64::MAX
)),
Ok(u64::MAX),
);
}
#[cfg(feature = "mysql")]
#[test]
fn u64_stores_natively_on_mysql() {
let stored: u64 = <u64 as IdColumn<sqlx::MySql>>::to_stored(&9);
assert_eq!(
stored, 9,
"the stored form IS the value — no blob, no rendering"
);
assert_eq!(<u64 as IdColumn<sqlx::MySql>>::from_stored(stored), Ok(9));
}
#[cfg(feature = "sqlite")]
#[test]
fn the_stored_bytes_now_agree_with_the_integers_own_ord() {
let (nine, ten): (crate::BeU64, crate::BeU64) = (
<u64 as IdColumn<sqlx::Sqlite>>::to_stored(&9),
<u64 as IdColumn<sqlx::Sqlite>>::to_stored(&10),
);
assert!(
nine.to_blob() < ten.to_blob(),
"the stored bytes compare the same way the values do",
);
assert!(
"9" > "10",
"which the abandoned text road would have gotten backwards",
);
}
#[cfg(feature = "mysql")]
#[test]
fn a_u64_inner_id_on_mysql_orders_numerically_not_lexicographically() {
let scrambled = [10u64, 100, 2, 9, 1];
let mut stored: Vec<u64> = scrambled
.iter()
.map(<u64 as IdColumn<sqlx::MySql>>::to_stored)
.collect();
stored.sort_unstable();
assert_eq!(
stored,
[1, 2, 9, 10, 100],
"BIGINT UNSIGNED sorts numerically because the stored form IS the value — nothing here \
re-encodes it, so there is no byte order to disagree with `u64::Ord`",
);
let mut decimal: Vec<String> = scrambled.iter().map(ToString::to_string).collect();
decimal.sort_unstable();
assert_ne!(
decimal,
["1", "2", "9", "10", "100"],
"the abandoned text road would have sorted these lexicographically instead",
);
}
#[test]
fn the_text_members_that_are_columns_are_stored_as_themselves() {
assert_eq!(round_trip("abc".to_owned()), Ok("abc".to_owned()));
let carried = smol_bytes::Utf8Bytes::from("abc");
assert_eq!(round_trip(carried.clone()), Ok(carried));
}
#[cfg(feature = "sqlite")]
#[test]
fn the_two_roads_declare_different_columns() {
use crate::StoredAs;
assert_eq!(
<<uuid::Uuid as IdColumn<sqlx::Sqlite>>::Stored as StoredAs<sqlx::Sqlite>>::WORDS,
["BLOB"],
"a uuid-inner id is declared at the native sixteen bytes",
);
assert_eq!(
<<i64 as IdColumn<sqlx::Sqlite>>::Stored as StoredAs<sqlx::Sqlite>>::WORDS,
["TEXT"],
"and every text-road member at the column its rendering goes into",
);
assert_eq!(
<<String as IdColumn<sqlx::Sqlite>>::Stored as StoredAs<sqlx::Sqlite>>::WORDS,
["TEXT"],
);
assert_eq!(
<<u64 as IdColumn<sqlx::Sqlite>>::Stored as StoredAs<sqlx::Sqlite>>::WORDS,
["BLOB"],
"a u64-inner id is declared at its eight big-endian bytes",
);
assert_eq!(
<<i128 as IdColumn<sqlx::Sqlite>>::Stored as StoredAs<sqlx::Sqlite>>::WORDS,
["BLOB"],
);
assert_eq!(
<<u128 as IdColumn<sqlx::Sqlite>>::Stored as StoredAs<sqlx::Sqlite>>::WORDS,
["BLOB"],
);
}
#[cfg(feature = "postgres")]
#[test]
fn the_native_road_takes_postgres_own_type() {
use crate::StoredAs;
assert_eq!(
<<uuid::Uuid as IdColumn<sqlx::Postgres>>::Stored as StoredAs<sqlx::Postgres>>::WORDS,
["uuid"],
);
assert_eq!(
<<i64 as IdColumn<sqlx::Postgres>>::Stored as StoredAs<sqlx::Postgres>>::WORDS,
["text"],
);
assert_eq!(
<<u64 as IdColumn<sqlx::Postgres>>::Stored as StoredAs<sqlx::Postgres>>::WORDS,
["bytea"],
);
assert_eq!(
<<i128 as IdColumn<sqlx::Postgres>>::Stored as StoredAs<sqlx::Postgres>>::WORDS,
["bytea"],
);
assert_eq!(
<<u128 as IdColumn<sqlx::Postgres>>::Stored as StoredAs<sqlx::Postgres>>::WORDS,
["bytea"],
);
}
#[cfg(feature = "mysql")]
#[test]
fn the_native_road_takes_mysqls_fixed_width_binary() {
use crate::StoredAs;
assert_eq!(
<<uuid::Uuid as IdColumn<sqlx::MySql>>::Stored as StoredAs<sqlx::MySql>>::WORDS,
["binary(16)"],
);
assert_eq!(
<<i64 as IdColumn<sqlx::MySql>>::Stored as StoredAs<sqlx::MySql>>::WORDS,
["varchar(255)"],
);
}
#[cfg(feature = "mysql")]
#[test]
fn the_128_bit_movers_declare_mysqls_blob_where_u64_declares_its_own_native_word() {
use crate::StoredAs;
assert_eq!(
<<i128 as IdColumn<sqlx::MySql>>::Stored as StoredAs<sqlx::MySql>>::WORDS,
["blob"],
);
assert_eq!(
<<u128 as IdColumn<sqlx::MySql>>::Stored as StoredAs<sqlx::MySql>>::WORDS,
["blob"],
);
assert_eq!(
<<u64 as IdColumn<sqlx::MySql>>::Stored as StoredAs<sqlx::MySql>>::WORDS,
["bigint unsigned"],
"a u64-inner id is declared at MySQL's own native column, not a blob",
);
}
#[cfg(feature = "smol_str")]
#[test]
fn the_compact_text_member_round_trips() {
let value = smol_str::SmolStr::new("a-b-c");
assert_eq!(round_trip(value.clone()), Ok(value));
}
#[cfg(feature = "mongo")]
#[test]
fn the_bson_pair_round_trips_through_its_canonical_text() {
let uuid = bson::Uuid::from_bytes([7; 16]);
assert_eq!(
<bson::Uuid as IdColumn<Blind>>::to_stored(&uuid),
uuid.to_string(),
);
assert!(round_trip(uuid).is_ok());
let oid = bson::oid::ObjectId::from_bytes([3; 12]);
assert_eq!(
<bson::oid::ObjectId as IdColumn<Blind>>::to_stored(&oid),
"030303030303030303030303",
);
assert!(round_trip(oid).is_ok());
}