T-176: tests

This commit is contained in:
Studio 2026-12-02 15:40:14 -05:00
parent 193798f8b0
commit bdebad6025

View file

@ -185,7 +185,11 @@ fn read_parquet_rows(path: &Path) -> Vec<MacroParquetRow> {
_ => panic!("expected BYTE_ARRAY column at index {col_idx}"),
}
vals.into_iter()
.map(|b| std::str::from_utf8(b.data()).expect("valid utf8").to_owned())
.map(|b| {
std::str::from_utf8(b.data())
.expect("valid utf8")
.to_owned()
})
.collect()
};
@ -297,7 +301,13 @@ fn sorted_dir_entries(dir: &Path) -> Vec<String> {
fn seed_existing_parquet(out_dir: &Path) -> (PathBuf, Vec<u8>, Vec<String>) {
let seed = vec![
expected_row("CA", "policy_rate", day(2024, 1, 24), POLICY_RATE_LAG_DAYS, 5.0),
expected_row(
"CA",
"policy_rate",
day(2024, 1, 24),
POLICY_RATE_LAG_DAYS,
5.0,
),
expected_row(
"US",
"cpi_yoy",
@ -369,29 +379,31 @@ fn decode_te_data_bson_curate_and_write_all_four_lag_classes_without_forecast_le
total_points_in += stats.points_in;
total_points_decoded += stats.points_decoded;
}
assert!(decode_errors.is_empty(), "unexpected decode errors: {decode_errors:?}");
assert!(
decode_errors.is_empty(),
"unexpected decode errors: {decode_errors:?}"
);
// Only the 8 historical data points were decoded; none of the 5 forecast points leaked.
assert_eq!(total_points_in, 8);
assert_eq!(total_points_decoded, 8);
assert_eq!(decoded_points.len(), 8);
let series_map = |te_symbol: &str, country: &str, category: &str| match (
te_symbol, country, category,
) {
("CABOCRATE", "Canada", "Interest Rate") => {
Some(target("CA", "policy_rate", MacroLagClass::PolicyRate))
}
("USCPIYOY", "United States", "Inflation Rate") => {
Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease))
}
("USGDPQOQ", "United States", "GDP Growth Rate") => {
Some(target("US", "gdp_qoq", MacroLagClass::Gdp65))
}
("CAGDPQOQ", "Canada", "GDP Growth Rate") => {
Some(target("CA", "gdp_qoq", MacroLagClass::Gdp90))
}
_ => None,
};
let series_map =
|te_symbol: &str, country: &str, category: &str| match (te_symbol, country, category) {
("CABOCRATE", "Canada", "Interest Rate") => {
Some(target("CA", "policy_rate", MacroLagClass::PolicyRate))
}
("USCPIYOY", "United States", "Inflation Rate") => {
Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease))
}
("USGDPQOQ", "United States", "GDP Growth Rate") => {
Some(target("US", "gdp_qoq", MacroLagClass::Gdp65))
}
("CAGDPQOQ", "Canada", "GDP Growth Rate") => {
Some(target("CA", "gdp_qoq", MacroLagClass::Gdp90))
}
_ => None,
};
let tmp = TempDirGuard::new("bson-roundtrip");
let report = curate_and_write_macro(
@ -424,10 +436,34 @@ fn decode_te_data_bson_curate_and_write_all_four_lag_classes_without_forecast_le
let expected = vec![
expected_row("CA", "gdp_qoq", day(2024, 3, 31), GDP90_LAG_DAYS, -0.25),
expected_row("CA", "policy_rate", day(2024, 1, 24), POLICY_RATE_LAG_DAYS, 5.0),
expected_row("CA", "policy_rate", day(2024, 6, 5), POLICY_RATE_LAG_DAYS, 0.0),
expected_row("US", "cpi_yoy", day(2024, 1, 31), MONTHLY_RELEASE_LAG_DAYS, 3.1),
expected_row("US", "cpi_yoy", day(2024, 2, 29), MONTHLY_RELEASE_LAG_DAYS, 3.2),
expected_row(
"CA",
"policy_rate",
day(2024, 1, 24),
POLICY_RATE_LAG_DAYS,
5.0,
),
expected_row(
"CA",
"policy_rate",
day(2024, 6, 5),
POLICY_RATE_LAG_DAYS,
0.0,
),
expected_row(
"US",
"cpi_yoy",
day(2024, 1, 31),
MONTHLY_RELEASE_LAG_DAYS,
3.1,
),
expected_row(
"US",
"cpi_yoy",
day(2024, 2, 29),
MONTHLY_RELEASE_LAG_DAYS,
3.2,
),
expected_row("US", "gdp_qoq", day(2024, 3, 31), GDP65_LAG_DAYS, 1.4),
expected_row("US", "gdp_qoq", day(2024, 6, 30), GDP65_LAG_DAYS, -0.0),
];
@ -547,17 +583,16 @@ fn permutation_invariance_unmapped_duplicates_and_non_finite_accounting() {
},
];
let series_map = |te_symbol: &str, country: &str, category: &str| match (
te_symbol, country, category,
) {
("USCPIYOY", "United States", "Inflation Rate") => {
Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease))
}
("CABOCRATE", "Canada", "Interest Rate") => {
Some(target("CA", "policy_rate", MacroLagClass::PolicyRate))
}
_ => None,
};
let series_map =
|te_symbol: &str, country: &str, category: &str| match (te_symbol, country, category) {
("USCPIYOY", "United States", "Inflation Rate") => {
Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease))
}
("CABOCRATE", "Canada", "Interest Rate") => {
Some(target("CA", "policy_rate", MacroLagClass::PolicyRate))
}
_ => None,
};
let mut reversed = base_points.clone();
reversed.reverse();
@ -598,9 +633,27 @@ fn permutation_invariance_unmapped_duplicates_and_non_finite_accounting() {
assert_rows_bitwise_eq(&out_base.rows, &out_inter.rows);
let expected = vec![
expected_row("CA", "policy_rate", day(2024, 1, 24), POLICY_RATE_LAG_DAYS, 5.0),
expected_row("US", "cpi_yoy", day(2024, 1, 31), MONTHLY_RELEASE_LAG_DAYS, 3.1),
expected_row("US", "cpi_yoy", day(2024, 2, 29), MONTHLY_RELEASE_LAG_DAYS, 3.2),
expected_row(
"CA",
"policy_rate",
day(2024, 1, 24),
POLICY_RATE_LAG_DAYS,
5.0,
),
expected_row(
"US",
"cpi_yoy",
day(2024, 1, 31),
MONTHLY_RELEASE_LAG_DAYS,
3.1,
),
expected_row(
"US",
"cpi_yoy",
day(2024, 2, 29),
MONTHLY_RELEASE_LAG_DAYS,
3.2,
),
];
assert_rows_bitwise_eq(&out_base.rows, &expected);
}
@ -669,9 +722,8 @@ fn empty_and_all_unmapped_inputs_produce_zero_rows_and_do_not_touch_out_dir() {
#[test]
fn conflicting_duplicate_and_signed_zero_propagate_macro_error() {
let map_cpi = |_: &str, _: &str, _: &str| {
Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease))
};
let map_cpi =
|_: &str, _: &str, _: &str| Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease));
// Unequal finite values at same (country_iso, indicator, period_date)
let unequal = [
@ -827,7 +879,10 @@ fn known_at_overflow_and_invalid_write_options_and_single_line_display() {
);
assert!(err.source().is_some());
let msg = err.to_string();
assert!(!msg.contains('\n') && !msg.contains('\r'), "multi-line: {msg:?}");
assert!(
!msg.contains('\n') && !msg.contains('\r'),
"multi-line: {msg:?}"
);
// 2. InvalidRowGroupRows wrapped in CurateMacroWriteError::Parquet
let tmp = TempDirGuard::new("invalid-opts");
@ -856,7 +911,10 @@ fn known_at_overflow_and_invalid_write_options_and_single_line_display() {
));
assert!(write_err.source().is_some());
let msg = write_err.to_string();
assert!(!msg.contains('\n') && !msg.contains('\r'), "multi-line: {msg:?}");
assert!(
!msg.contains('\n') && !msg.contains('\r'),
"multi-line: {msg:?}"
);
// 3. Newline-bearing diagnostic labels stay single-line in both CurateMacroError and
// CurateMacroWriteError (covering ConflictingLagClass, ConflictingDuplicate, and Parquet).
@ -874,13 +932,12 @@ fn known_at_overflow_and_invalid_write_options_and_single_line_display() {
assert!(nl_lag_msg.contains("US\\nline2"));
assert!(nl_lag_msg.contains("cpi\\r\\nyoy"));
let nl_dup_err = CurateMacroWriteError::from(CurateMacroError::from(
MacroError::ConflictingDuplicate {
let nl_dup_err =
CurateMacroWriteError::from(CurateMacroError::from(MacroError::ConflictingDuplicate {
country_iso: "CA\n2".to_owned(),
indicator: "gdp\r3".to_owned(),
period_date: day(2024, 1, 1),
},
));
}));
let nl_dup_msg = nl_dup_err.to_string();
assert!(
!nl_dup_msg.contains('\n') && !nl_dup_msg.contains('\r'),
@ -893,7 +950,11 @@ fn known_at_overflow_and_invalid_write_options_and_single_line_display() {
tmp.path(),
valid_pt,
|_: &str, _: &str, _: &str| {
Some(target(" ", "bad\nindicator\rname", MacroLagClass::PolicyRate))
Some(target(
" ",
"bad\nindicator\rname",
MacroLagClass::PolicyRate,
))
},
MacroWriteOptions::default(),
)
@ -952,9 +1013,8 @@ fn curation_errors_never_write_or_modify_out_dir() {
value: 3.2,
},
];
let cpi_map = |_: &str, _: &str, _: &str| {
Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease))
};
let cpi_map =
|_: &str, _: &str, _: &str| Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease));
// 3. KnownAtOverflow
let overflow_points = [TeDataPoint {
@ -1025,7 +1085,14 @@ fn curation_errors_never_write_or_modify_out_dir() {
lag_conflict_map,
MacroWriteOptions::default(),
);
("ConflictingLagClass", res, part, before_bytes, before_entries, dir)
(
"ConflictingLagClass",
res,
part,
before_bytes,
before_entries,
dir,
)
},
{
let dir = TempDirGuard::new("err-existing-dup");
@ -1036,7 +1103,14 @@ fn curation_errors_never_write_or_modify_out_dir() {
cpi_map,
MacroWriteOptions::default(),
);
("ConflictingDuplicate", res, part, before_bytes, before_entries, dir)
(
"ConflictingDuplicate",
res,
part,
before_bytes,
before_entries,
dir,
)
},
{
let dir = TempDirGuard::new("err-existing-overflow");
@ -1047,7 +1121,14 @@ fn curation_errors_never_write_or_modify_out_dir() {
gdp_map,
MacroWriteOptions::default(),
);
("KnownAtOverflow", res, part, before_bytes, before_entries, dir)
(
"KnownAtOverflow",
res,
part,
before_bytes,
before_entries,
dir,
)
},
] {
let err = result.expect_err(&format!("{label} must fail"));
@ -1067,3 +1148,368 @@ fn curation_errors_never_write_or_modify_out_dir() {
);
}
}
fn te_point(
te_symbol: &'static str,
country: &'static str,
category: &'static str,
period_date: i32,
value: f64,
) -> TeDataPoint<'static> {
TeDataPoint {
te_symbol,
country,
category,
period_date,
value,
}
}
#[test]
fn mapped_target_with_only_non_finite_values_does_not_count_as_emitted_series() {
let points = vec![
te_point(
"USCPIYOY",
"United States",
"Inflation Rate",
day(2024, 1, 31),
3.1,
),
te_point(
"CABOCRATE",
"Canada",
"Interest Rate",
day(2024, 1, 24),
5.0,
),
te_point(
"DEGDP",
"Germany",
"GDP Growth Rate",
day(2024, 3, 31),
f64::NAN,
),
te_point(
"DEGDP",
"Germany",
"GDP Growth Rate",
day(2024, 6, 30),
f64::INFINITY,
),
te_point(
"DEGDP",
"Germany",
"GDP Growth Rate",
day(2024, 9, 30),
f64::NEG_INFINITY,
),
];
let series_map = |te_symbol: &str, _: &str, _: &str| match te_symbol {
"USCPIYOY" => Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease)),
"CABOCRATE" => Some(target("CA", "policy_rate", MacroLagClass::PolicyRate)),
"DEGDP" => Some(target("DE", "gdp_qoq", MacroLagClass::Gdp65)),
_ => None,
};
let mut reversed = points.clone();
reversed.reverse();
let rotated: Vec<_> = points[2..]
.iter()
.chain(points[..2].iter())
.copied()
.collect();
let mut outputs = Vec::new();
for perm in [points, reversed, rotated] {
let out = curate_macro(perm, series_map).expect("curation succeeds");
assert_stats_conserved(&out.stats);
assert_eq!(out.stats.points_in, 5);
assert_eq!(out.stats.dropped_unmapped_series, 0);
assert_eq!(out.stats.builder.input, 5);
assert_eq!(out.stats.builder.skipped_non_finite_value, 3);
assert_eq!(out.stats.builder.emitted, 2);
assert_eq!(out.stats.rows_out, 2);
assert_eq!(
out.stats.series_out, 2,
"non-finite-only target must not count"
);
assert!(out.rows.iter().all(|r| r.country_iso != "DE"));
outputs.push(out);
}
assert_eq!(outputs[0].stats, outputs[1].stats);
assert_eq!(outputs[0].stats, outputs[2].stats);
assert_rows_bitwise_eq(&outputs[0].rows, &outputs[1].rows);
assert_rows_bitwise_eq(&outputs[0].rows, &outputs[2].rows);
}
#[test]
fn lookup_receives_verbatim_provider_labels_once_per_point() {
use std::cell::RefCell;
let points = [
te_point(
" USCPIYOY",
"United States ",
"Inflation\tRate",
day(2024, 1, 31),
3.1,
),
te_point(
"cabocrate",
"CANADA",
"Interest Rate",
day(2024, 1, 24),
5.0,
),
te_point(
"ÜMLAUT",
"Österreich",
"Kategorie",
day(2024, 2, 1),
f64::NAN,
),
];
let calls: RefCell<Vec<(String, String, String)>> = RefCell::new(Vec::new());
let out = curate_macro(points, |s: &str, c: &str, k: &str| {
calls
.borrow_mut()
.push((s.to_owned(), c.to_owned(), k.to_owned()));
None
})
.expect("all-unmapped succeeds");
assert_eq!(
calls.into_inner(),
vec![
(
" USCPIYOY".into(),
"United States ".into(),
"Inflation\tRate".into()
),
("cabocrate".into(), "CANADA".into(), "Interest Rate".into()),
("ÜMLAUT".into(), "Österreich".into(), "Kategorie".into()),
]
);
assert_stats_conserved(&out.stats);
assert_eq!(out.stats.dropped_unmapped_series, 3);
assert_eq!(out.stats.builder.input, 0);
// Unmapped non-finite points never reach the builder's non-finite counter.
assert_eq!(out.stats.builder.skipped_non_finite_value, 0);
assert!(out.rows.is_empty());
}
#[test]
fn unmapped_points_never_trigger_builder_errors() {
// Unmapped points that would overflow known_at or conflict with a mapped value are dropped
// before build_macro_rows, so they cannot fail the stage.
let points = [
te_point(
"USCPIYOY",
"United States",
"Inflation Rate",
day(2024, 1, 31),
3.1,
),
te_point(
"USCPIYOY_ALT",
"United States",
"Inflation Rate",
day(2024, 1, 31),
9.9,
),
te_point("EXTREME", "United States", "GDP", i32::MAX, 1.0),
];
let series_map = |te_symbol: &str, _: &str, _: &str| match te_symbol {
"USCPIYOY" => Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease)),
_ => None,
};
let out = curate_macro(points, series_map).expect("unmapped points must be ignored");
assert_stats_conserved(&out.stats);
assert_eq!(out.stats.dropped_unmapped_series, 2);
assert_rows_bitwise_eq(
&out.rows,
&[expected_row(
"US",
"cpi_yoy",
day(2024, 1, 31),
MONTHLY_RELEASE_LAG_DAYS,
3.1,
)],
);
}
#[test]
fn aliases_with_same_lag_class_merge_into_one_series_and_cross_alias_duplicates_collapse() {
let points = [
te_point(
"USCPIYOY",
"United States",
"Inflation Rate",
day(2024, 1, 31),
3.1,
),
te_point("USCPI_ALT", "USA", "CPI YoY", day(2024, 1, 31), 3.1),
te_point("USCPI_ALT", "USA", "CPI YoY", day(2024, 2, 29), 3.2),
];
let series_map = |te_symbol: &str, _: &str, _: &str| match te_symbol {
"USCPIYOY" | "USCPI_ALT" => Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease)),
_ => None,
};
let out = curate_macro(points, series_map).expect("same lag class is not a conflict");
assert_stats_conserved(&out.stats);
assert_eq!(out.stats.builder.skipped_duplicate, 1);
assert_eq!(out.stats.series_out, 1);
assert_rows_bitwise_eq(
&out.rows,
&[
expected_row(
"US",
"cpi_yoy",
day(2024, 1, 31),
MONTHLY_RELEASE_LAG_DAYS,
3.1,
),
expected_row(
"US",
"cpi_yoy",
day(2024, 2, 29),
MONTHLY_RELEASE_LAG_DAYS,
3.2,
),
],
);
// Cross-alias conflicting values on the same target/date are rejected by the builder.
let conflicting = [
te_point(
"USCPIYOY",
"United States",
"Inflation Rate",
day(2024, 1, 31),
3.1,
),
te_point("USCPI_ALT", "USA", "CPI YoY", day(2024, 1, 31), 3.0),
];
assert_eq!(
curate_macro(conflicting, series_map).expect_err("cross-alias conflict"),
CurateMacroError::Macro(MacroError::ConflictingDuplicate {
country_iso: "US".to_owned(),
indicator: "cpi_yoy".to_owned(),
period_date: day(2024, 1, 31),
})
);
}
#[test]
fn same_indicator_in_different_countries_may_use_different_lag_classes() {
let points = [
te_point("USGDP", "United States", "GDP", day(2024, 3, 31), 1.4),
te_point("CAGDP", "Canada", "GDP", day(2024, 3, 31), 0.5),
];
let series_map = |te_symbol: &str, _: &str, _: &str| match te_symbol {
"USGDP" => Some(target("US", "gdp_qoq", MacroLagClass::Gdp65)),
"CAGDP" => Some(target("CA", "gdp_qoq", MacroLagClass::Gdp90)),
_ => None,
};
let out = curate_macro(points, series_map).expect("per-country lag classes are independent");
assert_eq!(out.stats.series_out, 2);
assert_rows_bitwise_eq(
&out.rows,
&[
expected_row("CA", "gdp_qoq", day(2024, 3, 31), GDP90_LAG_DAYS, 0.5),
expected_row("US", "gdp_qoq", day(2024, 3, 31), GDP65_LAG_DAYS, 1.4),
],
);
}
#[test]
fn three_way_lag_conflict_reports_min_and_max_class_for_every_order() {
let a = te_point("A", "X", "Y", day(2024, 1, 31), 1.0);
let b = te_point("B", "X", "Y", day(2024, 2, 29), 2.0);
let c = te_point("C", "X", "Y", day(2024, 3, 31), 3.0);
let series_map = |te_symbol: &str, _: &str, _: &str| match te_symbol {
"A" => Some(target("US", "mix", MacroLagClass::MonthlyRelease)),
"B" => Some(target("US", "mix", MacroLagClass::Gdp90)),
"C" => Some(target("US", "mix", MacroLagClass::PolicyRate)),
_ => None,
};
let expected = CurateMacroError::ConflictingLagClass {
country_iso: "US".to_owned(),
indicator: "mix".to_owned(),
first_lag_class: MacroLagClass::PolicyRate,
second_lag_class: MacroLagClass::Gdp90,
};
for perm in [
[a, b, c],
[a, c, b],
[b, a, c],
[b, c, a],
[c, a, b],
[c, b, a],
] {
assert_eq!(
curate_macro(perm, series_map).expect_err("conflict"),
expected
);
}
let msg = expected.to_string();
assert!(msg.contains("+1d") && msg.contains("+90d"), "{msg}");
}
#[test]
fn write_error_source_chain_reaches_macro_error() {
let points = [
te_point("A", "US", "CPI", day(2024, 1, 31), 3.1),
te_point("A", "US", "CPI", day(2024, 1, 31), 3.2),
];
let tmp = TempDirGuard::new("source-chain");
let err = curate_and_write_macro(
tmp.path(),
points,
|_: &str, _: &str, _: &str| Some(target("US", "cpi_yoy", MacroLagClass::MonthlyRelease)),
MacroWriteOptions::default(),
)
.expect_err("duplicate conflict");
let lvl1 = err.source().expect("curate error source");
assert!(lvl1.downcast_ref::<CurateMacroError>().is_some());
let lvl2 = lvl1.source().expect("macro error source");
assert!(lvl2.downcast_ref::<MacroError<String>>().is_some());
assert!(err.to_string().starts_with("macro curation failed: "));
}
#[test]
fn successful_write_replaces_existing_part_parquet_with_curated_rows() {
let tmp = TempDirGuard::new("replace-existing");
let (part, before_bytes, _) = seed_existing_parquet(tmp.path());
let points = [te_point(
"USGDP",
"United States",
"GDP",
day(2024, 6, 30),
2.8,
)];
let report = curate_and_write_macro(
tmp.path(),
points,
|_: &str, _: &str, _: &str| Some(target("US", "gdp_qoq", MacroLagClass::Gdp90)),
MacroWriteOptions::default(),
)
.expect("write succeeds");
assert_eq!(report.write_stats.rows_written, 1);
assert_ne!(fs::read(&part).expect("read after"), before_bytes);
assert_eq!(
sorted_dir_entries(&tmp.path().join("macro")),
vec!["part.parquet"]
);
assert_rows_bitwise_eq(
&read_parquet_rows(&part),
&[expected_row(
"US",
"gdp_qoq",
day(2024, 6, 30),
GDP90_LAG_DAYS,
2.8,
)],
);
}