426 lines
15 KiB
Rust
426 lines
15 KiB
Rust
//! Contract tests for the public `curate_etl` `fx_1d` builder API (T-75 / M1.2e).
|
|
//!
|
|
//! Uses only the crate's public surface, guarding the USD/CAD merge precedence, bad-row
|
|
//! resilience, deduplication isolation, read-only 1% overlap audit, permutation invariance,
|
|
//! accounting invariants, and no-look-ahead carry-forward lookup semantics.
|
|
|
|
use curate_etl::bench::{run_fx_benchmark, DEFAULT_FX_BENCH_LOOKUPS};
|
|
use curate_etl::{
|
|
build_fx_1d, fx_rate_on_or_before, ymd_to_epoch_days, Fx1dBuild, Fx1dRow, Fx1dStats,
|
|
FxDailyBar, FxSource, FX_OVERLAP_REL_TOLERANCE, FX_PAIR_USDCAD,
|
|
};
|
|
|
|
fn day(y: i32, m: u32, d: u32) -> i32 {
|
|
ymd_to_epoch_days(y, m, d).expect("valid fixture date")
|
|
}
|
|
|
|
fn bar(session_date: i32, open: f64, high: f64, low: f64, close: f64) -> FxDailyBar {
|
|
FxDailyBar {
|
|
session_date,
|
|
open,
|
|
high,
|
|
low,
|
|
close,
|
|
}
|
|
}
|
|
|
|
fn flat(session_date: i32, rate: f64) -> FxDailyBar {
|
|
bar(session_date, rate, rate, rate, rate)
|
|
}
|
|
|
|
struct Rng(u64);
|
|
|
|
impl Rng {
|
|
fn next(&mut self) -> u64 {
|
|
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
|
|
let mut z = self.0;
|
|
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
|
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
|
|
z ^ (z >> 31)
|
|
}
|
|
}
|
|
|
|
fn shuffle<T>(items: &mut [T], rng: &mut Rng) {
|
|
for i in (1..items.len()).rev() {
|
|
let j = (rng.next() % (i as u64 + 1)) as usize;
|
|
items.swap(i, j);
|
|
}
|
|
}
|
|
|
|
fn assert_accounting_and_order(build: &Fx1dBuild) {
|
|
let s = &build.stats;
|
|
assert_eq!(
|
|
s.eod.rows_in,
|
|
s.eod.rows_kept
|
|
+ s.eod.dropped_invalid_ohlc
|
|
+ s.eod.dropped_inconsistent_ohlc
|
|
+ s.eod.duplicate_collapsed
|
|
+ s.eod.duplicate_conflict_dropped,
|
|
"EOD accounting invariant failed: {s:?}"
|
|
);
|
|
assert_eq!(
|
|
s.te.rows_in,
|
|
s.te.rows_kept
|
|
+ s.te.dropped_invalid_ohlc
|
|
+ s.te.dropped_inconsistent_ohlc
|
|
+ s.te.duplicate_collapsed
|
|
+ s.te.duplicate_conflict_dropped,
|
|
"TE accounting invariant failed: {s:?}"
|
|
);
|
|
assert_eq!(
|
|
s.rows_out,
|
|
s.eod.rows_kept + s.te.rows_kept - s.te_superseded_by_eod,
|
|
"merge rows_out invariant failed: {s:?}"
|
|
);
|
|
assert_eq!(
|
|
s.rows_out,
|
|
s.eod_rows_out + s.te_rows_out,
|
|
"eod_rows_out + te_rows_out != rows_out: {s:?}"
|
|
);
|
|
assert_eq!(s.rows_out, build.rows.len());
|
|
assert_eq!(
|
|
s.overlap_within_tolerance + s.overlap_mismatched,
|
|
s.overlap_checked,
|
|
"overlap within + mismatched != checked: {s:?}"
|
|
);
|
|
assert_eq!(
|
|
s.overlap_checked, s.te_superseded_by_eod,
|
|
"overlap_checked != te_superseded_by_eod: {s:?}"
|
|
);
|
|
|
|
for pair in build.rows.windows(2) {
|
|
assert!(
|
|
pair[0].session_date < pair[1].session_date,
|
|
"rows must be strictly ascending and unique: {} >= {}",
|
|
pair[0].session_date,
|
|
pair[1].session_date
|
|
);
|
|
}
|
|
for row in &build.rows {
|
|
assert_eq!(row.pair, FX_PAIR_USDCAD);
|
|
assert_eq!(row.pair, "USDCAD");
|
|
}
|
|
}
|
|
|
|
fn assert_rows_bit_eq(a: &[Fx1dRow], b: &[Fx1dRow]) {
|
|
assert_eq!(a.len(), b.len());
|
|
for (x, y) in a.iter().zip(b) {
|
|
assert_eq!(x.pair, y.pair);
|
|
assert_eq!(x.session_date, y.session_date);
|
|
assert_eq!(x.open.to_bits(), y.open.to_bits());
|
|
assert_eq!(x.high.to_bits(), y.high.to_bits());
|
|
assert_eq!(x.low.to_bits(), y.low.to_bits());
|
|
assert_eq!(x.close.to_bits(), y.close.to_bits());
|
|
assert_eq!(x.source, y.source);
|
|
}
|
|
}
|
|
|
|
fn assert_stats_bit_eq(a: &Fx1dStats, b: &Fx1dStats) {
|
|
assert_eq!(a.eod, b.eod);
|
|
assert_eq!(a.te, b.te);
|
|
assert_eq!(a.rows_out, b.rows_out);
|
|
assert_eq!(a.eod_rows_out, b.eod_rows_out);
|
|
assert_eq!(a.te_rows_out, b.te_rows_out);
|
|
assert_eq!(a.te_superseded_by_eod, b.te_superseded_by_eod);
|
|
assert_eq!(a.overlap_checked, b.overlap_checked);
|
|
assert_eq!(a.overlap_within_tolerance, b.overlap_within_tolerance);
|
|
assert_eq!(a.overlap_mismatched, b.overlap_mismatched);
|
|
assert_eq!(a.max_overlap_rel.to_bits(), b.max_overlap_rel.to_bits());
|
|
}
|
|
|
|
#[test]
|
|
fn eod_precedence_te_gap_and_tail_fill_and_provenance_strings() {
|
|
assert_eq!(FX_PAIR_USDCAD, "USDCAD");
|
|
assert_eq!(FX_OVERLAP_REL_TOLERANCE, 0.01);
|
|
assert_eq!(FxSource::EodCadForex.as_str(), "eod_cad_forex");
|
|
assert_eq!(FxSource::TeUsdCad.as_str(), "te_usdcad");
|
|
|
|
let d1971 = day(1971, 1, 4);
|
|
let d_gap = day(2024, 11, 12);
|
|
let d_overlap = day(2025, 12, 31);
|
|
let d_tail1 = day(2026, 1, 2);
|
|
let d_tail2 = day(2026, 1, 5);
|
|
|
|
let eod = [
|
|
bar(d_overlap, 1.435, 1.445, 1.430, 1.440),
|
|
bar(d1971, 1.010, 1.015, 1.005, 1.012),
|
|
];
|
|
let te = [
|
|
bar(d_tail2, 1.382, 1.390, 1.378, 1.385),
|
|
bar(d_overlap, 1.436, 1.446, 1.431, 1.441),
|
|
bar(d_gap, 1.395, 1.402, 1.390, 1.398),
|
|
bar(d_tail1, 1.380, 1.388, 1.376, 1.384),
|
|
];
|
|
|
|
let built = build_fx_1d(&eod, &te);
|
|
assert_accounting_and_order(&built);
|
|
assert_eq!(built.rows.len(), 5);
|
|
assert_eq!(built.stats.eod_rows_out, 2);
|
|
assert_eq!(built.stats.te_rows_out, 3);
|
|
assert_eq!(built.stats.te_superseded_by_eod, 1);
|
|
|
|
let dates: Vec<i32> = built.rows.iter().map(|r| r.session_date).collect();
|
|
assert_eq!(dates, vec![d1971, d_gap, d_overlap, d_tail1, d_tail2]);
|
|
|
|
assert_eq!(built.rows[0].source.as_str(), "eod_cad_forex");
|
|
assert_eq!(built.rows[1].source.as_str(), "te_usdcad");
|
|
assert_eq!(built.rows[2].source.as_str(), "eod_cad_forex");
|
|
// Overlap date preserves EOD OHLC bit-for-bit.
|
|
assert_eq!(built.rows[2].open.to_bits(), eod[0].open.to_bits());
|
|
assert_eq!(built.rows[2].high.to_bits(), eod[0].high.to_bits());
|
|
assert_eq!(built.rows[2].low.to_bits(), eod[0].low.to_bits());
|
|
assert_eq!(built.rows[2].close.to_bits(), eod[0].close.to_bits());
|
|
assert_eq!(built.rows[3].source.as_str(), "te_usdcad");
|
|
assert_eq!(built.rows[4].source.as_str(), "te_usdcad");
|
|
}
|
|
|
|
#[test]
|
|
fn bad_row_priority_and_duplicate_conflict_isolation_across_sources() {
|
|
let base = day(2024, 1, 2);
|
|
let eod = [
|
|
// Invalid beats inconsistent when both apply
|
|
bar(base, f64::NAN, 1.0, 2.0, 1.5),
|
|
bar(base + 1, 0.0, 1.0, 2.0, 1.5),
|
|
bar(base + 2, -1.0, 1.5, 1.0, 1.2),
|
|
bar(base + 3, 1.2, f64::INFINITY, 1.1, 1.15),
|
|
bar(base + 4, 1.2, 1.3, f64::NEG_INFINITY, 1.25),
|
|
// Inconsistent OHLC
|
|
bar(base + 5, 1.2, 1.1, 1.3, 1.2),
|
|
bar(base + 6, 1.0, 1.3, 1.1, 1.2),
|
|
bar(base + 7, 1.4, 1.3, 1.1, 1.2),
|
|
bar(base + 8, 1.2, 1.3, 1.1, 1.0),
|
|
bar(base + 9, 1.2, 1.3, 1.1, 1.4),
|
|
// Identical duplicate collapses
|
|
flat(base + 10, 1.35),
|
|
flat(base + 10, 1.35),
|
|
// Conflicting duplicate group drops entirely in EOD; TE fills base+11
|
|
flat(base + 11, 1.36),
|
|
flat(base + 11, 1.37),
|
|
flat(base + 11, 1.36),
|
|
];
|
|
let te = [
|
|
flat(base + 11, 1.365),
|
|
// Conflicting TE group on base+10 drops cleanly without affecting EOD row
|
|
flat(base + 10, 1.35),
|
|
flat(base + 10, 1.40),
|
|
// Invalid TE row on base+12 + valid TE row on base+12 -> valid survives
|
|
bar(base + 12, -0.0, 1.4, 1.3, 1.35),
|
|
flat(base + 12, 1.355),
|
|
];
|
|
|
|
let built = build_fx_1d(&eod, &te);
|
|
assert_accounting_and_order(&built);
|
|
assert_eq!(built.stats.eod.dropped_invalid_ohlc, 5);
|
|
assert_eq!(built.stats.eod.dropped_inconsistent_ohlc, 5);
|
|
assert_eq!(built.stats.eod.duplicate_collapsed, 1);
|
|
assert_eq!(built.stats.eod.duplicate_conflict_dropped, 3);
|
|
assert_eq!(built.stats.eod.rows_kept, 1);
|
|
|
|
assert_eq!(built.stats.te.dropped_invalid_ohlc, 1);
|
|
assert_eq!(built.stats.te.dropped_inconsistent_ohlc, 0);
|
|
assert_eq!(built.stats.te.duplicate_collapsed, 0);
|
|
assert_eq!(built.stats.te.duplicate_conflict_dropped, 2);
|
|
assert_eq!(built.stats.te.rows_kept, 2);
|
|
|
|
assert_eq!(built.rows.len(), 3);
|
|
assert_eq!(built.rows[0].session_date, base + 10);
|
|
assert_eq!(built.rows[0].source, FxSource::EodCadForex);
|
|
assert_eq!(built.rows[1].session_date, base + 11);
|
|
assert_eq!(built.rows[1].source, FxSource::TeUsdCad);
|
|
assert_eq!(built.rows[2].session_date, base + 12);
|
|
assert_eq!(built.rows[2].source, FxSource::TeUsdCad);
|
|
}
|
|
|
|
#[test]
|
|
fn overlap_one_percent_boundary_is_inclusive_and_strictly_read_only() {
|
|
let base = day(2025, 3, 1);
|
|
let eod = [
|
|
bar(base, 99.5, 101.0, 99.0, 100.0), // te = 101.0 -> exact +1% (within)
|
|
bar(base + 1, 99.5, 101.0, 99.0, 100.0), // te = 99.0 -> exact -1% (within)
|
|
bar(base + 2, 99.5, 101.0, 99.0, 100.0), // te = 101.01 -> +1.01% (mismatch)
|
|
bar(base + 3, 99.5, 101.0, 99.0, 100.0), // te = 98.99 -> -1.01% (mismatch)
|
|
bar(base + 4, 99.5, 101.0, 99.0, 100.0), // te = 200.0 -> +100% (mismatch)
|
|
];
|
|
let te = [
|
|
flat(base, 101.0),
|
|
flat(base + 1, 99.0),
|
|
flat(base + 2, 101.01),
|
|
flat(base + 3, 98.99),
|
|
flat(base + 4, 200.0),
|
|
];
|
|
|
|
let built = build_fx_1d(&eod, &te);
|
|
assert_accounting_and_order(&built);
|
|
assert_eq!(built.stats.overlap_checked, 5);
|
|
assert_eq!(built.stats.overlap_within_tolerance, 2);
|
|
assert_eq!(built.stats.overlap_mismatched, 3);
|
|
assert_eq!(built.stats.max_overlap_rel, 1.0);
|
|
|
|
// Output rows must be bit-identical to EOD inputs.
|
|
for (row, orig) in built.rows.iter().zip(&eod) {
|
|
assert_eq!(row.source, FxSource::EodCadForex);
|
|
assert_eq!(row.open.to_bits(), orig.open.to_bits());
|
|
assert_eq!(row.high.to_bits(), orig.high.to_bits());
|
|
assert_eq!(row.low.to_bits(), orig.low.to_bits());
|
|
assert_eq!(row.close.to_bits(), orig.close.to_bits());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn overlap_decimal_usdcad_quotes_at_one_percent_boundary_avoid_f64_false_mismatches() {
|
|
let base = day(2025, 5, 1);
|
|
let eod = [
|
|
flat(base, 1.4000), // te = 1.4140 -> exact +1% (within despite f64 rounding)
|
|
flat(base + 1, 1.4000), // te = 1.3860 -> exact -1% (within)
|
|
flat(base + 2, 1.4000), // te = 1.4141 -> just above +1% (mismatch)
|
|
flat(base + 3, 1.4000), // te = 1.3859 -> just below -1% (mismatch)
|
|
];
|
|
let te = [
|
|
flat(base, 1.4140),
|
|
flat(base + 1, 1.3860),
|
|
flat(base + 2, 1.4141),
|
|
flat(base + 3, 1.3859),
|
|
];
|
|
|
|
let built = build_fx_1d(&eod, &te);
|
|
assert_accounting_and_order(&built);
|
|
assert_eq!(built.stats.overlap_checked, 4);
|
|
assert_eq!(built.stats.overlap_within_tolerance, 2);
|
|
assert_eq!(built.stats.overlap_mismatched, 2);
|
|
assert_eq!(
|
|
built.stats.max_overlap_rel.to_bits(),
|
|
((1.4000_f64 - 1.4141_f64).abs() / 1.4000_f64).to_bits()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn permutation_invariance_holds_across_multiple_fixed_seed_shuffles() {
|
|
let base = day(2020, 1, 2);
|
|
let mut eod = Vec::new();
|
|
let mut te = Vec::new();
|
|
|
|
for i in 0..80_i32 {
|
|
let d = base + i;
|
|
let close = 1.30 + (i % 15) as f64 * 0.005;
|
|
let b = bar(d, close - 0.002, close + 0.004, close - 0.004, close);
|
|
if i % 7 != 0 {
|
|
eod.push(b);
|
|
if i % 11 == 0 {
|
|
eod.push(b); // identical dup
|
|
}
|
|
if i % 19 == 0 {
|
|
let mut c = b;
|
|
c.close += 0.001;
|
|
eod.push(c); // conflicting dup
|
|
}
|
|
}
|
|
if i >= 40 {
|
|
let factor = if i % 9 == 0 { 1.03 } else { 1.002 };
|
|
let tb = bar(
|
|
d,
|
|
b.open * factor,
|
|
b.high * factor,
|
|
b.low * factor,
|
|
b.close * factor,
|
|
);
|
|
te.push(tb);
|
|
if i % 13 == 0 {
|
|
te.push(tb); // identical dup
|
|
}
|
|
if i % 23 == 0 {
|
|
let mut c = tb;
|
|
c.close += 0.001;
|
|
te.push(c); // conflicting dup
|
|
}
|
|
}
|
|
}
|
|
// Inject invalid and inconsistent rows into both sources.
|
|
eod.push(bar(base + 200, f64::NAN, 1.3, 1.2, 1.25));
|
|
eod.push(bar(base + 201, 1.25, 1.20, 1.30, 1.25));
|
|
te.push(bar(base + 202, -1.0, 1.3, 1.2, 1.25));
|
|
te.push(bar(base + 203, 1.50, 1.35, 1.25, 1.30));
|
|
|
|
let baseline = build_fx_1d(&eod, &te);
|
|
assert_accounting_and_order(&baseline);
|
|
assert!(baseline.stats.overlap_checked > 0);
|
|
assert!(baseline.stats.overlap_within_tolerance > 0);
|
|
assert!(baseline.stats.overlap_mismatched > 0);
|
|
|
|
let mut rng = Rng(0x570C_4B07_F81D_9999);
|
|
for _ in 0..16 {
|
|
let mut eod_perm = eod.clone();
|
|
let mut te_perm = te.clone();
|
|
shuffle(&mut eod_perm, &mut rng);
|
|
shuffle(&mut te_perm, &mut rng);
|
|
|
|
let trial = build_fx_1d(&eod_perm, &te_perm);
|
|
assert_accounting_and_order(&trial);
|
|
assert_rows_bit_eq(&baseline.rows, &trial.rows);
|
|
assert_stats_bit_eq(&baseline.stats, &trial.stats);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn fx_rate_on_or_before_enforces_no_lookahead_and_staleness_bounds() {
|
|
let fri = day(2025, 2, 14);
|
|
let sat = day(2025, 2, 15);
|
|
let sun = day(2025, 2, 16);
|
|
let holiday_mon = day(2025, 2, 17); // Family Day / Presidents' Day
|
|
let tue = day(2025, 2, 18);
|
|
let later = day(2025, 3, 1);
|
|
|
|
let built = build_fx_1d(
|
|
&[flat(fri, 1.420), flat(tue, 1.425)],
|
|
&[flat(later, 1.440)],
|
|
);
|
|
let rows = &built.rows;
|
|
|
|
// Empty slice always None
|
|
assert_eq!(fx_rate_on_or_before(&[], fri, 10), None);
|
|
// Date before first row always None (never looks ahead)
|
|
assert_eq!(fx_rate_on_or_before(rows, fri - 1, u32::MAX), None);
|
|
// Exact hits with max_stale_days == 0
|
|
assert_eq!(fx_rate_on_or_before(rows, fri, 0).map(|r| r.close), Some(1.420));
|
|
assert_eq!(fx_rate_on_or_before(rows, tue, 0).map(|r| r.close), Some(1.425));
|
|
assert_eq!(fx_rate_on_or_before(rows, later, 0).map(|r| r.close), Some(1.440));
|
|
|
|
// Long weekend carry-forward (Fri -> Sat, Sun, Mon)
|
|
assert_eq!(fx_rate_on_or_before(rows, sat, 3).map(|r| r.session_date), Some(fri));
|
|
assert_eq!(fx_rate_on_or_before(rows, sun, 3).map(|r| r.session_date), Some(fri));
|
|
assert_eq!(fx_rate_on_or_before(rows, holiday_mon, 3).map(|r| r.session_date), Some(fri));
|
|
// Staleness limit exceeded on holiday_mon (age = 3 > 2) -> None, never looks ahead to Tue
|
|
assert_eq!(fx_rate_on_or_before(rows, holiday_mon, 2), None);
|
|
|
|
// Exhaustive no-look-ahead sweep across the full range
|
|
for q in (fri - 5)..=(later + 5) {
|
|
if let Some(found) = fx_rate_on_or_before(rows, q, 4) {
|
|
assert!(found.session_date <= q, "look-ahead violation: {} > {}", found.session_date, q);
|
|
assert!(i64::from(q) - i64::from(found.session_date) <= 4);
|
|
}
|
|
}
|
|
|
|
// Extreme i32 dates do not overflow
|
|
let extreme = build_fx_1d(&[flat(i32::MIN, 1.0), flat(0, 1.1)], &[]);
|
|
assert_eq!(
|
|
fx_rate_on_or_before(&extreme.rows, i32::MIN, 0).map(|r| r.session_date),
|
|
Some(i32::MIN)
|
|
);
|
|
assert_eq!(fx_rate_on_or_before(&extreme.rows, i32::MAX, 5), None);
|
|
assert_eq!(
|
|
fx_rate_on_or_before(&extreme.rows, i32::MAX, u32::MAX).map(|r| r.session_date),
|
|
Some(0)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn run_fx_benchmark_is_deterministic_across_runs() {
|
|
let a = run_fx_benchmark(1_000);
|
|
let b = run_fx_benchmark(1_000);
|
|
assert_eq!(a.rows, 1_000);
|
|
assert_eq!(a.lookups, DEFAULT_FX_BENCH_LOOKUPS);
|
|
assert_eq!(a.rows_out, b.rows_out);
|
|
assert_eq!(a.lookup_hits, b.lookup_hits);
|
|
assert_eq!(a.stats, b.stats);
|
|
assert_eq!(a.checksum, b.checksum);
|
|
assert_ne!(a.checksum, 0);
|
|
}
|