From 0fa33eadcc2a723a212df2e352ca794d77457868 Mon Sep 17 00:00:00 2001 From: Studio Date: Wed, 2 Dec 2026 16:21:51 -0500 Subject: [PATCH] T-178: tests --- tools/curate-etl/src/bson_stream_tests.rs | 207 ++++++++++++++++++++++ 1 file changed, 207 insertions(+) diff --git a/tools/curate-etl/src/bson_stream_tests.rs b/tools/curate-etl/src/bson_stream_tests.rs index 042e8c83..a903b778 100644 --- a/tools/curate-etl/src/bson_stream_tests.rs +++ b/tools/curate-etl/src/bson_stream_tests.rs @@ -3625,3 +3625,210 @@ fn te_data_stream_returns_terminal_errors_for_truncated_and_oversized_docs() { } if offset == oversize_offset )); } + +/// Builds a two-point teData doc whose byte length depends only on the string lengths, so +/// fixtures can be made of equal-length documents and chunk layouts are exact. +fn make_fixed_len_te_data_doc(te_symbol: &str, v0: f64, v1: f64) -> Vec { + make_te_data_doc( + Some(te_symbol), + "Canada", + "Inflation Rate", + &[("2024-01-31", v0), ("2024-02-29", v1)], + &[], + ) +} + +#[test] +fn te_data_stream_rebases_errors_at_nonzero_local_offsets_inside_later_multi_doc_chunks() { + // Arrange: six equal-length docs, chunk capacity = 2 docs, so every chunk holds exactly two + // documents and each offending document sits at local offset L inside a chunk with base 2L/4L. + // This separates `chunk.start_offset` from the in-chunk local offset, which single-doc chunks + // (local offset 0) cannot do. + let doc_a = make_fixed_len_te_data_doc("TEDOCA", 1.0, 1.5); + let len = doc_a.len(); + let doc_b = make_fixed_len_te_data_doc("TEDOCB", 2.0, 2.5); + let doc_c = make_fixed_len_te_data_doc("TEDOCC", 3.0, 3.5); + let doc_d_corrupt_top = make_corrupt_bson_doc(len); + let doc_e = make_fixed_len_te_data_doc("TEDOCE", 5.0, 5.5); + let mut doc_f_corrupt_point = make_fixed_len_te_data_doc("TEDOCF", 6.0, 6.5); + let second_d_pos = doc_f_corrupt_point + .windows(3) + .enumerate() + .filter(|(_, w)| *w == b"\x02d\0") + .nth(1) + .map(|(idx, _)| idx) + .expect("locate second point's 'd' element"); + doc_f_corrupt_point[second_d_pos] = 0x7E; + + let docs = [ + &doc_a, + &doc_b, + &doc_c, + &doc_d_corrupt_top, + &doc_e, + &doc_f_corrupt_point, + ]; + for d in docs { + assert_eq!(d.len(), len, "fixture docs must be equal length"); + } + let fixture: Vec = docs.iter().flat_map(|d| d.iter().copied()).collect(); + let opts = BsonStreamOptions { + chunk_capacity_bytes: 2 * len, + max_doc_bytes: 4096, + }; + + // Pin the chunk layout the assertions below rely on. + let mut layout_reader = + RawBsonChunkReader::with_options(Cursor::new(fixture.as_slice()), opts).unwrap(); + let mut layout = Vec::new(); + while let Some(chunk) = layout_reader.next_chunk().unwrap() { + layout.push((chunk.start_offset, chunk.bytes.len())); + } + let l = len as u64; + assert_eq!( + layout, + vec![(0, 2 * len), (2 * l, 2 * len), (4 * l, 2 * len)] + ); + + // Act: both entry points. + let mut stream_points = Vec::new(); + let mut stream_errs = Vec::new(); + let stream_stats = decode_te_data_stream( + Cursor::new(fixture.as_slice()), + opts, + |p| stream_points.push(owned_te_data_point(p)), + |off, err| stream_errs.push((off, err)), + ) + .unwrap(); + + let mut chunk_reader = + RawBsonChunkReader::with_options(Cursor::new(fixture.as_slice()), opts).unwrap(); + let mut chunk_points = Vec::new(); + let mut chunk_errs = Vec::new(); + let chunk_stats = decode_te_data_chunks( + &mut chunk_reader, + |p| chunk_points.push(owned_te_data_point(p)), + |off, err| chunk_errs.push((off, err)), + ) + .unwrap(); + + // Assert: absolute offsets 3L and 5L both in the callback argument and the embedded error. + assert_eq!(stream_errs.len(), 2, "{stream_errs:?}"); + match &stream_errs[0] { + (abs, BsonTeDataError::CorruptBson { offset, .. }) => { + assert_eq!(*abs, 3 * l); + assert_eq!(*offset as u64, 3 * l); + } + other => panic!("expected CorruptBson at {}, got {other:?}", 3 * l), + } + match &stream_errs[1] { + ( + abs, + BsonTeDataError::CorruptPointBson { + offset, + point_index, + .. + }, + ) => { + assert_eq!(*abs, 5 * l); + assert_eq!(*offset as u64, 5 * l); + assert_eq!(*point_index, 1); + } + other => panic!("expected CorruptPointBson at {}, got {other:?}", 5 * l), + } + assert_eq!(chunk_errs, stream_errs); + + let symbols: Vec<(&str, u64)> = stream_points + .iter() + .map(|p| (p.te_symbol.as_str(), p.value_bits)) + .collect(); + assert_eq!( + symbols, + vec![ + ("TEDOCA", 1.0_f64.to_bits()), + ("TEDOCA", 1.5_f64.to_bits()), + ("TEDOCB", 2.0_f64.to_bits()), + ("TEDOCB", 2.5_f64.to_bits()), + ("TEDOCC", 3.0_f64.to_bits()), + ("TEDOCC", 3.5_f64.to_bits()), + ("TEDOCE", 5.0_f64.to_bits()), + ("TEDOCE", 5.5_f64.to_bits()), + ("TEDOCF", 6.0_f64.to_bits()), + ] + ); + assert_eq!(chunk_points, stream_points); + + assert_eq!(stream_stats, chunk_stats); + assert_eq!(stream_stats.docs_in, 6); + assert_eq!(stream_stats.docs_accepted, 5); + assert_eq!(stream_stats.rejected_corrupt_document, 1); + assert_eq!(stream_stats.points_in, 10); + assert_eq!(stream_stats.points_decoded, 9); + assert_eq!(stream_stats.rejected_corrupt_point, 1); + assert_eq!( + stream_stats.docs_in, + stream_stats.docs_accepted + stream_stats.docs_rejected_total() + ); + assert_eq!( + stream_stats.points_in, + stream_stats.points_decoded + stream_stats.rejected_total() + ); + assert_eq!(chunk_reader.docs_framed(), 6); +} + +#[test] +fn te_data_rebase_preserves_payload_fields_and_saturates_offsets() { + // CorruptBson keeps its detail; CorruptPointBson keeps point_index and detail. + assert_eq!( + super::rebase_te_data_error( + 4_096, + BsonTeDataError::CorruptBson { + offset: 10, + detail: "bad top".to_owned(), + }, + ), + BsonTeDataError::CorruptBson { + offset: 4_106, + detail: "bad top".to_owned(), + } + ); + assert_eq!( + super::rebase_te_data_error( + 4_096, + BsonTeDataError::CorruptPointBson { + offset: 0, + point_index: 7, + detail: "bad point".to_owned(), + }, + ), + BsonTeDataError::CorruptPointBson { + offset: 4_096, + point_index: 7, + detail: "bad point".to_owned(), + } + ); + // Overflowing absolute offsets saturate instead of wrapping (shared `rebase_offset` policy). + assert_eq!( + super::rebase_te_data_error( + u64::MAX, + BsonTeDataError::CorruptBson { + offset: 1, + detail: String::new(), + }, + ), + BsonTeDataError::CorruptBson { + offset: usize::MAX, + detail: String::new(), + } + ); + // Point-level variants without an offset pass through unchanged. + let passthrough = BsonTeDataError::WrongPointFieldType { + point_index: 2, + field: "v", + actual: bson::spec::ElementType::String, + }; + assert_eq!( + super::rebase_te_data_error(9_999, passthrough.clone()), + passthrough + ); +}