stock-bot/tools/curate-etl/tests/events_reference.rs

281 lines
10 KiB
Rust

//! Independent reference / point-in-time QA tests for the `events` builder (T-77 / M1.2g).
//!
//! Verifies:
//! - Independent EST (+5h) / EDT (+4h) UTC math oracle across pre-2008, leap-day, and
//! year-end cross-midnight filings,
//! - Cross-midnight ET calendar-date linking (`2024-12-31T21:30:00 EST` links to `2024-12-31`
//! while partitioning into UTC `year=2025`),
//! - Future-date poison-row stability (appending filings and earnings on later dates never alters
//! emitted rows from earlier dates), plus an explicit fixture showing that a same-date evening
//! filing replaces the `16:05 ET` fallback (which is intended for T+1 open use, not same-day
//! intraday cutoffs).
use curate_etl::bench::{run_events_benchmark, DEFAULT_EVENTS_BENCH_ROWS};
use curate_etl::{
build_events, parse_filing_timefiled, partition_year_utc, ymd_to_epoch_days, QmEarning,
QmFiling, EVENT_KIND_EARNINGS, KNOWN_AT_SOURCE_FILING_TIMESTAMP, KNOWN_AT_SOURCE_STATUTORY_LAG,
};
const MICROS_PER_SEC: i64 = 1_000_000;
const MICROS_PER_HOUR: i64 = 3_600 * MICROS_PER_SEC;
const MICROS_PER_DAY: i64 = 24 * MICROS_PER_HOUR;
fn day(y: i32, m: u32, d: u32) -> i32 {
ymd_to_epoch_days(y, m, d).expect("valid fixture date")
}
fn oracle_utc_micros(
year: i32,
month: u32,
dom: u32,
hour: u32,
minute: u32,
second: u32,
offset_hours: i64,
) -> i64 {
let d = i64::from(day(year, month, dom));
d * MICROS_PER_DAY
+ (i64::from(hour) + offset_hours) * MICROS_PER_HOUR
+ i64::from(minute) * 60 * MICROS_PER_SEC
+ i64::from(second) * MICROS_PER_SEC
}
#[test]
fn est_and_edt_conversions_match_independent_oracle_across_decades() {
let cases = [
(1999, 12, 31, 23, 59, 59, "EST", 5),
(2000, 2, 29, 16, 30, 0, "EST", 5),
(2003, 7, 15, 14, 30, 0, "EDT", 4),
(2007, 11, 4, 0, 15, 10, "EDT", 4),
(2024, 12, 31, 21, 30, 0, "EST", 5),
(2025, 1, 31, 6, 1, 27, "EST", 5),
(2026, 3, 6, 17, 0, 0, "EST", 5),
(2026, 3, 9, 17, 0, 0, "EDT", 4),
];
for (y, m, d, hh, mm, ss, zone, offset) in cases {
let raw = format!("{y:04}-{m:02}-{d:02}T{hh:02}:{mm:02}:{ss:02} {zone}");
let parsed = parse_filing_timefiled(&raw).expect("valid oracle fixture");
assert_eq!(parsed.et_date, day(y, m, d), "et_date mismatch for {raw}");
assert_eq!(
parsed.utc_micros,
oracle_utc_micros(y, m, d, hh, mm, ss, offset),
"utc_micros mismatch for {raw}"
);
}
}
#[test]
fn year_end_late_et_filing_links_by_et_date_and_partitions_by_utc_year() {
let dec31 = day(2024, 12, 31);
let built = build_events(
vec![QmFiling {
key: 42_i32,
timefiled: "2024-12-31T21:30:00 EST".to_string(),
form_type: "News release".to_string(),
filing_id: Some("nye-nr".to_string()),
url: None,
}],
vec![QmEarning {
key: 42_i32,
report_date: dec31,
announce_time: "N/A".to_string(),
fiscal_period_end: Some(day(2024, 10, 31)),
actual_eps: Some(0.42),
revenue: None,
}],
);
assert_eq!(built.rows.len(), 2);
let expected_utc = oracle_utc_micros(2025, 1, 1, 2, 30, 0, 0);
assert_eq!(partition_year_utc(expected_utc), 2025);
for row in &built.rows {
assert_eq!(row.ts_micros, expected_utc);
assert_eq!(row.known_at_micros, expected_utc);
assert_eq!(row.known_at_source, KNOWN_AT_SOURCE_FILING_TIMESTAMP);
assert_eq!(row.partition_year(), 2025);
}
let earning = built
.rows
.iter()
.find(|r| r.kind == EVENT_KIND_EARNINGS)
.expect("earnings row present");
assert!(earning
.payload_json
.contains("\"linked_filing_id\":\"nye-nr\""));
}
#[test]
fn appending_future_date_poison_filings_and_earnings_never_alters_prior_dates() {
let cutoff_utc = oracle_utc_micros(2025, 3, 1, 0, 0, 0, 0);
let base_filings = vec![
QmFiling {
key: 1_i32,
timefiled: "2025-01-31T08:00:00 EST".to_string(),
form_type: "8-K".to_string(),
filing_id: Some("f-jan".to_string()),
url: None,
},
QmFiling {
key: 2_i32,
timefiled: "2025-02-10T16:30:00 EST".to_string(),
form_type: "10-K".to_string(),
filing_id: Some("f-feb".to_string()),
url: None,
},
];
let base_earnings = vec![
QmEarning {
key: 1_i32,
report_date: day(2025, 1, 31),
announce_time: "BTO".to_string(),
fiscal_period_end: Some(day(2024, 12, 31)),
actual_eps: Some(1.1),
revenue: Some(50_000.0),
},
QmEarning {
key: 2_i32,
report_date: day(2025, 2, 10),
announce_time: "AMC".to_string(),
fiscal_period_end: Some(day(2024, 12, 31)),
actual_eps: Some(2.2),
revenue: Some(80_000.0),
},
];
let before = build_events(base_filings.clone(), base_earnings.clone());
let mut poisoned_filings = base_filings;
poisoned_filings.push(QmFiling {
key: 1,
timefiled: "2025-05-01T07:00:00 EDT".to_string(),
form_type: "8-K".to_string(),
filing_id: Some("f-may-poison".to_string()),
url: None,
});
poisoned_filings.push(QmFiling {
key: 2,
timefiled: "2025-02-11T07:00:00 EST".to_string(),
form_type: "8-K".to_string(),
filing_id: Some("f-next-day".to_string()),
url: None,
});
poisoned_filings.push(QmFiling {
key: 1,
timefiled: "malformed".to_string(),
form_type: "8-K".to_string(),
filing_id: None,
url: None,
});
let mut poisoned_earnings = base_earnings;
poisoned_earnings.push(QmEarning {
key: 1,
report_date: day(2025, 5, 1),
announce_time: "N/A".to_string(),
fiscal_period_end: None,
actual_eps: Some(999.0),
revenue: None,
});
poisoned_earnings.push(QmEarning {
key: 2,
report_date: day(2025, 6, 1),
announce_time: "AMC".to_string(),
fiscal_period_end: None,
actual_eps: Some(1.0),
revenue: None,
});
poisoned_earnings.push(QmEarning {
key: 2,
report_date: day(2025, 6, 1),
announce_time: "AMC".to_string(),
fiscal_period_end: None,
actual_eps: Some(2.0),
revenue: None,
});
let after = build_events(poisoned_filings, poisoned_earnings);
let earlier: Vec<_> = after
.rows
.iter()
.filter(|r| r.ts_micros < cutoff_utc && r.known_at_micros < cutoff_utc)
.filter(|r| !(r.key == 2 && r.payload_json.contains("f-next-day")))
.cloned()
.collect();
assert_eq!(earlier, before.rows);
}
#[test]
fn same_date_late_filing_replaces_1605_fallback_across_intraday_cutoff() {
// The 16:05 ET fallback (`statutory_lag_estimate`) is intended for T+1 open use, not same-day
// intraday cutoffs: if a same-date qualifying filing arrives at 21:30 ET, link precedence
// moves the earnings timestamp from 16:05 ET (before a 17:00 ET intraday cutoff) to 21:30 ET
// (after that cutoff).
let report_date = day(2025, 2, 10);
let intraday_cutoff_utc = oracle_utc_micros(2025, 2, 10, 17, 0, 0, 5); // 17:00 EST = 22:00Z
let fallback_1605_utc = oracle_utc_micros(2025, 2, 10, 16, 5, 0, 5); // 16:05 EST = 21:05Z
let late_filing_utc = oracle_utc_micros(2025, 2, 10, 21, 30, 0, 5); // 21:30 EST = 02:30Z Feb 11
let earning = QmEarning {
key: 2_i32,
report_date,
announce_time: "AMC".to_string(),
fiscal_period_end: Some(day(2024, 12, 31)),
actual_eps: Some(2.2),
revenue: Some(80_000.0),
};
let without_filing = build_events(Vec::<QmFiling<i32>>::new(), vec![earning.clone()]);
assert_eq!(without_filing.rows.len(), 1);
let fallback_row = &without_filing.rows[0];
assert_eq!(fallback_row.known_at_micros, fallback_1605_utc);
assert_eq!(fallback_row.known_at_source, KNOWN_AT_SOURCE_STATUTORY_LAG);
assert!(fallback_row.known_at_micros < intraday_cutoff_utc);
let with_late_filing = build_events(
vec![QmFiling {
key: 2_i32,
timefiled: "2025-02-10T21:30:00 EST".to_string(),
form_type: "8-K".to_string(),
filing_id: Some("f-late-8k".to_string()),
url: None,
}],
vec![earning],
);
let linked_row = with_late_filing
.rows
.iter()
.find(|r| r.kind == EVENT_KIND_EARNINGS)
.expect("earnings row present");
assert_eq!(linked_row.known_at_micros, late_filing_utc);
assert_eq!(linked_row.known_at_source, KNOWN_AT_SOURCE_FILING_TIMESTAMP);
assert!(linked_row.known_at_micros > intraday_cutoff_utc);
}
#[test]
fn default_bench_events_matches_readme_recorded_output() {
// README records rows_out=491190, filings_out=484531, earnings_out=6659, keys_out=20000,
// checksum 0xbf74cd745a2646c6 and the stats line below for
// `cargo run --release -p curate-etl --offline -- bench-events`.
let report = run_events_benchmark(DEFAULT_EVENTS_BENCH_ROWS);
assert_eq!(DEFAULT_EVENTS_BENCH_ROWS, 500_000);
assert_eq!(report.filings_in, 492_500);
assert_eq!(report.earnings_in, 7_500);
assert_eq!(report.rows_out, 491_190);
assert_eq!(report.stats.filings_out, 484_531);
assert_eq!(report.stats.earnings_out, 6_659);
assert_eq!(report.keys_out, 20_000);
assert_eq!(report.stats.filings_skipped_malformed_timestamp, 1_525);
assert_eq!(report.stats.filings_skipped_unknown_zone, 1_496);
assert_eq!(report.stats.filings_skipped_invalid_date_time, 2_476);
assert_eq!(report.stats.filings_duplicate_collapsed, 2_472);
assert_eq!(report.stats.earnings_linked_to_filing, 2_569);
assert_eq!(report.stats.earnings_lagged_1605, 4_090);
assert_eq!(report.stats.earnings_skipped_out_of_range, 37);
assert_eq!(report.stats.earnings_duplicate_collapsed, 98);
assert_eq!(report.stats.earnings_duplicate_conflict_dropped, 706);
assert_eq!(report.checksum, 0xbf74_cd74_5a26_46c6);
}