425 lines
14 KiB
Rust
425 lines
14 KiB
Rust
//! End-to-end tests for the file-mode `curate-macro` CLI.
|
|
|
|
use std::fs::{self, File};
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use bson::raw::{cstr, RawArrayBuf, RawDocumentBuf};
|
|
use bson::DateTime;
|
|
use curate_etl::macro_series::POLICY_RATE_LAG_DAYS;
|
|
use curate_etl::run_curate_macro::{
|
|
format_curate_macro_report, run_curate_macro, CurateMacroArgs, MACRO_SERIES_MAP_HEADER,
|
|
};
|
|
use curate_etl::split::ymd_to_epoch_days;
|
|
use parquet::file::reader::{FileReader, SerializedFileReader};
|
|
use parquet::record::{Field, Row};
|
|
|
|
const MICROS_PER_DAY: i64 = 86_400_000_000;
|
|
|
|
static TEMP_SEQ: AtomicU64 = AtomicU64::new(0);
|
|
|
|
struct TempDir(PathBuf);
|
|
|
|
impl TempDir {
|
|
fn new(label: &str) -> Self {
|
|
let seq = TEMP_SEQ.fetch_add(1, Ordering::Relaxed);
|
|
let path = std::env::temp_dir().join(format!(
|
|
"curate-macro-cli-{label}-{}-{seq}",
|
|
std::process::id()
|
|
));
|
|
let _ = fs::remove_dir_all(&path);
|
|
fs::create_dir_all(&path).expect("create isolated test directory");
|
|
Self(path)
|
|
}
|
|
}
|
|
|
|
impl Drop for TempDir {
|
|
fn drop(&mut self) {
|
|
let _ = fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
|
|
fn cli() -> Command {
|
|
let mut command = Command::new(env!("CARGO_BIN_EXE_curate-etl"));
|
|
command.arg("curate-macro");
|
|
command
|
|
}
|
|
|
|
fn make_point_doc(d: &str, v: f64) -> RawDocumentBuf {
|
|
let mut pt = RawDocumentBuf::new();
|
|
pt.append(cstr!("d"), d);
|
|
pt.append(cstr!("v"), v);
|
|
pt
|
|
}
|
|
|
|
fn make_te_data_doc(
|
|
te_symbol: &str,
|
|
country: &str,
|
|
category: &str,
|
|
data_points: &[(&str, f64)],
|
|
forecast_points: &[(&str, f64)],
|
|
) -> RawDocumentBuf {
|
|
let mut data_arr = RawArrayBuf::new();
|
|
for &(d, v) in data_points {
|
|
data_arr.push(make_point_doc(d, v));
|
|
}
|
|
|
|
let mut forecast_arr = RawArrayBuf::new();
|
|
for &(d, v) in forecast_points {
|
|
forecast_arr.push(make_point_doc(d, v));
|
|
}
|
|
|
|
let mut doc = RawDocumentBuf::new();
|
|
doc.append(cstr!("teSymbol"), te_symbol);
|
|
doc.append(cstr!("name"), format!("{country} {category}"));
|
|
doc.append(cstr!("unit"), "percent");
|
|
doc.append(cstr!("source"), "TradingEconomics");
|
|
doc.append(cstr!("frequency"), "Monthly");
|
|
doc.append(cstr!("country"), country);
|
|
doc.append(cstr!("category"), category);
|
|
doc.append(cstr!("data"), data_arr);
|
|
doc.append(cstr!("forecast"), forecast_arr);
|
|
doc.append(
|
|
cstr!("lastUpdated"),
|
|
DateTime::from_millis(1_710_000_000_000),
|
|
);
|
|
doc.append(cstr!("createdAt"), DateTime::from_millis(1_700_000_000_000));
|
|
doc
|
|
}
|
|
|
|
fn write_policy_rate_fixture(dir: &Path) -> (PathBuf, PathBuf) {
|
|
let te_data_path = dir.join("teData.bson");
|
|
let series_map_path = dir.join("macro_series_map.tsv");
|
|
|
|
let doc = make_te_data_doc(
|
|
"FDTR",
|
|
"United States",
|
|
"Interest Rate",
|
|
&[("2024-03-20", 5.5)],
|
|
&[("2025-01-01", 99.0)],
|
|
);
|
|
fs::write(&te_data_path, doc.as_bytes()).expect("write teData.bson");
|
|
|
|
let tsv = format!(
|
|
"{MACRO_SERIES_MAP_HEADER}\nFDTR\tUnited States\tInterest Rate\tUS\tfed_funds_rate\tpolicy_rate\n"
|
|
);
|
|
fs::write(&series_map_path, tsv).expect("write macro_series_map.tsv");
|
|
|
|
(te_data_path, series_map_path)
|
|
}
|
|
|
|
fn col<'a>(row: &'a Row, name: &str) -> &'a Field {
|
|
row.get_column_iter()
|
|
.find(|(col_name, _)| col_name.as_str() == name)
|
|
.unwrap_or_else(|| panic!("missing Parquet column '{name}'"))
|
|
.1
|
|
}
|
|
|
|
#[test]
|
|
fn cli_publishes_runner_equivalent_macro_parquet_and_excludes_forecast_sentinel() {
|
|
let dir = TempDir::new("e2e");
|
|
let (te_data_path, series_map_path) = write_policy_rate_fixture(&dir.0);
|
|
|
|
let baseline_out = dir.0.join("baseline_out");
|
|
let baseline_report = run_curate_macro(&CurateMacroArgs {
|
|
te_data: te_data_path.clone(),
|
|
series_map: series_map_path.clone(),
|
|
out_dir: baseline_out.clone(),
|
|
})
|
|
.expect("run library baseline");
|
|
let expected_stdout = format!("{}\n", format_curate_macro_report(&baseline_report));
|
|
|
|
let cli_out = dir.0.join("cli_out");
|
|
let result = cli()
|
|
.current_dir(&dir.0)
|
|
.args([
|
|
"--te-data",
|
|
"teData.bson",
|
|
"--series-map=macro_series_map.tsv",
|
|
"--out",
|
|
"cli_out",
|
|
])
|
|
.output()
|
|
.expect("run curate-macro");
|
|
|
|
assert!(result.status.success(), "{result:?}");
|
|
assert!(
|
|
result.stderr.is_empty(),
|
|
"unexpected stderr: {}",
|
|
String::from_utf8_lossy(&result.stderr)
|
|
);
|
|
let stdout = String::from_utf8(result.stdout).expect("utf-8 stdout");
|
|
assert_eq!(stdout, expected_stdout);
|
|
|
|
let cli_parquet = cli_out.join("macro/part.parquet");
|
|
let baseline_parquet = baseline_out.join("macro/part.parquet");
|
|
assert!(cli_parquet.is_file(), "expected {}", cli_parquet.display());
|
|
assert_eq!(
|
|
fs::read(&cli_parquet).expect("read cli parquet"),
|
|
fs::read(&baseline_parquet).expect("read baseline parquet"),
|
|
"CLI Parquet output must match library runner bytes"
|
|
);
|
|
|
|
let reader = SerializedFileReader::new(File::open(&cli_parquet).expect("open parquet"))
|
|
.expect("open parquet reader");
|
|
let rows: Vec<Row> = reader
|
|
.get_row_iter(None)
|
|
.expect("row iter")
|
|
.map(|row| row.expect("decode row"))
|
|
.collect();
|
|
assert_eq!(
|
|
rows.len(),
|
|
1,
|
|
"expected exactly one mapped policy-rate row and no forecast sentinel row"
|
|
);
|
|
|
|
let expected_period_date = ymd_to_epoch_days(2024, 3, 20).expect("valid date");
|
|
let expected_known_at = i64::from(expected_period_date + POLICY_RATE_LAG_DAYS) * MICROS_PER_DAY;
|
|
assert_eq!(POLICY_RATE_LAG_DAYS, 1);
|
|
|
|
let row = &rows[0];
|
|
assert_eq!(col(row, "country_iso"), &Field::Str("US".to_string()));
|
|
assert_eq!(
|
|
col(row, "indicator"),
|
|
&Field::Str("fed_funds_rate".to_string())
|
|
);
|
|
assert_eq!(col(row, "period_date"), &Field::Date(expected_period_date));
|
|
assert_eq!(
|
|
col(row, "known_at"),
|
|
&Field::TimestampMicros(expected_known_at)
|
|
);
|
|
assert_eq!(
|
|
col(row, "known_at_source"),
|
|
&Field::Str("publication_lag_estimate".to_string())
|
|
);
|
|
assert_eq!(col(row, "value"), &Field::Double(5.5));
|
|
}
|
|
|
|
#[test]
|
|
fn cli_missing_te_data_exits_nonzero_with_single_line_escaped_error_and_no_parquet() {
|
|
let dir = TempDir::new("missing-te-data");
|
|
let (_, series_map_path) = write_policy_rate_fixture(&dir.0);
|
|
|
|
let missing_path = dir.0.join("missing\nte_data\r.bson");
|
|
let cli_out = dir.0.join("cli_out");
|
|
|
|
let result = cli()
|
|
.arg("--te-data")
|
|
.arg(&missing_path)
|
|
.arg("--series-map")
|
|
.arg(&series_map_path)
|
|
.arg("--out")
|
|
.arg(&cli_out)
|
|
.output()
|
|
.expect("run curate-macro");
|
|
|
|
assert_eq!(result.status.code(), Some(1), "{result:?}");
|
|
assert!(
|
|
result.stdout.is_empty(),
|
|
"unexpected stdout: {}",
|
|
String::from_utf8_lossy(&result.stdout)
|
|
);
|
|
|
|
let stderr = String::from_utf8(result.stderr).expect("utf-8 stderr");
|
|
assert_eq!(
|
|
stderr.lines().count(),
|
|
1,
|
|
"expected 1 stderr line: {stderr:?}"
|
|
);
|
|
assert!(!stderr.contains('\r'), "stderr must escape CR: {stderr:?}");
|
|
assert!(stderr.starts_with("error: "), "{stderr:?}");
|
|
assert!(stderr.contains("te-data"), "{stderr:?}");
|
|
assert!(
|
|
stderr.contains("missing\\nte_data\\r.bson"),
|
|
"expected escaped path in stderr: {stderr:?}"
|
|
);
|
|
assert!(!stderr.contains("Usage:"), "{stderr:?}");
|
|
assert!(
|
|
!cli_out.join("macro/part.parquet").exists(),
|
|
"macro/part.parquet must not be written on failure"
|
|
);
|
|
assert!(!cli_out.exists(), "out dir must not be created on failure");
|
|
}
|
|
|
|
// ---- QA edge cases (T-202) ----
|
|
|
|
fn run_cli_paths(te_data: &Path, series_map: &Path, out: &Path) -> std::process::Output {
|
|
cli()
|
|
.arg("--te-data")
|
|
.arg(te_data)
|
|
.arg("--series-map")
|
|
.arg(series_map)
|
|
.arg("--out")
|
|
.arg(out)
|
|
.output()
|
|
.expect("run curate-macro")
|
|
}
|
|
|
|
#[test]
|
|
fn cli_help_lists_curate_macro_and_parse_errors_print_usage_without_writing() {
|
|
let help = Command::new(env!("CARGO_BIN_EXE_curate-etl"))
|
|
.arg("--help")
|
|
.output()
|
|
.expect("run --help");
|
|
assert!(help.status.success(), "{help:?}");
|
|
let help_text = String::from_utf8(help.stdout).expect("utf-8 help");
|
|
assert!(
|
|
help_text.contains("curate-macro --te-data <path> --series-map <path> --out <dir>"),
|
|
"{help_text}"
|
|
);
|
|
|
|
let dir = TempDir::new("parse-error");
|
|
let (te_data_path, series_map_path) = write_policy_rate_fixture(&dir.0);
|
|
let cli_out = dir.0.join("cli_out");
|
|
|
|
// `--mongo-uri` is not a file-mode flag; it must be rejected before any run.
|
|
let result = cli()
|
|
.arg("--te-data")
|
|
.arg(&te_data_path)
|
|
.arg("--series-map")
|
|
.arg(&series_map_path)
|
|
.arg("--out")
|
|
.arg(&cli_out)
|
|
.args(["--mongo-uri", "mongodb://user:secret@host:27017/stock"])
|
|
.output()
|
|
.expect("run curate-macro");
|
|
|
|
assert_eq!(result.status.code(), Some(1), "{result:?}");
|
|
assert!(result.stdout.is_empty(), "{result:?}");
|
|
let stderr = String::from_utf8(result.stderr).expect("utf-8 stderr");
|
|
assert!(
|
|
stderr.starts_with("error: unknown flag '--mongo-uri' for curate-macro\n"),
|
|
"{stderr:?}"
|
|
);
|
|
assert!(stderr.contains("Usage:"), "{stderr:?}");
|
|
assert!(
|
|
!stderr.contains("secret"),
|
|
"rejected flag value must not be echoed: {stderr:?}"
|
|
);
|
|
assert!(
|
|
!cli_out.exists(),
|
|
"out dir must not be created on parse error"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn cli_runtime_failures_keep_existing_parquet_byte_identical() {
|
|
let dir = TempDir::new("preserve");
|
|
let (te_data_path, series_map_path) = write_policy_rate_fixture(&dir.0);
|
|
let out = dir.0.join("out");
|
|
|
|
let first = run_cli_paths(&te_data_path, &series_map_path, &out);
|
|
assert!(first.status.success(), "{first:?}");
|
|
let parquet = out.join("macro/part.parquet");
|
|
let original = fs::read(&parquet).expect("read published parquet");
|
|
|
|
let bad_header = dir.0.join("bad_header.tsv");
|
|
fs::write(&bad_header, "not\ta\theader\n").expect("write bad tsv");
|
|
let corrupt_bson = dir.0.join("corrupt.bson");
|
|
// Declares a 64-byte document but provides only 8 bytes.
|
|
fs::write(&corrupt_bson, [64u8, 0, 0, 0, 1, 2, 3, 4]).expect("write corrupt bson");
|
|
let missing_map = dir.0.join("missing_map.tsv");
|
|
|
|
for (label, te, map, role) in [
|
|
("bad series-map header", &te_data_path, &bad_header, None),
|
|
(
|
|
"truncated teData",
|
|
&corrupt_bson,
|
|
&series_map_path,
|
|
Some("te-data"),
|
|
),
|
|
(
|
|
"missing series-map",
|
|
&te_data_path,
|
|
&missing_map,
|
|
Some("series-map"),
|
|
),
|
|
] {
|
|
let result = run_cli_paths(te, map, &out);
|
|
assert_eq!(result.status.code(), Some(1), "{label}: {result:?}");
|
|
assert!(result.stdout.is_empty(), "{label}: {result:?}");
|
|
let stderr = String::from_utf8(result.stderr).expect("utf-8 stderr");
|
|
assert_eq!(stderr.lines().count(), 1, "{label}: {stderr:?}");
|
|
assert!(stderr.starts_with("error: "), "{label}: {stderr:?}");
|
|
assert!(!stderr.contains("Usage:"), "{label}: {stderr:?}");
|
|
if let Some(role) = role {
|
|
assert!(stderr.contains(role), "{label}: {stderr:?}");
|
|
}
|
|
assert_eq!(
|
|
fs::read(&parquet).expect("re-read parquet"),
|
|
original,
|
|
"{label}: failed run must not touch the published parquet"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn cli_rerun_on_same_inputs_is_byte_deterministic() {
|
|
let dir = TempDir::new("rerun");
|
|
let (te_data_path, series_map_path) = write_policy_rate_fixture(&dir.0);
|
|
let out_a = dir.0.join("out_a");
|
|
let out_b = dir.0.join("out_b");
|
|
|
|
let a = run_cli_paths(&te_data_path, &series_map_path, &out_a);
|
|
let b = run_cli_paths(&te_data_path, &series_map_path, &out_b);
|
|
assert!(a.status.success() && b.status.success(), "{a:?} {b:?}");
|
|
assert_eq!(a.stdout, b.stdout);
|
|
assert_eq!(
|
|
fs::read(out_a.join("macro/part.parquet")).expect("read a"),
|
|
fs::read(out_b.join("macro/part.parquet")).expect("read b")
|
|
);
|
|
|
|
// Republishing into the same directory replaces the file with identical bytes.
|
|
let again = run_cli_paths(&te_data_path, &series_map_path, &out_a);
|
|
assert!(again.status.success(), "{again:?}");
|
|
assert_eq!(
|
|
fs::read(out_a.join("macro/part.parquet")).expect("read a again"),
|
|
fs::read(out_b.join("macro/part.parquet")).expect("read b")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn cli_unmapped_only_input_succeeds_and_matches_runner_report() {
|
|
let dir = TempDir::new("unmapped");
|
|
let te_data_path = dir.0.join("teData.bson");
|
|
let series_map_path = dir.0.join("macro_series_map.tsv");
|
|
let doc = make_te_data_doc(
|
|
"UNMAPPED",
|
|
"Atlantis",
|
|
"Interest Rate",
|
|
&[("2024-03-20", 1.25)],
|
|
&[],
|
|
);
|
|
fs::write(&te_data_path, doc.as_bytes()).expect("write teData");
|
|
fs::write(
|
|
&series_map_path,
|
|
format!(
|
|
"{MACRO_SERIES_MAP_HEADER}\nFDTR\tUnited States\tInterest Rate\tUS\tfed_funds_rate\tpolicy_rate\n"
|
|
),
|
|
)
|
|
.expect("write tsv");
|
|
|
|
let baseline_out = dir.0.join("baseline_out");
|
|
let baseline = run_curate_macro(&CurateMacroArgs {
|
|
te_data: te_data_path.clone(),
|
|
series_map: series_map_path.clone(),
|
|
out_dir: baseline_out.clone(),
|
|
})
|
|
.expect("library run");
|
|
assert_eq!(baseline.curate_stats.rows_out, 0);
|
|
|
|
let cli_out = dir.0.join("cli_out");
|
|
let result = run_cli_paths(&te_data_path, &series_map_path, &cli_out);
|
|
assert!(result.status.success(), "{result:?}");
|
|
assert!(result.stderr.is_empty(), "{result:?}");
|
|
assert_eq!(
|
|
String::from_utf8(result.stdout).expect("utf-8 stdout"),
|
|
format!("{}\n", format_curate_macro_report(&baseline))
|
|
);
|
|
assert_eq!(
|
|
cli_out.join("macro/part.parquet").exists(),
|
|
baseline_out.join("macro/part.parquet").exists(),
|
|
"CLI and runner must agree on zero-row publication"
|
|
);
|
|
}
|