330 lines
9.8 KiB
Rust
330 lines
9.8 KiB
Rust
//! End-to-end binary integration tests for `curate-etl curate-bars-1m` (`M2.3n`).
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::{Command, Output};
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use bson::raw::{cstr, RawDocumentBuf};
|
|
use curate_etl::{
|
|
et_to_utc_micros, format_curate_bars_1m_report, run_curate_bars_1m, ymd_to_epoch_days,
|
|
BsonStreamOptions, CurateBars1mArgs, IlpWriterOptions, DEFAULT_BARS_1M_MAPPING_SOURCE,
|
|
};
|
|
|
|
static TEST_DIR_SEQ: AtomicU64 = AtomicU64::new(0);
|
|
|
|
struct TempDir {
|
|
path: PathBuf,
|
|
}
|
|
|
|
impl TempDir {
|
|
fn new(tag: &str) -> Self {
|
|
let pid = std::process::id();
|
|
let seq = TEST_DIR_SEQ.fetch_add(1, Ordering::Relaxed);
|
|
let path =
|
|
std::env::temp_dir().join(format!("curate-etl-it-bars-1m-{tag}-{pid}-{seq}"));
|
|
let _ = fs::remove_dir_all(&path);
|
|
fs::create_dir_all(&path).expect("create integration test temp dir");
|
|
Self { path }
|
|
}
|
|
|
|
fn path(&self) -> &Path {
|
|
&self.path
|
|
}
|
|
}
|
|
|
|
impl Drop for TempDir {
|
|
fn drop(&mut self) {
|
|
let _ = fs::remove_dir_all(&self.path);
|
|
}
|
|
}
|
|
|
|
fn run_bin(args: &[&str]) -> Output {
|
|
Command::new(env!("CARGO_BIN_EXE_curate-etl"))
|
|
.args(args)
|
|
.output()
|
|
.expect("spawn curate-etl binary")
|
|
}
|
|
|
|
fn day(y: i32, m: u32, d: u32) -> i32 {
|
|
ymd_to_epoch_days(y, m, d).expect("valid date")
|
|
}
|
|
|
|
fn et_ts_sec(y: i32, m: u32, d: u32, hour: u8, minute: u8) -> i64 {
|
|
let micros = et_to_utc_micros(day(y, m, d), hour, minute, 0).expect("valid ET timestamp");
|
|
assert_eq!(micros % 1_000_000, 0);
|
|
micros / 1_000_000
|
|
}
|
|
|
|
fn make_intraday_doc(
|
|
code: &str,
|
|
timestamp_sec: i64,
|
|
open: f64,
|
|
high: f64,
|
|
low: f64,
|
|
close: f64,
|
|
volume: f64,
|
|
) -> RawDocumentBuf {
|
|
let mut doc = RawDocumentBuf::new();
|
|
doc.append(cstr!("eodSearchCode"), code);
|
|
doc.append(cstr!("timestamp"), timestamp_sec);
|
|
doc.append(cstr!("open"), open);
|
|
doc.append(cstr!("high"), high);
|
|
doc.append(cstr!("low"), low);
|
|
doc.append(cstr!("close"), close);
|
|
doc.append(cstr!("volume"), volume);
|
|
doc
|
|
}
|
|
|
|
fn make_split_doc(
|
|
qm_search_code: &str,
|
|
ex_date: &str,
|
|
for_factor: f64,
|
|
to_factor: f64,
|
|
) -> RawDocumentBuf {
|
|
let mut doc = RawDocumentBuf::new();
|
|
doc.append(cstr!("qmSearchCode"), qm_search_code);
|
|
doc.append(cstr!("date"), ex_date);
|
|
doc.append(cstr!("forfactor"), for_factor);
|
|
doc.append(cstr!("tofactor"), to_factor);
|
|
doc.append(cstr!("splitratio"), for_factor / to_factor);
|
|
doc
|
|
}
|
|
|
|
fn concat_docs(docs: &[RawDocumentBuf]) -> Vec<u8> {
|
|
let mut out = Vec::new();
|
|
for doc in docs {
|
|
out.extend_from_slice(doc.as_bytes());
|
|
}
|
|
out
|
|
}
|
|
|
|
fn read_dir_files(dir: &Path) -> BTreeMap<String, Vec<u8>> {
|
|
let mut out = BTreeMap::new();
|
|
for entry in fs::read_dir(dir).expect("read output dir") {
|
|
let entry = entry.expect("dir entry");
|
|
let name = entry
|
|
.file_name()
|
|
.into_string()
|
|
.expect("UTF-8 file name in output dir");
|
|
let bytes = fs::read(entry.path()).expect("read output file");
|
|
out.insert(name, bytes);
|
|
}
|
|
out
|
|
}
|
|
|
|
fn strip_timing_line(report: &str) -> Vec<&str> {
|
|
report
|
|
.lines()
|
|
.filter(|line| !line.trim_start().starts_with("timing:"))
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn curate_bars_1m_binary_matches_direct_runner_bytes_and_report() {
|
|
let tmp = TempDir::new("parity");
|
|
|
|
// Pin NVDA 10:1 forward split (2024-06-10) and NEXM 1:20 reverse split (2025-06-20)
|
|
// fixtures in the style of src/run_curate_bars_1m_tests.rs.
|
|
let intraday_path = tmp.path().join("eodIntraday1M.bson");
|
|
let intraday_bytes = concat_docs(&[
|
|
// NVDA pre-split (2024-06-07): 10:1 split un-adjusts prices x10 and volume /10.
|
|
make_intraday_doc(
|
|
"NVDA.US",
|
|
et_ts_sec(2024, 6, 7, 15, 59),
|
|
120.50,
|
|
121.00,
|
|
120.40,
|
|
120.888,
|
|
412_390.0,
|
|
),
|
|
// NVDA ex-date (2024-06-10): already post-split (factor = 1.0).
|
|
make_intraday_doc(
|
|
"NVDA.US",
|
|
et_ts_sec(2024, 6, 10, 15, 59),
|
|
121.50,
|
|
122.00,
|
|
121.20,
|
|
121.79,
|
|
350_000.0,
|
|
),
|
|
// NEXM pre-split (2025-06-18): 1:20 reverse split un-adjusts prices x0.05 and volume x20.
|
|
make_intraday_doc(
|
|
"NEXM.V",
|
|
et_ts_sec(2025, 6, 18, 15, 59),
|
|
24.20,
|
|
24.40,
|
|
24.10,
|
|
24.29,
|
|
5_000.0,
|
|
),
|
|
// NEXM ex-date (2025-06-20): already post-split (factor = 1.0).
|
|
make_intraday_doc(
|
|
"NEXM.V",
|
|
et_ts_sec(2025, 6, 20, 15, 59),
|
|
24.10,
|
|
24.35,
|
|
24.00,
|
|
24.28,
|
|
12_500.0,
|
|
),
|
|
]);
|
|
fs::write(&intraday_path, &intraday_bytes).expect("write intraday bson");
|
|
|
|
let splits_path = tmp.path().join("qmSplits.bson");
|
|
let splits_bytes = concat_docs(&[
|
|
make_split_doc("NVDA", "2024-06-10", 1.0, 10.0),
|
|
make_split_doc("NEXM:CA", "2025-06-20", 20.0, 1.0),
|
|
]);
|
|
fs::write(&splits_path, &splits_bytes).expect("write splits bson");
|
|
|
|
let mapping_path = tmp.path().join("provider_mapping.tsv");
|
|
fs::write(
|
|
&mapping_path,
|
|
"eod\tNVDA.US\t2000-01-01\t\t1001\n\
|
|
qm\tNVDA\t2000-01-01\t\t1001\n\
|
|
eod\tNEXM.V\t2000-01-01\t\t2002\n\
|
|
qm\tNEXM:CA\t2000-01-01\t\t2002\n",
|
|
)
|
|
.expect("write provider_mapping.tsv");
|
|
|
|
let known_at_micros: i64 = 1_750_000_000_000_000;
|
|
let shard_count: usize = 2;
|
|
|
|
let bin_out = tmp.path().join("out-bin");
|
|
let direct_out = tmp.path().join("out-direct");
|
|
|
|
let output = run_bin(&[
|
|
"curate-bars-1m",
|
|
"--eod-intraday",
|
|
intraday_path.to_str().unwrap(),
|
|
"--qm-splits",
|
|
splits_path.to_str().unwrap(),
|
|
"--provider-mapping",
|
|
mapping_path.to_str().unwrap(),
|
|
"--out",
|
|
bin_out.to_str().unwrap(),
|
|
"--shards",
|
|
"2",
|
|
"--known-at-micros",
|
|
"1750000000000000",
|
|
]);
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
output.status.success(),
|
|
"expected exit 0, got {:?}; stderr:\n{stderr}\nstdout:\n{stdout}",
|
|
output.status.code()
|
|
);
|
|
assert!(
|
|
stderr.is_empty(),
|
|
"expected empty stderr on success, got:\n{stderr}"
|
|
);
|
|
|
|
let direct_args = CurateBars1mArgs {
|
|
intraday_path,
|
|
splits_path,
|
|
provider_mapping_path: mapping_path,
|
|
out_dir: direct_out.clone(),
|
|
mapping_source: DEFAULT_BARS_1M_MAPPING_SOURCE.to_string(),
|
|
shard_count,
|
|
known_at_micros,
|
|
stream_options: BsonStreamOptions::default(),
|
|
ilp_options: IlpWriterOptions::default(),
|
|
};
|
|
let direct_report = run_curate_bars_1m(&direct_args).expect("direct run_curate_bars_1m");
|
|
assert_eq!(
|
|
direct_report.rows_written, 4,
|
|
"expected 4 emitted bars_1m rows"
|
|
);
|
|
|
|
let direct_formatted = format_curate_bars_1m_report(&direct_report);
|
|
assert_eq!(
|
|
strip_timing_line(&stdout),
|
|
strip_timing_line(&direct_formatted),
|
|
"binary stdout report should match direct runner report excluding timing line"
|
|
);
|
|
assert!(
|
|
stdout
|
|
.lines()
|
|
.any(|line| line.trim_start().starts_with("timing: elapsed_secs=")),
|
|
"expected timing line in stdout:\n{stdout}"
|
|
);
|
|
|
|
let bin_files = read_dir_files(&bin_out);
|
|
let direct_files = read_dir_files(&direct_out);
|
|
|
|
let expected_names: Vec<String> = vec![
|
|
"bars_1m.shard-00.ilp".to_string(),
|
|
"bars_1m.shard-01.ilp".to_string(),
|
|
];
|
|
assert_eq!(
|
|
bin_files.keys().cloned().collect::<Vec<_>>(),
|
|
expected_names,
|
|
"unexpected files in binary output dir"
|
|
);
|
|
assert_eq!(
|
|
bin_files, direct_files,
|
|
"binary shard files must be byte-identical to direct runner output"
|
|
);
|
|
assert!(
|
|
bin_files.values().map(Vec::len).sum::<usize>() > 0,
|
|
"expected non-empty ILP output across shards"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn curate_bars_1m_binary_missing_input_exits_nonzero_and_leaves_out_clean() {
|
|
let tmp = TempDir::new("missing-input");
|
|
|
|
let missing_intraday = tmp.path().join("does-not-exist.bson");
|
|
let splits_path = tmp.path().join("qmSplits.bson");
|
|
fs::write(&splits_path, concat_docs(&[])).expect("write empty splits bson");
|
|
|
|
let mapping_path = tmp.path().join("provider_mapping.tsv");
|
|
fs::write(&mapping_path, "eod\tNVDA.US\t2000-01-01\t\t1001\n")
|
|
.expect("write provider_mapping.tsv");
|
|
|
|
let out_dir = tmp.path().join("out-empty");
|
|
fs::create_dir_all(&out_dir).expect("create empty out dir");
|
|
|
|
let output = run_bin(&[
|
|
"curate-bars-1m",
|
|
"--eod-intraday",
|
|
missing_intraday.to_str().unwrap(),
|
|
"--qm-splits",
|
|
splits_path.to_str().unwrap(),
|
|
"--provider-mapping",
|
|
mapping_path.to_str().unwrap(),
|
|
"--out",
|
|
out_dir.to_str().unwrap(),
|
|
"--shards",
|
|
"2",
|
|
"--known-at-micros",
|
|
"1750000000000000",
|
|
]);
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
!output.status.success(),
|
|
"expected non-zero exit when intraday file is missing"
|
|
);
|
|
assert!(
|
|
stdout.is_empty(),
|
|
"expected no success report on stdout, got:\n{stdout}"
|
|
);
|
|
assert!(
|
|
stderr.contains("error:") && stderr.contains("failed to open eod-intraday"),
|
|
"expected input-open error on stderr, got:\n{stderr}"
|
|
);
|
|
|
|
let remaining = read_dir_files(&out_dir);
|
|
assert!(
|
|
remaining.is_empty(),
|
|
"expected no shard or staging files in output dir, found: {:?}",
|
|
remaining.keys().collect::<Vec<_>>()
|
|
);
|
|
}
|