T-185: tests

This commit is contained in:
Studio 2026-12-02 19:23:52 -05:00
parent 859453edd7
commit f1a9915cdf

View file

@ -185,7 +185,10 @@ fn decode_ilp_bars_1m(data: &[u8]) -> Vec<DecodedIlpBar1m> {
_ => i += 1,
}
}
assert!(i < len && data[i] == b' ', "missing space after ILP tag set");
assert!(
i < len && data[i] == b' ',
"missing space after ILP tag set"
);
let tag_str = std::str::from_utf8(&data[tag_start..i]).expect("valid UTF-8 tag set");
i += 1;
@ -461,7 +464,9 @@ fn happy_path_file_mode_decodes_bit_identical_raw_ohlcv_date_boundary_and_determ
// Choose a raw close whose LE IEEE-754 bytes include 0x0a to prove binary ILP decoding does
// not split on embedded newlines.
let raw_close_with_lf = f64::from_le_bytes([0x0a, 0x11, 0x22, 0x33, 0x44, 0x55, 0x59, 0x40]);
assert!(raw_close_with_lf.is_finite() && raw_close_with_lf > 100.0 && raw_close_with_lf < 102.0);
assert!(
raw_close_with_lf.is_finite() && raw_close_with_lf > 100.0 && raw_close_with_lf < 102.0
);
let ts_pre = et_ts_millis(2024, 6, 10, 8, 15);
let ts_rth_old = et_ts_millis(2024, 6, 10, 10, 30);
@ -484,24 +489,10 @@ fn happy_path_file_mode_decodes_bit_identical_raw_ohlcv_date_boundary_and_determ
),
make_qm_intraday_doc_with_adjusted("REUSE", ts_pre, 99.5, 100.0, 99.0, 99.8, -0.0, 10.0),
make_qm_intraday_doc_with_adjusted(
"REUSE",
ts_rth_new,
15.25,
15.75,
15.0,
15.5,
8_000.0,
4.0,
"REUSE", ts_rth_new, 15.25, 15.75, 15.0, 15.5, 8_000.0, 4.0,
),
make_qm_intraday_doc_with_adjusted(
"AAPL",
ts_post,
210.0,
211.25,
209.5,
210.75,
4_200.0,
2.0,
"AAPL", ts_post, 210.0, 211.25, 209.5, 210.75, 4_200.0, 2.0,
),
];
let intraday_bytes = concat_docs(&docs);
@ -545,7 +536,10 @@ fn happy_path_file_mode_decodes_bit_identical_raw_ohlcv_date_boundary_and_determ
let path = out_dir_1.join(format!("bars_1m_qm.shard-{shard_idx:02}.ilp"));
let bytes = fs::read(&path).expect("read shard file");
let shard_rows = decode_ilp_bars_1m(&bytes);
assert_eq!(shard_rows.len(), report_1.ilp_stats.per_shard_rows[shard_idx]);
assert_eq!(
shard_rows.len(),
report_1.ilp_stats.per_shard_rows[shard_idx]
);
for row in &shard_rows {
assert_eq!(row.source, BARS_1M_SOURCE_QM);
assert_eq!(row.known_at, known_at_micros);
@ -581,6 +575,8 @@ fn happy_path_file_mode_decodes_bit_identical_raw_ohlcv_date_boundary_and_determ
assert_eq!(decoded_all[2].ts, ts_rth_new * 1_000);
assert_eq!(decoded_all[2].session, IntradaySession::Rth);
assert_eq!(decoded_all[2].open.to_bits(), 15.25_f64.to_bits());
assert_eq!(decoded_all[2].high.to_bits(), 15.75_f64.to_bits());
assert_eq!(decoded_all[2].low.to_bits(), 15.0_f64.to_bits());
assert_eq!(decoded_all[2].close.to_bits(), 15.5_f64.to_bits());
assert_eq!(decoded_all[2].volume.to_bits(), 8_000.0_f64.to_bits());
@ -589,6 +585,8 @@ fn happy_path_file_mode_decodes_bit_identical_raw_ohlcv_date_boundary_and_determ
assert_eq!(decoded_all[3].ts, ts_post * 1_000);
assert_eq!(decoded_all[3].session, IntradaySession::Post);
assert_eq!(decoded_all[3].open.to_bits(), 210.0_f64.to_bits());
assert_eq!(decoded_all[3].high.to_bits(), 211.25_f64.to_bits());
assert_eq!(decoded_all[3].low.to_bits(), 209.5_f64.to_bits());
assert_eq!(decoded_all[3].close.to_bits(), 210.75_f64.to_bits());
assert_eq!(decoded_all[3].volume.to_bits(), 4_200.0_f64.to_bits());
@ -644,10 +642,7 @@ fn bounded_chunk_streaming_matches_single_chunk_and_calls_finish_flush_once_per_
let dir = TempDirGuard::new("qm-bars1m-chunked");
let mapping_path = write_mapping_tsv(
dir.path(),
&[
"qm\tAAA\t2024-01-01\t\t11",
"qm\tBBB\t2024-01-01\t\t22",
],
&["qm\tAAA\t2024-01-01\t\t11", "qm\tBBB\t2024-01-01\t\t22"],
);
let known_at_micros = et_to_utc_micros(day(2024, 7, 2), 18, 0, 0).unwrap();
@ -841,8 +836,8 @@ fn conservation_counts_unmapped_semantic_decode_errors_collisions_and_cross_chun
}
#[test]
fn blank_mapping_source_rejected_before_opening_inputs_or_creating_out_dir_and_custom_namespace_works()
{
fn blank_mapping_source_rejected_before_opening_inputs_or_creating_out_dir_and_custom_namespace_works(
) {
let dir = TempDirGuard::new("qm-bars1m-source");
let known_at_micros = et_to_utc_micros(day(2024, 6, 12), 18, 0, 0).unwrap();
@ -868,10 +863,11 @@ fn blank_mapping_source_rejected_before_opening_inputs_or_creating_out_dir_and_c
"out_dir must not be created on blank mapping_source"
);
let reader_err = run_curate_qm_bars_1m_with_readers(&args, PanicReader, |_idx| -> Vec<u8> {
panic!("sink factory must not be called on blank mapping_source");
})
.expect_err("blank source must fail reader mode");
let reader_err =
run_curate_qm_bars_1m_with_readers(&args, PanicReader, |_idx| -> Vec<u8> {
panic!("sink factory must not be called on blank mapping_source");
})
.expect_err("blank source must fail reader mode");
assert!(matches!(
reader_err,
RunCurateQmBars1mError::EmptyMappingSource
@ -1049,7 +1045,10 @@ fn preflight_rejects_high_numbered_qm_shard_and_allows_eod_bars_1m_shard_coexist
RunCurateQmBars1mError::OutputFileExists { ref path } if path == &stale_qm_shard
));
assert_eq!(fs::read(&stale_qm_shard).unwrap(), stale_bytes);
assert_eq!(list_dir_names(&out_conflict), vec!["bars_1m_qm.shard-07.ilp"]);
assert_eq!(
list_dir_names(&out_conflict),
vec!["bars_1m_qm.shard-07.ilp"]
);
// (b) Pre-existing bars_1m.shard-00.ilp (EOD runner output) does NOT block the QM run and is
// left byte-identical.
@ -1367,12 +1366,11 @@ fn report_formatter_exposes_every_counter_and_isolates_elapsed_secs_on_timing_li
BsonStreamOptions::default(),
);
let report = run_curate_qm_bars_1m_with_readers(
&args,
Cursor::new(intraday_bytes.as_slice()),
|_idx| Vec::<u8>::new(),
)
.expect("format run should succeed");
let report =
run_curate_qm_bars_1m_with_readers(&args, Cursor::new(intraday_bytes.as_slice()), |_idx| {
Vec::<u8>::new()
})
.expect("format run should succeed");
let text = format_curate_qm_bars_1m_report(&report);
let lines: Vec<&str> = text.lines().collect();
@ -1428,3 +1426,321 @@ fn report_formatter_exposes_every_counter_and_isolates_elapsed_secs_on_timing_li
assert!(lines.last().unwrap().trim_start().starts_with("timing:"));
assert!(lines.last().unwrap().contains("elapsed_secs="));
}
// ---------------------------------------------------------------------------------------------
// QA additions (T-185): byte-level baselines, empty input, verbatim namespace, oversized docs.
// ---------------------------------------------------------------------------------------------
/// Runs the pure `curate_and_write_qm_bars_1m` stage once per given byte slice (each slice treated
/// as one chunk) against one shared writer, returning per-shard bytes.
fn pure_stage_shard_bytes(
chunks: &[&[u8]],
mapping: &ProviderMappingTable,
mapping_source: &str,
shard_count: usize,
known_at_micros: i64,
) -> Vec<Vec<u8>> {
let sinks: Vec<RecordingSink> = (0..shard_count).map(|_| RecordingSink::new()).collect();
let mut writer = IlpShardedWriter::new(sinks.clone(), IlpWriterOptions::default()).unwrap();
for chunk in chunks {
let mut bars = Vec::new();
decode_qm_intraday_buffer(
chunk,
|raw| bars.push(raw.into_eod_intraday_bar(|k, _| k)),
|_, _| {},
);
curate_and_write_qm_bars_1m(bars, mapping, mapping_source, &mut writer, known_at_micros)
.unwrap();
}
writer.finish().unwrap();
sinks
.iter()
.map(|sink| sink.bytes.lock().unwrap().clone())
.collect()
}
fn read_qm_shards(out_dir: &Path, shard_count: usize) -> Vec<Vec<u8>> {
(0..shard_count)
.map(|idx| fs::read(out_dir.join(format!("bars_1m_qm.shard-{idx:02}.ilp"))).unwrap())
.collect()
}
#[test]
fn file_mode_single_chunk_shards_match_pure_stage_byte_for_byte() {
let dir = TempDirGuard::new("qm-bars1m-qa-bytes");
let mapping_path = write_mapping_tsv(
dir.path(),
&[
"qm\tAAA\t2024-01-01\t\t11",
"qm\tBBB\t2024-01-01\t\t22",
"qm\tCCC\t2024-01-01\t\t33",
"qm\tDDD\t2024-01-01\t\t44",
],
);
let known_at_micros = et_to_utc_micros(day(2025, 3, 4), 18, 0, 0).unwrap();
// Unsorted input across several listings/sessions with irregular (non-round) doubles so a wrong
// fixed-width encoding of any single OHLCV field would change bytes even if aggregates match.
let mut docs = Vec::new();
for (i, code) in ["DDD", "AAA", "CCC", "BBB", "AAA", "DDD", "CCC", "BBB"]
.iter()
.enumerate()
{
let i = i as u8;
let base = 17.0 + f64::from(i) * 1.337;
docs.push(make_qm_intraday_doc_with_adjusted(
code,
et_ts_millis(2025, 3, 3, 7 + i, 3 * i),
base,
base + 0.123_456_789,
base - 0.987_654_321,
base + 0.000_001,
1_000.5 + f64::from(i),
3.0,
));
}
let bytes = concat_docs(&docs);
let intraday_path = dir.path().join("qm.bson");
fs::write(&intraday_path, &bytes).unwrap();
let shard_count = 3;
let out_dir = dir.path().join("out");
let report = run_curate_qm_bars_1m(&make_args(
intraday_path,
mapping_path.clone(),
out_dir.clone(),
DEFAULT_QM_BARS_1M_MAPPING_SOURCE,
shard_count,
known_at_micros,
BsonStreamOptions::default(),
))
.unwrap();
assert_report_conservation(&report);
assert_eq!(report.chunks, 1);
assert_eq!(report.rows_written, 8);
let mapping = load_provider_mapping_table(&mapping_path).unwrap();
let expected = pure_stage_shard_bytes(
&[bytes.as_slice()],
&mapping,
DEFAULT_QM_BARS_1M_MAPPING_SOURCE,
shard_count,
known_at_micros,
);
let actual = read_qm_shards(&out_dir, shard_count);
for idx in 0..shard_count {
assert_eq!(
actual[idx], expected[idx],
"shard {idx} bytes differ from pure stage"
);
}
assert!(expected.iter().filter(|b| !b.is_empty()).count() >= 2);
}
#[test]
fn file_mode_multi_chunk_shards_match_pure_stage_applied_per_document_chunk() {
let dir = TempDirGuard::new("qm-bars1m-qa-chunk-bytes");
let mapping_path = write_mapping_tsv(
dir.path(),
&["qm\tAAA\t2024-01-01\t\t11", "qm\tBBB\t2024-01-01\t\t22"],
);
let known_at_micros = et_to_utc_micros(day(2024, 9, 4), 18, 0, 0).unwrap();
// Equal-size docs (same code length) so chunk_capacity == doc_len yields one doc per chunk.
let docs: Vec<RawDocumentBuf> = (0..6u8)
.map(|m| {
let code = if m % 2 == 0 { "AAA" } else { "BBB" };
let base = 30.0 + f64::from(m) * 0.25;
make_qm_intraday_doc(
code,
et_ts_millis(2024, 9, 3, 15, 59 - m),
base,
base + 1.0,
base - 1.0,
base,
10.0,
)
})
.collect();
let doc_len = docs[0].as_bytes().len();
assert!(docs.iter().all(|d| d.as_bytes().len() == doc_len));
let bytes = concat_docs(&docs);
let intraday_path = dir.path().join("qm.bson");
fs::write(&intraday_path, &bytes).unwrap();
let out_dir = dir.path().join("out");
let report = run_curate_qm_bars_1m(&make_args(
intraday_path,
mapping_path.clone(),
out_dir.clone(),
DEFAULT_QM_BARS_1M_MAPPING_SOURCE,
2,
known_at_micros,
BsonStreamOptions {
chunk_capacity_bytes: doc_len,
max_doc_bytes: 1024 * 1024,
},
))
.unwrap();
assert_report_conservation(&report);
assert_eq!(report.chunks, 6);
assert_eq!(report.max_rows_per_chunk, 1);
assert_eq!(
report.curate_stats.listings_out, 6,
"per-chunk sum, not distinct"
);
let mapping = load_provider_mapping_table(&mapping_path).unwrap();
let chunks: Vec<&[u8]> = bytes.chunks(doc_len).collect();
let expected = pure_stage_shard_bytes(
&chunks,
&mapping,
DEFAULT_QM_BARS_1M_MAPPING_SOURCE,
2,
known_at_micros,
);
assert_eq!(read_qm_shards(&out_dir, 2), expected);
}
#[test]
fn empty_input_publishes_all_empty_shards_with_zero_counters() {
let dir = TempDirGuard::new("qm-bars1m-qa-empty");
let mapping_path = write_mapping_tsv(dir.path(), &["qm\tAAA\t2024-01-01\t\t1"]);
let intraday_path = dir.path().join("empty.bson");
fs::write(&intraday_path, b"").unwrap();
let out_dir = dir.path().join("out");
let report = run_curate_qm_bars_1m(&make_args(
intraday_path,
mapping_path,
out_dir.clone(),
DEFAULT_QM_BARS_1M_MAPPING_SOURCE,
3,
123,
BsonStreamOptions::default(),
))
.expect("empty input should succeed");
assert_report_conservation(&report);
assert_eq!(report.intraday_docs_framed, 0);
assert_eq!(report.intraday_bytes_read, 0);
assert_eq!(report.chunks, 0);
assert_eq!(report.max_rows_per_chunk, 0);
assert_eq!(report.rows_written, 0);
assert_eq!(report.ilp_stats.per_shard_rows, vec![0, 0, 0]);
assert_eq!(
list_dir_names(&out_dir),
vec![
"bars_1m_qm.shard-00.ilp",
"bars_1m_qm.shard-01.ilp",
"bars_1m_qm.shard-02.ilp",
]
);
assert!(read_qm_shards(&out_dir, 3).iter().all(Vec::is_empty));
}
#[test]
fn nonblank_mapping_source_is_forwarded_verbatim_without_trimming() {
let dir = TempDirGuard::new("qm-bars1m-qa-verbatim");
let mapping_path = write_mapping_tsv(dir.path(), &["qm\tAAA\t2024-01-01\t\t1"]);
let bytes = concat_docs(&[make_qm_intraday_doc(
"AAA",
et_ts_millis(2024, 6, 10, 10, 0),
10.0,
10.5,
9.8,
10.2,
100.0,
)]);
let args = make_args(
dir.path().join("in.bson"),
mapping_path,
dir.path().join("unused"),
" qm ",
1,
0,
BsonStreamOptions::default(),
);
let report = run_curate_qm_bars_1m_with_readers(&args, Cursor::new(bytes.as_slice()), |_| {
Vec::<u8>::new()
})
.expect("padded nonblank namespace is accepted");
assert_report_conservation(&report);
// " qm " is not "qm": the row must be unmapped, proving the value was not trimmed.
assert_eq!(report.curate_stats.dropped_unmapped, 1);
assert_eq!(report.rows_written, 0);
}
#[test]
fn doc_larger_than_chunk_capacity_succeeds_and_doc_over_max_doc_bytes_fails_clean() {
let dir = TempDirGuard::new("qm-bars1m-qa-oversize");
let mapping_path = write_mapping_tsv(dir.path(), &["qm\tAAA\t2024-01-01\t\t7"]);
let docs = vec![
make_qm_intraday_doc(
"AAA",
et_ts_millis(2024, 6, 10, 10, 0),
10.0,
10.5,
9.8,
10.2,
1.0,
),
make_qm_intraday_doc(
"AAA",
et_ts_millis(2024, 6, 10, 10, 1),
10.0,
10.5,
9.8,
10.2,
2.0,
),
];
let doc_len = docs[0].as_bytes().len();
let bytes = concat_docs(&docs);
let intraday_path = dir.path().join("in.bson");
fs::write(&intraday_path, &bytes).unwrap();
// Chunk capacity smaller than one document: reader grows for a single doc up to max_doc_bytes.
let out_ok = dir.path().join("out-ok");
let report = run_curate_qm_bars_1m(&make_args(
intraday_path.clone(),
mapping_path.clone(),
out_ok.clone(),
DEFAULT_QM_BARS_1M_MAPPING_SOURCE,
1,
0,
BsonStreamOptions {
chunk_capacity_bytes: 8,
max_doc_bytes: 1024 * 1024,
},
))
.expect("doc larger than chunk capacity must still stream");
assert_report_conservation(&report);
assert_eq!(report.rows_written, 2);
assert!(report.chunks >= 2);
assert_eq!(report.max_rows_per_chunk, 1);
// max_doc_bytes below the doc length: fatal at offset 0, nothing left behind.
let out_bad = dir.path().join("out-bad");
let err = run_curate_qm_bars_1m(&make_args(
intraday_path,
mapping_path,
out_bad.clone(),
DEFAULT_QM_BARS_1M_MAPPING_SOURCE,
2,
0,
BsonStreamOptions {
chunk_capacity_bytes: 64 * 1024,
max_doc_bytes: doc_len - 1,
},
))
.expect_err("doc over max_doc_bytes must fail");
assert!(
matches!(
err,
RunCurateQmBars1mError::BsonStream {
source: BsonStreamError::CorruptDocumentLength { offset: 0, .. },
..
}
),
"got {err:?}"
);
assert!(list_dir_names(&out_bad).is_empty());
}