T-70: edits made in the worktree
This commit is contained in:
parent
722fdea615
commit
740014a4d0
1 changed files with 620 additions and 0 deletions
620
tools/curate-etl/tests/corporate_actions_contract.rs
Normal file
620
tools/curate-etl/tests/corporate_actions_contract.rs
Normal file
|
|
@ -0,0 +1,620 @@
|
|||
//! QA contract tests for the public `curate_etl` `corporate_actions` API (T-70 / M1.2c).
|
||||
//!
|
||||
//! Uses only the crate's public surface plus independent reference checks, so these guard the
|
||||
//! behaviour later Parquet writers and point-in-time backtest readers will rely on.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::process::{Command, Output};
|
||||
|
||||
use curate_etl::bench::{run_corporate_actions_benchmark, DEFAULT_CORPORATE_ACTIONS_BENCH_EVENTS};
|
||||
use curate_etl::{
|
||||
build_corporate_actions, ymd_to_epoch_days, CorporateActionError, CorporateActionKind,
|
||||
CorporateActionRow, CorporateActionsBuild, CorporateActionsStats, DelistingOutcomeEvent,
|
||||
DividendActionEvent, KnownAtSource, SplitActionEvent,
|
||||
};
|
||||
|
||||
fn day(y: i32, m: u32, d: u32) -> i32 {
|
||||
ymd_to_epoch_days(y, m, d).expect("valid fixture date")
|
||||
}
|
||||
|
||||
fn split(ex_date: i32, ratio: f64, declared_date: Option<i32>) -> SplitActionEvent {
|
||||
SplitActionEvent {
|
||||
ex_date,
|
||||
ratio,
|
||||
declared_date,
|
||||
}
|
||||
}
|
||||
|
||||
fn div(ex_date: i32, cash: f64, currency: &str, declared_date: Option<i32>) -> DividendActionEvent {
|
||||
DividendActionEvent {
|
||||
ex_date,
|
||||
cash,
|
||||
currency: currency.to_string(),
|
||||
declared_date,
|
||||
}
|
||||
}
|
||||
|
||||
fn delist<K>(
|
||||
delist_date: i32,
|
||||
successor_key: Option<K>,
|
||||
terminal_return: Option<f64>,
|
||||
) -> DelistingOutcomeEvent<K> {
|
||||
DelistingOutcomeEvent {
|
||||
delist_date,
|
||||
successor_key,
|
||||
terminal_return,
|
||||
}
|
||||
}
|
||||
|
||||
fn no_splits<K>() -> std::iter::Empty<(K, SplitActionEvent)> {
|
||||
std::iter::empty()
|
||||
}
|
||||
|
||||
fn no_divs<K>() -> std::iter::Empty<(K, DividendActionEvent)> {
|
||||
std::iter::empty()
|
||||
}
|
||||
|
||||
fn no_delists<K>() -> std::iter::Empty<(K, DelistingOutcomeEvent<K>)> {
|
||||
std::iter::empty()
|
||||
}
|
||||
|
||||
/// Fixed-seed SplitMix64 so fixtures are reproducible without external crates.
|
||||
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 below(&mut self, n: u64) -> u64 {
|
||||
self.next() % n
|
||||
}
|
||||
}
|
||||
|
||||
fn shuffle<T>(items: &mut [T], rng: &mut Rng) {
|
||||
for i in (1..items.len()).rev() {
|
||||
let j = rng.below(i as u64 + 1) as usize;
|
||||
items.swap(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
/// Canonical byte encoding of a row (f64 by bits) so equality is byte-level, not `PartialEq`.
|
||||
fn encode<K: std::fmt::Debug>(row: &CorporateActionRow<K>) -> String {
|
||||
let bits = |v: Option<f64>| v.map(f64::to_bits);
|
||||
format!(
|
||||
"{:?}|{}|{:?}|{}|{:?}|{:?}|{:?}|{:?}|{:?}|{}|{}",
|
||||
row.key,
|
||||
row.ex_date,
|
||||
row.declared_date,
|
||||
row.kind.as_str(),
|
||||
bits(row.ratio),
|
||||
bits(row.cash_amount),
|
||||
row.currency,
|
||||
row.successor_key,
|
||||
bits(row.terminal_return),
|
||||
row.known_at_date,
|
||||
row.known_at_source.as_str()
|
||||
)
|
||||
}
|
||||
|
||||
fn assert_conservation<K>(build: &CorporateActionsBuild<K>) {
|
||||
let s = &build.stats;
|
||||
assert_eq!(s.splits_in, s.splits_out + s.split_noops_dropped);
|
||||
assert_eq!(
|
||||
s.dividends_in,
|
||||
s.dividends_out + s.dividend_zero_cash_dropped
|
||||
);
|
||||
assert_eq!(
|
||||
s.delistings_in,
|
||||
s.otc_delist_transfers_out + s.delistings_out + s.delist_without_outcome
|
||||
);
|
||||
assert_eq!(s.rows_out, build.rows.len());
|
||||
assert_eq!(
|
||||
s.rows_out,
|
||||
s.known_at_declared_date + s.known_at_ex_date_fallback + s.known_at_delist_date
|
||||
);
|
||||
assert!(s.declared_after_ex_date <= s.known_at_ex_date_fallback);
|
||||
}
|
||||
|
||||
/// Mixed fixture with i32 keys covering every kind, drop reason and `known_at` branch.
|
||||
type Inputs = (
|
||||
Vec<(i32, SplitActionEvent)>,
|
||||
Vec<(i32, DividendActionEvent)>,
|
||||
Vec<(i32, DelistingOutcomeEvent<i32>)>,
|
||||
);
|
||||
|
||||
fn random_inputs(seed: u64, n: usize) -> Inputs {
|
||||
let mut rng = Rng(seed);
|
||||
let base = day(2000, 1, 1);
|
||||
let mut splits = Vec::new();
|
||||
let mut divs = Vec::new();
|
||||
let mut dels = Vec::new();
|
||||
let ratios = [1.0, 2.0, 0.5, 10.0, 0.05, 3.0];
|
||||
let cashes = [0.0, -0.0, 0.01, 0.25, 0.25, 1.42];
|
||||
for _ in 0..n {
|
||||
// Small key/date space forces many same-(key, ex_date) ties.
|
||||
let key = rng.below(7) as i32;
|
||||
let ex = base + rng.below(5) as i32;
|
||||
let declared = match rng.below(4) {
|
||||
0 => None,
|
||||
1 => Some(ex - 10),
|
||||
2 => Some(ex),
|
||||
_ => Some(ex + 1),
|
||||
};
|
||||
match rng.below(3) {
|
||||
0 => splits.push((key, split(ex, ratios[rng.below(6) as usize], declared))),
|
||||
1 => {
|
||||
let cur = ["USD", "CAD", "CAD"][rng.below(3) as usize];
|
||||
divs.push((key, div(ex, cashes[rng.below(6) as usize], cur, declared)));
|
||||
}
|
||||
_ => {
|
||||
let ev = match rng.below(5) {
|
||||
0 => delist(ex, Some(100 + key), None),
|
||||
1 => delist(ex, None, Some(-0.3)),
|
||||
2 => delist(ex, None, Some(-1.0)),
|
||||
3 => delist(ex, None, Some(-0.0)),
|
||||
_ => delist(ex, None, None),
|
||||
};
|
||||
dels.push((key, ev));
|
||||
}
|
||||
}
|
||||
}
|
||||
(splits, divs, dels)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seeded_random_permutations_produce_byte_identical_rows_and_stats() {
|
||||
let (splits, divs, dels) = random_inputs(0xC0A7_0001, 600);
|
||||
let baseline = build_corporate_actions(splits.clone(), divs.clone(), dels.clone()).unwrap();
|
||||
assert_conservation(&baseline);
|
||||
let baseline_bytes: Vec<String> = baseline.rows.iter().map(encode).collect();
|
||||
|
||||
let mut rng = Rng(0xC0A7_0002);
|
||||
for _ in 0..20 {
|
||||
let (mut s, mut d, mut l) = (splits.clone(), divs.clone(), dels.clone());
|
||||
shuffle(&mut s, &mut rng);
|
||||
shuffle(&mut d, &mut rng);
|
||||
shuffle(&mut l, &mut rng);
|
||||
let candidate = build_corporate_actions(s, d, l).unwrap();
|
||||
assert_eq!(candidate.stats, baseline.stats);
|
||||
let bytes: Vec<String> = candidate.rows.iter().map(encode).collect();
|
||||
assert_eq!(bytes, baseline_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rows_are_strictly_sorted_by_key_ex_date_then_kind_order() {
|
||||
let (splits, divs, dels) = random_inputs(0xC0A7_0003, 400);
|
||||
let built = build_corporate_actions(splits, divs, dels).unwrap();
|
||||
for w in built.rows.windows(2) {
|
||||
let a = (w[0].key, w[0].ex_date, w[0].kind);
|
||||
let b = (w[1].key, w[1].ex_date, w[1].kind);
|
||||
assert!(a <= b, "out of order: {:?} then {:?}", w[0], w[1]);
|
||||
}
|
||||
// keys_out is the number of distinct keys actually emitted.
|
||||
let mut distinct: Vec<i32> = built.rows.iter().map(|r| r.key).collect();
|
||||
distinct.dedup();
|
||||
assert_eq!(built.stats.keys_out, distinct.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_key_same_day_events_order_split_dividend_otc_transfer_delisting() {
|
||||
let ex = day(2023, 3, 28);
|
||||
let built = build_corporate_actions(
|
||||
[("X".to_string(), split(ex, 2.0, None))],
|
||||
[("X".to_string(), div(ex, 0.1, "USD", None))],
|
||||
[
|
||||
("X".to_string(), delist(ex, None, Some(-0.3))),
|
||||
("X".to_string(), delist(ex, Some("XQ".to_string()), None)),
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
let kinds: Vec<&str> = built.rows.iter().map(|r| r.kind.as_str()).collect();
|
||||
assert_eq!(
|
||||
kinds,
|
||||
["split", "dividend", "otc_delist_transfer", "delisting"]
|
||||
);
|
||||
assert_eq!(built.stats.keys_out, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_emitted_row_has_only_the_columns_of_its_kind() {
|
||||
let (splits, divs, dels) = random_inputs(0xC0A7_0004, 400);
|
||||
let built = build_corporate_actions(splits, divs, dels).unwrap();
|
||||
for r in &built.rows {
|
||||
match r.kind {
|
||||
CorporateActionKind::Split => {
|
||||
assert!(r
|
||||
.ratio
|
||||
.is_some_and(|x| x.is_finite() && x > 0.0 && x != 1.0));
|
||||
assert!(r.cash_amount.is_none() && r.currency.is_none());
|
||||
assert!(r.successor_key.is_none() && r.terminal_return.is_none());
|
||||
assert_ne!(r.known_at_source, KnownAtSource::DelistDate);
|
||||
}
|
||||
CorporateActionKind::Dividend => {
|
||||
assert!(r.cash_amount.is_some_and(|x| x.is_finite() && x > 0.0));
|
||||
assert!(r.currency.as_deref().is_some_and(|c| !c.trim().is_empty()));
|
||||
assert!(r.ratio.is_none());
|
||||
assert!(r.successor_key.is_none() && r.terminal_return.is_none());
|
||||
assert_ne!(r.known_at_source, KnownAtSource::DelistDate);
|
||||
}
|
||||
CorporateActionKind::OtcDelistTransfer => {
|
||||
assert!(r.successor_key.is_some() && r.terminal_return.is_none());
|
||||
assert!(r.ratio.is_none() && r.cash_amount.is_none() && r.currency.is_none());
|
||||
assert_eq!(r.declared_date, None);
|
||||
assert_eq!(r.known_at_date, r.ex_date);
|
||||
assert_eq!(r.known_at_source, KnownAtSource::DelistDate);
|
||||
}
|
||||
CorporateActionKind::Delisting => {
|
||||
let tr = r.terminal_return.expect("terminal return");
|
||||
assert!((-1.0..=0.0).contains(&tr));
|
||||
assert_ne!(tr.to_bits(), (-0.0f64).to_bits(), "-0.0 must be normalised");
|
||||
assert!(r.successor_key.is_none());
|
||||
assert!(r.ratio.is_none() && r.cash_amount.is_none() && r.currency.is_none());
|
||||
assert_eq!(r.declared_date, None);
|
||||
assert_eq!(r.known_at_date, r.ex_date);
|
||||
assert_eq!(r.known_at_source, KnownAtSource::DelistDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_at_matches_independent_reference_and_never_exceeds_ex_date() {
|
||||
let (splits, divs, _) = random_inputs(0xC0A7_0005, 500);
|
||||
// Reference multiset of expected (key, ex, declared, known_at, source) for emitted events.
|
||||
let mut expected: BTreeMap<(i32, i32, Option<i32>, i32, &str), usize> = BTreeMap::new();
|
||||
let mut late = 0usize;
|
||||
let declared_refs = splits
|
||||
.iter()
|
||||
.filter(|(_, s)| s.ratio != 1.0)
|
||||
.map(|(k, s)| (*k, s.ex_date, s.declared_date))
|
||||
.chain(
|
||||
divs.iter()
|
||||
.filter(|(_, d)| d.cash != 0.0)
|
||||
.map(|(k, d)| (*k, d.ex_date, d.declared_date)),
|
||||
);
|
||||
for (k, ex, declared) in declared_refs {
|
||||
let (known, src) = match declared {
|
||||
Some(d) if d <= ex => (d, "declared_date"),
|
||||
Some(_) => {
|
||||
late += 1;
|
||||
(ex, "ex_date_fallback")
|
||||
}
|
||||
None => (ex, "ex_date_fallback"),
|
||||
};
|
||||
*expected.entry((k, ex, declared, known, src)).or_default() += 1;
|
||||
}
|
||||
|
||||
let built = build_corporate_actions(splits, divs, no_delists()).unwrap();
|
||||
let mut actual: BTreeMap<(i32, i32, Option<i32>, i32, &str), usize> = BTreeMap::new();
|
||||
for r in &built.rows {
|
||||
assert!(r.known_at_date <= r.ex_date, "look-ahead: {r:?}");
|
||||
*actual
|
||||
.entry((
|
||||
r.key,
|
||||
r.ex_date,
|
||||
r.declared_date,
|
||||
r.known_at_date,
|
||||
r.known_at_source.as_str(),
|
||||
))
|
||||
.or_default() += 1;
|
||||
}
|
||||
assert_eq!(actual, expected);
|
||||
assert_eq!(built.stats.declared_after_ex_date, late);
|
||||
assert!(late > 0, "fixture must exercise the late-declaration path");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mutating_future_events_leaves_rows_known_by_cutoff_byte_identical() {
|
||||
// Point-in-time check: corrupt every event first knowable after t0 and confirm the subset of
|
||||
// rows a decision at t0 could read is unchanged, while some later row does change.
|
||||
let (splits, divs, dels) = random_inputs(0xC0A7_0006, 500);
|
||||
let t0 = day(2000, 1, 3);
|
||||
let visible = |b: &CorporateActionsBuild<i32>| -> Vec<String> {
|
||||
b.rows
|
||||
.iter()
|
||||
.filter(|r| r.known_at_date <= t0)
|
||||
.map(encode)
|
||||
.collect()
|
||||
};
|
||||
let base = build_corporate_actions(splits.clone(), divs.clone(), dels.clone()).unwrap();
|
||||
|
||||
let known_at = |ex: i32, decl: Option<i32>| match decl {
|
||||
Some(d) if d <= ex => d,
|
||||
_ => ex,
|
||||
};
|
||||
let m_splits: Vec<_> = splits
|
||||
.into_iter()
|
||||
.map(|(k, mut s)| {
|
||||
if known_at(s.ex_date, s.declared_date) > t0 && s.ratio != 1.0 {
|
||||
s.ratio *= 1000.0;
|
||||
}
|
||||
(k, s)
|
||||
})
|
||||
.collect();
|
||||
let m_divs: Vec<_> = divs
|
||||
.into_iter()
|
||||
.map(|(k, mut d)| {
|
||||
if known_at(d.ex_date, d.declared_date) > t0 && d.cash != 0.0 {
|
||||
d.cash *= 1000.0;
|
||||
}
|
||||
(k, d)
|
||||
})
|
||||
.collect();
|
||||
let m_dels: Vec<_> = dels
|
||||
.into_iter()
|
||||
.map(|(k, mut l)| {
|
||||
if l.delist_date > t0 && l.successor_key.is_some() {
|
||||
l.successor_key = Some(9_999);
|
||||
}
|
||||
(k, l)
|
||||
})
|
||||
.collect();
|
||||
let mutated = build_corporate_actions(m_splits, m_divs, m_dels).unwrap();
|
||||
|
||||
assert!(!visible(&base).is_empty());
|
||||
assert_eq!(visible(&mutated), visible(&base));
|
||||
// Not toothless: the full output must differ once the future rows are included.
|
||||
let all = |b: &CorporateActionsBuild<i32>| b.rows.iter().map(encode).collect::<Vec<_>>();
|
||||
assert_ne!(all(&mutated), all(&base));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moving_declared_date_across_ex_date_flips_known_at_at_the_boundary() {
|
||||
let ex = day(2024, 8, 15);
|
||||
let build_with = |declared: i32| {
|
||||
build_corporate_actions(
|
||||
[(1, split(ex, 2.0, Some(declared)))],
|
||||
no_divs(),
|
||||
no_delists(),
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
let on = build_with(ex);
|
||||
assert_eq!(on.rows[0].known_at_date, ex);
|
||||
assert_eq!(on.rows[0].known_at_source, KnownAtSource::DeclaredDate);
|
||||
assert_eq!(on.stats.declared_after_ex_date, 0);
|
||||
|
||||
let one_after = build_with(ex + 1);
|
||||
assert_eq!(one_after.rows[0].known_at_date, ex);
|
||||
assert_eq!(one_after.rows[0].declared_date, Some(ex + 1));
|
||||
assert_eq!(
|
||||
one_after.rows[0].known_at_source,
|
||||
KnownAtSource::ExDateFallback
|
||||
);
|
||||
assert_eq!(one_after.stats.declared_after_ex_date, 1);
|
||||
|
||||
let one_before = build_with(ex - 1);
|
||||
assert_eq!(one_before.rows[0].known_at_date, ex - 1);
|
||||
assert_eq!(
|
||||
one_before.rows[0].known_at_source,
|
||||
KnownAtSource::DeclaredDate
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dividend_cash_and_currency_pass_through_unchanged() {
|
||||
let ex = day(2024, 11, 15);
|
||||
// Odd cash value and a non-trimmed, lowercase currency must not be rounded or normalised.
|
||||
let cash = 0.123_456_789_012_345_67_f64;
|
||||
let built = build_corporate_actions(
|
||||
no_splits(),
|
||||
[
|
||||
(1, div(ex, cash, "CAD", None)),
|
||||
(1, div(ex, cash, "cad ", None)),
|
||||
(1, div(ex, f64::MIN_POSITIVE, "USD", None)),
|
||||
],
|
||||
no_delists(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(built.rows.len(), 3);
|
||||
let currencies: Vec<&str> = built
|
||||
.rows
|
||||
.iter()
|
||||
.map(|r| r.currency.as_deref().unwrap())
|
||||
.collect();
|
||||
assert!(currencies.contains(&"CAD"));
|
||||
assert!(currencies.contains(&"cad "));
|
||||
for r in &built.rows {
|
||||
let c = r.cash_amount.unwrap();
|
||||
assert!(c.to_bits() == cash.to_bits() || c.to_bits() == f64::MIN_POSITIVE.to_bits());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_zero_cash_is_dropped_as_zero_not_rejected() {
|
||||
let ex = day(2024, 1, 2);
|
||||
let built =
|
||||
build_corporate_actions(no_splits(), [(1, div(ex, -0.0, "", None))], no_delists()).unwrap();
|
||||
assert!(built.rows.is_empty());
|
||||
assert_eq!(built.stats.dividend_zero_cash_dropped, 1);
|
||||
assert_eq!(built.stats.keys_out, 0);
|
||||
assert_conservation(&built);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropped_only_inputs_emit_no_rows_and_no_keys() {
|
||||
let ex = day(2024, 1, 2);
|
||||
let built = build_corporate_actions(
|
||||
[(1, split(ex, 1.0, Some(ex + 5)))],
|
||||
[(2, div(ex, 0.0, "USD", Some(ex + 5)))],
|
||||
[(3, delist::<i32>(ex, None, None))],
|
||||
)
|
||||
.unwrap();
|
||||
assert!(built.rows.is_empty());
|
||||
assert_eq!(built.stats.keys_out, 0);
|
||||
// Dropped events must not touch the known_at counters.
|
||||
assert_eq!(built.stats.known_at_ex_date_fallback, 0);
|
||||
assert_eq!(built.stats.declared_after_ex_date, 0);
|
||||
assert_eq!(built.stats.split_noops_dropped, 1);
|
||||
assert_eq!(built.stats.dividend_zero_cash_dropped, 1);
|
||||
assert_eq!(built.stats.delist_without_outcome, 1);
|
||||
assert_conservation(&built);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn terminal_return_bounds_are_inclusive_and_just_outside_is_rejected() {
|
||||
let d = day(2022, 6, 30);
|
||||
for ok in [-1.0, 0.0, -f64::EPSILON, -1.0 + f64::EPSILON] {
|
||||
let built =
|
||||
build_corporate_actions(no_splits(), no_divs(), [(1, delist(d, None, Some(ok)))])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
built.rows[0].terminal_return.map(f64::to_bits),
|
||||
Some(ok.to_bits())
|
||||
);
|
||||
}
|
||||
for bad in [-1.0 - f64::EPSILON, f64::MIN_POSITIVE] {
|
||||
let err =
|
||||
build_corporate_actions(no_splits(), no_divs(), [(1, delist(d, None, Some(bad)))])
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(err, CorporateActionError::InvalidTerminalReturn { delist_date, .. } if delist_date == d),
|
||||
"{bad}: {err:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_event_after_many_valid_events_still_returns_typed_error() {
|
||||
let ex = day(2020, 1, 2);
|
||||
let mut divs: Vec<(i32, DividendActionEvent)> =
|
||||
(0..1_000).map(|i| (i, div(ex, 0.1, "USD", None))).collect();
|
||||
divs.push((7, div(ex + 3, f64::NAN, "USD", None)));
|
||||
let err = build_corporate_actions(no_splits(), divs, no_delists()).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, CorporateActionError::InvalidDividendCash { ex_date, .. } if ex_date == ex + 3)
|
||||
);
|
||||
|
||||
let err = build_corporate_actions(
|
||||
[(1, split(ex, 2.0, None)), (2, split(ex + 1, -0.0, None))],
|
||||
no_divs(),
|
||||
no_delists(),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(err, CorporateActionError::InvalidSplitRatio { ex_date, .. } if ex_date == ex + 1)
|
||||
);
|
||||
|
||||
let err = build_corporate_actions(
|
||||
no_splits(),
|
||||
no_divs(),
|
||||
[
|
||||
(1, delist(ex, None, Some(-0.3))),
|
||||
(2, delist(ex + 2, Some(3), Some(-0.3))),
|
||||
],
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(
|
||||
err,
|
||||
CorporateActionError::ConflictingDelistingOutcome {
|
||||
delist_date: ex + 2
|
||||
}
|
||||
);
|
||||
// Typed error is usable as `dyn Error`.
|
||||
let boxed: Box<dyn std::error::Error> = Box::new(err);
|
||||
assert!(boxed.to_string().contains("mutually exclusive"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_vec_and_borrowed_iterator_inputs_with_composite_keys() {
|
||||
// Generic K: Ord + Clone with a tuple key (e.g. (venue, listing_id)).
|
||||
let ex = day(2023, 3, 28);
|
||||
let splits = [((2u8, 10i32), split(ex, 2.0, None))];
|
||||
let dels = vec![((1u8, 10i32), delist(ex, Some((9u8, 10i32)), None))];
|
||||
let built = build_corporate_actions(splits.iter().cloned(), no_divs(), dels).unwrap();
|
||||
assert_eq!(built.rows[0].key, (1, 10));
|
||||
assert_eq!(built.rows[0].successor_key, Some((9, 10)));
|
||||
assert_eq!(built.rows[1].key, (2, 10));
|
||||
assert_eq!(built.stats.keys_out, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_stats_is_all_zero() {
|
||||
let s = CorporateActionsStats::default();
|
||||
assert_eq!(
|
||||
s.rows_out + s.keys_out + s.splits_in + s.dividends_in + s.delistings_in,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_bench_corporate_actions_matches_readme_recorded_output() {
|
||||
// README records these values for `bench-corporate-actions` with the default event count.
|
||||
let r = run_corporate_actions_benchmark(DEFAULT_CORPORATE_ACTIONS_BENCH_EVENTS).unwrap();
|
||||
assert_eq!(r.events, 1_000_000);
|
||||
assert_eq!(
|
||||
(r.splits_in, r.dividends_in, r.delistings_in),
|
||||
(39_872, 920_134, 39_994)
|
||||
);
|
||||
assert_eq!((r.rows_out, r.keys_out), (980_694, 20_000));
|
||||
assert_eq!(r.checksum, 0xbfd2_ceda_3737_62f5);
|
||||
let s = r.stats;
|
||||
assert_eq!(s.split_noops_dropped, 1_963);
|
||||
assert_eq!(s.dividend_zero_cash_dropped, 9_302);
|
||||
assert_eq!(s.delist_without_outcome, 8_041);
|
||||
assert_eq!(s.otc_delist_transfers_out, 11_942);
|
||||
assert_eq!(s.delistings_out, 20_011);
|
||||
assert_eq!(s.known_at_declared_date, 758_098);
|
||||
assert_eq!(s.known_at_ex_date_fallback, 190_643);
|
||||
assert_eq!(s.declared_after_ex_date, 19_108);
|
||||
assert_eq!(s.known_at_delist_date, 31_953);
|
||||
assert_eq!(s.splits_in, r.splits_in);
|
||||
assert_eq!(s.dividends_in, r.dividends_in);
|
||||
assert_eq!(s.delistings_in, r.delistings_in);
|
||||
assert_eq!(s.splits_in, s.splits_out + s.split_noops_dropped);
|
||||
assert_eq!(
|
||||
s.dividends_in,
|
||||
s.dividends_out + s.dividend_zero_cash_dropped
|
||||
);
|
||||
assert_eq!(
|
||||
s.delistings_in,
|
||||
s.otc_delist_transfers_out + s.delistings_out + s.delist_without_outcome
|
||||
);
|
||||
assert_eq!(
|
||||
s.rows_out,
|
||||
s.known_at_declared_date + s.known_at_ex_date_fallback + s.known_at_delist_date
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bench_checksum_changes_with_event_count() {
|
||||
let a = run_corporate_actions_benchmark(2_000).unwrap();
|
||||
let b = run_corporate_actions_benchmark(2_001).unwrap();
|
||||
assert_ne!(a.checksum, b.checksum);
|
||||
}
|
||||
|
||||
fn run_bin(args: &[&str]) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_curate-etl"))
|
||||
.args(args)
|
||||
.output()
|
||||
.expect("spawn curate-etl")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bench_corporate_actions_binary_checksum_matches_library_report() {
|
||||
let lib = run_corporate_actions_benchmark(3_000).unwrap();
|
||||
let out = run_bin(&["bench-corporate-actions", "3000"]);
|
||||
assert!(out.status.success());
|
||||
let text = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
text.contains(&format!("checksum=0x{:016x}", lib.checksum)),
|
||||
"{text}"
|
||||
);
|
||||
assert!(
|
||||
text.contains(&format!("rows_out={} ", lib.rows_out)),
|
||||
"{text}"
|
||||
);
|
||||
assert!(
|
||||
text.contains(&format!(
|
||||
"splits={} dividends={} delistings={}",
|
||||
lib.splits_in, lib.dividends_in, lib.delistings_in
|
||||
)),
|
||||
"{text}"
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue