T-186: tests

This commit is contained in:
Studio 2026-12-02 19:51:01 -05:00
parent ff7db8abda
commit eb2356009f

View file

@ -328,3 +328,308 @@ fn curate_bars_1m_binary_missing_input_exits_nonzero_and_leaves_out_clean() {
remaining.keys().collect::<Vec<_>>()
);
}
// ---------------------------------------------------------------------------
// QA edge cases (T-186): binary-level behaviour not covered by the parity tests.
// ---------------------------------------------------------------------------
struct Fixture {
intraday: PathBuf,
splits: PathBuf,
mapping: PathBuf,
}
/// NVDA 10:1 split fixture: one pre-split bar (2024-06-07) and one ex-date bar (2024-06-10).
fn write_nvda_fixture(dir: &Path) -> Fixture {
let intraday = dir.join("eodIntraday1M.bson");
fs::write(
&intraday,
concat_docs(&[
make_intraday_doc(
"NVDA.US",
et_ts_sec(2024, 6, 7, 15, 59),
120.50,
121.00,
120.40,
120.75,
412_390.0,
),
make_intraday_doc(
"NVDA.US",
et_ts_sec(2024, 6, 10, 15, 59),
121.50,
122.00,
121.20,
121.75,
350_000.0,
),
]),
)
.expect("write intraday bson");
let splits = dir.join("qmSplits.bson");
fs::write(&splits, concat_docs(&[make_split_doc("NVDA", "2024-06-10", 1.0, 10.0)]))
.expect("write splits bson");
let mapping = dir.join("provider_mapping.tsv");
fs::write(
&mapping,
"eod\tNVDA.US\t2000-01-01\t\t1001\nqm\tNVDA\t2000-01-01\t\t1001\n",
)
.expect("write provider_mapping.tsv");
Fixture {
intraday,
splits,
mapping,
}
}
fn bin_args<'a>(
fx: &'a Fixture,
out: &'a Path,
known_at: &'a str,
extra: &[&'a str],
) -> Vec<&'a str> {
let mut argv = vec![
"curate-bars-1m",
"--eod-intraday",
fx.intraday.to_str().unwrap(),
"--qm-splits",
fx.splits.to_str().unwrap(),
"--provider-mapping",
fx.mapping.to_str().unwrap(),
"--out",
out.to_str().unwrap(),
"--shards",
"2",
"--known-at-micros",
known_at,
];
argv.extend_from_slice(extra);
argv
}
#[derive(Debug)]
struct IlpRow {
listing_id: i32,
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
known_at: i64,
ts: i64,
}
/// Minimal ILP v2 `bars_1m` decoder (binary DOUBLE fields, `known_at=<i64>t`, `<ts>t\n`).
fn decode_rows(data: &[u8]) -> Vec<IlpRow> {
let mut rows = Vec::new();
let mut i = 0usize;
while i < data.len() {
let tag_start = i;
while data[i] != b' ' {
i += 1;
}
let tags = std::str::from_utf8(&data[tag_start..i]).unwrap();
i += 1;
let listing_id: i32 = tags
.split(',')
.find_map(|t| t.strip_prefix("listing_id="))
.expect("listing_id tag")
.parse()
.unwrap();
let mut read_f64 = |name: &str| -> f64 {
let prefix = format!("{name}==");
assert!(data[i..].starts_with(prefix.as_bytes()), "expected {prefix}");
i += prefix.len() + 1; // skip binary DOUBLE type tag
let v = f64::from_le_bytes(data[i..i + 8].try_into().unwrap());
i += 8;
if data[i] == b',' {
i += 1;
}
v
};
let open = read_f64("open");
let high = read_f64("high");
let low = read_f64("low");
let close = read_f64("close");
let volume = read_f64("volume");
assert!(data[i..].starts_with(b"known_at="), "expected known_at=");
i += b"known_at=".len();
let ks = i;
while data[i] != b' ' {
i += 1;
}
let known_at: i64 = std::str::from_utf8(&data[ks..i])
.unwrap()
.strip_suffix('t')
.unwrap()
.parse()
.unwrap();
i += 1;
let ts_start = i;
while data[i] != b'\n' {
i += 1;
}
let ts: i64 = std::str::from_utf8(&data[ts_start..i])
.unwrap()
.strip_suffix('t')
.unwrap()
.parse()
.unwrap();
i += 1;
rows.push(IlpRow {
listing_id,
open,
high,
low,
close,
volume,
known_at,
ts,
});
}
rows
}
fn assert_close(actual: f64, expected: f64, what: &str) {
assert!(
(actual - expected).abs() <= 1e-9 * expected.abs().max(1.0),
"{what}: expected {expected}, got {actual}"
);
}
fn shard_and_staging_files(dir: &Path) -> Vec<String> {
if !dir.exists() {
return Vec::new();
}
read_dir_files(dir).into_keys().collect()
}
#[test]
fn curate_bars_1m_binary_writes_known_at_verbatim_and_unadjusts_nvda_split() {
let tmp = TempDir::new("values");
let fx = write_nvda_fixture(tmp.path());
let out = tmp.path().join("out");
let output = run_bin(&bin_args(&fx, &out, "1700000000123456", &[]));
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let mut rows: Vec<IlpRow> = read_dir_files(&out)
.values()
.flat_map(|bytes| decode_rows(bytes))
.collect();
rows.sort_by_key(|r| r.ts);
assert_eq!(rows.len(), 2, "rows: {rows:?}");
for row in &rows {
assert_eq!(row.listing_id, 1001);
assert_eq!(row.known_at, 1_700_000_000_123_456, "known_at must be verbatim");
}
let pre = &rows[0];
assert_eq!(pre.ts, et_ts_sec(2024, 6, 7, 15, 59) * 1_000_000);
assert_close(pre.open, 1205.0, "pre-split open x10");
assert_close(pre.high, 1210.0, "pre-split high x10");
assert_close(pre.low, 1204.0, "pre-split low x10");
assert_close(pre.close, 1207.5, "pre-split close x10");
assert_close(pre.volume, 41_239.0, "pre-split volume /10");
let ex = &rows[1];
assert_eq!(ex.ts, et_ts_sec(2024, 6, 10, 15, 59) * 1_000_000);
assert_close(ex.close, 121.75, "ex-date close unchanged");
assert_close(ex.volume, 350_000.0, "ex-date volume unchanged");
}
#[test]
fn curate_bars_1m_binary_is_deterministic_across_runs() {
let tmp = TempDir::new("determinism");
let fx = write_nvda_fixture(tmp.path());
let out_a = tmp.path().join("out-a");
let out_b = tmp.path().join("out-b");
let a = run_bin(&bin_args(&fx, &out_a, "1750000000000000", &[]));
let b = run_bin(&bin_args(&fx, &out_b, "1750000000000000", &[]));
assert!(a.status.success() && b.status.success());
let files_a = read_dir_files(&out_a);
assert!(!files_a.is_empty());
assert_eq!(files_a, read_dir_files(&out_b), "repeat runs must be byte-identical");
assert_eq!(
strip_timing_line(&String::from_utf8_lossy(&a.stdout)),
strip_timing_line(&String::from_utf8_lossy(&b.stdout)),
);
}
#[test]
fn curate_bars_1m_binary_refuses_to_overwrite_existing_shards() {
let tmp = TempDir::new("overwrite");
let fx = write_nvda_fixture(tmp.path());
let out = tmp.path().join("out");
let first = run_bin(&bin_args(&fx, &out, "1750000000000000", &[]));
assert!(first.status.success());
let before = read_dir_files(&out);
// Different known_at so any overwrite would change bytes.
let second = run_bin(&bin_args(&fx, &out, "1760000000000000", &[]));
assert!(!second.status.success(), "second run into populated --out must fail");
assert!(second.stdout.is_empty(), "no success report expected");
assert!(String::from_utf8_lossy(&second.stderr).starts_with("error:"));
assert_eq!(
read_dir_files(&out),
before,
"existing shards must be untouched and no staging files left"
);
}
#[test]
fn curate_bars_1m_binary_rejects_non_eod_mapping_source_without_output() {
let tmp = TempDir::new("mapping-source");
let fx = write_nvda_fixture(tmp.path());
let out = tmp.path().join("out");
let output = run_bin(&bin_args(
&fx,
&out,
"1750000000000000",
&["--mapping-source", "qm"],
));
assert!(!output.status.success());
assert!(output.stdout.is_empty());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("error:") && stderr.contains("qm"), "stderr: {stderr}");
assert!(
shard_and_staging_files(&out).is_empty(),
"no files expected in --out: {:?}",
shard_and_staging_files(&out)
);
}
#[test]
fn curate_bars_1m_binary_parse_error_prints_usage_hides_secret_and_writes_nothing() {
let tmp = TempDir::new("parse-error");
let fx = write_nvda_fixture(tmp.path());
let out = tmp.path().join("out");
let secret = "--mongo-uri=mongodb://user:s3cr3t_pw@localhost:27017/stock";
let output = run_bin(&bin_args(&fx, &out, "1750000000000000", &[secret]));
assert!(!output.status.success());
assert!(output.stdout.is_empty());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("file mode only"), "stderr: {stderr}");
assert!(stderr.contains("curate-bars-1m --eod-intraday"), "usage expected: {stderr}");
assert!(!stderr.contains("s3cr3t_pw"), "secret leaked: {stderr}");
assert!(!out.exists(), "parse failure must not create --out");
// Missing --known-at-micros: no clock fallback, nothing written.
let mut argv = bin_args(&fx, &out, "1", &[]);
argv.truncate(argv.len() - 2);
let output = run_bin(&argv);
assert!(!output.status.success());
assert!(String::from_utf8_lossy(&output.stderr)
.contains("missing required flag '--known-at-micros'"));
assert!(!out.exists());
}