1874 lines
65 KiB
Rust
1874 lines
65 KiB
Rust
//! 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-ilp-bars-1m"), "{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"][..],
|
|
&["bench-ilp-bars-1m", "abc"][..],
|
|
&["bench-ilp-bars-1m", "0"][..],
|
|
&["bench-ilp-bars-1m", "-5"][..],
|
|
&["bench-ilp-bars-1m", "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="));
|
|
}
|
|
|
|
#[test]
|
|
fn bench_ilp_bars_1m_small_run_is_headless_deterministic_and_reports_rates() {
|
|
let a = run(&["bench-ilp-bars-1m", "2000"]);
|
|
let b = run(&["bench-ilp-bars-1m", "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-ilp-bars-1m"), "{text_a}");
|
|
assert!(text_a.contains("rows=2000 "), "{text_a}");
|
|
assert!(text_a.contains("rows_encoded=2000 "), "{text_a}");
|
|
assert!(text_a.contains("rows/s"), "{text_a}");
|
|
assert!(text_a.contains("bytes/row"), "{text_a}");
|
|
assert!(text_a.contains("MB/s"), "{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, "bytes="), field(&text_b, "bytes="));
|
|
assert_eq!(field(&text_a, "flushes="), field(&text_b, "flushes="));
|
|
}
|
|
|
|
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<u8> {
|
|
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<u8> {
|
|
let mut doc = RawDocumentBuf::new();
|
|
doc.append(cstr!("qmSearchCode"), code);
|
|
doc.append(cstr!("date"), date);
|
|
if ratio.is_finite() && ratio > 0.0 {
|
|
doc.append(cstr!("splitratio"), 1.0 / ratio);
|
|
doc.append(cstr!("forfactor"), 1.0_f64);
|
|
doc.append(cstr!("tofactor"), ratio);
|
|
} else {
|
|
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::<Vec<_>>()
|
|
.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}");
|
|
}
|
|
|
|
#[test]
|
|
fn curate_bars_1d_cli_threads_flag_parity_and_validation() {
|
|
let dir = CliTempDir::new("bars-threads-cli");
|
|
let map_path = dir.path.join("provider_mapping.tsv");
|
|
let curr_path = dir.path.join("listing_currencies.tsv");
|
|
let eod_path = dir.path.join("eod.bson");
|
|
let splits_path = dir.path.join("splits.bson");
|
|
|
|
fs::write(
|
|
&map_path,
|
|
"eod\tAAPL.US\t2020-01-01\t\t10\neod\tSHOP.TO\t2020-01-01\t\t20\nqm\tAAPL:US\t2020-01-01\t\t10\n",
|
|
)
|
|
.unwrap();
|
|
fs::write(&curr_path, "10\tUSD\n20\tCAD\n").unwrap();
|
|
|
|
let mut eod_bytes = Vec::new();
|
|
eod_bytes.extend_from_slice(&make_eod_doc("AAPL.US", "2022-05-03", 150.0, 2_000.0));
|
|
eod_bytes.extend_from_slice(&make_eod_doc("AAPL.US", "2023-06-05", 170.0, 1_500.0));
|
|
eod_bytes.extend_from_slice(&make_eod_doc("SHOP.TO", "2024-07-08", 80.0, 3_000.0));
|
|
eod_bytes.extend_from_slice(&make_eod_doc("SHOP.TO", "2025-02-10", 90.0, 2_500.0));
|
|
fs::write(&eod_path, &eod_bytes).unwrap();
|
|
fs::write(&splits_path, make_split_doc("AAPL:US", "2023-01-10", 2.0)).unwrap();
|
|
|
|
let out_t1 = dir.path.join("out_t1");
|
|
let out_t4 = dir.path.join("out_t4");
|
|
|
|
let run_1 = 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_t1.to_str().unwrap(),
|
|
"--threads",
|
|
"1",
|
|
]);
|
|
assert!(run_1.status.success(), "{}", stderr(&run_1));
|
|
assert!(stderr(&run_1).is_empty(), "{}", stderr(&run_1));
|
|
|
|
let run_4 = 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_t4.to_str().unwrap(),
|
|
"--threads",
|
|
"4",
|
|
]);
|
|
assert!(run_4.status.success(), "{}", stderr(&run_4));
|
|
assert!(stderr(&run_4).is_empty(), "{}", stderr(&run_4));
|
|
|
|
let text_1 = stdout(&run_1);
|
|
let text_4 = stdout(&run_4);
|
|
assert!(text_1.contains(" config: threads=1\n"), "{text_1}");
|
|
assert!(text_4.contains(" config: threads=4\n"), "{text_4}");
|
|
|
|
let strip_threads_and_timing = |s: &str| {
|
|
s.lines()
|
|
.filter(|l| {
|
|
let t = l.trim_start();
|
|
!t.starts_with("config:") && !t.starts_with("timing:")
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
};
|
|
assert_eq!(
|
|
strip_threads_and_timing(&text_1),
|
|
strip_threads_and_timing(&text_4)
|
|
);
|
|
|
|
for year in [2022_i32, 2023, 2024, 2025] {
|
|
let rel = format!("bars_1d/year={year:04}/part.parquet");
|
|
assert_eq!(
|
|
fs::read(out_t1.join(&rel)).unwrap(),
|
|
fs::read(out_t4.join(&rel)).unwrap(),
|
|
"CLI --threads 1 vs --threads 4 diverged on {rel}"
|
|
);
|
|
}
|
|
|
|
// Invalid --threads invocations must exit non-zero, print usage on stderr, and write nothing.
|
|
for (label, extra) in [
|
|
("zero", vec!["--threads", "0"]),
|
|
("alpha", vec!["--threads", "abc"]),
|
|
("neg", vec!["--threads=-1"]),
|
|
("missing", vec!["--threads"]),
|
|
("dup", vec!["--threads", "2", "--threads", "4"]),
|
|
] {
|
|
let bad_out = dir.path.join(format!("out_bad_{label}"));
|
|
let mut argv = vec![
|
|
"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",
|
|
bad_out.to_str().unwrap(),
|
|
];
|
|
argv.extend(extra);
|
|
let res = run(&argv);
|
|
assert!(
|
|
!res.status.success(),
|
|
"{label}: expected non-zero exit, stdout={}",
|
|
stdout(&res)
|
|
);
|
|
let err_text = stderr(&res);
|
|
assert!(err_text.contains("Usage:"), "{label}: missing usage in {err_text}");
|
|
assert!(
|
|
!bad_out.exists(),
|
|
"{label}: invalid --threads must not create '{}'",
|
|
bad_out.display()
|
|
);
|
|
}
|
|
}
|
|
|
|
fn make_qm_price_doc(code: &str, date: &str, share_volume: f64) -> Vec<u8> {
|
|
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=<path>`) via the documented `--curate-inputs <dir>`
|
|
// 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
|
|
// `<out>/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_1: threads=1 "), "{text}");
|
|
assert!(text.contains("write:"), "{text}");
|
|
assert!(text.contains("speedup="), "{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<u8> {
|
|
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="));
|
|
}
|
|
|
|
#[test]
|
|
fn send_ilp_cli_help_missing_flag_success_and_partial_error_routing() {
|
|
use std::io::{self, Read};
|
|
use std::net::TcpListener;
|
|
use std::thread;
|
|
use std::time::{Duration, Instant};
|
|
|
|
// 1. --help lists send-ilp, required flags, and delivery caveat.
|
|
let help = run(&["--help"]);
|
|
assert!(help.status.success(), "{}", stderr(&help));
|
|
let help_text = stdout(&help);
|
|
assert!(help_text.contains("send-ilp"), "{help_text}");
|
|
assert!(
|
|
help_text.contains("--dir")
|
|
&& help_text.contains("--prefix")
|
|
&& help_text.contains("--addr"),
|
|
"{help_text}"
|
|
);
|
|
assert!(help_text.contains("PGWire"), "{help_text}");
|
|
assert!(help_text.contains("10.2.0"), "{help_text}");
|
|
|
|
// 2. Missing --addr exits non-zero with error + Usage on stderr and empty stdout.
|
|
let missing_addr = run(&["send-ilp", "--dir", "/tmp/shards", "--prefix", "fx_1m"]);
|
|
assert!(!missing_addr.status.success());
|
|
assert_eq!(missing_addr.status.code(), Some(1));
|
|
assert!(stdout(&missing_addr).is_empty());
|
|
let missing_err = stderr(&missing_addr);
|
|
assert!(
|
|
missing_err.contains("error: missing required flag '--addr'"),
|
|
"{missing_err}"
|
|
);
|
|
assert!(missing_err.contains("Usage:"), "{missing_err}");
|
|
assert!(missing_err.contains("send-ilp"), "{missing_err}");
|
|
|
|
// 3. Binary success path over loopback prints format_ilp_send_report to stdout.
|
|
let dir = CliTempDir::new("send-ilp-bin");
|
|
let payload0 = b"fx_1m,pair=USDCAD close=1.38 1700000000000000t\n";
|
|
let payload1 = b"";
|
|
write_file(&dir.path.join("fx_1m.shard-00.ilp"), payload0);
|
|
write_file(&dir.path.join("fx_1m.shard-01.ilp"), payload1);
|
|
|
|
let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback listener");
|
|
let listen_addr = listener.local_addr().expect("local addr");
|
|
listener
|
|
.set_nonblocking(true)
|
|
.expect("set loopback listener nonblocking");
|
|
let server = thread::spawn(move || {
|
|
let deadline = Instant::now() + Duration::from_secs(5);
|
|
let mut received = Vec::with_capacity(2);
|
|
for accepted in 0..2 {
|
|
let mut stream = loop {
|
|
match listener.accept() {
|
|
Ok((s, _)) => {
|
|
s.set_nonblocking(false).expect("set stream blocking");
|
|
break s;
|
|
}
|
|
Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
|
|
assert!(
|
|
Instant::now() < deadline,
|
|
"timed out waiting for connection {accepted}/2"
|
|
);
|
|
thread::sleep(Duration::from_millis(5));
|
|
}
|
|
Err(err) => panic!("accept failed: {err}"),
|
|
}
|
|
};
|
|
stream
|
|
.set_read_timeout(Some(Duration::from_secs(3)))
|
|
.expect("set read timeout");
|
|
let mut buf = Vec::new();
|
|
stream.read_to_end(&mut buf).expect("read shard to EOF");
|
|
received.push(buf);
|
|
}
|
|
received
|
|
});
|
|
|
|
let ok_out = run(&[
|
|
"send-ilp",
|
|
"--dir",
|
|
dir.path.to_str().unwrap(),
|
|
"--prefix",
|
|
"fx_1m",
|
|
"--addr",
|
|
&listen_addr.to_string(),
|
|
]);
|
|
let mut received = server.join().expect("join server");
|
|
received.sort();
|
|
let mut expected_payloads = vec![payload0.to_vec(), payload1.to_vec()];
|
|
expected_payloads.sort();
|
|
assert_eq!(received, expected_payloads);
|
|
|
|
assert!(ok_out.status.success(), "{}", stderr(&ok_out));
|
|
assert!(stderr(&ok_out).is_empty(), "{}", stderr(&ok_out));
|
|
assert_eq!(
|
|
stdout(&ok_out),
|
|
format!(
|
|
"curate-etl ilp-send\n ilp_send: shards=2 total_bytes_sent={} per_shard_bytes=00={},01=0\n",
|
|
payload0.len(),
|
|
payload0.len()
|
|
)
|
|
);
|
|
|
|
// 4. Runtime connect failure prints error + formatted partial report to stderr, empty stdout.
|
|
let dead_listener = TcpListener::bind("127.0.0.1:0").expect("bind ephemeral port");
|
|
let dead_addr = dead_listener.local_addr().expect("ephemeral addr");
|
|
drop(dead_listener);
|
|
|
|
let fail_out = run(&[
|
|
"send-ilp",
|
|
"--dir",
|
|
dir.path.to_str().unwrap(),
|
|
"--prefix",
|
|
"fx_1m",
|
|
"--addr",
|
|
&dead_addr.to_string(),
|
|
]);
|
|
assert!(!fail_out.status.success());
|
|
assert_eq!(fail_out.status.code(), Some(1));
|
|
assert!(stdout(&fail_out).is_empty());
|
|
let fail_err = stderr(&fail_out);
|
|
assert!(fail_err.contains("error: ILP shard 0"), "{fail_err}");
|
|
assert!(
|
|
fail_err.contains(
|
|
"curate-etl ilp-send\n ilp_send: shards=2 total_bytes_sent=0 per_shard_bytes=00=0,01=0"
|
|
),
|
|
"{fail_err}"
|
|
);
|
|
assert!(!fail_err.contains("Usage:"), "{fail_err}");
|
|
}
|