use super::super::{Cursor, CursorError, CursorValue, OrderTerm, TermName};
use crate::{ColumnKind, FieldName, OrderDirection, Timestamp};
const AT: &TermName = TermName::new("at");
fn instant() -> OrderTerm {
OrderTerm::spanning(
AT,
FieldName::new("at"),
<Timestamp as ColumnKind>::SEGMENTS,
<Timestamp as ColumnKind>::ORDER,
OrderDirection::Desc,
)
}
fn id() -> OrderTerm {
OrderTerm::new(
TermName::new("id"),
FieldName::new("id"),
OrderDirection::Asc,
)
}
fn at(second: i64, nanosecond: i32) -> Timestamp {
Timestamp::from_parts(second, nanosecond).expect("a legal instant")
}
#[test]
fn a_composite_term_is_one_subject_of_several_columns() {
let term = instant();
assert_eq!(term.width(), 2);
assert_eq!(term.segments().len(), 2);
assert_eq!(
term.column(),
FieldName::new("at"),
"the FIELD's name, which the columns are rendered beneath rather than a column of its own"
);
assert_eq!(id().width(), 1, "and a scalar is the empty answer");
}
#[test]
fn the_slot_carries_every_segment() {
let mut bytes = Vec::new();
at(7, 250).write_cursor(&mut bytes);
assert_eq!(bytes.len(), 12, "eight of seconds and four of nanoseconds");
assert_eq!(&bytes[..8], &7i64.to_be_bytes());
assert_eq!(&bytes[8..], &250i32.to_be_bytes());
}
#[test]
fn a_composite_boundary_resumes_its_own_order() {
let order = [instant(), id()];
let boundary = Cursor::at(
&order,
[Some(Value::At(at(1_700_000_000, 42))), Some(Value::Id(9))],
);
let text = boundary.encode();
let read = Cursor::<Value>::decode(&text).expect("this build minted it");
assert_eq!(read, boundary, "the frame round-trips");
assert_eq!(
read.resume(&order).expect("the same order"),
[Some(Value::At(at(1_700_000_000, 42))), Some(Value::Id(9))],
"both segments of the instant survive, as one value"
);
}
#[test]
fn a_composite_cursor_is_refused_under_another_order() {
let order = [instant(), id()];
let boundary = Cursor::at(&order, [Some(Value::At(at(1, 0))), Some(Value::Id(9))]);
let other = [
OrderTerm::spanning(
AT,
FieldName::new("at"),
<Timestamp as ColumnKind>::SEGMENTS,
<Timestamp as ColumnKind>::ORDER,
OrderDirection::Asc,
),
id(),
];
assert!(matches!(
boundary.resume(&other),
Err(CursorError::Order(_))
));
}
#[test]
fn a_truncated_pair_is_not_an_instant() {
for short in [&[0u8; 11][..], &[0; 13][..], &[]] {
assert!(
<Timestamp as CursorValue>::read_cursor(short).is_none(),
"a client's {} bytes are not an instant",
short.len()
);
}
}
#[test]
fn a_pair_the_type_normalises_is_refused() {
let mut bytes = Vec::new();
bytes.extend_from_slice(&7i64.to_be_bytes());
bytes.extend_from_slice(&1_000_000_000i32.to_be_bytes());
assert!(<Timestamp as CursorValue>::read_cursor(&bytes).is_none());
}
#[derive(Debug, Clone, PartialEq)]
enum Value {
At(Timestamp),
Id(i64),
}
impl CursorValue for Value {
fn write_cursor(&self, out: &mut Vec<u8>) {
match self {
Self::At(instant) => {
out.push(0);
instant.write_cursor(out);
}
Self::Id(id) => {
out.push(1);
id.write_cursor(out);
}
}
}
fn read_cursor(bytes: &[u8]) -> Option<Self> {
let (tag, rest) = bytes.split_first()?;
match tag {
0 => Timestamp::read_cursor(rest).map(Self::At),
1 => i64::read_cursor(rest).map(Self::Id),
_ => None,
}
}
}