diff --git a/tools/curate-etl/src/run_curate_macro_tests.rs b/tools/curate-etl/src/run_curate_macro_tests.rs index 1c7a691f..6399ef2e 100644 --- a/tools/curate-etl/src/run_curate_macro_tests.rs +++ b/tools/curate-etl/src/run_curate_macro_tests.rs @@ -1045,3 +1045,284 @@ fn report_formatting_is_deterministic_and_all_error_variants_are_single_line() { assert_eq!(err.source().is_some(), *expect_source, "source for {err:?}"); } } + +// --------------------------------------------------------------------------------------------- +// QA additions (T-199): contract literals pinned independently of production constants, extra +// structural-corruption shapes, file-mode path attachment, and builder fail-closed paths. +// --------------------------------------------------------------------------------------------- + +#[test] +fn published_rows_match_literal_contract_timestamps_and_source_tag_for_all_lag_classes() { + // Literal expectations so a change to lag constants or `MACRO_KNOWN_AT_SOURCE` is caught here + // rather than silently moving the fixture with the output. + // 2024-01-24 +1d -> 2024-01-25T00:00:00Z = 1_706_140_800_000_000 us + // 2024-01-31 +35d -> 2024-03-06T00:00:00Z = 1_709_683_200_000_000 us + // 2023-12-31 +65d -> 2024-03-05T00:00:00Z = 1_709_596_800_000_000 us + // 2023-09-30 +90d -> 2023-12-29T00:00:00Z = 1_703_808_000_000_000 us + let bson_bytes = concat_docs(&[ + make_te_data_doc("USGDPQ", "United States", "GDP Growth Rate", &[("2023-09-30", 4.9)], &[]), + make_te_data_doc("CABOC", "Canada", "Interest Rate", &[("2024-01-24", 5.0)], &[]), + make_te_data_doc("USCPIYOY", "United States", "Inflation Rate", &[("2024-01-31", 3.1)], &[]), + make_te_data_doc("CAGDPQ", "Canada", "GDP Growth Rate", &[("2023-12-31", 1.0)], &[]), + ]); + let map = format!( + "{MACRO_SERIES_MAP_HEADER}\n\ + USGDPQ\tUnited States\tGDP Growth Rate\tUS\tgdp_qoq\tgdp90\n\ + CABOC\tCanada\tInterest Rate\tCA\tpolicy_rate\tpolicy_rate\n\ + USCPIYOY\tUnited States\tInflation Rate\tUS\tcpi_yoy\tmonthly_release\n\ + CAGDPQ\tCanada\tGDP Growth Rate\tCA\tgdp_qoq\tgdp65\n" + ); + + let out = TempDirGuard::new("literal-contract"); + let report = run_curate_macro_with_readers(bson_bytes.as_slice(), map.as_bytes(), out.path()) + .expect("run should succeed"); + let rows = read_parquet_rows(&out.path().join("macro").join("part.parquet")); + assert_report_conservation(&report, rows.len()); + + let actual: Vec<(String, String, i32, i64, String, f64)> = rows + .iter() + .map(|r| { + ( + r.country_iso.to_string(), + r.indicator.to_string(), + r.period_date, + r.known_at_micros, + r.known_at_source.to_string(), + r.value, + ) + }) + .collect(); + let src = "publication_lag_estimate".to_owned(); + let expected = vec![ + ("CA".to_owned(), "gdp_qoq".to_owned(), 19_722, 1_709_596_800_000_000_i64, src.clone(), 1.0), + ("CA".to_owned(), "policy_rate".to_owned(), 19_746, 1_706_140_800_000_000, src.clone(), 5.0), + ("US".to_owned(), "cpi_yoy".to_owned(), 19_753, 1_709_683_200_000_000, src.clone(), 3.1), + ("US".to_owned(), "gdp_qoq".to_owned(), 19_630, 1_703_808_000_000_000, src, 4.9), + ]; + assert_eq!(actual, expected); + for (_, _, _, known_at, _, _) in &actual { + assert_eq!(known_at % MICROS_PER_DAY, 0, "known_at must be 00:00:00 UTC"); + } +} + +#[test] +fn file_mode_structural_bson_error_names_te_data_path_and_preserves_existing_output() { + let dir = TempDirGuard::new("file-corrupt-bson"); + let te_data_path = dir.path().join("teData.bson"); + let map_path = dir.path().join("series_map.tsv"); + let out_dir = dir.path().join("out"); + + let mut bson_bytes = make_te_data_doc( + "FDTR", + "United States", + "Interest Rate", + &[("2024-03-20", 5.5)], + &[], + ) + .as_bytes() + .to_vec(); + let corrupt_offset = bson_bytes.len(); + // Declared length below the 5-byte BSON minimum. + bson_bytes.extend_from_slice(&2_i32.to_le_bytes()); + bson_bytes.extend_from_slice(&[0u8; 8]); + fs::write(&te_data_path, &bson_bytes).expect("write bson"); + fs::write( + &map_path, + format!( + "{MACRO_SERIES_MAP_HEADER}\n\ + FDTR\tUnited States\tInterest Rate\tUS\tpolicy_rate\tpolicy_rate\n" + ), + ) + .expect("write map"); + let (part_path, sentinel) = seed_existing_parquet(&out_dir); + + let err = run_curate_macro(&CurateMacroArgs { + te_data: te_data_path.clone(), + series_map: map_path, + out_dir: out_dir.clone(), + }) + .expect_err("corrupt document length must fail closed"); + + assert!( + matches!( + &err, + RunCurateMacroError::CorruptTeDataBson { + path: Some(p), + offset, + source: BsonTeDataError::CorruptDocumentLength { .. } + | BsonTeDataError::TruncatedDocument { .. } + | BsonTeDataError::CorruptBson { .. }, + } if p == &te_data_path && *offset == corrupt_offset + ), + "unexpected error: {err:?}" + ); + let msg = err.to_string(); + assert_single_line(&msg); + assert!(msg.contains("te-data"), "msg: {msg}"); + assert!(msg.contains(&te_data_path.display().to_string()), "msg: {msg}"); + assert_eq!(fs::read(&part_path).expect("read sentinel"), sentinel); +} + +#[test] +fn trailing_partial_length_prefix_after_valid_doc_fails_closed_without_publishing() { + let valid = make_te_data_doc( + "FDTR", + "United States", + "Interest Rate", + &[("2024-03-20", 5.5)], + &[], + ); + let mut bson_bytes = valid.as_bytes().to_vec(); + // Fewer than 4 bytes left: cannot even read a length prefix. + bson_bytes.extend_from_slice(&[0x10, 0x00]); + let map = format!( + "{MACRO_SERIES_MAP_HEADER}\n\ + FDTR\tUnited States\tInterest Rate\tUS\tpolicy_rate\tpolicy_rate\n" + ); + + let out = TempDirGuard::new("partial-length-prefix"); + let err = run_curate_macro_with_readers(bson_bytes.as_slice(), map.as_bytes(), out.path()) + .expect_err("partial trailing length prefix must fail closed"); + assert!( + matches!(err, RunCurateMacroError::CorruptTeDataBson { .. }), + "unexpected error: {err:?}" + ); + assert_single_line(&err.to_string()); + assert!(!out.path().join("macro").exists()); +} + +#[test] +fn file_mode_series_map_error_is_reported_before_te_data_is_read() { + // A bad map must fail closed even when te_data is garbage: map validation runs first. + let dir = TempDirGuard::new("file-bad-map-first"); + let te_data_path = dir.path().join("teData.bson"); + let map_path = dir.path().join("series_map.tsv"); + let out_dir = dir.path().join("out"); + fs::write(&te_data_path, [0xffu8; 3]).expect("write garbage bson"); + fs::write(&map_path, "not a header\n").expect("write bad map"); + + let err = run_curate_macro(&CurateMacroArgs { + te_data: te_data_path, + series_map: map_path, + out_dir: out_dir.clone(), + }) + .expect_err("bad map must fail"); + assert!( + matches!( + err, + RunCurateMacroError::SeriesMapBadHeader { + line: 1, + found: Some(_) + } + ), + "unexpected error: {err:?}" + ); + assert!(!out_dir.exists()); +} + +#[test] +fn tsv_header_with_utf8_bom_or_trailing_tab_is_rejected_at_line_1() { + for header in [ + format!("\u{feff}{MACRO_SERIES_MAP_HEADER}\n"), + format!("{MACRO_SERIES_MAP_HEADER}\t\n"), + format!("{}\n", MACRO_SERIES_MAP_HEADER.to_uppercase()), + ] { + let err = load_macro_series_map_tsv(header.as_bytes()).expect_err("header must fail"); + assert!( + matches!(err, RunCurateMacroError::SeriesMapBadHeader { line: 1, .. }), + "unexpected error for {header:?}: {err:?}" + ); + assert_single_line(&err.to_string()); + } +} + +#[test] +fn duplicate_key_detected_on_later_physical_line_with_crlf_endings() { + let tsv = format!( + "{MACRO_SERIES_MAP_HEADER}\r\n\ + A\tB\tC\tUS\tx\tpolicy_rate\r\n\ + D\tE\tF\tUS\ty\tgdp65\r\n\ + A\tB\tC\tCA\tz\tgdp90\r\n" + ); + let err = load_macro_series_map_tsv(tsv.as_bytes()).expect_err("duplicate must fail"); + assert!( + matches!( + &err, + RunCurateMacroError::SeriesMapDuplicateKey { line: 4, te_symbol, country, category } + if te_symbol == "A" && country == "B" && category == "C" + ), + "unexpected error: {err:?}" + ); + // Same symbol + country but a different category is a distinct key. + let ok = format!( + "{MACRO_SERIES_MAP_HEADER}\n\ + A\tB\tC\tUS\tx\tpolicy_rate\n\ + A\tB\tc\tUS\tx2\tpolicy_rate\n" + ); + assert_eq!(load_macro_series_map_tsv(ok.as_bytes()).expect("distinct keys").len(), 2); +} + +#[test] +fn split_series_duplicates_collapse_and_conflicting_values_fail_closed() { + let map = format!( + "{MACRO_SERIES_MAP_HEADER}\n\ + FDTR\tUnited States\tInterest Rate\tUS\tpolicy_rate\tpolicy_rate\n\ + FDTR2\tUnited States\tFed Funds\tUS\tpolicy_rate\tpolicy_rate\n" + ); + + // Two provider series mapping to the same target with bitwise-identical values collapse. + let same = concat_docs(&[ + make_te_data_doc("FDTR", "United States", "Interest Rate", &[("2024-03-20", 5.5), ("2024-01-31", f64::NAN)], &[]), + make_te_data_doc("FDTR2", "United States", "Fed Funds", &[("2024-03-20", 5.5)], &[]), + ]); + let out = TempDirGuard::new("dup-collapse"); + let report = run_curate_macro_with_readers(same.as_slice(), map.as_bytes(), out.path()) + .expect("identical duplicates should collapse"); + let rows = read_parquet_rows(&out.path().join("macro").join("part.parquet")); + assert_eq!(rows.len(), 1); + assert_eq!(report.curate_stats.builder.skipped_duplicate, 1); + assert_eq!(report.curate_stats.builder.skipped_non_finite_value, 1); + assert_report_conservation(&report, rows.len()); + + // Conflicting values on the same (country_iso, indicator, period_date) fail before publishing. + let conflict = concat_docs(&[ + make_te_data_doc("FDTR", "United States", "Interest Rate", &[("2024-03-20", 5.5)], &[]), + make_te_data_doc("FDTR2", "United States", "Fed Funds", &[("2024-03-20", 5.25)], &[]), + ]); + for seeded in [false, true] { + let out = TempDirGuard::new("dup-conflict"); + let existing = seeded.then(|| seed_existing_parquet(out.path())); + let err = run_curate_macro_with_readers(conflict.as_slice(), map.as_bytes(), out.path()) + .expect_err("conflicting duplicate must fail"); + assert!( + matches!( + err, + RunCurateMacroError::CurateWrite(CurateMacroWriteError::Curate(_)) + ), + "unexpected error: {err:?}" + ); + assert_single_line(&err.to_string()); + if let Some((part_path, sentinel)) = existing { + assert_eq!(fs::read(&part_path).expect("read sentinel"), sentinel); + } else { + assert!(!out.path().join("macro").exists()); + } + } +} + +#[test] +fn successful_run_replaces_preexisting_part_parquet() { + let out = TempDirGuard::new("replace-existing"); + let (part_path, sentinel) = seed_existing_parquet(out.path()); + let doc = make_te_data_doc("FDTR", "United States", "Interest Rate", &[("2024-03-20", 5.5)], &[]); + let map = format!( + "{MACRO_SERIES_MAP_HEADER}\n\ + FDTR\tUnited States\tInterest Rate\tUS\tpolicy_rate\tpolicy_rate\n" + ); + run_curate_macro_with_readers(doc.as_bytes(), map.as_bytes(), out.path()) + .expect("run should succeed"); + assert_ne!(fs::read(&part_path).expect("read part"), sentinel); + let rows = read_parquet_rows(&part_path); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].indicator, "policy_rate"); +}