359 lines
15 KiB
Rust
359 lines
15 KiB
Rust
//! Cross-language golden fixture tests verifying that TSV files emitted by the TypeScript
|
|
//! security-master exporter (`apps/stock/data-pipeline/src/security-master/export-curate-inputs.ts`)
|
|
//! load cleanly through the real Rust TSV loaders and curate with zero drops.
|
|
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
use crate::bson_eod_prices::parse_ymd_strict_days;
|
|
use crate::cli::{parse_args, CliAction};
|
|
use crate::corporate_actions::{CorporateActionKind, DividendActionEvent, SplitActionEvent};
|
|
use crate::curate_corporate_actions::curate_corporate_actions;
|
|
use crate::provider_mapping::ProviderMappingTable;
|
|
use crate::run_curate_bars::{load_listing_currencies_tsv, load_provider_mapping_tsv};
|
|
use crate::run_curate_corporate_actions::{load_delistings_tsv, run_curate_corporate_actions};
|
|
|
|
fn fixture_dir() -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("tests")
|
|
.join("fixtures")
|
|
.join("curate-inputs")
|
|
}
|
|
|
|
#[test]
|
|
fn loads_ts_golden_curate_inputs_and_curates_delistings_with_zero_drops() {
|
|
let dir = fixture_dir();
|
|
|
|
// 1. Load provider_mapping.tsv and build ProviderMappingTable.
|
|
let pm_rows = load_provider_mapping_tsv(&dir.join("provider_mapping.tsv"))
|
|
.expect("provider_mapping.tsv fixture should load");
|
|
assert_eq!(pm_rows.len(), 16);
|
|
let mapping = ProviderMappingTable::from_rows(pm_rows)
|
|
.expect("provider_mapping.tsv fixture should build a valid ProviderMappingTable");
|
|
|
|
let fb_old_date = parse_ymd_strict_days("2022-06-08").unwrap();
|
|
let fb_new_date = parse_ymd_strict_days("2022-06-09").unwrap();
|
|
assert_eq!(mapping.resolve("eod", "FB.US", fb_old_date), Some(2));
|
|
assert_eq!(mapping.resolve("eod", "FB.US", fb_new_date), Some(3));
|
|
assert_eq!(mapping.resolve("qm", "SHOP:CA", fb_new_date), Some(8));
|
|
|
|
// 2. Load listing_currencies.tsv.
|
|
let currencies = load_listing_currencies_tsv(&dir.join("listing_currencies.tsv"))
|
|
.expect("listing_currencies.tsv fixture should load");
|
|
assert_eq!(currencies.len(), 8);
|
|
assert_eq!(currencies.get(&1).map(|c| c.as_ref()), Some("USD"));
|
|
assert_eq!(currencies.get(&6).map(|c| c.as_ref()), Some("USD"));
|
|
assert_eq!(currencies.get(&8).map(|c| c.as_ref()), Some("CAD"));
|
|
|
|
// 3. Load delistings.tsv and run curate_corporate_actions against "qm".
|
|
let delistings = load_delistings_tsv(&dir.join("delistings.tsv"))
|
|
.expect("delistings.tsv fixture should load");
|
|
assert_eq!(delistings.len(), 3);
|
|
|
|
let curated = curate_corporate_actions(
|
|
Vec::<(String, SplitActionEvent)>::new(),
|
|
Vec::<(String, DividendActionEvent)>::new(),
|
|
delistings,
|
|
"qm",
|
|
&mapping,
|
|
)
|
|
.expect("curate_corporate_actions should succeed on golden fixtures");
|
|
|
|
assert_eq!(curated.stats.rows_in, 3);
|
|
assert_eq!(curated.stats.rows_out, 3);
|
|
assert_eq!(curated.stats.dropped_unmapped, 0);
|
|
assert_eq!(curated.stats.dropped_unmapped_successor, 0);
|
|
assert_eq!(curated.stats.dropped_self_successor, 0);
|
|
assert_eq!(curated.stats.dropped_known_at_out_of_range, 0);
|
|
assert_eq!(curated.stats.listing_collision_collapsed, 0);
|
|
assert_eq!(curated.stats.listing_collision_dropped, 0);
|
|
|
|
// Curated rows are sorted by (listing_id, ex_date, kind):
|
|
// listing 4: SIVB -> SIVBQ OTC transfer on 2023-03-28 (despite SIVB reuse on 2023-03-20!)
|
|
// listing 6: SIVBQ bankruptcy cancellation (-1.0) on 2024-11-07
|
|
// listing 7: DEL30 Shumway terminal delisting (-0.3) on 2023-06-15
|
|
assert_eq!(curated.rows.len(), 3);
|
|
|
|
let otc = &curated.rows[0];
|
|
assert_eq!(otc.listing_id, 4);
|
|
assert_eq!(otc.ex_date, parse_ymd_strict_days("2023-03-28").unwrap());
|
|
assert_eq!(otc.kind, CorporateActionKind::OtcDelistTransfer);
|
|
assert_eq!(otc.successor_listing_id, Some(6));
|
|
assert_eq!(otc.terminal_return, None);
|
|
|
|
let sivbq_term = &curated.rows[1];
|
|
assert_eq!(sivbq_term.listing_id, 6);
|
|
assert_eq!(sivbq_term.ex_date, parse_ymd_strict_days("2024-11-07").unwrap());
|
|
assert_eq!(sivbq_term.kind, CorporateActionKind::Delisting);
|
|
assert_eq!(sivbq_term.successor_listing_id, None);
|
|
assert_eq!(sivbq_term.terminal_return, Some(-1.0));
|
|
|
|
let del30_term = &curated.rows[2];
|
|
assert_eq!(del30_term.listing_id, 7);
|
|
assert_eq!(del30_term.ex_date, parse_ymd_strict_days("2023-06-15").unwrap());
|
|
assert_eq!(del30_term.kind, CorporateActionKind::Delisting);
|
|
assert_eq!(del30_term.successor_listing_id, None);
|
|
assert_eq!(del30_term.terminal_return, Some(-0.3));
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn make_temp_dir(prefix: &str) -> PathBuf {
|
|
let nanos = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.expect("system clock after epoch")
|
|
.as_nanos();
|
|
let dir = std::env::temp_dir().join(format!("{prefix}-{}-{nanos}", std::process::id()));
|
|
fs::create_dir_all(&dir).expect("create temp dir");
|
|
dir
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn atomic_swap_current_link(inputs_dir: &Path, relative_gen: &str) {
|
|
use std::os::unix::fs::symlink;
|
|
let tmp = inputs_dir.join(format!(".current.swap.{}.tmp", std::process::id()));
|
|
let _ = fs::remove_file(&tmp);
|
|
symlink(relative_gen, &tmp).expect("create temp current symlink");
|
|
fs::rename(&tmp, inputs_dir.join("current")).expect("atomically swap current symlink");
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn curate_corporate_actions_with_curate_inputs_pins_generation_across_refresh() {
|
|
use std::os::unix::fs::symlink;
|
|
|
|
let root = make_temp_dir("curate-inputs-pin-ca");
|
|
let inputs_dir = root.join("curate-inputs");
|
|
let gen1 = inputs_dir.join("generations").join("gen-1");
|
|
let gen2 = inputs_dir.join("generations").join("gen-2");
|
|
fs::create_dir_all(&gen1).unwrap();
|
|
fs::create_dir_all(&gen2).unwrap();
|
|
|
|
// Generation 1: SIVB active only (no SIVBQ successor, empty delistings.tsv).
|
|
fs::write(
|
|
gen1.join("provider_mapping.tsv"),
|
|
"qm\tSIVB\t1988-01-04\t\t4\n",
|
|
)
|
|
.unwrap();
|
|
fs::write(gen1.join("listing_currencies.tsv"), "4\tUSD\n").unwrap();
|
|
fs::write(gen1.join("delistings.tsv"), "").unwrap();
|
|
|
|
// Generation 2: SIVB -> SIVBQ OTC transfer added to both provider_mapping.tsv and delistings.tsv.
|
|
fs::write(
|
|
gen2.join("provider_mapping.tsv"),
|
|
"qm\tSIVB\t1988-01-04\t2023-03-10\t4\nqm\tSIVBQ\t2023-03-28\t\t6\n",
|
|
)
|
|
.unwrap();
|
|
fs::write(gen2.join("listing_currencies.tsv"), "4\tUSD\n6\tUSD\n").unwrap();
|
|
fs::write(gen2.join("delistings.tsv"), "SIVB\t2023-03-28\t4\tSIVBQ\t\n").unwrap();
|
|
|
|
symlink("generations/gen-1", inputs_dir.join("current")).unwrap();
|
|
symlink(
|
|
"current/provider_mapping.tsv",
|
|
inputs_dir.join("provider_mapping.tsv"),
|
|
)
|
|
.unwrap();
|
|
symlink(
|
|
"current/listing_currencies.tsv",
|
|
inputs_dir.join("listing_currencies.tsv"),
|
|
)
|
|
.unwrap();
|
|
symlink("current/delistings.tsv", inputs_dir.join("delistings.tsv")).unwrap();
|
|
|
|
let empty_splits = root.join("qmSplits.bson");
|
|
let empty_divs = root.join("qmDividends.bson");
|
|
fs::write(&empty_splits, b"").unwrap();
|
|
fs::write(&empty_divs, b"").unwrap();
|
|
let out1 = root.join("out-1");
|
|
let out2 = root.join("out-2");
|
|
|
|
// 1. Parse invocation while `current` points to gen-1: pins gen-1 before opening any TSV.
|
|
let argv1 = vec![
|
|
"curate-corporate-actions".to_string(),
|
|
"--qm-splits".to_string(),
|
|
empty_splits.display().to_string(),
|
|
"--qm-dividends".to_string(),
|
|
empty_divs.display().to_string(),
|
|
"--curate-inputs".to_string(),
|
|
inputs_dir.display().to_string(),
|
|
"--out".to_string(),
|
|
out1.display().to_string(),
|
|
];
|
|
let action1 = parse_args(&argv1).expect("parse --curate-inputs on gen-1");
|
|
let CliAction::CurateCorporateActions(args1) = action1 else {
|
|
panic!("expected CurateCorporateActions action");
|
|
};
|
|
assert_eq!(args1.provider_mapping, fs::canonicalize(&gen1).unwrap().join("provider_mapping.tsv"));
|
|
assert_eq!(
|
|
args1.delistings,
|
|
Some(fs::canonicalize(&gen1).unwrap().join("delistings.tsv"))
|
|
);
|
|
|
|
// 2. Refresh `current` -> gen-2 BEFORE `run_curate_corporate_actions` opens either TSV!
|
|
atomic_swap_current_link(&inputs_dir, "generations/gen-2");
|
|
|
|
// Invocation 1 still reads both TSVs from pinned gen-1 (zero mixed snapshot, zero drops).
|
|
let report1 = run_curate_corporate_actions(&args1).expect("run pinned gen-1 after refresh");
|
|
assert_eq!(report1.delistings_rows_loaded, 0);
|
|
assert_eq!(report1.curate_stats.dropped_unmapped_successor, 0);
|
|
assert_eq!(report1.curate_stats.rows_out, 0);
|
|
|
|
// 3. A new invocation after the refresh pins gen-2 and curates SIVB -> SIVBQ with 0 drops.
|
|
let argv2 = vec![
|
|
"curate-corporate-actions".to_string(),
|
|
"--qm-splits".to_string(),
|
|
empty_splits.display().to_string(),
|
|
"--qm-dividends".to_string(),
|
|
empty_divs.display().to_string(),
|
|
"--curate-inputs".to_string(),
|
|
inputs_dir.display().to_string(),
|
|
"--out".to_string(),
|
|
out2.display().to_string(),
|
|
];
|
|
let action2 = parse_args(&argv2).expect("parse --curate-inputs on gen-2");
|
|
let CliAction::CurateCorporateActions(args2) = action2 else {
|
|
panic!("expected CurateCorporateActions action");
|
|
};
|
|
let report2 = run_curate_corporate_actions(&args2).expect("run pinned gen-2");
|
|
assert_eq!(report2.delistings_rows_loaded, 1);
|
|
assert_eq!(report2.curate_stats.builder.otc_delist_transfers_out, 1);
|
|
assert_eq!(report2.curate_stats.dropped_unmapped, 0);
|
|
assert_eq!(report2.curate_stats.dropped_unmapped_successor, 0);
|
|
assert_eq!(report2.curate_stats.rows_out, 1);
|
|
|
|
// 4. Combining --curate-inputs with individual TSV flags is rejected.
|
|
let bad_argv = vec![
|
|
"curate-corporate-actions".to_string(),
|
|
"--qm-splits".to_string(),
|
|
empty_splits.display().to_string(),
|
|
"--qm-dividends".to_string(),
|
|
empty_divs.display().to_string(),
|
|
"--curate-inputs".to_string(),
|
|
inputs_dir.display().to_string(),
|
|
"--delistings".to_string(),
|
|
gen2.join("delistings.tsv").display().to_string(),
|
|
"--out".to_string(),
|
|
out2.display().to_string(),
|
|
];
|
|
let err = parse_args(&bad_argv).unwrap_err();
|
|
assert!(err.contains("cannot combine '--curate-inputs' with '--delistings'"));
|
|
|
|
let _ = fs::remove_dir_all(&root);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn curate_bars_1d_with_curate_inputs_pins_generation_across_refresh() {
|
|
use std::os::unix::fs::symlink;
|
|
|
|
let root = make_temp_dir("curate-inputs-pin-bars");
|
|
let inputs_dir = root.join("curate-inputs");
|
|
let gen1 = inputs_dir.join("generations").join("gen-1");
|
|
let gen2 = inputs_dir.join("generations").join("gen-2");
|
|
fs::create_dir_all(&gen1).unwrap();
|
|
fs::create_dir_all(&gen2).unwrap();
|
|
fs::write(gen1.join("provider_mapping.tsv"), "eod\tAAPL.US\t2020-01-01\t\t1\n").unwrap();
|
|
fs::write(gen1.join("listing_currencies.tsv"), "1\tUSD\n").unwrap();
|
|
fs::write(gen2.join("provider_mapping.tsv"), "eod\tSHOP.TO\t2020-01-01\t\t2\n").unwrap();
|
|
fs::write(gen2.join("listing_currencies.tsv"), "2\tCAD\n").unwrap();
|
|
symlink("generations/gen-1", inputs_dir.join("current")).unwrap();
|
|
|
|
let argv = vec![
|
|
"curate-bars-1d".to_string(),
|
|
"--eod-prices".to_string(),
|
|
root.join("eodPrices.bson").display().to_string(),
|
|
"--qm-splits".to_string(),
|
|
root.join("qmSplits.bson").display().to_string(),
|
|
"--curate-inputs".to_string(),
|
|
inputs_dir.display().to_string(),
|
|
"--out".to_string(),
|
|
root.join("out").display().to_string(),
|
|
];
|
|
let action1 = parse_args(&argv).expect("parse curate-bars-1d --curate-inputs gen-1");
|
|
let CliAction::CurateBars1d(args1) = action1 else {
|
|
panic!("expected CurateBars1d action");
|
|
};
|
|
let canon_gen1 = fs::canonicalize(&gen1).unwrap();
|
|
assert_eq!(args1.provider_mapping, canon_gen1.join("provider_mapping.tsv"));
|
|
assert_eq!(args1.listing_currencies, canon_gen1.join("listing_currencies.tsv"));
|
|
|
|
// Swap `current` -> gen-2 and verify pinned paths from args1 still load gen-1 consistently.
|
|
atomic_swap_current_link(&inputs_dir, "generations/gen-2");
|
|
let pm1 = load_provider_mapping_tsv(&args1.provider_mapping).unwrap();
|
|
let lc1 = load_listing_currencies_tsv(&args1.listing_currencies).unwrap();
|
|
assert_eq!(pm1.len(), 1);
|
|
assert_eq!(pm1[0].source_id, "AAPL.US");
|
|
assert_eq!(lc1.get(&1).map(|c| c.as_ref()), Some("USD"));
|
|
assert!(!lc1.contains_key(&2));
|
|
|
|
// A subsequent parse pins gen-2.
|
|
let action2 = parse_args(&argv).expect("parse curate-bars-1d --curate-inputs gen-2");
|
|
let CliAction::CurateBars1d(args2) = action2 else {
|
|
panic!("expected CurateBars1d action");
|
|
};
|
|
let canon_gen2 = fs::canonicalize(&gen2).unwrap();
|
|
assert_eq!(args2.provider_mapping, canon_gen2.join("provider_mapping.tsv"));
|
|
assert_eq!(args2.listing_currencies, canon_gen2.join("listing_currencies.tsv"));
|
|
|
|
// Combining --curate-inputs with --listing-currencies is rejected.
|
|
let mut bad_argv = argv.clone();
|
|
bad_argv.push("--listing-currencies".to_string());
|
|
bad_argv.push(gen2.join("listing_currencies.tsv").display().to_string());
|
|
let err = parse_args(&bad_argv).unwrap_err();
|
|
assert!(err.contains("cannot combine '--curate-inputs' with '--listing-currencies'"));
|
|
|
|
let _ = fs::remove_dir_all(&root);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn curate_inputs_rejects_flat_directory_without_committed_current_link() {
|
|
let root = make_temp_dir("curate-inputs-flat-reject");
|
|
let flat_dir = root.join("flat-inputs");
|
|
fs::create_dir_all(&flat_dir).unwrap();
|
|
fs::write(
|
|
flat_dir.join("provider_mapping.tsv"),
|
|
"qm\tSIVB\t1988-01-04\t\t4\n",
|
|
)
|
|
.unwrap();
|
|
fs::write(flat_dir.join("listing_currencies.tsv"), "4\tUSD\n").unwrap();
|
|
fs::write(flat_dir.join("delistings.tsv"), "").unwrap();
|
|
|
|
let ca_argv = vec![
|
|
"curate-corporate-actions".to_string(),
|
|
"--qm-splits".to_string(),
|
|
root.join("qmSplits.bson").display().to_string(),
|
|
"--qm-dividends".to_string(),
|
|
root.join("qmDividends.bson").display().to_string(),
|
|
"--curate-inputs".to_string(),
|
|
flat_dir.display().to_string(),
|
|
"--out".to_string(),
|
|
root.join("out-ca").display().to_string(),
|
|
];
|
|
let ca_err = parse_args(&ca_argv).unwrap_err();
|
|
assert!(
|
|
ca_err.contains("requires a committed 'current' link"),
|
|
"unexpected error for curate-corporate-actions: {ca_err}"
|
|
);
|
|
assert!(ca_err.contains("--provider-mapping"));
|
|
|
|
let bars_argv = vec![
|
|
"curate-bars-1d".to_string(),
|
|
"--eod-prices".to_string(),
|
|
root.join("eodPrices.bson").display().to_string(),
|
|
"--qm-splits".to_string(),
|
|
root.join("qmSplits.bson").display().to_string(),
|
|
"--curate-inputs".to_string(),
|
|
flat_dir.display().to_string(),
|
|
"--out".to_string(),
|
|
root.join("out-bars").display().to_string(),
|
|
];
|
|
let bars_err = parse_args(&bars_argv).unwrap_err();
|
|
assert!(
|
|
bars_err.contains("requires a committed 'current' link"),
|
|
"unexpected error for curate-bars-1d: {bars_err}"
|
|
);
|
|
assert!(bars_err.contains("--listing-currencies"));
|
|
|
|
let _ = fs::remove_dir_all(&root);
|
|
}
|