use jiff::{civil, tz};
use crate::{ColumnKind, ColumnShape, CursorValue, OrderLeaves};
use super::{
LOCAL, LocalTimestamp, LocalTimestampLeaf, LocalTimestampPartsError, OFFSET_SECONDS,
OFFSET_STATED, ORDER, SEGMENTS, UTC_NANOS, civil_of, offset_of,
};
fn wall() -> civil::DateTime {
civil::date(2021, 8, 14).at(16, 45, 9, 123_456_789)
}
fn tokyo() -> LocalTimestamp {
LocalTimestamp::at(wall(), tz::Offset::constant(9))
}
#[test]
fn three_leaves_are_the_value_and_the_derived_one_is_not() {
let table: Vec<_> = SEGMENTS
.iter()
.map(|shape| (shape.holds_value(), shape.self_nullable()))
.collect();
assert_eq!(
table,
[(true, false), (true, false), (true, false), (false, true)],
"local, offset_seconds and offset_stated are the VALUE; utc_nanos is a nullable INDEX"
);
assert_eq!(crate::width(SEGMENTS), 4);
assert_eq!(crate::value_width(SEGMENTS), 3);
assert_eq!(
SEGMENTS.iter().filter_map(ColumnShape::field).count(),
4,
"every leaf names a field, both derived and not"
);
}
#[test]
fn the_segments_are_named_as_the_module_documents_them() {
assert_eq!(
SEGMENTS
.iter()
.filter_map(ColumnShape::field)
.map(|field| field.stored().as_str().to_owned())
.collect::<Vec<_>>(),
["local", "offset_seconds", "offset_stated", "utc_nanos"]
);
assert_eq!(LOCAL.as_str(), "local");
assert_eq!(OFFSET_SECONDS.as_str(), "offset_seconds");
assert_eq!(OFFSET_STATED.as_str(), "offset_stated");
assert_eq!(UTC_NANOS.as_str(), "utc_nanos");
}
#[test]
fn the_order_is_utc_led_and_covers_every_leaf() {
assert_eq!(
ORDER.iter().map(|level| level.leaf()).collect::<Vec<_>>(),
[3, 0, 1, 2]
);
assert_eq!(
<LocalTimestamp as ColumnKind>::ORDER.len(),
crate::width(SEGMENTS),
"every leaf sits in exactly one level"
);
}
#[test]
fn the_leaves_answer_the_value_then_the_recomputed_position() {
let capture = tokyo();
assert_eq!(capture.leaf(0), Some(LocalTimestampLeaf::Local(wall())));
assert_eq!(
capture.leaf(1),
Some(LocalTimestampLeaf::OffsetSeconds(32_400))
);
assert_eq!(
capture.leaf(2),
Some(LocalTimestampLeaf::OffsetStated(true))
);
assert_eq!(
capture.leaf(3),
Some(LocalTimestampLeaf::UtcNanos(1_628_927_109_123_456_789))
);
assert_eq!(capture.leaf(4), None, "there is no fifth leaf");
}
#[test]
fn an_unzoned_value_answers_none_at_the_derived_level_only() {
let capture = LocalTimestamp::wall(wall());
assert_eq!(capture.leaf(0), Some(LocalTimestampLeaf::Local(wall())));
assert_eq!(capture.leaf(1), Some(LocalTimestampLeaf::OffsetSeconds(0)));
assert_eq!(
capture.leaf(2),
Some(LocalTimestampLeaf::OffsetStated(false)),
"the presence bit is a VALUE leaf and is never absent"
);
assert_eq!(capture.leaf(3), None, "and there is no position");
}
#[test]
fn the_cursor_carries_the_value_leaves_and_round_trips_both_shapes() {
for capture in [tokyo(), LocalTimestamp::wall(wall())] {
let mut bytes = Vec::new();
capture.write_cursor(&mut bytes);
assert_eq!(
bytes.len(),
16,
"eleven civil bytes, four seconds, one flag"
);
assert_eq!(
<LocalTimestamp as CursorValue>::read_cursor(&bytes),
Some(capture)
);
}
}
#[test]
fn a_cursor_of_the_wrong_width_is_refused() {
let mut bytes = Vec::new();
tokyo().write_cursor(&mut bytes);
assert!(<LocalTimestamp as CursorValue>::read_cursor(&bytes[..15]).is_none());
let mut wide = bytes.clone();
wide.push(0);
assert!(<LocalTimestamp as CursorValue>::read_cursor(&wide).is_none());
}
#[test]
fn a_forged_cursor_is_refused_by_the_same_rules_the_decode_uses() {
let mut bytes = Vec::new();
tokyo().write_cursor(&mut bytes);
let mut month = bytes.clone();
month[2] = 13;
assert!(<LocalTimestamp as CursorValue>::read_cursor(&month).is_none());
let mut unstated = bytes.clone();
unstated[15] = 0;
assert!(<LocalTimestamp as CursorValue>::read_cursor(&unstated).is_none());
}
#[test]
fn a_leaf_slot_round_trips_and_refuses_an_unknown_tag() {
for leaf in [
LocalTimestampLeaf::Local(wall()),
LocalTimestampLeaf::OffsetSeconds(-16_200),
LocalTimestampLeaf::OffsetStated(false),
LocalTimestampLeaf::UtcNanos(1_628_927_109_123_456_789),
] {
let mut bytes = Vec::new();
leaf.write_cursor(&mut bytes);
assert_eq!(
<LocalTimestampLeaf as CursorValue>::read_cursor(&bytes),
Some(leaf)
);
}
assert!(<LocalTimestampLeaf as CursorValue>::read_cursor(&[9, 0, 0, 0, 0]).is_none());
assert!(<LocalTimestampLeaf as CursorValue>::read_cursor(&[]).is_none());
}
#[test]
fn the_offset_pair_reads_back_and_refuses_the_arrangement_the_type_cannot_hold() {
assert_eq!(offset_of(0, false).expect("a silent file"), None);
assert_eq!(
offset_of(32_400, true).expect("a Tokyo file"),
Some(tz::Offset::constant(9))
);
assert_eq!(
offset_of(0, true).expect("a file that said UTC"),
Some(tz::Offset::UTC)
);
assert!(matches!(
offset_of(32_400, false),
Err(LocalTimestampPartsError::Unstated { seconds: 32_400 })
));
assert!(matches!(
offset_of(93_600, true),
Err(LocalTimestampPartsError::Offset(_))
));
}
#[test]
fn the_reverse_crossing_is_the_forward_ones_inverse() {
use crate::TemporalStorage as _;
for local in [
civil::DateTime::MIN,
civil::DateTime::MAX,
wall(),
civil::date(-4000, 2, 29).at(23, 59, 59, 999_999_999),
] {
assert_eq!(
civil_of(local.to_storage()).expect("the span is the same on both sides"),
local
);
}
}
#[cfg(feature = "sqlite")]
#[test]
fn the_declared_words_are_the_leaf_types_own() {
use crate::StoredAs;
assert_eq!(
<LocalTimestamp as StoredAs<sqlx::Sqlite>>::WORDS,
[
<time::PrimitiveDateTime as StoredAs<sqlx::Sqlite>>::WORDS[0],
<i32 as StoredAs<sqlx::Sqlite>>::WORDS[0],
<bool as StoredAs<sqlx::Sqlite>>::WORDS[0],
<i64 as StoredAs<sqlx::Sqlite>>::WORDS[0],
]
);
}
#[cfg(feature = "mongo")]
#[test]
fn the_bson_document_writes_four_keys_and_reads_three_back() {
use crate::{MongoCarrier, query::mongo::MongoExtract};
for capture in [tokyo(), LocalTimestamp::wall(wall())] {
let written = capture.to_bson();
let bson::Bson::Document(held) = &written else {
panic!("a capture time is a sub-document");
};
assert_eq!(
held.keys().map(String::as_str).collect::<Vec<_>>(),
["utc_nanos", "local", "offset_seconds", "offset_stated"],
"written in the order the ORDER means them"
);
assert_eq!(
held.get("utc_nanos").expect("the derived key is written"),
&match capture.to_utc_nanos() {
Ok(nanos) => bson::Bson::Int64(nanos),
Err(_) => bson::Bson::Null,
}
);
assert_eq!(
<LocalTimestamp as MongoExtract>::extract(Some(&written)).expect("it reads back"),
capture
);
}
}
#[cfg(feature = "mongo")]
#[test]
fn a_bson_document_naming_an_impossible_pair_is_refused() {
use crate::query::mongo::{MongoExtract, MongoTypeError};
let mut held = bson::Document::new();
held.insert("utc_nanos", bson::Bson::Null);
held.insert("local", "2021-08-14T16:45:09");
held.insert("offset_seconds", 32_400);
held.insert("offset_stated", false);
assert!(matches!(
<LocalTimestamp as MongoExtract>::extract(Some(&bson::Bson::Document(held))),
Err(MongoTypeError::Range(_))
));
}