T-183: tests

This commit is contained in:
Studio 2026-12-02 19:02:07 -05:00
parent 0fb55e9777
commit 749323080a

View file

@ -1309,3 +1309,279 @@ fn file_mode_reports_staging_unlink_failures_with_paths_on_flush_error() {
other => panic!("expected RollbackFailed, got {other:?}"),
}
}
// ---------------------------------------------------------------------------------------------
// QA edge cases (T-183)
// ---------------------------------------------------------------------------------------------
fn snapshot_dir(dir: &Path) -> Vec<(String, Vec<u8>)> {
list_dir_names(dir)
.into_iter()
.map(|name| {
let bytes = fs::read(dir.join(&name)).unwrap();
(name, bytes)
})
.collect()
}
#[test]
fn rerun_into_published_out_dir_fails_closed_and_preserves_first_run_bytes() {
let dir = TempDirGuard::new("fx1m-qa-rerun");
let intraday_path = dir.path().join("eodIntraday1M.bson");
fs::write(
&intraday_path,
concat_docs(&[
make_intraday_doc(EOD_CAD_FOREX_CODE, 1_718_700_000, 1.37, 1.371, 1.369, 1.3705, 0.0),
make_intraday_doc(EOD_CAD_FOREX_CODE, 1_718_700_060, 1.3705, 1.3715, 1.37, 1.371, 0.0),
]),
)
.unwrap();
let out_dir = dir.path().join("out");
let args = make_args(intraday_path, out_dir.clone(), 2, BsonStreamOptions::default());
let first = run_curate_fx_1m(&args).expect("first run succeeds");
assert_eq!(first.rows_written, 2);
let before = snapshot_dir(&out_dir);
assert_eq!(
before.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>(),
vec!["fx_1m.shard-00.ilp", "fx_1m.shard-01.ilp"]
);
let err = run_curate_fx_1m(&args).unwrap_err();
let expected = out_dir.join("fx_1m.shard-00.ilp");
assert!(
matches!(err, RunCurateFx1mError::OutputFileExists { ref path } if path == &expected),
"{err:?}"
);
assert_eq!(snapshot_dir(&out_dir), before, "rerun must not touch published shards");
}
#[test]
fn preflight_is_table_scoped_and_ignores_non_shard_names() {
let dir = TempDirGuard::new("fx1m-qa-preflight-scope");
let intraday_path = dir.path().join("eodIntraday1M.bson");
fs::write(
&intraday_path,
concat_docs(&[make_intraday_doc(
EOD_CAD_FOREX_CODE,
1_718_800_000,
1.37,
1.371,
1.369,
1.3705,
0.0,
)]),
)
.unwrap();
let out_dir = dir.path().join("out");
fs::create_dir_all(&out_dir).unwrap();
// None of these are `fx_1m.shard-<ASCII digits>.ilp`, so they must not block the run.
let bystanders = [
"fx_1m.shard-.ilp",
"fx_1m.shard-0a.ilp",
"fx_1m.shard-00.ilp.bak",
"xfx_1m.shard-00.ilp",
".fx_1m.shard-00.stale.tmp",
"bars_1m.shard-00.ilp",
"bars_1m.shard-7.ilp",
];
for name in bystanders {
fs::write(out_dir.join(name), name.as_bytes()).unwrap();
}
let report = run_curate_fx_1m(&make_args(
intraday_path,
out_dir.clone(),
1,
BsonStreamOptions::default(),
))
.expect("non-shard names must not trip fx_1m preflight");
assert_eq!(report.rows_written, 1);
for name in bystanders {
assert_eq!(fs::read(out_dir.join(name)).unwrap(), name.as_bytes(), "{name} touched");
}
let mut expected: Vec<String> = bystanders.iter().map(|s| s.to_string()).collect();
expected.push("fx_1m.shard-00.ilp".to_string());
expected.sort();
assert_eq!(list_dir_names(&out_dir), expected);
// The parameterised matcher must not leak across tables (bars_1m regression guard).
use crate::ilp::TABLE_BARS_1M;
use crate::run_curate_bars_1m::is_final_shard_file_name_for_table;
use std::ffi::OsStr;
assert!(is_final_shard_file_name_for_table(TABLE_FX_1M, OsStr::new("fx_1m.shard-123.ilp")));
assert!(!is_final_shard_file_name_for_table(TABLE_BARS_1M, OsStr::new("fx_1m.shard-00.ilp")));
assert!(!is_final_shard_file_name_for_table(TABLE_FX_1M, OsStr::new("bars_1m.shard-00.ilp")));
assert!(preflight_shard_out_dir(&out_dir, TABLE_BARS_1M).is_err());
let fx_only = dir.path().join("fx-only");
fs::create_dir_all(&fx_only).unwrap();
fs::write(fx_only.join("fx_1m.shard-00.ilp"), b"x").unwrap();
assert!(
preflight_shard_out_dir(&fx_only, TABLE_BARS_1M).is_ok(),
"fx_1m shards must not block a bars_1m run"
);
}
#[test]
fn with_readers_truncated_dump_keeps_path_and_emits_nothing_to_sinks() {
let valid = make_intraday_doc(EOD_CAD_FOREX_CODE, 1_718_900_000, 1.37, 1.371, 1.369, 1.3705, 0.0);
let mut bytes = concat_docs(&[valid.clone(), valid]);
let first_len = bytes.len() / 2;
bytes.truncate(bytes.len() - 3);
let mut sink0 = Vec::new();
let mut sink1 = Vec::new();
let writer =
IlpShardedWriter::new(vec![&mut sink0, &mut sink1], IlpWriterOptions::default()).unwrap();
let path = PathBuf::from("/dumps/eodIntraday1M.bson");
let args = make_args(path.clone(), PathBuf::from("unused"), 2, BsonStreamOptions::default());
let err = run_curate_fx_1m_with_readers(&args, Cursor::new(bytes), writer).unwrap_err();
match &err {
RunCurateFx1mError::BsonStream { label, path: p, .. } => {
assert_eq!(*label, "eod-intraday");
assert_eq!(p, &path);
}
other => panic!("expected BsonStream, got {other:?}"),
}
let rendered = err.to_string();
assert!(rendered.contains("/dumps/eodIntraday1M.bson"), "{rendered}");
assert!(rendered.contains(&first_len.to_string()), "offset missing: {rendered}");
// The stage only writes after clean EOF, so a fatal stream error leaves sinks untouched.
assert!(sink0.is_empty() && sink1.is_empty());
}
#[test]
fn three_digit_shard_indices_keep_min_width_two_naming() {
let dir = TempDirGuard::new("fx1m-qa-wide");
let intraday_path = dir.path().join("eodIntraday1M.bson");
fs::write(
&intraday_path,
concat_docs(&[make_intraday_doc(
EOD_CAD_FOREX_CODE,
1_719_000_000,
1.37,
1.371,
1.369,
1.3705,
0.0,
)]),
)
.unwrap();
let out_dir = dir.path().join("out");
let shard_count = 101usize;
let report = run_curate_fx_1m(&make_args(
intraday_path,
out_dir.clone(),
shard_count,
BsonStreamOptions::default(),
))
.unwrap();
assert_report_conservation(&report);
let mut expected: Vec<String> =
(0..shard_count).map(|i| format!("fx_1m.shard-{i:02}.ilp")).collect();
expected.sort();
assert_eq!(list_dir_names(&out_dir), expected);
assert!(list_dir_names(&out_dir).contains(&"fx_1m.shard-100.ilp".to_string()));
let target = shard_for_fx_pair(FX_PAIR_USDCAD, shard_count);
for i in 0..shard_count {
let len = fs::metadata(out_dir.join(format!("fx_1m.shard-{i:02}.ilp"))).unwrap().len();
assert_eq!(len > 0, i == target, "shard {i} non-empty mismatch (target {target})");
assert_eq!(report.ilp_stats.per_shard_rows[i], usize::from(i == target));
}
}
/// Streams `n` generated non-FX docs followed by FX docs without materialising the dump.
struct GeneratedDump {
docs: std::vec::IntoIter<Vec<u8>>,
current: Vec<u8>,
pos: usize,
}
impl io::Read for GeneratedDump {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
while self.pos == self.current.len() {
match self.docs.next() {
Some(next) => {
self.current = next;
self.pos = 0;
}
None => return Ok(0),
}
}
let n = buf.len().min(self.current.len() - self.pos);
buf[..n].copy_from_slice(&self.current[self.pos..self.pos + n]);
self.pos += n;
Ok(n)
}
}
#[test]
fn many_wrong_code_docs_across_tiny_chunks_are_only_counted() {
let wrong = make_intraday_doc("AAPL.US", 1_719_100_000, 195.0, 196.0, 194.5, 195.5, 1.0);
let fx_a = make_intraday_doc(EOD_CAD_FOREX_CODE, 1_719_100_060, 1.37, 1.371, 1.369, 1.3705, 0.0);
let fx_b = make_intraday_doc(EOD_CAD_FOREX_CODE, 1_719_100_000, 1.36, 1.361, 1.359, 1.3605, 0.0);
let n_wrong = 20_000usize;
let mut docs: Vec<Vec<u8>> = vec![fx_a.as_bytes().to_vec()];
docs.extend(std::iter::repeat_n(wrong.as_bytes().to_vec(), n_wrong));
docs.push(fx_b.as_bytes().to_vec());
let reader = GeneratedDump {
docs: docs.into_iter(),
current: Vec::new(),
pos: 0,
};
let mut sink = Vec::new();
let writer = IlpShardedWriter::new(vec![&mut sink], IlpWriterOptions::default()).unwrap();
let opts = BsonStreamOptions {
chunk_capacity_bytes: wrong.as_bytes().len() * 4,
..BsonStreamOptions::default()
};
let args = make_args(PathBuf::from("gen.bson"), PathBuf::from("unused"), 1, opts);
let report = run_curate_fx_1m_with_readers(&args, reader, writer).unwrap();
assert_report_conservation(&report);
assert_eq!(report.decode.docs_seen, n_wrong + 2);
assert_eq!(report.curate_stats.dropped_wrong_code, n_wrong);
assert_eq!(report.rows_written, 2);
// Rows separated by 20k wrong-code docs across many chunks are still globally sorted.
let rows = decode_ilp_fx_1m(&sink);
assert_eq!(
rows.iter().map(|r| r.ts).collect::<Vec<_>>(),
vec![1_719_100_000_000_000, 1_719_100_060_000_000]
);
}
#[test]
fn error_display_is_single_line_and_names_paths() {
let errs = [
RunCurateFx1mError::ZeroShardCount,
RunCurateFx1mError::ShardCountMismatch { expected: 2, actual: 1 },
RunCurateFx1mError::NonEmptyWriter { flushed_rows: 3, pending_rows: 4 },
RunCurateFx1mError::InvalidOutDir {
path: PathBuf::from("/o/d"),
detail: "not a dir".into(),
},
RunCurateFx1mError::OutputFileExists { path: PathBuf::from("/o/fx_1m.shard-00.ilp") },
RunCurateFx1mError::OpenInput {
label: "eod-intraday",
path: PathBuf::from("/i/x.bson"),
source: io::Error::new(io::ErrorKind::NotFound, "nf"),
},
RunCurateFx1mError::Io {
path: PathBuf::from("/o/tmp"),
source: io::Error::other("boom"),
},
];
for err in &errs {
let s = err.to_string();
assert!(!s.is_empty() && !s.contains('\n'), "bad display: {s:?}");
}
assert!(errs[2].to_string().contains("3 flushed") && errs[2].to_string().contains("4 pending"));
assert!(errs[3].to_string().contains("/o/d"));
assert!(errs[4].to_string().contains("/o/fx_1m.shard-00.ilp"));
assert!(errs[5].to_string().contains("/i/x.bson"));
assert!(errs[6].to_string().contains("/o/tmp"));
}