T-212: tests
This commit is contained in:
parent
4e8173cc43
commit
374d506f32
2 changed files with 349 additions and 0 deletions
|
|
@ -1538,3 +1538,207 @@ fn undated_legacy_only_late_filing_bounds_availability_without_claiming_filing_b
|
|||
assert_eq!(ttm.flows.revenue, Some(420.0));
|
||||
assert!(ttm.has_estimated_lag);
|
||||
}
|
||||
|
||||
/// Deterministic Fisher-Yates shuffle driven by a fixed-seed 64-bit LCG (no external crates).
|
||||
fn seeded_shuffle<T>(items: &mut [T], seed: u64) {
|
||||
let mut state = seed;
|
||||
for i in (1..items.len()).rev() {
|
||||
state = state
|
||||
.wrapping_mul(6_364_136_223_846_793_005)
|
||||
.wrapping_add(1_442_695_040_888_963_407);
|
||||
let j = ((state >> 33) % (i as u64 + 1)) as usize;
|
||||
items.swap(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undated_legacy_only_with_in_window_amendment_is_not_visible_until_the_amendment_timestamp() {
|
||||
let venues = venues_map(&[("ACME", StatutoryLagVenue::UsTsx)]);
|
||||
let q1_24 = day(2024, 3, 31);
|
||||
// Original 10-Q on May 5 and a 10-Q/A on June 10 (inside the 75-day window ending June 14).
|
||||
// The undated legacy values cannot be tied to either release, so availability must be
|
||||
// bounded by the later of the two (June 10 16:30 EDT = 20:30 UTC), not the May 15 deadline.
|
||||
let filings = vec![
|
||||
filing("ACME", "2024-05-05T16:30:00 EDT", "10-Q"),
|
||||
filing("ACME", "2024-06-10T16:30:00 EDT", "10-Q/A"),
|
||||
];
|
||||
let legacy = vec![obs(
|
||||
"ACME",
|
||||
q1_24,
|
||||
FiscalPeriod::Quarterly,
|
||||
"USD",
|
||||
None,
|
||||
fields_rev(120.0),
|
||||
)];
|
||||
|
||||
let built = build_fundamentals_rows_with_legacy(Vec::new(), legacy, filings, &venues);
|
||||
assert_reconciliation(&built);
|
||||
assert_eq!(built.stats.rows_out, 1);
|
||||
assert_eq!(built.stats.rows_linked_to_filing, 0);
|
||||
assert_eq!(built.stats.rows_statutory_lag_estimate, 1);
|
||||
let amendment_ts = utc_micros(2024, 6, 10, 20, 30, 0);
|
||||
assert_eq!(built.rows[0].known_at_utc_micros, amendment_ts);
|
||||
assert_eq!(built.rows[0].known_at_source, KNOWN_AT_SOURCE_STATUTORY_LAG);
|
||||
|
||||
// Inclusive cutoff boundary: one microsecond earlier sees nothing; the amendment instant sees Q1.
|
||||
let snaps = build_fundamentals_snapshots(&built.rows, 4).unwrap();
|
||||
assert!(as_of(&snaps, "ACME", amendment_ts - 1).is_none());
|
||||
assert!(as_of(&snaps, "ACME", utc_micros(2024, 5, 20, 16, 0, 0)).is_none());
|
||||
let at = as_of(&snaps, "ACME", amendment_ts).expect("visible at amendment instant");
|
||||
assert_eq!(at.periods[0].fields.revenue, Some(120.0));
|
||||
assert!(at.has_estimated_lag);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_merge_intra_legacy_conflicts_drop_identical_legacy_duplicates_collapse() {
|
||||
let venues = venues_map(&[("ACME", StatutoryLagVenue::UsTsx)]);
|
||||
let q1 = day(2024, 3, 31);
|
||||
let q2 = day(2024, 6, 30);
|
||||
let filings = vec![
|
||||
filing("ACME", "2024-05-02T16:30:00 EDT", "10-Q"),
|
||||
filing("ACME", "2024-08-02T16:30:00 EDT", "10-Q"),
|
||||
];
|
||||
|
||||
// Q1: two dated legacy observations disagree with no primary to arbitrate -> both dropped.
|
||||
// Q2: two bit-identical dated legacy observations -> collapse to one filing-backed row.
|
||||
let legacy = vec![
|
||||
obs("ACME", q1, FiscalPeriod::Quarterly, "USD", Some(day(2024, 5, 2)), fields_rev(100.0)),
|
||||
obs("ACME", q1, FiscalPeriod::Quarterly, "USD", Some(day(2024, 5, 2)), fields_rev(101.0)),
|
||||
obs("ACME", q2, FiscalPeriod::Quarterly, "USD", Some(day(2024, 8, 2)), fields_rev(130.0)),
|
||||
obs("ACME", q2, FiscalPeriod::Quarterly, "USD", Some(day(2024, 8, 2)), fields_rev(130.0)),
|
||||
];
|
||||
|
||||
let built = build_fundamentals_rows_with_legacy(Vec::new(), legacy, filings, &venues);
|
||||
assert_reconciliation(&built);
|
||||
assert_eq!(built.stats.observations_in, 4);
|
||||
assert_eq!(built.stats.duplicate_conflict_dropped, 2);
|
||||
assert_eq!(built.stats.duplicate_collapsed, 1);
|
||||
assert_eq!(built.stats.rows_out, 1);
|
||||
assert_eq!(built.rows[0].period_end, q2);
|
||||
assert_eq!(built.rows[0].fields.revenue, Some(130.0));
|
||||
assert_eq!(built.rows[0].known_at_source, KNOWN_AT_SOURCE_FILING_TIMESTAMP);
|
||||
assert_eq!(built.rows[0].known_at_utc_micros, utc_micros(2024, 8, 2, 20, 30, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_merge_dated_legacy_withholds_undated_legacy_of_same_kind_but_not_other_fiscal_kind() {
|
||||
let venues = venues_map(&[("ACME", StatutoryLagVenue::UsTsx)]);
|
||||
let fy_end = day(2023, 12, 31);
|
||||
// Only a quarterly 10-Q for the Dec 31 period-end exists; no 10-K.
|
||||
let filings = vec![filing("ACME", "2024-02-20T16:30:00 EST", "10-Q")];
|
||||
|
||||
let legacy = vec![
|
||||
// Dated Q4 anchors (ACME, 2023-12-31, Q).
|
||||
obs("ACME", fy_end, FiscalPeriod::Quarterly, "USD", Some(day(2024, 2, 20)), fields_rev(110.0)),
|
||||
// Undated Q4 for the same kind must be withheld (unverified overlapping vintage).
|
||||
obs("ACME", fy_end, FiscalPeriod::Quarterly, "USD", None, fields_rev(111.0)),
|
||||
// Undated FY for the same period_end is a different fiscal kind and must survive at the
|
||||
// US/TSX +90d statutory deadline (2024-03-30 16:05 EDT = 20:05 UTC).
|
||||
obs("ACME", fy_end, FiscalPeriod::Annual, "USD", None, fields_rev(440.0)),
|
||||
];
|
||||
|
||||
let built = build_fundamentals_rows_with_legacy(Vec::new(), legacy, filings, &venues);
|
||||
assert_reconciliation(&built);
|
||||
assert_eq!(built.stats.observations_in, 3);
|
||||
assert_eq!(built.stats.observations_dropped_unanchored_vintage, 1);
|
||||
assert_eq!(built.stats.rows_out, 2);
|
||||
assert_eq!(built.stats.rows_linked_to_filing, 1);
|
||||
assert_eq!(built.stats.rows_statutory_lag_estimate, 1);
|
||||
|
||||
let q_row = built
|
||||
.rows
|
||||
.iter()
|
||||
.find(|r| r.fiscal_period == FiscalPeriod::Quarterly)
|
||||
.expect("dated Q4 row");
|
||||
assert_eq!(q_row.fields.revenue, Some(110.0));
|
||||
assert_eq!(q_row.known_at_source, KNOWN_AT_SOURCE_FILING_TIMESTAMP);
|
||||
|
||||
let fy_row = built
|
||||
.rows
|
||||
.iter()
|
||||
.find(|r| r.fiscal_period == FiscalPeriod::Annual)
|
||||
.expect("undated FY row");
|
||||
assert_eq!(fy_row.fields.revenue, Some(440.0));
|
||||
assert_eq!(fy_row.known_at_source, KNOWN_AT_SOURCE_STATUTORY_LAG);
|
||||
assert_eq!(fy_row.known_at_utc_micros, utc_micros(2024, 3, 30, 20, 5, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_merge_output_is_identical_under_seeded_permutations_of_primary_legacy_and_filings() {
|
||||
let venues = venues_map(&[
|
||||
("ACME", StatutoryLagVenue::UsTsx),
|
||||
("VENT", StatutoryLagVenue::TsxvCse),
|
||||
]);
|
||||
let q3 = day(2023, 9, 30);
|
||||
let q4 = day(2023, 12, 31);
|
||||
let q1 = day(2024, 3, 31);
|
||||
let q2 = day(2024, 6, 30);
|
||||
|
||||
let filings = vec![
|
||||
filing("ACME", "2023-11-05T16:30:00 EST", "10-Q"),
|
||||
filing("ACME", "2024-02-20T16:30:00 EST", "10-Q"),
|
||||
filing("ACME", "2024-05-05T16:30:00 EDT", "10-Q"),
|
||||
filing("ACME", "2024-08-05T16:30:00 EDT", "10-Q"),
|
||||
filing("ACME", "2024-09-10T16:30:00 EDT", "10-Q/A"),
|
||||
filing("VENT", "2024-05-25T16:30:00 EDT", "10-Q"),
|
||||
filing("UNKNOWN", "2024-05-25T16:30:00 EDT", "10-Q"),
|
||||
];
|
||||
let primary = vec![
|
||||
obs("ACME", q2, FiscalPeriod::Quarterly, "USD", Some(day(2024, 8, 5)), fields_rev(130.0)),
|
||||
obs("ACME", q1, FiscalPeriod::Quarterly, "USD", Some(day(2024, 9, 10)), fields_rev(125.0)),
|
||||
// Collides with the legacy original Q1 at the same known_at: primary must win.
|
||||
obs("ACME", q1, FiscalPeriod::Quarterly, "USD", Some(day(2024, 5, 5)), fields_rev(121.0)),
|
||||
obs("UNKNOWN", q1, FiscalPeriod::Quarterly, "USD", None, fields_rev(1.0)),
|
||||
];
|
||||
let legacy = vec![
|
||||
obs("ACME", q3, FiscalPeriod::Quarterly, "USD", Some(day(2023, 11, 5)), fields_rev(100.0)),
|
||||
obs("ACME", q4, FiscalPeriod::Quarterly, "USD", Some(day(2024, 2, 20)), fields_rev(110.0)),
|
||||
obs("ACME", q1, FiscalPeriod::Quarterly, "USD", Some(day(2024, 5, 5)), fields_rev(120.0)),
|
||||
// Undated overlapping vintage: withheld.
|
||||
obs("ACME", q1, FiscalPeriod::Quarterly, "USD", None, fields_rev(126.0)),
|
||||
// Undated legacy-only TSXV quarter bounded by its May 25 filing.
|
||||
obs("VENT", q1, FiscalPeriod::Quarterly, "CAD", None, fields_rev(7.0)),
|
||||
obs("VENT", q1, FiscalPeriod::Quarterly, "CAD", None, fields_rev(7.0)),
|
||||
];
|
||||
|
||||
let reference =
|
||||
build_fundamentals_rows_with_legacy(primary.clone(), legacy.clone(), filings.clone(), &venues);
|
||||
assert_reconciliation(&reference);
|
||||
assert_eq!(reference.stats.observations_in, 10);
|
||||
assert_eq!(reference.stats.observations_dropped_unknown_key, 1);
|
||||
assert_eq!(reference.stats.observations_dropped_unanchored_vintage, 1);
|
||||
assert_eq!(reference.stats.duplicate_collapsed, 2);
|
||||
assert_eq!(reference.stats.duplicate_conflict_dropped, 0);
|
||||
assert_eq!(reference.stats.rows_out, 6);
|
||||
let acme_q1_may = reference
|
||||
.rows
|
||||
.iter()
|
||||
.find(|r| r.key == "ACME" && r.period_end == q1 && r.known_at_utc_micros == utc_micros(2024, 5, 5, 20, 30, 0))
|
||||
.expect("ACME original Q1 vintage");
|
||||
assert_eq!(acme_q1_may.fields.revenue, Some(121.0), "primary wins same-known_at collision");
|
||||
let vent = reference.rows.iter().find(|r| r.key == "VENT").expect("VENT row");
|
||||
assert_eq!(vent.known_at_utc_micros, utc_micros(2024, 5, 30, 20, 5, 0).max(utc_micros(2024, 5, 25, 20, 30, 0)));
|
||||
assert_eq!(vent.known_at_source, KNOWN_AT_SOURCE_STATUTORY_LAG);
|
||||
|
||||
for seed in 1..=16u64 {
|
||||
let mut p = primary.clone();
|
||||
let mut l = legacy.clone();
|
||||
let mut f = filings.clone();
|
||||
seeded_shuffle(&mut p, seed);
|
||||
seeded_shuffle(&mut l, seed.wrapping_mul(31));
|
||||
seeded_shuffle(&mut f, seed.wrapping_mul(97));
|
||||
let built = build_fundamentals_rows_with_legacy(p, l, f, &venues);
|
||||
assert_eq!(built, reference, "seed {seed} changed merged output");
|
||||
}
|
||||
|
||||
// Empty legacy input must reproduce the primary-only builder exactly.
|
||||
assert_eq!(
|
||||
build_fundamentals_rows_with_legacy(
|
||||
primary.clone(),
|
||||
std::iter::empty::<FinancialObservation<String>>(),
|
||||
filings.clone(),
|
||||
&venues,
|
||||
),
|
||||
build_fundamentals_rows(primary, filings, &venues),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2213,3 +2213,148 @@ fn external_sort_bounded_memory_on_representative_multi_symbol_backfill() {
|
|||
}
|
||||
assert_eq!(list_dir_names(&out_bounded).len(), 2);
|
||||
}
|
||||
|
||||
/// Deterministic Fisher-Yates shuffle driven by a fixed-seed 64-bit LCG (no external crates).
|
||||
fn seeded_shuffle<T>(items: &mut [T], seed: u64) {
|
||||
let mut state = seed;
|
||||
for i in (1..items.len()).rev() {
|
||||
state = state
|
||||
.wrapping_mul(6_364_136_223_846_793_005)
|
||||
.wrapping_add(1_442_695_040_888_963_407);
|
||||
let j = ((state >> 33) % (i as u64 + 1)) as usize;
|
||||
items.swap(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_sort_duplicate_heavy_groups_spanning_many_runs_are_order_and_run_size_invariant() {
|
||||
let dir = TempDirGuard::new("bars1m-ext-sort-dup-heavy");
|
||||
let mapping_path = write_mapping_tsv(
|
||||
dir.path(),
|
||||
&[
|
||||
"eod\tA1.US\t2020-01-01\t\t10",
|
||||
"eod\tA2.US\t2020-01-01\t\t10",
|
||||
"eod\tA3.US\t2020-01-01\t\t10",
|
||||
"eod\tB.US\t2020-01-01\t\t20",
|
||||
],
|
||||
);
|
||||
let splits_bytes: Vec<u8> = Vec::new();
|
||||
|
||||
let t1 = et_ts_sec(2024, 6, 7, 9, 30);
|
||||
let t2 = et_ts_sec(2024, 6, 7, 9, 31);
|
||||
let t3 = et_ts_sec(2024, 6, 7, 9, 32);
|
||||
|
||||
let mut docs = Vec::new();
|
||||
// t1: 6x A1 + 5x A2 + 4x A3, all bit-identical -> 12 same-key collapses, 2 listing collapses.
|
||||
for (code, copies) in [("A1.US", 6), ("A2.US", 5), ("A3.US", 4)] {
|
||||
for _ in 0..copies {
|
||||
docs.push(make_intraday_doc(code, t1, 50.0, 51.0, 49.0, 50.5, 200.0));
|
||||
}
|
||||
}
|
||||
// t2: 6x identical A1 plus one conflicting A1 -> all 7 dropped; lone A2 survives.
|
||||
for _ in 0..6 {
|
||||
docs.push(make_intraday_doc("A1.US", t2, 51.0, 52.0, 50.0, 51.5, 100.0));
|
||||
}
|
||||
docs.push(make_intraday_doc("A1.US", t2, 51.0, 52.0, 50.0, 51.6, 100.0));
|
||||
docs.push(make_intraday_doc("A2.US", t2, 51.0, 52.0, 50.0, 51.5, 100.0));
|
||||
// t3: cross-source conflict on listing 10 -> both dropped.
|
||||
docs.push(make_intraday_doc("A1.US", t3, 52.0, 53.0, 51.0, 52.5, 90.0));
|
||||
docs.push(make_intraday_doc("A2.US", t3, 52.0, 53.0, 51.0, 52.4, 90.0));
|
||||
// Clean B.US bars on listing 20.
|
||||
for ts in [t1, t2, t3] {
|
||||
docs.push(make_intraday_doc("B.US", ts, 20.0, 21.0, 19.0, 20.5, 50.0));
|
||||
}
|
||||
|
||||
let out_ref = dir.path().join("out-ref");
|
||||
let (ref_report, ref_metrics) = run_curate_bars_1m_from_readers_to_dir_with_sort_config(
|
||||
&make_args(
|
||||
mapping_path.clone(),
|
||||
dir.path().join("qmSplits.bson"),
|
||||
dir.path().join("eodIntraday1M.bson"),
|
||||
out_ref.clone(),
|
||||
2,
|
||||
1_750_000_000_000_000,
|
||||
BsonStreamOptions::default(),
|
||||
),
|
||||
Cursor::new(concat_docs(&docs)),
|
||||
Cursor::new(&splits_bytes),
|
||||
Bars1mSortConfig {
|
||||
spill_dir: Some(out_ref.clone()),
|
||||
spill_prefix: TABLE_BARS_1M,
|
||||
max_run_rows: 10_000,
|
||||
max_merge_fan_in: 64,
|
||||
},
|
||||
)
|
||||
.expect("in-memory reference run");
|
||||
assert_report_conservation(&ref_report);
|
||||
assert_eq!(ref_metrics.runs_spilled, 0);
|
||||
assert_eq!(ref_report.rows_written, 5);
|
||||
assert_eq!(ref_report.curate_stats.builder.duplicate_collapsed, 12);
|
||||
assert_eq!(ref_report.curate_stats.builder.duplicate_conflict_dropped, 7);
|
||||
assert_eq!(ref_report.curate_stats.listing_collision_collapsed, 2);
|
||||
assert_eq!(ref_report.curate_stats.listing_collision_dropped, 2);
|
||||
|
||||
let expected_names = vec![
|
||||
"bars_1m.shard-00.ilp".to_string(),
|
||||
"bars_1m.shard-01.ilp".to_string(),
|
||||
];
|
||||
assert_eq!(list_dir_names(&out_ref), expected_names);
|
||||
let ref_shards: Vec<Vec<u8>> = expected_names
|
||||
.iter()
|
||||
.map(|name| fs::read(out_ref.join(name)).unwrap())
|
||||
.collect();
|
||||
|
||||
let mut case = 0usize;
|
||||
for seed in 1..=4u64 {
|
||||
let mut permuted = docs.clone();
|
||||
seeded_shuffle(&mut permuted, seed);
|
||||
let permuted_bytes = concat_docs(&permuted);
|
||||
for max_run_rows in [1usize, 2, 3, 5, 7] {
|
||||
for max_merge_fan_in in [2usize, 3] {
|
||||
case += 1;
|
||||
let out = dir.path().join(format!("out-{case}"));
|
||||
let (report, metrics) = run_curate_bars_1m_from_readers_to_dir_with_sort_config(
|
||||
&make_args(
|
||||
mapping_path.clone(),
|
||||
dir.path().join("qmSplits.bson"),
|
||||
dir.path().join("eodIntraday1M.bson"),
|
||||
out.clone(),
|
||||
2,
|
||||
1_750_000_000_000_000,
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 256,
|
||||
max_doc_bytes: 1024 * 1024,
|
||||
},
|
||||
),
|
||||
Cursor::new(&permuted_bytes),
|
||||
Cursor::new(&splits_bytes),
|
||||
Bars1mSortConfig {
|
||||
spill_dir: Some(out.clone()),
|
||||
spill_prefix: TABLE_BARS_1M,
|
||||
max_run_rows,
|
||||
max_merge_fan_in,
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
panic!("seed={seed} run={max_run_rows} fan_in={max_merge_fan_in}: {e:?}")
|
||||
});
|
||||
let ctx = format!("seed={seed} run={max_run_rows} fan_in={max_merge_fan_in}");
|
||||
assert_report_conservation(&report);
|
||||
assert!(metrics.peak_buffer_rows <= max_run_rows, "{ctx}");
|
||||
assert!(metrics.runs_spilled >= docs.len() / max_run_rows, "{ctx}");
|
||||
assert_eq!(report.curate_stats, ref_report.curate_stats, "{ctx}");
|
||||
assert_eq!(report.rows_written, ref_report.rows_written, "{ctx}");
|
||||
assert_eq!(report.ilp_stats, ref_report.ilp_stats, "{ctx}");
|
||||
// Only the published shards remain: every spilled sort-run file was removed.
|
||||
assert_eq!(list_dir_names(&out), expected_names, "{ctx}");
|
||||
for (name, expected) in expected_names.iter().zip(&ref_shards) {
|
||||
assert_eq!(
|
||||
&fs::read(out.join(name)).unwrap(),
|
||||
expected,
|
||||
"{ctx}: shard {name} bytes differ"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue