335 lines
11 KiB
Rust
335 lines
11 KiB
Rust
//! QA contract tests for `session_close::et_session_close_utc_micros` (T-99 / M1.2q) and the
|
|
//! pre-2008 `bars_1d` write path.
|
|
//!
|
|
//! The DST transition table below is NOT derived from the crate's Sunday-rule logic: it was
|
|
//! generated once from IANA tzdata (Python `zoneinfo`, `America/New_York` and `America/Toronto`
|
|
//! produced identical tables for 1976..=2030) by finding the first day each year whose 16:00 local
|
|
//! close maps to 20:00 UTC (EDT start) and back to 21:00 UTC (EST start). It also confirmed that in
|
|
//! 1974 the two zones diverged (New York 16:00 = 20:00 UTC on 1974-01-15, Toronto 21:00 UTC), which
|
|
//! is why dates before 1976-01-01 must stay out of range.
|
|
|
|
use std::borrow::Cow;
|
|
use std::collections::HashMap;
|
|
use std::error::Error;
|
|
use std::fs::{self, File};
|
|
use std::path::PathBuf;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use curate_etl::{
|
|
curate_and_write_bars_1d, curate_bars_1d, et_session_close_utc_micros, Bars1dWriteOptions,
|
|
EodDailyBar, ProviderMappingRow, ProviderMappingTable, QmRawVolume, SessionCloseError,
|
|
SplitTable, SESSION_CLOSE_MAX_SUPPORTED_DATE, SESSION_CLOSE_MAX_SUPPORTED_YEAR,
|
|
SESSION_CLOSE_MIN_SUPPORTED_DATE, SESSION_CLOSE_MIN_SUPPORTED_YEAR,
|
|
};
|
|
use parquet::file::reader::{FileReader, SerializedFileReader};
|
|
|
|
const MICROS_PER_HOUR: i64 = 3_600_000_000;
|
|
const MICROS_PER_DAY: i64 = 86_400_000_000;
|
|
|
|
/// `(year, edt_start_month, edt_start_day, est_start_month, est_start_day)` from IANA tzdata.
|
|
const TZDATA_TRANSITIONS: [(i32, u32, u32, u32, u32); 55] = [
|
|
(1976, 4, 25, 10, 31),
|
|
(1977, 4, 24, 10, 30),
|
|
(1978, 4, 30, 10, 29),
|
|
(1979, 4, 29, 10, 28),
|
|
(1980, 4, 27, 10, 26),
|
|
(1981, 4, 26, 10, 25),
|
|
(1982, 4, 25, 10, 31),
|
|
(1983, 4, 24, 10, 30),
|
|
(1984, 4, 29, 10, 28),
|
|
(1985, 4, 28, 10, 27),
|
|
(1986, 4, 27, 10, 26),
|
|
(1987, 4, 5, 10, 25),
|
|
(1988, 4, 3, 10, 30),
|
|
(1989, 4, 2, 10, 29),
|
|
(1990, 4, 1, 10, 28),
|
|
(1991, 4, 7, 10, 27),
|
|
(1992, 4, 5, 10, 25),
|
|
(1993, 4, 4, 10, 31),
|
|
(1994, 4, 3, 10, 30),
|
|
(1995, 4, 2, 10, 29),
|
|
(1996, 4, 7, 10, 27),
|
|
(1997, 4, 6, 10, 26),
|
|
(1998, 4, 5, 10, 25),
|
|
(1999, 4, 4, 10, 31),
|
|
(2000, 4, 2, 10, 29),
|
|
(2001, 4, 1, 10, 28),
|
|
(2002, 4, 7, 10, 27),
|
|
(2003, 4, 6, 10, 26),
|
|
(2004, 4, 4, 10, 31),
|
|
(2005, 4, 3, 10, 30),
|
|
(2006, 4, 2, 10, 29),
|
|
(2007, 3, 11, 11, 4),
|
|
(2008, 3, 9, 11, 2),
|
|
(2009, 3, 8, 11, 1),
|
|
(2010, 3, 14, 11, 7),
|
|
(2011, 3, 13, 11, 6),
|
|
(2012, 3, 11, 11, 4),
|
|
(2013, 3, 10, 11, 3),
|
|
(2014, 3, 9, 11, 2),
|
|
(2015, 3, 8, 11, 1),
|
|
(2016, 3, 13, 11, 6),
|
|
(2017, 3, 12, 11, 5),
|
|
(2018, 3, 11, 11, 4),
|
|
(2019, 3, 10, 11, 3),
|
|
(2020, 3, 8, 11, 1),
|
|
(2021, 3, 14, 11, 7),
|
|
(2022, 3, 13, 11, 6),
|
|
(2023, 3, 12, 11, 5),
|
|
(2024, 3, 10, 11, 3),
|
|
(2025, 3, 9, 11, 2),
|
|
(2026, 3, 8, 11, 1),
|
|
(2027, 3, 14, 11, 7),
|
|
(2028, 3, 12, 11, 5),
|
|
(2029, 3, 11, 11, 4),
|
|
(2030, 3, 10, 11, 3),
|
|
];
|
|
|
|
static SEQ: AtomicU64 = AtomicU64::new(0);
|
|
|
|
struct TempDir(PathBuf);
|
|
|
|
impl TempDir {
|
|
fn new(tag: &str) -> Self {
|
|
let seq = SEQ.fetch_add(1, Ordering::Relaxed);
|
|
let path = std::env::temp_dir().join(format!(
|
|
"curate-etl-session-close-qa-{tag}-{}-{seq}",
|
|
std::process::id()
|
|
));
|
|
let _ = fs::remove_dir_all(&path);
|
|
fs::create_dir_all(&path).expect("create temp dir");
|
|
Self(path)
|
|
}
|
|
}
|
|
|
|
impl Drop for TempDir {
|
|
fn drop(&mut self) {
|
|
let _ = fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
|
|
fn day(y: i32, m: u32, d: u32) -> i32 {
|
|
curate_etl::ymd_to_epoch_days(y, m, d).expect("valid date")
|
|
}
|
|
|
|
fn close_at(epoch_day: i32, utc_hour: i64) -> i64 {
|
|
i64::from(epoch_day) * MICROS_PER_DAY + utc_hour * MICROS_PER_HOUR
|
|
}
|
|
|
|
fn bar(key: &str, d: i32, c: f64) -> EodDailyBar<String> {
|
|
EodDailyBar {
|
|
key: key.to_string(),
|
|
session_date: d,
|
|
open: c,
|
|
high: c + 1.0,
|
|
low: c - 1.0,
|
|
close: c,
|
|
adj_volume: 1_000.0,
|
|
}
|
|
}
|
|
|
|
fn open_mapping(id: &str, lid: i32) -> ProviderMappingTable {
|
|
ProviderMappingTable::from_rows([ProviderMappingRow {
|
|
source: "eod".to_string(),
|
|
source_id: id.to_string(),
|
|
valid_from: i32::MIN,
|
|
valid_to: None,
|
|
listing_id: lid,
|
|
}])
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn tzdata_table_covers_exactly_the_supported_years() {
|
|
assert_eq!(TZDATA_TRANSITIONS[0].0, SESSION_CLOSE_MIN_SUPPORTED_YEAR);
|
|
assert_eq!(
|
|
TZDATA_TRANSITIONS[TZDATA_TRANSITIONS.len() - 1].0,
|
|
SESSION_CLOSE_MAX_SUPPORTED_YEAR
|
|
);
|
|
assert_eq!(SESSION_CLOSE_MIN_SUPPORTED_DATE, day(1976, 1, 1));
|
|
assert_eq!(SESSION_CLOSE_MAX_SUPPORTED_DATE, day(2030, 12, 31));
|
|
}
|
|
|
|
#[test]
|
|
fn every_day_1976_to_2030_matches_iana_tzdata_close() {
|
|
for &(y, sm, sd, em, ed) in &TZDATA_TRANSITIONS {
|
|
let edt_start = day(y, sm, sd);
|
|
let est_start = day(y, em, ed);
|
|
for d in day(y, 1, 1)..=day(y, 12, 31) {
|
|
let hour = if d >= edt_start && d < est_start { 20 } else { 21 };
|
|
assert_eq!(
|
|
et_session_close_utc_micros(d),
|
|
Ok(close_at(d, hour)),
|
|
"epoch_day={d} in {y}: expected 16:00 ET = {hour}:00 UTC per tzdata"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn transition_eve_and_day_after_match_tzdata_for_every_year() {
|
|
// Saturday before each switch keeps the old offset; Monday after (first trading session)
|
|
// has the new one. These are the dates real bars land on.
|
|
for &(y, sm, sd, em, ed) in &TZDATA_TRANSITIONS {
|
|
let s = day(y, sm, sd);
|
|
let e = day(y, em, ed);
|
|
assert_eq!(et_session_close_utc_micros(s - 1), Ok(close_at(s - 1, 21)), "{y} spring eve");
|
|
assert_eq!(et_session_close_utc_micros(s + 1), Ok(close_at(s + 1, 20)), "{y} spring Mon");
|
|
assert_eq!(et_session_close_utc_micros(e - 1), Ok(close_at(e - 1, 20)), "{y} fall eve");
|
|
assert_eq!(et_session_close_utc_micros(e + 1), Ok(close_at(e + 1, 21)), "{y} fall Mon");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pre_1976_divergent_emergency_dst_years_and_post_2030_are_out_of_range() {
|
|
// 1974-01-15: New York was on emergency DST (20:00 UTC) while Toronto was not (21:00 UTC).
|
|
let probes = [
|
|
day(1974, 1, 15),
|
|
day(1975, 2, 24),
|
|
day(1975, 12, 31),
|
|
day(1970, 1, 1),
|
|
day(1900, 1, 1),
|
|
day(2031, 1, 1),
|
|
day(2100, 6, 1),
|
|
0,
|
|
-1,
|
|
i32::MIN,
|
|
i32::MIN + 1,
|
|
i32::MAX,
|
|
i32::MAX - 1,
|
|
];
|
|
for d in probes {
|
|
let err = et_session_close_utc_micros(d).unwrap_err();
|
|
let SessionCloseError::DateOutOfRange {
|
|
epoch_day,
|
|
min_supported,
|
|
max_supported,
|
|
} = err;
|
|
assert_eq!(epoch_day, d);
|
|
assert_eq!(min_supported, SESSION_CLOSE_MIN_SUPPORTED_DATE);
|
|
assert_eq!(max_supported, SESSION_CLOSE_MAX_SUPPORTED_DATE);
|
|
assert!(err.source().is_none());
|
|
// Usable as a boxed error.
|
|
let boxed: Box<dyn Error + Send + Sync> = Box::new(err);
|
|
assert!(boxed.to_string().contains(&d.to_string()));
|
|
}
|
|
// Boundaries themselves are in range (both winter, EST).
|
|
assert_eq!(
|
|
et_session_close_utc_micros(SESSION_CLOSE_MIN_SUPPORTED_DATE),
|
|
Ok(close_at(SESSION_CLOSE_MIN_SUPPORTED_DATE, 21))
|
|
);
|
|
assert_eq!(
|
|
et_session_close_utc_micros(SESSION_CLOSE_MAX_SUPPORTED_DATE),
|
|
Ok(close_at(SESSION_CLOSE_MAX_SUPPORTED_DATE, 21))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn close_is_strictly_increasing_and_never_before_1600_et() {
|
|
// Monotonic: a later session never gets an earlier known_at (no look-ahead via ordering).
|
|
// Never earlier than 16:00 EDT (20:00 UTC) and never later than 16:00 EST (21:00 UTC).
|
|
let mut prev = i64::MIN;
|
|
for d in SESSION_CLOSE_MIN_SUPPORTED_DATE..=SESSION_CLOSE_MAX_SUPPORTED_DATE {
|
|
let t = et_session_close_utc_micros(d).unwrap();
|
|
assert!(t > prev, "non-monotonic at epoch_day={d}");
|
|
assert!(t >= close_at(d, 20) && t <= close_at(d, 21));
|
|
prev = t;
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn curate_stage_drops_bars_on_either_side_of_1976_boundary_correctly() {
|
|
let mapping = open_mapping("GE.US", 7);
|
|
let curr: HashMap<i32, Cow<'static, str>> = [(7, Cow::Borrowed("USD"))].into_iter().collect();
|
|
// 1975-12-31 (Wed) dropped; 1976-01-02 (Fri, first 1976 session) kept at 21:00 UTC;
|
|
// 1976-04-26 (Mon after first 1976 spring-forward) kept at 20:00 UTC.
|
|
let input = vec![
|
|
bar("GE.US", day(1975, 12, 31), 10.0),
|
|
bar("GE.US", day(1976, 1, 2), 11.0),
|
|
bar("GE.US", day(1976, 4, 26), 12.0),
|
|
];
|
|
let out = curate_bars_1d(
|
|
input,
|
|
&SplitTable::<String>::default(),
|
|
std::iter::empty::<QmRawVolume<String>>(),
|
|
0.01,
|
|
&mapping,
|
|
"eod",
|
|
&curr,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(out.stats.rows_in, 3);
|
|
assert_eq!(out.stats.dropped_ts_out_of_range, 1);
|
|
assert_eq!(out.stats.rows_out, 2);
|
|
assert_eq!(
|
|
out.stats.rows_in,
|
|
out.stats.rows_out
|
|
+ out.stats.dropped_unmapped
|
|
+ out.stats.dropped_missing_currency
|
|
+ out.stats.dropped_ts_out_of_range
|
|
+ out.stats.listing_collision_collapsed
|
|
+ out.stats.listing_collision_dropped
|
|
);
|
|
let d0 = day(1976, 1, 2);
|
|
let d1 = day(1976, 4, 26);
|
|
assert_eq!(out.rows[0].session_date, d0);
|
|
assert_eq!(out.rows[0].ts_micros, close_at(d0, 21));
|
|
assert_eq!(out.rows[0].known_at_micros, close_at(d0, 21));
|
|
assert_eq!(out.rows[1].session_date, d1);
|
|
assert_eq!(out.rows[1].ts_micros, close_at(d1, 20));
|
|
assert_eq!(out.rows[1].known_at_micros, close_at(d1, 20));
|
|
}
|
|
|
|
#[test]
|
|
fn write_wrapper_publishes_pre_2008_year_partitions_with_footer_row_counts() {
|
|
let tmp = TempDir::new("pre2008-write");
|
|
let mapping = open_mapping("IBM.US", 42);
|
|
let curr: HashMap<i32, Cow<'static, str>> =
|
|
[(42, Cow::Borrowed("USD"))].into_iter().collect();
|
|
let input = vec![
|
|
bar("IBM.US", day(1975, 6, 16), 5.0), // dropped (out of range)
|
|
bar("IBM.US", day(1976, 3, 1), 6.0),
|
|
bar("IBM.US", day(1976, 7, 1), 7.0),
|
|
bar("IBM.US", day(1999, 1, 15), 8.0),
|
|
bar("IBM.US", day(2006, 6, 15), 9.0),
|
|
bar("IBM.US", day(2007, 12, 31), 10.0),
|
|
bar("IBM.US", day(2008, 1, 2), 11.0),
|
|
];
|
|
let report = curate_and_write_bars_1d(
|
|
&tmp.0,
|
|
input,
|
|
&SplitTable::<String>::default(),
|
|
std::iter::empty::<QmRawVolume<String>>(),
|
|
0.01,
|
|
&mapping,
|
|
"eod",
|
|
&curr,
|
|
Bars1dWriteOptions::default(),
|
|
)
|
|
.unwrap();
|
|
|
|
assert_eq!(report.curate_stats.dropped_ts_out_of_range, 1);
|
|
assert_eq!(report.write_stats.rows_written, 6);
|
|
let by_year: Vec<(i32, usize)> = report
|
|
.write_stats
|
|
.rows_by_year
|
|
.iter()
|
|
.map(|(&y, &n)| (y, n))
|
|
.collect();
|
|
assert_eq!(
|
|
by_year,
|
|
vec![(1976, 2), (1999, 1), (2006, 1), (2007, 1), (2008, 1)]
|
|
);
|
|
assert_eq!(report.write_stats.files_written, 5);
|
|
assert!(!tmp.0.join("bars_1d/year=1975").exists());
|
|
for (year, n) in by_year {
|
|
let path = tmp.0.join(format!("bars_1d/year={year:04}/part.parquet"));
|
|
let reader = SerializedFileReader::new(File::open(&path).expect("open")).expect("reader");
|
|
assert_eq!(
|
|
reader.metadata().file_metadata().num_rows(),
|
|
n as i64,
|
|
"footer row count for {}",
|
|
path.display()
|
|
);
|
|
}
|
|
}
|