T-171: tests

This commit is contained in:
Studio 2026-12-02 14:29:40 -05:00
parent a9f153940f
commit 4275a1c972

View file

@ -2467,3 +2467,293 @@ fn listing_venues_rejects_interior_blank() {
assert!(msg.contains(&format!("{}:2:", path.display())));
assert_single_line(&msg);
}
// ---------------------------------------------------------------------------------------------
// QA additions (T-171): replay determinism, missing-venue fail-closed drop, regular-file year
// entry, and strict venue-token parsing edge cases.
// ---------------------------------------------------------------------------------------------
fn write_qa_replay_fixture(dir: &Path) -> (PathBuf, PathBuf, PathBuf, PathBuf) {
let fin_path = dir.join("qmFinancials.bson");
let fil_path = dir.join("qmFilings.bson");
let map_path = dir.join("provider_mapping.tsv");
let venues_path = dir.join("listing_venues.tsv");
fs::write(
&map_path,
"qm\tNVDA\t2020-01-01\t\t101\nqm\tMINE:V\t2020-01-01\t\t202\n",
)
.unwrap();
fs::write(&venues_path, "101\tus_tsx\n202\ttsxv_cse\n").unwrap();
let mut fin = Vec::new();
for (end, rd, period, rev) in [
("2023-09-30", "2023-11-04", "Q3", 120.0),
("2023-12-31", "2024-02-10", "Q4", 130.0),
("2024-03-31", "2024-05-02", "Q1", 140.0),
] {
fin.extend_from_slice(&make_financials_doc(
Some("NVDA"),
end,
Some(rd),
period,
"USD",
FundamentalsFields {
revenue: Some(rev),
net_income: Some(rev / 5.0),
total_assets: Some(1000.0),
..Default::default()
},
));
}
fin.extend_from_slice(&make_financials_doc(
Some("MINE:V"),
"2024-06-30",
None,
"Q2",
"CAD",
FundamentalsFields {
revenue: Some(5.0),
..Default::default()
},
));
fs::write(&fin_path, &fin).unwrap();
let mut fil = Vec::new();
for (ts, id) in [
("2023-11-04T16:30:00 EDT", "f-q3"),
("2024-02-10T16:05:00 EST", "f-q4"),
("2024-05-02T16:15:00 EDT", "f-q1"),
] {
fil.extend_from_slice(&make_filing_doc(Some("NVDA"), ts, "10-Q", Some(id), None));
}
fs::write(&fil_path, &fil).unwrap();
(fin_path, fil_path, map_path, venues_path)
}
#[test]
fn fundamentals_rerun_same_inputs_is_byte_identical() {
// Arrange: one pinned fixture, two fresh output roots.
let tmp = TempDirGuard::new("qa-replay");
let (fin, fil, map, venues) = write_qa_replay_fixture(tmp.path());
let out_a = tmp.path().join("out_a");
let out_b = tmp.path().join("out_b");
// Act: file API into A, injected-reader API into B.
let report_a = run_curate_fundamentals(&default_test_args(
fin.clone(),
fil.clone(),
map.clone(),
venues.clone(),
out_a.clone(),
))
.unwrap();
let report_b = run_curate_fundamentals_with_readers(
&default_test_args(fin.clone(), fil.clone(), map, venues, out_b.clone()),
File::open(&fin).unwrap(),
File::open(&fil).unwrap(),
)
.unwrap();
// Assert: identical counters and byte-identical Parquet partitions.
assert_conservation(&report_a);
assert_reports_eq_ignoring_timing(&report_a, &report_b);
assert!(report_a.write_stats.rows_written > 0);
let tree_a = snapshot_tree(&out_a.join("fundamentals_pit"));
let tree_b = snapshot_tree(&out_b.join("fundamentals_pit"));
assert!(!tree_a.is_empty());
assert_eq!(tree_a, tree_b, "replay must produce byte-identical partitions");
// No staging/backup leftovers after a successful publish.
for path in tree_a.keys() {
let name = path.to_string_lossy();
assert!(
!name.ends_with(".tmp") && !name.contains(".bak"),
"unexpected leftover {name}"
);
}
}
#[test]
fn fundamentals_rerun_into_existing_output_replaces_partitions_identically() {
// Arrange
let tmp = TempDirGuard::new("qa-rerun-same-out");
let (fin, fil, map, venues) = write_qa_replay_fixture(tmp.path());
let out = tmp.path().join("out");
let args = default_test_args(fin, fil, map, venues, out.clone());
// Act
let first = run_curate_fundamentals(&args).unwrap();
let before = snapshot_tree(&out);
let second = run_curate_fundamentals(&args).unwrap();
// Assert
assert_reports_eq_ignoring_timing(&first, &second);
assert_eq!(snapshot_tree(&out), before);
}
#[test]
fn fundamentals_mapped_listing_without_venue_is_dropped_not_guessed() {
// Arrange: listing 303 is mapped but absent from listing_venues.tsv; it has a filing-linked
// quarter and an unlinked one. Neither may be published with a guessed venue lag.
let tmp = TempDirGuard::new("qa-missing-venue");
let fin_path = tmp.path().join("qmFinancials.bson");
let fil_path = tmp.path().join("qmFilings.bson");
let map_path = tmp.path().join("provider_mapping.tsv");
let venues_path = tmp.path().join("listing_venues.tsv");
let out = tmp.path().join("out");
fs::write(
&map_path,
"qm\tUSCO\t2020-01-01\t\t10\nqm\tNOVEN\t2020-01-01\t\t303\n",
)
.unwrap();
fs::write(&venues_path, "10\tus_tsx\n").unwrap();
let mut fin = Vec::new();
for sym in ["USCO", "NOVEN"] {
fin.extend_from_slice(&make_financials_doc(
Some(sym),
"2024-03-31",
Some("2024-05-02"),
"Q1",
"USD",
FundamentalsFields {
revenue: Some(10.0),
..Default::default()
},
));
fin.extend_from_slice(&make_financials_doc(
Some(sym),
"2024-06-30",
None,
"Q2",
"USD",
FundamentalsFields {
revenue: Some(11.0),
..Default::default()
},
));
}
fs::write(&fin_path, &fin).unwrap();
let mut fil = Vec::new();
for sym in ["USCO", "NOVEN"] {
fil.extend_from_slice(&make_filing_doc(
Some(sym),
"2024-05-02T16:15:00 EDT",
"10-Q",
None,
None,
));
}
fs::write(&fil_path, &fil).unwrap();
// Act
let report = run_curate_fundamentals(&default_test_args(
fin_path,
fil_path,
map_path,
venues_path,
out.clone(),
))
.unwrap();
// Assert
assert_conservation(&report);
assert_eq!(report.curate_stats.observations_dropped_unmapped, 0);
assert_eq!(report.curate_stats.build.observations_dropped_unknown_key, 2);
assert_eq!(report.curate_stats.listings_out, 1);
let by_year = read_dataset_by_year(&out);
let all: Vec<_> = by_year.values().flatten().collect();
assert!(!all.is_empty());
assert!(all.iter().all(|r| r.listing_id == 10));
}
#[test]
fn fundamentals_regular_file_year_entry_fails_without_partial_publish() {
// Arrange: a backfill writing 2023 and 2024, where `year=2024` is occupied by a regular file.
// A pre-existing 2023 partition must survive the failed run byte-for-byte.
let tmp = TempDirGuard::new("qa-file-year");
let (fin, fil, map, venues) = write_qa_replay_fixture(tmp.path());
let out = tmp.path().join("out");
let dataset = out.join("fundamentals_pit");
fs::create_dir_all(dataset.join("year=2023")).unwrap();
fs::write(dataset.join("year=2023").join("part.parquet"), b"seed-2023").unwrap();
fs::write(dataset.join("year=2024"), b"not-a-directory").unwrap();
let before = snapshot_tree(&out);
for use_readers in [false, true] {
// Act
let args = default_test_args(
fin.clone(),
fil.clone(),
map.clone(),
venues.clone(),
out.clone(),
);
let result = if use_readers {
run_curate_fundamentals_with_readers(
&args,
File::open(&fin).unwrap(),
File::open(&fil).unwrap(),
)
} else {
run_curate_fundamentals(&args)
};
// Assert
let err = result.expect_err("regular-file year entry must fail");
assert_single_line(&err.to_string());
assert_eq!(snapshot_tree(&out), before, "output tree must be unchanged");
}
}
#[test]
fn listing_venues_accepts_crlf_and_single_trailing_blank() {
let tmp = TempDirGuard::new("qa-venues-ok");
let path = tmp.path().join("venues.tsv");
fs::write(&path, "7\ttsxv_cse\r\n3\tus_tsx\n\n").unwrap();
let venues = load_listing_venues_tsv(&path).unwrap();
assert_eq!(venues.len(), 2);
assert_eq!(venues[&3], crate::fundamentals::StatutoryLagVenue::UsTsx);
assert_eq!(venues[&7], crate::fundamentals::StatutoryLagVenue::TsxvCse);
let empty = tmp.path().join("empty.tsv");
fs::write(&empty, "").unwrap();
assert!(load_listing_venues_tsv(&empty).unwrap().is_empty());
}
#[test]
fn listing_venues_rejects_padded_signed_and_case_variant_tokens() {
let tmp = TempDirGuard::new("qa-venues-strict");
let path = tmp.path().join("venues.tsv");
for (content, line) in [
("1\tus_tsx\n2\t us_tsx\n", 2usize),
("1\tus_tsx\n2\tUS_TSX\n", 2),
("1\tus_tsx \n", 1),
("+5\tus_tsx\n", 1),
(" 5\tus_tsx\n", 1),
("2147483648\tus_tsx\n", 1),
("1\tus_tsx\n2\t\n", 2),
// Shared `read_tsv_lines` reports the second trailing blank line.
("1\tus_tsx\n\n\n", 3),
] {
fs::write(&path, content).unwrap();
let err = load_listing_venues_tsv(&path).expect_err(content);
let msg = err.to_string();
assert_single_line(&msg);
match &err {
RunCurateFundamentalsError::TsvParse { path: p, line: l, .. } => {
assert_eq!(p, &path, "{content:?}");
assert_eq!(*l, line, "{content:?}: {msg}");
}
other => panic!("expected TsvParse for {content:?}, got {other:?}"),
}
assert!(msg.contains(&format!("{}:{line}:", path.display())), "{msg}");
}
}
#[test]
fn listing_venues_missing_file_reports_open_error_with_path() {
let tmp = TempDirGuard::new("qa-venues-missing");
let path = tmp.path().join("nope.tsv");
let err = load_listing_venues_tsv(&path).unwrap_err();
assert!(matches!(err, RunCurateFundamentalsError::OpenInput { ref path, .. } if path.ends_with("nope.tsv")));
assert!(err.to_string().contains(&path.display().to_string()));
}