use crate::{ColumnKind, CursorValue, DialectCursor, dialect};
use super::*;
use crate::primitives::mediatime::tests::rate;
const NTSC_TOKEN: [u8; 8] = [0, 0, 117, 48, 0, 0, 3, 233];
fn at<Db>(value: &Rate) -> Vec<u8>
where
Rate: DialectCursor<Db>,
{
let mut bytes = Vec::new();
value.write_cursor_at(&mut bytes);
bytes
}
#[test]
fn a_rate_spans_the_two_columns_it_is_bound_and_read_through() {
let names: Vec<_> = <Rate as ColumnKind>::SEGMENTS
.iter()
.map(|shape| {
shape
.field()
.expect("a plain segment names its column")
.declared()
.as_str()
})
.collect();
assert_eq!(names, ["num", "den"]);
}
#[test]
fn an_old_kit_token_decodes_through_the_mounted_seat() {
assert_eq!(
<Rate as DialectCursor<dialect::Sqlite>>::read_cursor_at(&NTSC_TOKEN),
Some(rate(30_000, 1001)),
"the token the hand kit wrote is still this seat's encoding"
);
assert_eq!(
at::<dialect::Sqlite>(&rate(30_000, 1001)),
NTSC_TOKEN,
"and the seat mints the identical bytes back"
);
assert_eq!(
NTSC_TOKEN.len(),
2 * size_of::<i32>(),
"eight flat bytes, not the framed sixteen"
);
}
#[test]
fn every_declared_dialect_walks_the_same_bytes() {
let held = rate(30_000, 1001);
assert_eq!(at::<dialect::Sqlite>(&held), NTSC_TOKEN);
assert_eq!(at::<dialect::Postgres>(&held), NTSC_TOKEN);
assert_eq!(at::<dialect::Mysql>(&held), NTSC_TOKEN);
for bytes in [
<Rate as DialectCursor<dialect::Postgres>>::read_cursor_at(&NTSC_TOKEN),
<Rate as DialectCursor<dialect::Mysql>>::read_cursor_at(&NTSC_TOKEN),
] {
assert_eq!(bytes, Some(held), "each dialect reads its own minting");
}
}
#[test]
fn the_stored_pair_is_the_rate_and_not_its_timebase() {
let mut reciprocal = Vec::new();
crate::primitives::mediatime::tests::timebase(1001, 30_000).write_cursor(&mut reciprocal);
assert_ne!(
at::<dialect::Sqlite>(&rate(30_000, 1001)),
reciprocal,
"a rate column and a timebase column of one value hold different pairs"
);
assert_eq!(
<Rate as DialectCursor<dialect::Sqlite>>::read_cursor_at(&NTSC_TOKEN)
.expect("its own bytes")
.get()
.num(),
30_000
);
}
#[test]
fn the_cursor_writes_the_pair_a_column_holds() {
assert_eq!(
at::<dialect::Sqlite>(&rate(60_000, 2002)),
at::<dialect::Sqlite>(&rate(30_000, 1001))
);
assert_eq!(
at::<dialect::Sqlite>(&rate(60_000, 2002)).len(),
2 * size_of::<i32>()
);
}
#[test]
fn a_written_cursor_reads_back_as_the_stored_value() {
for (num, den) in [(0, 1), (30, 1), (60_000, 2002), (30_000, 1001)] {
let written = rate(num, den);
let bytes = at::<dialect::Sqlite>(&written);
let read =
<Rate as DialectCursor<dialect::Sqlite>>::read_cursor_at(&bytes).expect("its own bytes");
assert_eq!(read, written, "{num}/{den} survives the round trip");
assert_eq!(read.reduced(), written.reduced());
}
}
#[test]
fn a_forged_cursor_is_refused_rather_than_read() {
for forged in [[0u8; 0].as_slice(), &[0; 4], &[0; 7], &[0; 9], &[0; 16]] {
assert_eq!(
<Rate as DialectCursor<dialect::Sqlite>>::read_cursor_at(forged),
None,
"{forged:?}"
);
}
let zero_denominator = [0, 0, 0, 30, 0, 0, 0, 0];
assert_eq!(
<Rate as DialectCursor<dialect::Sqlite>>::read_cursor_at(&zero_denominator),
None
);
let negative_numerator = [255, 255, 255, 255, 0, 0, 0, 1];
assert_eq!(
<Rate as DialectCursor<dialect::Sqlite>>::read_cursor_at(&negative_numerator),
None
);
}
#[test]
fn the_column_reader_states_which_invariant_a_pair_broke() {
assert_eq!(assembled(30, 0), Err(RateColumnError::ZeroDenominator));
assert_eq!(assembled(-1, 1), Err(RateColumnError::NegativeHalf));
assert_eq!(assembled(1, -2), Err(RateColumnError::NegativeHalf));
assert_eq!(assembled(0, 1), Ok(rate(0, 1)));
assert_eq!(assembled(60_000, 2002), Ok(rate(30_000, 1001)));
assert_eq!(
RateColumnError::ZeroDenominator.to_string(),
"the stored denominator must not be zero"
);
}