//! QA end-to-end tests for the `curate-etl` binary (T-47 / M1.2a CLI contract). use std::fs::{self, File}; use std::io::Write; use std::path::PathBuf; use std::process::{Command, Output}; use std::sync::atomic::{AtomicU64, Ordering}; use bson::raw::{cstr, RawDocumentBuf}; fn run(args: &[&str]) -> Output { Command::new(env!("CARGO_BIN_EXE_curate-etl")) .args(args) .output() .expect("spawn curate-etl binary") } fn stdout(o: &Output) -> String { String::from_utf8_lossy(&o.stdout).into_owned() } fn stderr(o: &Output) -> String { String::from_utf8_lossy(&o.stderr).into_owned() } #[test] fn no_args_and_help_print_usage_and_succeed() { for args in [&[][..], &["--help"][..], &["-h"][..]] { let out = run(args); assert!(out.status.success(), "{args:?}: {}", stderr(&out)); let text = stdout(&out); assert!(text.contains("Usage:"), "{args:?}: {text}"); assert!(text.contains("curate-bars-1d"), "{args:?}: {text}"); assert!(text.contains("curate-adj-factors"), "{args:?}: {text}"); assert!(text.contains("curate-corporate-actions"), "{args:?}: {text}"); assert!(text.contains("curate-fx-1d"), "{args:?}: {text}"); assert!(text.contains("curate-shorts"), "{args:?}: {text}"); assert!(text.contains("bench-curate-bars-cmd"), "{args:?}: {text}"); assert!(text.contains("bench-curate-adj-cmd"), "{args:?}: {text}"); assert!( text.contains("bench-curate-corporate-actions-cmd"), "{args:?}: {text}" ); assert!( text.contains("bench-curate-shorts-command"), "{args:?}: {text}" ); assert!(text.contains("bench-split"), "{args:?}: {text}"); assert!(text.contains("bench-corporate-actions"), "{args:?}: {text}"); assert!(text.contains("bench-fx"), "{args:?}: {text}"); assert!(text.contains("bench-events"), "{args:?}: {text}"); assert!(text.contains("bench-curate-fx"), "{args:?}: {text}"); } } #[test] fn version_prints_package_name_and_version() { for flag in ["--version", "-V"] { let out = run(&[flag]); assert!(out.status.success()); assert_eq!( stdout(&out).trim(), format!("curate-etl {}", env!("CARGO_PKG_VERSION")) ); } } #[test] fn invalid_invocations_exit_nonzero_with_usage_on_stderr() { for args in [ &["--version", "--offine"][..], &["--help", "--bad"][..], &["bench-split", "abc"][..], &["bench-split", "0"][..], &["bench-split", "-5"][..], &["bench-split", "10", "20"][..], &["bench-corporate-actions", "abc"][..], &["bench-corporate-actions", "0"][..], &["bench-corporate-actions", "-5"][..], &["bench-corporate-actions", "10", "20"][..], &["bench-fx", "abc"][..], &["bench-fx", "0"][..], &["bench-fx", "-5"][..], &["bench-fx", "10", "20"][..], &["bench-events", "abc"][..], &["bench-events", "0"][..], &["bench-events", "-5"][..], &["bench-events", "10", "20"][..], &["bench-curate-fx", "abc"][..], &["bench-curate-fx", "0"][..], &["bench-curate-fx", "-5"][..], &["bench-curate-fx", "10", "20"][..], &["--unknown"][..], &["export"][..], ] { let out = run(args); assert!(!out.status.success(), "{args:?} should fail"); assert_eq!(out.status.code(), Some(1), "{args:?}"); let err = stderr(&out); assert!(err.contains("error:"), "{args:?}: {err}"); assert!(err.contains("Usage:"), "{args:?}: {err}"); assert!(stdout(&out).is_empty(), "{args:?} wrote to stdout"); } } #[test] fn bench_split_small_run_is_headless_deterministic_and_reports_rates() { let a = run(&["bench-split", "1000"]); let b = run(&["bench-split", "1000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); let text = stdout(&a); assert!( text.contains("keys=20000 keys_with_splits=12153 events=18999"), "{text}" ); assert!(text.contains("rows=1000 "), "{text}"); assert!(text.contains("rows/s"), "{text}"); assert!(text.contains("ns/row"), "{text}"); assert!(text.contains("checksum=20285840610.475"), "{text}"); let checksum = |s: &str| { s.split_whitespace() .find(|t| t.starts_with("checksum=")) .map(str::to_owned) }; assert_eq!(checksum(&text), checksum(&stdout(&b))); } #[test] fn bench_corporate_actions_small_run_is_headless_deterministic_and_reports_rates() { let a = run(&["bench-corporate-actions", "1000"]); let b = run(&["bench-corporate-actions", "1000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "unexpected stderr: {}", stderr(&a)); assert!(stderr(&b).is_empty(), "unexpected stderr: {}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!( text_a.contains("curate-etl bench-corporate-actions"), "{text_a}" ); assert!(text_a.contains("events=1000 "), "{text_a}"); assert!(text_a.contains("keys=20000"), "{text_a}"); assert!(text_a.contains("rows_out="), "{text_a}"); assert!(text_a.contains("events/s"), "{text_a}"); assert!(text_a.contains("ns/event"), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); assert!(text_a.contains("stats: split_noops="), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; let checksum_a = field(&text_a, "checksum="); let checksum_b = field(&text_b, "checksum="); assert!(checksum_a.is_some(), "{text_a}"); assert_eq!(checksum_a, checksum_b); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); } #[test] fn bench_fx_small_run_is_headless_deterministic_and_reports_rates() { let a = run(&["bench-fx", "1000"]); let b = run(&["bench-fx", "1000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "unexpected stderr: {}", stderr(&a)); assert!(stderr(&b).is_empty(), "unexpected stderr: {}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!(text_a.contains("curate-etl bench-fx"), "{text_a}"); assert!(text_a.contains("rows=1000 "), "{text_a}"); assert!(text_a.contains("rows_out="), "{text_a}"); assert!(text_a.contains("rows/s"), "{text_a}"); assert!(text_a.contains("lookups=1000000 "), "{text_a}"); assert!(text_a.contains("lookups/s"), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); assert!(text_a.contains("stats: eod_invalid="), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; let checksum_a = field(&text_a, "checksum="); let checksum_b = field(&text_b, "checksum="); assert!(checksum_a.is_some(), "{text_a}"); assert_eq!(checksum_a, checksum_b); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); } #[test] fn bench_events_small_run_is_headless_deterministic_and_reports_rates() { let a = run(&["bench-events", "1000"]); let b = run(&["bench-events", "1000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "unexpected stderr: {}", stderr(&a)); assert!(stderr(&b).is_empty(), "unexpected stderr: {}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!(text_a.contains("curate-etl bench-events"), "{text_a}"); assert!(text_a.contains("rows=1000 "), "{text_a}"); assert!(text_a.contains("rows_out="), "{text_a}"); assert!(text_a.contains("rows/s"), "{text_a}"); assert!(text_a.contains("ns/row"), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); assert!(text_a.contains("stats: malformed_ts="), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; let checksum_a = field(&text_a, "checksum="); let checksum_b = field(&text_b, "checksum="); assert!(checksum_a.is_some(), "{text_a}"); assert_eq!(checksum_a, checksum_b); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); } #[test] fn bench_curate_fx_small_run_is_headless_deterministic_and_reports_rates() { let a = run(&["bench-curate-fx", "2000"]); let b = run(&["bench-curate-fx", "2000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "unexpected stderr: {}", stderr(&a)); assert!(stderr(&b).is_empty(), "unexpected stderr: {}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!(text_a.contains("curate-etl bench-curate-fx"), "{text_a}"); assert!(text_a.contains("rows=2000 "), "{text_a}"); assert!(text_a.contains("stage:"), "{text_a}"); assert!(text_a.contains("parquet:"), "{text_a}"); assert!(text_a.contains("total:"), "{text_a}"); assert!(text_a.contains("overlap:"), "{text_a}"); assert!(text_a.contains("rows/s"), "{text_a}"); assert!(text_a.contains("bytes="), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; let checksum_a = field(&text_a, "checksum="); let checksum_b = field(&text_b, "checksum="); assert!(checksum_a.is_some(), "{text_a}"); assert_eq!(checksum_a, checksum_b); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); assert_eq!(field(&text_a, "bytes="), field(&text_b, "bytes=")); } static CLI_TEST_DIR_SEQ: AtomicU64 = AtomicU64::new(0); struct CliTempDir { path: PathBuf, } impl CliTempDir { fn new(tag: &str) -> Self { let pid = std::process::id(); let seq = CLI_TEST_DIR_SEQ.fetch_add(1, Ordering::Relaxed); let path = std::env::temp_dir().join(format!("curate-etl-cli-{tag}-{pid}-{seq}")); fs::create_dir_all(&path).expect("create cli test temp dir"); Self { path } } } impl Drop for CliTempDir { fn drop(&mut self) { let _ = fs::remove_dir_all(&self.path); } } fn write_file(path: &std::path::Path, bytes: &[u8]) { let mut f = File::create(path).expect("create file"); f.write_all(bytes).expect("write file"); } fn make_eod_doc(code: &str, date: &str, close: f64, volume: f64) -> Vec { let mut doc = RawDocumentBuf::new(); doc.append(cstr!("eodSearchCode"), code); doc.append(cstr!("date"), date); doc.append(cstr!("open"), close); doc.append(cstr!("high"), close + 1.0); doc.append(cstr!("low"), close - 1.0); doc.append(cstr!("close"), close); doc.append(cstr!("volume"), volume); doc.into_bytes() } fn make_split_doc(code: &str, date: &str, ratio: f64) -> Vec { let mut doc = RawDocumentBuf::new(); doc.append(cstr!("qmSearchCode"), code); doc.append(cstr!("date"), date); doc.append(cstr!("splitratio"), ratio); doc.into_bytes() } #[test] fn curate_bars_1d_cli_flag_errors_exit_nonzero_with_usage() { for args in [ &["curate-bars-1d"][..], &[ "curate-bars-1d", "--eod-prices", "e.bson", "--qm-splits", "s.bson", "--provider-mapping", "m.tsv", "--listing-currencies", "c.tsv", ][..], &[ "curate-bars-1d", "--eod-prices", "e.bson", "--eod-prices", "e2.bson", "--qm-splits", "s.bson", "--provider-mapping", "m.tsv", "--listing-currencies", "c.tsv", "--out", "o", ][..], &[ "curate-bars-1d", "--eod-prices", "e.bson", "--qm-splits", "s.bson", "--provider-mapping", "m.tsv", "--listing-currencies", "c.tsv", "--out", "o", "--bogus", "1", ][..], &[ "curate-bars-1d", "--eod-prices", "e.bson", "--qm-splits", "s.bson", "--provider-mapping", "m.tsv", "--listing-currencies", "c.tsv", "--out", "o", "--rel-tolerance", "not-a-number", ][..], ] { let out = run(args); assert!(!out.status.success(), "{args:?} should fail"); assert_eq!(out.status.code(), Some(1), "{args:?}"); assert!(stdout(&out).is_empty(), "{args:?} wrote to stdout"); let err = stderr(&out); assert!(err.contains("error:"), "{args:?}: {err}"); assert!(err.contains("Usage:"), "{args:?}: {err}"); } } #[test] fn curate_bars_1d_cli_happy_path_and_determinism_with_shuffled_bson() { let dir = CliTempDir::new("happy"); let eod_a = dir.path.join("eod_a.bson"); let eod_b = dir.path.join("eod_b.bson"); let splits_a = dir.path.join("splits_a.bson"); let splits_b = dir.path.join("splits_b.bson"); let map_path = dir.path.join("provider_mapping.tsv"); let curr_path = dir.path.join("listing_currencies.tsv"); let out_a = dir.path.join("out_a"); let out_b = dir.path.join("out_b"); write_file( &map_path, b"eod\tNVDA.US\t2010-01-01\t\t100\nqm\tNVDA:US\t2010-01-01\t\t100\n", ); write_file(&curr_path, b"100\tUSD\n"); let d1 = make_eod_doc("NVDA.US", "2024-06-07", 1208.88, 410_000_000.0); let d2 = make_eod_doc("NVDA.US", "2024-06-10", 121.79, 310_000_000.0); write_file(&eod_a, &[d1.as_slice(), d2.as_slice()].concat()); write_file(&eod_b, &[d2.as_slice(), d1.as_slice()].concat()); let s1 = make_split_doc("NVDA:US", "2024-06-10", 10.0); write_file(&splits_a, &s1); write_file(&splits_b, &s1); let run_a = run(&[ "curate-bars-1d", "--eod-prices", eod_a.to_str().unwrap(), "--qm-splits", splits_a.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", out_a.to_str().unwrap(), ]); assert!(run_a.status.success(), "{}", stderr(&run_a)); assert!(stderr(&run_a).is_empty(), "{}", stderr(&run_a)); let run_b = run(&[ "curate-bars-1d", "--eod-prices", eod_b.to_str().unwrap(), "--qm-splits", splits_b.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", out_b.to_str().unwrap(), ]); assert!(run_b.status.success(), "{}", stderr(&run_b)); assert!(stderr(&run_b).is_empty(), "{}", stderr(&run_b)); let strip_timing = |s: &str| { s.lines() .filter(|l| !l.trim_start().starts_with("timing:")) .collect::>() .join("\n") }; let text_a = stdout(&run_a); let text_b = stdout(&run_b); assert!(text_a.contains("curate-etl curate-bars-1d"), "{text_a}"); assert!(text_a.contains("rows_written=2"), "{text_a}"); assert!(text_a.contains("2024=2"), "{text_a}"); assert_eq!(strip_timing(&text_a), strip_timing(&text_b)); let pq_a = fs::read(out_a.join("bars_1d/year=2024/part.parquet")).unwrap(); let pq_b = fs::read(out_b.join("bars_1d/year=2024/part.parquet")).unwrap(); assert_eq!(pq_a, pq_b); assert!(text_a.contains("qm_check: qm_prices=absent"), "{text_a}"); } fn make_qm_price_doc(code: &str, date: &str, share_volume: f64) -> Vec { let mut doc = RawDocumentBuf::new(); doc.append(cstr!("qmSearchCode"), code); doc.append(cstr!("date"), date); doc.append(cstr!("sharevolume"), share_volume); doc.into_bytes() } #[test] fn curate_bars_1d_cli_qm_prices_cross_check_and_framing_failures() { let dir = CliTempDir::new("qm-prices-cli"); let eod_path = dir.path.join("eod.bson"); let splits_path = dir.path.join("splits.bson"); let qm_path = dir.path.join("qm.bson"); let map_path = dir.path.join("map.tsv"); let curr_path = dir.path.join("curr.tsv"); write_file( &map_path, b"eod\tNVDA.US\t2020-01-01\t\t1\nqm\tNVDA:US\t2020-01-01\t\t1\n", ); write_file(&curr_path, b"1\tUSD\n"); write_file(&splits_path, &make_split_doc("NVDA:US", "2024-06-10", 10.0)); let d1 = make_eod_doc("NVDA.US", "2024-06-07", 1208.88, 412_390_000.0); let d2 = make_eod_doc("NVDA.US", "2024-06-10", 121.79, 310_000_000.0); write_file(&eod_path, &[d1.as_slice(), d2.as_slice()].concat()); // 2024-06-07 matches (41_239_000), 2024-06-10 has an injected >1% mismatch (300_000_000 vs 310_000_000). let q1 = make_qm_price_doc("NVDA:US", "2024-06-07", 41_239_000.0); let q2 = make_qm_price_doc("NVDA:US", "2024-06-10", 300_000_000.0); write_file(&qm_path, &[q1.as_slice(), q2.as_slice()].concat()); let out_no_qm = dir.path.join("out_no_qm"); let out_with_qm = dir.path.join("out_with_qm"); let out_eq_qm = dir.path.join("out_eq_qm"); let run_no_qm = run(&[ "curate-bars-1d", "--eod-prices", eod_path.to_str().unwrap(), "--qm-splits", splits_path.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", out_no_qm.to_str().unwrap(), ]); assert!(run_no_qm.status.success(), "{}", stderr(&run_no_qm)); assert!(stdout(&run_no_qm).contains("qm_check: qm_prices=absent")); // Mismatch still exits 0 and emits byte-identical Parquet. let run_with_qm = run(&[ "curate-bars-1d", "--eod-prices", eod_path.to_str().unwrap(), "--qm-splits", splits_path.to_str().unwrap(), "--qm-prices", qm_path.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", out_with_qm.to_str().unwrap(), ]); assert!(run_with_qm.status.success(), "{}", stderr(&run_with_qm)); assert!(stderr(&run_with_qm).is_empty(), "{}", stderr(&run_with_qm)); let out_text = stdout(&run_with_qm); assert!(out_text.contains("qm_bson:"), "{out_text}"); assert!(out_text.contains("qm_rekey:"), "{out_text}"); assert!( out_text.contains("volume_checked=2 volume_within_tolerance=1 volume_mismatched=1 volume_unchecked=0"), "{out_text}" ); assert!( out_text.contains("qm_mismatch: key=NVDA.US session_date=2024-06-10"), "{out_text}" ); let pq_no_qm = fs::read(out_no_qm.join("bars_1d/year=2024/part.parquet")).unwrap(); let pq_with_qm = fs::read(out_with_qm.join("bars_1d/year=2024/part.parquet")).unwrap(); assert_eq!(pq_no_qm, pq_with_qm); // Equals syntax (`--qm-prices=`) via the documented `--curate-inputs ` // directory + `current` symlink also succeeds, prints identical check counters/mismatches, // and emits byte-identical Parquet. let curate_inputs_dir = dir.path.join("curate_inputs"); let gen_dir = curate_inputs_dir.join("gen-001"); fs::create_dir_all(&gen_dir).unwrap(); write_file( &gen_dir.join("provider_mapping.tsv"), b"eod\tNVDA.US\t2020-01-01\t\t1\nqm\tNVDA:US\t2020-01-01\t\t1\n", ); write_file(&gen_dir.join("listing_currencies.tsv"), b"1\tUSD\n"); #[cfg(unix)] std::os::unix::fs::symlink(&gen_dir, curate_inputs_dir.join("current")).unwrap(); #[cfg(not(unix))] { fs::create_dir_all(curate_inputs_dir.join("current")).unwrap(); write_file( &curate_inputs_dir.join("current/provider_mapping.tsv"), b"eod\tNVDA.US\t2020-01-01\t\t1\nqm\tNVDA:US\t2020-01-01\t\t1\n", ); write_file( &curate_inputs_dir.join("current/listing_currencies.tsv"), b"1\tUSD\n", ); } let eq_flag = format!("--qm-prices={}", qm_path.to_str().unwrap()); let run_eq = run(&[ "curate-bars-1d", "--eod-prices", eod_path.to_str().unwrap(), "--qm-splits", splits_path.to_str().unwrap(), &eq_flag, "--curate-inputs", curate_inputs_dir.to_str().unwrap(), "--out", out_eq_qm.to_str().unwrap(), ]); assert!(run_eq.status.success(), "{}", stderr(&run_eq)); assert!(stderr(&run_eq).is_empty(), "{}", stderr(&run_eq)); let eq_out_text = stdout(&run_eq); assert!(eq_out_text.contains("qm_bson:"), "{eq_out_text}"); assert!(eq_out_text.contains("qm_rekey:"), "{eq_out_text}"); assert!( eq_out_text.contains( "volume_checked=2 volume_within_tolerance=1 volume_mismatched=1 volume_unchecked=0" ), "{eq_out_text}" ); assert!( eq_out_text.contains("qm_mismatch: key=NVDA.US session_date=2024-06-10"), "{eq_out_text}" ); assert_eq!( fs::read(out_eq_qm.join("bars_1d/year=2024/part.parquet")).unwrap(), pq_no_qm ); // Truncated --qm-prices fails closed with single-line stderr naming path, publishes nothing, // and preserves a pre-existing dataset. let trunc_qm = dir.path.join("trunc_qm.bson"); let mut trunc_bytes = q1.clone(); trunc_bytes.truncate(trunc_bytes.len() - 4); write_file(&trunc_qm, &trunc_bytes); let out_trunc_fresh = dir.path.join("out_trunc_fresh"); let res_trunc = run(&[ "curate-bars-1d", "--eod-prices", eod_path.to_str().unwrap(), "--qm-splits", splits_path.to_str().unwrap(), "--qm-prices", trunc_qm.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", out_trunc_fresh.to_str().unwrap(), ]); assert!(!res_trunc.status.success()); assert!(stdout(&res_trunc).is_empty()); let err_trunc = stderr(&res_trunc); assert_eq!(err_trunc.lines().count(), 1, "{err_trunc}"); assert!(err_trunc.contains("trunc_qm.bson"), "{err_trunc}"); assert!(!out_trunc_fresh.join("bars_1d").exists()); // Pre-existing published output is untouched on failure. let res_preserve = run(&[ "curate-bars-1d", "--eod-prices", eod_path.to_str().unwrap(), "--qm-splits", splits_path.to_str().unwrap(), "--qm-prices", trunc_qm.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", out_with_qm.to_str().unwrap(), ]); assert!(!res_preserve.status.success()); assert_eq!( fs::read(out_with_qm.join("bars_1d/year=2024/part.parquet")).unwrap(), pq_with_qm ); // Missing --qm-prices file fails closed. let missing_qm = dir.path.join("missing_qm.bson"); let res_missing = run(&[ "curate-bars-1d", "--eod-prices", eod_path.to_str().unwrap(), "--qm-splits", splits_path.to_str().unwrap(), "--qm-prices", missing_qm.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", dir.path.join("out_missing").to_str().unwrap(), ]); assert!(!res_missing.status.success()); let err_missing = stderr(&res_missing); assert_eq!(err_missing.lines().count(), 1, "{err_missing}"); assert!(err_missing.contains("missing_qm.bson"), "{err_missing}"); } #[test] fn curate_bars_1d_cli_runtime_failures_single_line_stderr_and_no_partial_dataset() { let dir = CliTempDir::new("fail"); let eod_ok = dir.path.join("eod.bson"); let splits_ok = dir.path.join("splits.bson"); let map_ok = dir.path.join("map.tsv"); let curr_ok = dir.path.join("curr.tsv"); write_file(&eod_ok, &make_eod_doc("NVDA.US", "2024-06-07", 100.0, 1000.0)); write_file(&splits_ok, b""); write_file(&map_ok, b"eod\tNVDA.US\t2020-01-01\t\t1\n"); write_file(&curr_ok, b"1\tUSD\n"); // 1. Malformed TSV line -> names path and 1-based line number, single-line stderr, no bars_1d. let bad_map = dir.path.join("bad_map.tsv"); write_file(&bad_map, b"eod\tNVDA.US\t2020-01-01\t\t1\nbad_line\n"); let out1 = dir.path.join("out1"); let res = run(&[ "curate-bars-1d", "--eod-prices", eod_ok.to_str().unwrap(), "--qm-splits", splits_ok.to_str().unwrap(), "--provider-mapping", bad_map.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", out1.to_str().unwrap(), ]); assert!(!res.status.success()); assert!(stdout(&res).is_empty()); let err = stderr(&res); assert_eq!(err.lines().count(), 1, "expected single-line stderr: {err}"); assert!(err.contains("bad_map.tsv:2:"), "{err}"); assert!(!out1.join("bars_1d").exists()); // 2. Truncated BSON -> single-line stderr naming path, no bars_1d. let trunc_eod = dir.path.join("trunc.bson"); let mut doc = make_eod_doc("NVDA.US", "2024-06-07", 100.0, 1000.0); doc.truncate(doc.len() - 4); write_file(&trunc_eod, &doc); let out2 = dir.path.join("out2"); let res2 = run(&[ "curate-bars-1d", "--eod-prices", trunc_eod.to_str().unwrap(), "--qm-splits", splits_ok.to_str().unwrap(), "--provider-mapping", map_ok.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", out2.to_str().unwrap(), ]); assert!(!res2.status.success()); assert!(stdout(&res2).is_empty()); let err2 = stderr(&res2); assert_eq!(err2.lines().count(), 1, "expected single-line stderr: {err2}"); assert!(err2.contains("trunc.bson"), "{err2}"); assert!(!out2.join("bars_1d").exists()); // 3. Missing input file -> single-line stderr naming path, no bars_1d. let missing_eod = dir.path.join("missing_eod.bson"); let out3 = dir.path.join("out3"); let res3 = run(&[ "curate-bars-1d", "--eod-prices", missing_eod.to_str().unwrap(), "--qm-splits", splits_ok.to_str().unwrap(), "--provider-mapping", map_ok.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", out3.to_str().unwrap(), ]); assert!(!res3.status.success()); assert!(stdout(&res3).is_empty()); let err3 = stderr(&res3); assert_eq!(err3.lines().count(), 1, "expected single-line stderr: {err3}"); assert!(err3.contains("missing_eod.bson"), "{err3}"); assert!(!out3.join("bars_1d").exists()); // 4. Oversized BSON document (> DEFAULT_MAX_DOC_BYTES) -> single-line stderr naming path, no bars_1d. let huge_eod = dir.path.join("huge_eod.bson"); let huge_len = (curate_etl::bson_stream::DEFAULT_MAX_DOC_BYTES as i32) + 1024; let mut huge_bytes = Vec::from(huge_len.to_le_bytes()); huge_bytes.push(0); write_file(&huge_eod, &huge_bytes); let out4 = dir.path.join("out4"); let res4 = run(&[ "curate-bars-1d", "--eod-prices", huge_eod.to_str().unwrap(), "--qm-splits", splits_ok.to_str().unwrap(), "--provider-mapping", map_ok.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", out4.to_str().unwrap(), ]); assert!(!res4.status.success()); assert!(stdout(&res4).is_empty()); let err4 = stderr(&res4); assert_eq!(err4.lines().count(), 1, "expected single-line stderr: {err4}"); assert!(err4.contains("huge_eod.bson"), "{err4}"); assert!(!out4.join("bars_1d").exists()); // 5. Overlapping mapping ranges -> single-line stderr naming path, no bars_1d. let overlap_map = dir.path.join("overlap_map.tsv"); write_file( &overlap_map, b"eod\tNVDA.US\t2020-01-01\t2022-12-31\t1\neod\tNVDA.US\t2022-06-01\t2024-12-31\t2\n", ); let out5 = dir.path.join("out5"); let res5 = run(&[ "curate-bars-1d", "--eod-prices", eod_ok.to_str().unwrap(), "--qm-splits", splits_ok.to_str().unwrap(), "--provider-mapping", overlap_map.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", out5.to_str().unwrap(), ]); assert!(!res5.status.success()); assert!(stdout(&res5).is_empty()); let err5 = stderr(&res5); assert_eq!(err5.lines().count(), 1, "expected single-line stderr: {err5}"); assert!(err5.contains("overlap_map.tsv"), "{err5}"); assert!(!out5.join("bars_1d").exists()); // 6. Conflicting cross-key QM alias splits -> single-line stderr naming path, no bars_1d. let alias_map = dir.path.join("alias_map.tsv"); write_file( &alias_map, b"eod\tNVDA.US\t2020-01-01\t\t1\nqm\tNVDA:US\t2020-01-01\t\t1\nqm\tNVDA_ALT:US\t2020-01-01\t\t1\n", ); let conflict_splits = dir.path.join("conflict_splits.bson"); let c1 = make_split_doc("NVDA:US", "2024-06-10", 10.0); let c2 = make_split_doc("NVDA_ALT:US", "2024-06-10", 4.0); write_file(&conflict_splits, &[c1.as_slice(), c2.as_slice()].concat()); let out6 = dir.path.join("out6"); let res6 = run(&[ "curate-bars-1d", "--eod-prices", eod_ok.to_str().unwrap(), "--qm-splits", conflict_splits.to_str().unwrap(), "--provider-mapping", alias_map.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", out6.to_str().unwrap(), ]); assert!(!res6.status.success()); assert!(stdout(&res6).is_empty()); let err6 = stderr(&res6); assert_eq!(err6.lines().count(), 1, "expected single-line stderr: {err6}"); assert!(err6.contains("conflict_splits.bson"), "{err6}"); assert!(!out6.join("bars_1d").exists()); // 7. Output write failure reaching the Parquet writer: `--out` is a valid directory, but // `/bars_1d` is occupied by a regular file. let out7 = dir.path.join("out7_obstructed"); fs::create_dir_all(&out7).expect("create out7 dir"); let bars_1d_file = out7.join("bars_1d"); write_file(&bars_1d_file, b"sentinel"); let res7 = run(&[ "curate-bars-1d", "--eod-prices", eod_ok.to_str().unwrap(), "--qm-splits", splits_ok.to_str().unwrap(), "--provider-mapping", map_ok.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", out7.to_str().unwrap(), ]); assert!(!res7.status.success()); assert!(stdout(&res7).is_empty()); let err7 = stderr(&res7); assert_eq!(err7.lines().count(), 1, "expected single-line stderr: {err7}"); assert!(err7.contains("out7_obstructed"), "{err7}"); assert!(bars_1d_file.is_file()); assert_eq!(fs::read(&bars_1d_file).unwrap(), b"sentinel"); assert!(!out7.join("bars_1d/year=2024/part.parquet").exists()); } #[test] fn curate_bars_1d_cli_rejects_following_option_token_as_out_value_without_writing() { let dir = CliTempDir::new("missing-out-opt"); let eod_ok = dir.path.join("eod.bson"); let splits_ok = dir.path.join("splits.bson"); let map_ok = dir.path.join("map.tsv"); let curr_ok = dir.path.join("curr.tsv"); write_file(&eod_ok, &make_eod_doc("NVDA.US", "2024-06-07", 100.0, 1000.0)); write_file(&splits_ok, b""); write_file(&map_ok, b"eod\tNVDA.US\t2020-01-01\t\t1\n"); write_file(&curr_ok, b"1\tUSD\n"); let res = Command::new(env!("CARGO_BIN_EXE_curate-etl")) .current_dir(&dir.path) .args([ "curate-bars-1d", "--eod-prices", eod_ok.to_str().unwrap(), "--qm-splits", splits_ok.to_str().unwrap(), "--provider-mapping", map_ok.to_str().unwrap(), "--listing-currencies", curr_ok.to_str().unwrap(), "--out", "--rel-tolerence=0.01", ]) .output() .expect("spawn curate-etl binary"); assert!(!res.status.success()); assert_eq!(res.status.code(), Some(1)); assert!(stdout(&res).is_empty()); let err = stderr(&res); assert!(err.contains("missing value for flag '--out'"), "{err}"); assert!(err.contains("Usage:"), "{err}"); assert!( !dir.path.join("--rel-tolerence=0.01").exists(), "must not create output directory named after following option token" ); } #[test] fn bench_curate_bars_cmd_small_run_succeeds_and_reports_splits() { let out = run(&["bench-curate-bars-cmd", "2000"]); assert!(out.status.success(), "{}", stderr(&out)); assert!(stderr(&out).is_empty(), "{}", stderr(&out)); let text = stdout(&out); assert!(text.contains("curate-etl bench-curate-bars-cmd"), "{text}"); assert!(text.contains("rows=2000 "), "{text}"); assert!(text.contains("qm_docs="), "{text}"); assert!(text.contains("decode:"), "{text}"); assert!(text.contains("qm:"), "{text}"); assert!(text.contains("injected="), "{text}"); assert!(text.contains("curate:"), "{text}"); assert!(text.contains("write:"), "{text}"); assert!(text.contains("total:"), "{text}"); assert!(text.contains("bytes/bar"), "{text}"); } fn make_div_doc(code: &str, date: &str, cash: f64, currency: &str) -> Vec { let mut doc = RawDocumentBuf::new(); doc.append(cstr!("qmSearchCode"), code); doc.append(cstr!("date"), date); doc.append(cstr!("amount"), cash); doc.append(cstr!("currency"), currency); doc.append(cstr!("usdFxRate"), 1_i32); doc.into_bytes() } #[test] fn curate_adj_factors_cli_flag_errors_exit_nonzero_with_usage() { for args in [ &["curate-adj-factors"][..], &[ "curate-adj-factors", "--qm-splits", "s.bson", "--qm-dividends", "d.bson", "--provider-mapping", "m.tsv", "--bars-1d", "bars", ][..], &[ "curate-adj-factors", "--qm-splits", "s.bson", "--qm-splits", "s2.bson", "--qm-dividends", "d.bson", "--provider-mapping", "m.tsv", "--bars-1d", "bars", "--out", "out", ][..], &[ "curate-adj-factors", "--qm-splits", "s.bson", "--qm-dividends", "d.bson", "--provider-mapping", "m.tsv", "--bars-1d", "bars", "--out", "out", "--unknown", "1", ][..], ] { let out = run(args); assert!(!out.status.success(), "{args:?} should fail"); assert_eq!(out.status.code(), Some(1), "{args:?}"); assert!(stdout(&out).is_empty(), "{args:?} wrote to stdout"); let err = stderr(&out); assert!(err.contains("error:"), "{args:?}: {err}"); assert!(err.contains("Usage:"), "{args:?}: {err}"); } } #[test] fn curate_adj_factors_cli_happy_path_publishes_verified_snapshot() { let dir = CliTempDir::new("adj-happy"); let eod_path = dir.path.join("eod.bson"); let splits_path = dir.path.join("splits.bson"); let divs_path = dir.path.join("divs.bson"); let map_path = dir.path.join("provider_mapping.tsv"); let curr_path = dir.path.join("listing_currencies.tsv"); let bars_out = dir.path.join("bars_out"); let adj_out = dir.path.join("adj_out"); write_file( &map_path, b"eod\tNVDA.US\t2015-01-01\t\t100\nqm\tNVDA:US\t2015-01-01\t\t100\n", ); write_file(&curr_path, b"100\tUSD\n"); write_file( &eod_path, &[ make_eod_doc("NVDA.US", "2024-06-07", 1208.88, 410_000_000.0).as_slice(), make_eod_doc("NVDA.US", "2024-06-10", 121.79, 310_000_000.0).as_slice(), ] .concat(), ); write_file(&splits_path, &make_split_doc("NVDA:US", "2024-06-10", 10.0)); write_file( &divs_path, &make_div_doc("NVDA:US", "2024-06-11", 0.01, "USD"), ); // First produce curated bars_1d via curate-bars-1d. let bars_res = run(&[ "curate-bars-1d", "--eod-prices", eod_path.to_str().unwrap(), "--qm-splits", splits_path.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--listing-currencies", curr_path.to_str().unwrap(), "--out", bars_out.to_str().unwrap(), ]); assert!(bars_res.status.success(), "{}", stderr(&bars_res)); // Then run curate-adj-factors against that bars_1d dataset. let adj_res = run(&[ "curate-adj-factors", "--qm-splits", splits_path.to_str().unwrap(), "--qm-dividends", divs_path.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--bars-1d", bars_out.join("bars_1d").to_str().unwrap(), "--out", adj_out.to_str().unwrap(), ]); assert!(adj_res.status.success(), "{}", stderr(&adj_res)); assert!(stderr(&adj_res).is_empty(), "{}", stderr(&adj_res)); let text = stdout(&adj_res); assert!(text.contains("curate-etl curate-adj-factors"), "{text}"); assert!(text.contains("with_prev_close=1"), "{text}"); assert!(text.contains("rows_written=2"), "{text}"); assert!(adj_out.join("adj_factors/part.parquet").is_file()); } #[test] fn bench_curate_adj_cmd_small_run_is_deterministic_and_reports_stages() { let a = run(&["bench-curate-adj-cmd", "2000"]); let b = run(&["bench-curate-adj-cmd", "2000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "{}", stderr(&a)); assert!(stderr(&b).is_empty(), "{}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!(text_a.contains("curate-etl bench-curate-adj-cmd"), "{text_a}"); assert!(text_a.contains("events=2000 "), "{text_a}"); assert!(text_a.contains("bars_load:"), "{text_a}"); assert!(text_a.contains("decode:"), "{text_a}"); assert!(text_a.contains("curate:"), "{text_a}"); assert!(text_a.contains("write:"), "{text_a}"); assert!(text_a.contains("total:"), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; assert_eq!(field(&text_a, "checksum="), field(&text_b, "checksum=")); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); } #[test] fn curate_corporate_actions_cli_flag_errors_exit_nonzero_with_usage() { for args in [ &["curate-corporate-actions"][..], &[ "curate-corporate-actions", "--qm-splits", "s.bson", "--qm-dividends", "d.bson", "--provider-mapping", "m.tsv", ][..], &[ "curate-corporate-actions", "--qm-splits", "s.bson", "--qm-splits", "s2.bson", "--qm-dividends", "d.bson", "--provider-mapping", "m.tsv", "--out", "out", ][..], &[ "curate-corporate-actions", "--qm-splits", "s.bson", "--qm-dividends", "d.bson", "--provider-mapping", "m.tsv", "--delistings", "a.tsv", "--delistings=b.tsv", "--out", "out", ][..], &[ "curate-corporate-actions", "--qm-splits", "s.bson", "--qm-dividends", "d.bson", "--provider-mapping", "m.tsv", "--out", "out", "--unknown", "1", ][..], ] { let out = run(args); assert!(!out.status.success(), "{args:?} should fail"); assert_eq!(out.status.code(), Some(1), "{args:?}"); assert!(stdout(&out).is_empty(), "{args:?} wrote to stdout"); let err = stderr(&out); assert!(err.contains("error:"), "{args:?}: {err}"); assert!(err.contains("Usage:"), "{args:?}: {err}"); } } #[test] fn curate_corporate_actions_cli_happy_path_and_single_line_runtime_error() { let dir = CliTempDir::new("corp-actions-cli"); let splits_path = dir.path.join("splits.bson"); let divs_path = dir.path.join("divs.bson"); let map_path = dir.path.join("provider_mapping.tsv"); let delist_path = dir.path.join("delistings.tsv"); let bad_delist_path = dir.path.join("bad_delistings.tsv"); let out_with_delist = dir.path.join("out_with"); let out_without_delist = dir.path.join("out_without"); let out_fail = dir.path.join("out_fail"); write_file( &map_path, b"qm\tNVDA:US\t2015-01-01\t\t100\n\ qm\tSIVB:US\t2015-01-01\t2023-03-10\t300\n\ qm\tSIVBQ:US\t2023-03-28\t\t301\n", ); write_file(&splits_path, &make_split_doc("NVDA:US", "2024-06-10", 10.0)); write_file( &divs_path, &make_div_doc("NVDA:US", "2024-06-11", 0.01, "USD"), ); write_file(&delist_path, b"SIVB:US\t2023-03-28\t300\tSIVBQ:US\t\n"); write_file(&bad_delist_path, b"SIVB:US\t2023-03-28\t\tSIVBQ:US\t\n"); // 1. Happy path with --delistings. let res = run(&[ "curate-corporate-actions", "--qm-splits", splits_path.to_str().unwrap(), "--qm-dividends", divs_path.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--delistings", delist_path.to_str().unwrap(), "--out", out_with_delist.to_str().unwrap(), ]); assert!(res.status.success(), "{}", stderr(&res)); assert!(stderr(&res).is_empty(), "{}", stderr(&res)); let text = stdout(&res); assert!(text.contains("curate-etl curate-corporate-actions"), "{text}"); assert!(text.contains("rows_loaded=1"), "{text}"); assert!(text.contains("rows_written=3"), "{text}"); assert!(out_with_delist.join("corporate_actions/part.parquet").is_file()); // 2. Happy path without --delistings. let res_no = run(&[ "curate-corporate-actions", "--qm-splits", splits_path.to_str().unwrap(), "--qm-dividends", divs_path.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--out", out_without_delist.to_str().unwrap(), ]); assert!(res_no.status.success(), "{}", stderr(&res_no)); assert!(stderr(&res_no).is_empty(), "{}", stderr(&res_no)); let text_no = stdout(&res_no); assert!(text_no.contains("rows_loaded=0"), "{text_no}"); assert!(text_no.contains("rows_written=2"), "{text_no}"); assert!(out_without_delist .join("corporate_actions/part.parquet") .is_file()); // 3. Runtime error (malformed delistings TSV) -> single-line stderr, no Usage, no output. let res_fail = run(&[ "curate-corporate-actions", "--qm-splits", splits_path.to_str().unwrap(), "--qm-dividends", divs_path.to_str().unwrap(), "--provider-mapping", map_path.to_str().unwrap(), "--delistings", bad_delist_path.to_str().unwrap(), "--out", out_fail.to_str().unwrap(), ]); assert!(!res_fail.status.success()); assert_eq!(res_fail.status.code(), Some(1)); assert!(stdout(&res_fail).is_empty()); let err = stderr(&res_fail); assert_eq!(err.lines().count(), 1, "expected single-line stderr: {err}"); assert!(err.contains("bad_delistings.tsv:1:"), "{err}"); assert!(!err.contains("Usage:"), "{err}"); assert!(!out_fail.join("corporate_actions").exists()); } #[test] fn bench_curate_corporate_actions_cmd_small_run_is_deterministic_and_reports_stages() { let a = run(&["bench-curate-corporate-actions-cmd", "2000"]); let b = run(&["bench-curate-corporate-actions-cmd", "2000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "{}", stderr(&a)); assert!(stderr(&b).is_empty(), "{}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!( text_a.contains("curate-etl bench-curate-corporate-actions-cmd"), "{text_a}" ); assert!(text_a.contains("events=2000 "), "{text_a}"); assert!(text_a.contains("decode:"), "{text_a}"); assert!(text_a.contains("curate:"), "{text_a}"); assert!(text_a.contains("write:"), "{text_a}"); assert!(text_a.contains("total:"), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; assert_eq!(field(&text_a, "checksum="), field(&text_b, "checksum=")); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); } #[test] fn curate_fx_1d_cli_flag_errors_exit_nonzero_with_usage() { for args in [ &["curate-fx-1d"][..], &[ "curate-fx-1d", "--eod-prices", "e.bson", "--te-market-data", "t.bson", ][..], &[ "curate-fx-1d", "--eod-prices", "e.bson", "--te-market-data", "t.bson", "--te-symbol", "USDCAD", "--te-symbol=USDCAD", "--out", "out", ][..], &[ "curate-fx-1d", "--eod-prices", "e.bson", "--te-market-data", "t.bson", "--te-symbol", "EURUSD", "--out", "out", ][..], &[ "curate-fx-1d", "--eod-prices", "e.bson", "--te-market-data", "t.bson", "--out", "out", "--unknown", "1", ][..], ] { let out = run(args); assert!(!out.status.success(), "{args:?} should fail"); assert_eq!(out.status.code(), Some(1), "{args:?}"); assert!(stdout(&out).is_empty(), "{args:?} wrote to stdout"); let err = stderr(&out); assert!(err.contains("error:"), "{args:?}: {err}"); assert!(err.contains("Usage:"), "{args:?}: {err}"); assert!(err.contains("curate-fx-1d"), "{args:?}: {err}"); } } #[test] fn curate_fx_1d_cli_happy_path_and_single_line_runtime_error() { use bson::raw::{cstr, RawArrayBuf, RawDocumentBuf}; let dir = CliTempDir::new("fx-1d-cli"); let eod_path = dir.path.join("eodPrices.bson"); let te_path = dir.path.join("teMarketData.bson"); let empty_eod_path = dir.path.join("empty_eod.bson"); let empty_te_path = dir.path.join("empty_te.bson"); let out_dir = dir.path.join("out"); let out_fail = dir.path.join("out_fail"); let mut eod_doc = RawDocumentBuf::new(); eod_doc.append(cstr!("eodSearchCode"), "CAD.FOREX"); eod_doc.append(cstr!("date"), "2025-01-02"); eod_doc.append(cstr!("open"), 1.430_f64); eod_doc.append(cstr!("high"), 1.440_f64); eod_doc.append(cstr!("low"), 1.425_f64); eod_doc.append(cstr!("close"), 1.435_f64); eod_doc.append(cstr!("volume"), 0.0_f64); write_file(&eod_path, eod_doc.as_bytes()); let mut daily = RawArrayBuf::new(); let mut pt = RawDocumentBuf::new(); pt.append(cstr!("t"), 1_767_312_000_000_i64); // 2026-01-02 UTC in ms pt.append(cstr!("o"), 1.380_f64); pt.append(cstr!("h"), 1.390_f64); pt.append(cstr!("l"), 1.375_f64); pt.append(cstr!("c"), 1.385_f64); daily.push(pt); let mut te_doc = RawDocumentBuf::new(); te_doc.append(cstr!("teSymbol"), "USDCAD"); te_doc.append(cstr!("daily"), daily); write_file(&te_path, te_doc.as_bytes()); write_file(&empty_eod_path, b""); write_file(&empty_te_path, b""); // 1. Happy path let res = run(&[ "curate-fx-1d", "--eod-prices", eod_path.to_str().unwrap(), "--te-market-data", te_path.to_str().unwrap(), "--out", out_dir.to_str().unwrap(), ]); assert!(res.status.success(), "{}", stderr(&res)); assert!(stderr(&res).is_empty(), "{}", stderr(&res)); let text = stdout(&res); assert!(text.contains("curate-etl curate-fx-1d"), "{text}"); assert!(text.contains("rows_written=2"), "{text}"); assert!(out_dir.join("fx_1d/part.parquet").is_file()); // 2. Runtime error (zero decoded bars) -> single-line stderr, no Usage, no output let res_fail = run(&[ "curate-fx-1d", "--eod-prices", empty_eod_path.to_str().unwrap(), "--te-market-data", empty_te_path.to_str().unwrap(), "--out", out_fail.to_str().unwrap(), ]); assert!(!res_fail.status.success()); assert_eq!(res_fail.status.code(), Some(1)); assert!(stdout(&res_fail).is_empty()); let err = stderr(&res_fail); assert_eq!(err.lines().count(), 1, "expected single-line stderr: {err}"); assert!(err.contains("no FX bars decoded"), "{err}"); assert!(!err.contains("Usage:"), "{err}"); assert!(!out_fail.join("fx_1d").exists()); } #[test] fn bench_curate_fx_cmd_small_run_is_deterministic_and_reports_stages() { let a = run(&["bench-curate-fx-cmd", "1000"]); let b = run(&["bench-curate-fx-cmd", "1000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "{}", stderr(&a)); assert!(stderr(&b).is_empty(), "{}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!(text_a.contains("curate-etl bench-curate-fx-cmd"), "{text_a}"); assert!(text_a.contains("rows=1000 "), "{text_a}"); assert!(text_a.contains("decode:"), "{text_a}"); assert!(text_a.contains("curate:"), "{text_a}"); assert!(text_a.contains("write:"), "{text_a}"); assert!(text_a.contains("total:"), "{text_a}"); assert!(text_a.contains("overlap:"), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; assert_eq!(field(&text_a, "checksum="), field(&text_b, "checksum=")); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); } #[test] fn curate_shorts_usage_errors_exit_nonzero_with_usage_on_stderr() { let cases: &[(&[&str], &str)] = &[ // Missing --shares-short-field ( &[ "curate-shorts", "--ceo-shorts", "s.bson", "--provider-mapping", "m.tsv", "--out", "out", ], "--shares-short-field", ), // Duplicate flag ( &[ "curate-shorts", "--ceo-shorts", "s.bson", "--ceo-shorts", "s2.bson", "--provider-mapping", "m.tsv", "--out", "out", "--shares-short-field", "sharesShort", ], "duplicate flag", ), // Unknown flag ( &[ "curate-shorts", "--ceo-shorts", "s.bson", "--provider-mapping", "m.tsv", "--out", "out", "--shares-short-field", "sharesShort", "--unknown-flag", "x", ], "unknown flag", ), // Empty field name ( &[ "curate-shorts", "--ceo-shorts", "s.bson", "--provider-mapping", "m.tsv", "--out", "out", "--shares-short-field=", ], "empty value", ), // Two roles sharing one field name (collision with default 'timestamp') ( &[ "curate-shorts", "--ceo-shorts", "s.bson", "--provider-mapping", "m.tsv", "--out", "out", "--shares-short-field", "timestamp", ], "cannot share BSON field", ), ]; for &(args, expected_substr) in cases { let out = run(args); assert!(!out.status.success(), "{args:?} should fail"); assert_eq!(out.status.code(), Some(1), "{args:?}"); assert!(stdout(&out).is_empty(), "{args:?} wrote to stdout"); let err = stderr(&out); assert!(err.contains("error:"), "{args:?}: {err}"); assert!(err.contains(expected_substr), "{args:?}: {err}"); assert!(err.contains("Usage:"), "{args:?}: {err}"); } } #[test] fn bench_curate_shorts_command_small_run_is_deterministic_and_reports_stages() { let a = run(&["bench-curate-shorts-command", "2000"]); let b = run(&["bench-curate-shorts-command", "2000"]); assert!(a.status.success(), "{}", stderr(&a)); assert!(b.status.success(), "{}", stderr(&b)); assert!(stderr(&a).is_empty(), "{}", stderr(&a)); assert!(stderr(&b).is_empty(), "{}", stderr(&b)); let text_a = stdout(&a); let text_b = stdout(&b); assert!( text_a.contains("curate-etl bench-curate-shorts-command"), "{text_a}" ); assert!(text_a.contains("rows=2000 "), "{text_a}"); assert!(text_a.contains("peak_chunk_bytes="), "{text_a}"); assert!(text_a.contains("decode:"), "{text_a}"); assert!(text_a.contains("curate:"), "{text_a}"); assert!(text_a.contains("write:"), "{text_a}"); assert!(text_a.contains("total:"), "{text_a}"); assert!(text_a.contains("rows/s"), "{text_a}"); assert!(text_a.contains("checksum=0x"), "{text_a}"); let field = |s: &str, prefix: &str| { s.split_whitespace() .find(|t| t.starts_with(prefix)) .map(str::to_owned) }; assert_eq!(field(&text_a, "checksum="), field(&text_b, "checksum=")); assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out=")); }