T-164: tests
This commit is contained in:
parent
3515ef487b
commit
2932f5640d
1 changed files with 339 additions and 12 deletions
|
|
@ -137,7 +137,10 @@ fn assert_report_invariants(report: &CuratePostsRunReport) {
|
|||
assert_eq!(c.rows_in, b.newswire + b.retail);
|
||||
assert_eq!(
|
||||
c.rows_in,
|
||||
c.rows_out + c.dropped_unmapped + c.dropped_unsupported_date + c.listing_duplicate_collapsed
|
||||
c.rows_out
|
||||
+ c.dropped_unmapped
|
||||
+ c.dropped_unsupported_date
|
||||
+ c.listing_duplicate_collapsed
|
||||
);
|
||||
assert_eq!(c.rows_out, c.newswire_out + c.retail_out);
|
||||
assert_eq!(report.write_stats.rows_written, c.rows_out);
|
||||
|
|
@ -151,7 +154,11 @@ fn read_events_parquet_and_verify_zstd(path: &Path) -> Vec<EventsParquetRow> {
|
|||
|
||||
for rg_idx in 0..meta.num_row_groups() {
|
||||
let rg_meta = meta.row_group(rg_idx);
|
||||
assert_eq!(rg_meta.num_columns(), 7, "expected 7 columns in events schema");
|
||||
assert_eq!(
|
||||
rg_meta.num_columns(),
|
||||
7,
|
||||
"expected 7 columns in events schema"
|
||||
);
|
||||
for col_idx in 0..rg_meta.num_columns() {
|
||||
let compression = rg_meta.column(col_idx).compression();
|
||||
assert!(
|
||||
|
|
@ -215,7 +222,11 @@ fn read_events_parquet_and_verify_zstd(path: &Path) -> Vec<EventsParquetRow> {
|
|||
_ => panic!("expected BYTE_ARRAY column at index {col_idx}"),
|
||||
}
|
||||
raw.iter()
|
||||
.map(|b| std::str::from_utf8(b.data()).expect("valid utf8").to_string())
|
||||
.map(|b| {
|
||||
std::str::from_utf8(b.data())
|
||||
.expect("valid utf8")
|
||||
.to_string()
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
|
||||
|
|
@ -258,7 +269,9 @@ fn read_all_event_partitions(out_dir: &Path) -> Vec<EventsParquetRow> {
|
|||
|
||||
let mut all = Vec::new();
|
||||
for yd in year_dirs {
|
||||
all.extend(read_events_parquet_and_verify_zstd(&yd.join("part.parquet")));
|
||||
all.extend(read_events_parquet_and_verify_zstd(
|
||||
&yd.join("part.parquet"),
|
||||
));
|
||||
}
|
||||
all.sort_by(|a, b| {
|
||||
a.listing_id
|
||||
|
|
@ -517,7 +530,10 @@ fn happy_path_matches_curate_posts_verifies_zstd_edit_time_and_invariants() {
|
|||
.find(|r| r.payload_json.contains("sp-wire-1"))
|
||||
.expect("newswire row");
|
||||
assert_eq!(wire_row.subtype, POST_SUBTYPE_NEWSWIRE);
|
||||
assert_eq!(wire_row.known_at_source.as_ref(), KNOWN_AT_SOURCE_POST_TIMESTAMP);
|
||||
assert_eq!(
|
||||
wire_row.known_at_source.as_ref(),
|
||||
KNOWN_AT_SOURCE_POST_TIMESTAMP
|
||||
);
|
||||
assert_eq!(wire_row.ts_micros, t_wire * 1_000);
|
||||
assert_eq!(wire_row.known_at_micros, t_wire * 1_000);
|
||||
|
||||
|
|
@ -528,7 +544,10 @@ fn happy_path_matches_curate_posts_verifies_zstd_edit_time_and_invariants() {
|
|||
assert_eq!(edited_row.subtype, POST_SUBTYPE_RETAIL);
|
||||
assert_eq!(edited_row.ts_micros, t_edited_post * 1_000);
|
||||
assert_eq!(edited_row.known_at_micros, t_edited_at * 1_000);
|
||||
assert_eq!(edited_row.known_at_source.as_ref(), KNOWN_AT_SOURCE_POST_EDITED);
|
||||
assert_eq!(
|
||||
edited_row.known_at_source.as_ref(),
|
||||
KNOWN_AT_SOURCE_POST_EDITED
|
||||
);
|
||||
|
||||
// Reversed input order must yield byte-identical Parquet files and deterministic report.
|
||||
let bson_rev = dir.path().join("ceoPosts_rev.bson");
|
||||
|
|
@ -551,7 +570,10 @@ fn happy_path_matches_curate_posts_verifies_zstd_edit_time_and_invariants() {
|
|||
for year in ["year=2024", "year=2025"] {
|
||||
let a = fs::read(out_a.join("events").join(year).join("part.parquet")).expect("read a");
|
||||
let b = fs::read(out_b.join("events").join(year).join("part.parquet")).expect("read b");
|
||||
assert_eq!(a, b, "reversed input must produce byte-identical {year}/part.parquet");
|
||||
assert_eq!(
|
||||
a, b,
|
||||
"reversed input must produce byte-identical {year}/part.parquet"
|
||||
);
|
||||
}
|
||||
|
||||
let strip_timing = |s: &str| {
|
||||
|
|
@ -674,7 +696,10 @@ fn clobber_guard_rejects_existing_year_partitions_and_preserves_bytes() {
|
|||
CeoPostsTimestampUnit::Millis,
|
||||
);
|
||||
let err = run_curate_posts(&args_a).unwrap_err();
|
||||
assert!(matches!(err, RunCuratePostsError::EventsDatasetExists { .. }));
|
||||
assert!(matches!(
|
||||
err,
|
||||
RunCuratePostsError::EventsDatasetExists { .. }
|
||||
));
|
||||
let msg = assert_single_line_err(&err);
|
||||
assert!(msg.contains("year=2024"), "expected year=2024 in {msg}");
|
||||
assert_eq!(fs::read(&sentinel_path).unwrap(), sentinel_bytes);
|
||||
|
|
@ -688,9 +713,17 @@ fn clobber_guard_rejects_existing_year_partitions_and_preserves_bytes() {
|
|||
let out_b = dir.path().join("out_empty_year_dir");
|
||||
let year_2025 = out_b.join("events/year=2025");
|
||||
fs::create_dir_all(&year_2025).unwrap();
|
||||
let args_b = default_args(bson_path, map_path, out_b.clone(), CeoPostsTimestampUnit::Millis);
|
||||
let args_b = default_args(
|
||||
bson_path,
|
||||
map_path,
|
||||
out_b.clone(),
|
||||
CeoPostsTimestampUnit::Millis,
|
||||
);
|
||||
let err = run_curate_posts(&args_b).unwrap_err();
|
||||
assert!(matches!(err, RunCuratePostsError::EventsDatasetExists { .. }));
|
||||
assert!(matches!(
|
||||
err,
|
||||
RunCuratePostsError::EventsDatasetExists { .. }
|
||||
));
|
||||
let msg = assert_single_line_err(&err);
|
||||
assert!(msg.contains("year=2025"), "expected year=2025 in {msg}");
|
||||
assert_eq!(fs::read_dir(&year_2025).unwrap().count(), 0);
|
||||
|
|
@ -770,7 +803,10 @@ fn fail_closed_errors_are_typed_single_line_and_leave_out_dir_untouched() {
|
|||
write_bytes(&marker, &marker_bytes);
|
||||
|
||||
let assert_out_untouched = || {
|
||||
assert!(!out_dir.join("events").exists(), "events dir must not be created");
|
||||
assert!(
|
||||
!out_dir.join("events").exists(),
|
||||
"events dir must not be created"
|
||||
);
|
||||
assert_eq!(fs::read(&marker).unwrap(), marker_bytes);
|
||||
let entries: Vec<_> = fs::read_dir(&out_dir)
|
||||
.unwrap()
|
||||
|
|
@ -949,7 +985,10 @@ fn fail_closed_errors_are_typed_single_line_and_leave_out_dir_untouched() {
|
|||
msg.contains("dir/bad\\nname.bson"),
|
||||
"expected escaped path in {msg}"
|
||||
);
|
||||
assert!(msg.contains("boom\\nline2"), "expected escaped cause in {msg}");
|
||||
assert!(
|
||||
msg.contains("boom\\nline2"),
|
||||
"expected escaped cause in {msg}"
|
||||
);
|
||||
assert_out_untouched();
|
||||
|
||||
// 7. Malformed TSV (line 2)
|
||||
|
|
@ -1121,3 +1160,291 @@ fn empty_and_all_unmapped_inputs_return_zero_rows_without_creating_events_dir()
|
|||
assert_eq!(rep_unmapped.write_stats.files_written, 0);
|
||||
assert!(!out_unmapped.join("events").exists());
|
||||
}
|
||||
|
||||
/// Reader that serves `bytes` and, on the first EOF, runs `on_eof` once (used to simulate another
|
||||
/// publisher creating an `events/year=*` partition while the dump is still being decoded).
|
||||
struct EofHookReader<F: FnMut()> {
|
||||
bytes: Vec<u8>,
|
||||
pos: usize,
|
||||
on_eof: Option<F>,
|
||||
}
|
||||
|
||||
impl<F: FnMut()> Read for EofHookReader<F> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
if self.pos < self.bytes.len() {
|
||||
let n = (self.bytes.len() - self.pos).min(buf.len());
|
||||
buf[..n].copy_from_slice(&self.bytes[self.pos..self.pos + n]);
|
||||
self.pos += n;
|
||||
return Ok(n);
|
||||
}
|
||||
if let Some(mut hook) = self.on_eof.take() {
|
||||
hook();
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn sorted_names(p: &Path) -> Vec<String> {
|
||||
let mut names: Vec<String> = fs::read_dir(p)
|
||||
.expect("read dir")
|
||||
.map(|e| e.expect("entry").file_name().to_string_lossy().into_owned())
|
||||
.collect();
|
||||
names.sort();
|
||||
names
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn post_decode_guard_rejects_partition_created_during_decode_and_preserves_bytes() {
|
||||
let dir = TempDirGuard::new("guard-recheck");
|
||||
let map_path = dir.path().join("mapping.tsv");
|
||||
write_bytes(&map_path, b"ceo\tCEO_RY\t2020-01-01\t\t101\n");
|
||||
let fields = CeoPostsFieldNames::default();
|
||||
let doc = make_post_doc(
|
||||
&fields,
|
||||
"CEO_RY",
|
||||
"sp-1",
|
||||
utc_ms(2024, 6, 15, 14, 0, 0),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
"hi",
|
||||
);
|
||||
|
||||
let out_dir = dir.path().join("out");
|
||||
let partition = out_dir.join("events").join("year=2024");
|
||||
let sentinel_path = partition.join("part.parquet");
|
||||
let sentinel = b"SENTINEL-CONCURRENT-FILINGS".to_vec();
|
||||
assert!(
|
||||
!out_dir.exists(),
|
||||
"first guard must see a fresh output root"
|
||||
);
|
||||
|
||||
let hook_partition = partition.clone();
|
||||
let hook_sentinel_path = sentinel_path.clone();
|
||||
let hook_sentinel = sentinel.clone();
|
||||
let err = run_curate_posts_with_reader(
|
||||
&map_path,
|
||||
DEFAULT_MAPPING_SOURCE,
|
||||
&fields,
|
||||
CeoPostsTimestampUnit::Millis,
|
||||
(
|
||||
Path::new("hooked.bson"),
|
||||
EofHookReader {
|
||||
bytes: doc,
|
||||
pos: 0,
|
||||
on_eof: Some(move || {
|
||||
fs::create_dir_all(&hook_partition).expect("create concurrent partition");
|
||||
write_bytes(&hook_sentinel_path, &hook_sentinel);
|
||||
}),
|
||||
},
|
||||
),
|
||||
&out_dir,
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(&err, RunCuratePostsError::EventsDatasetExists { path } if path == &partition),
|
||||
"expected EventsDatasetExists for {partition:?}, got {err:?}"
|
||||
);
|
||||
assert_single_line_err(&err);
|
||||
assert_eq!(fs::read(&sentinel_path).unwrap(), sentinel);
|
||||
assert_eq!(sorted_names(&out_dir.join("events")), vec!["year=2024"]);
|
||||
assert_eq!(sorted_names(&partition), vec!["part.parquet"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn events_dir_with_only_non_year_entries_is_published_and_non_year_entries_preserved() {
|
||||
let dir = TempDirGuard::new("non-year-entries");
|
||||
let map_path = dir.path().join("mapping.tsv");
|
||||
write_bytes(&map_path, b"ceo\tCEO_RY\t2020-01-01\t\t101\n");
|
||||
let fields = CeoPostsFieldNames::default();
|
||||
let bson_path = dir.path().join("posts.bson");
|
||||
write_bytes(
|
||||
&bson_path,
|
||||
&make_post_doc(
|
||||
&fields,
|
||||
"CEO_RY",
|
||||
"sp-1",
|
||||
utc_ms(2024, 6, 15, 14, 0, 0),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
"hi",
|
||||
),
|
||||
);
|
||||
|
||||
let out_dir = dir.path().join("out");
|
||||
let events_dir = out_dir.join("events");
|
||||
fs::create_dir_all(&events_dir).unwrap();
|
||||
let readme = events_dir.join("README.txt");
|
||||
write_bytes(&readme, b"not a partition");
|
||||
|
||||
let report = run_curate_posts(&default_args(
|
||||
bson_path,
|
||||
map_path,
|
||||
out_dir.clone(),
|
||||
CeoPostsTimestampUnit::Millis,
|
||||
))
|
||||
.expect("non-year entries must not trip the clobber guard");
|
||||
|
||||
assert_report_invariants(&report);
|
||||
assert_eq!(report.write_stats.rows_written, 1);
|
||||
assert_eq!(fs::read(&readme).unwrap(), b"not a partition");
|
||||
assert_eq!(sorted_names(&events_dir), vec!["README.txt", "year=2024"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_dir_through_existing_components_still_hits_events_guard() {
|
||||
let dir = TempDirGuard::new("parent-existing");
|
||||
let map_path = dir.path().join("mapping.tsv");
|
||||
write_bytes(&map_path, b"ceo\tCEO_RY\t2020-01-01\t\t101\n");
|
||||
let fields = CeoPostsFieldNames::default();
|
||||
let bson_path = dir.path().join("posts.bson");
|
||||
write_bytes(
|
||||
&bson_path,
|
||||
&make_post_doc(
|
||||
&fields,
|
||||
"CEO_RY",
|
||||
"sp-1",
|
||||
utc_ms(2024, 6, 15, 14, 0, 0),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
"hi",
|
||||
),
|
||||
);
|
||||
|
||||
let exports = dir.path().join("exports");
|
||||
let shared_partition = exports.join("shared/events/year=2024");
|
||||
fs::create_dir_all(&shared_partition).unwrap();
|
||||
fs::create_dir_all(exports.join("scratch")).unwrap();
|
||||
let sentinel_path = shared_partition.join("part.parquet");
|
||||
write_bytes(&sentinel_path, b"SENTINEL");
|
||||
|
||||
// `scratch` exists, so `..` resolves consistently and the guard must inspect `shared`.
|
||||
let out = exports.join("scratch").join("..").join("shared");
|
||||
let err = run_curate_posts(&default_args(
|
||||
bson_path,
|
||||
map_path,
|
||||
out,
|
||||
CeoPostsTimestampUnit::Millis,
|
||||
))
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(err, RunCuratePostsError::EventsDatasetExists { .. }),
|
||||
"got {err:?}"
|
||||
);
|
||||
assert_single_line_err(&err);
|
||||
assert_eq!(fs::read(&sentinel_path).unwrap(), b"SENTINEL");
|
||||
assert_eq!(sorted_names(&exports), vec!["scratch", "shared"]);
|
||||
assert!(sorted_names(&exports.join("scratch")).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eastern_posting_date_mapping_and_edit_clamp_match_curate_posts_baseline() {
|
||||
let dir = TempDirGuard::new("eastern-pit");
|
||||
let map_path = dir.path().join("mapping.tsv");
|
||||
// Listing switch at the Eastern calendar boundary 2023-12-31 -> 2024-01-01 (valid_to inclusive).
|
||||
write_bytes(
|
||||
&map_path,
|
||||
b"ceo\tCEO_RY\t2020-01-01\t2023-12-31\t101\nceo\tCEO_RY\t2024-01-01\t\t102\n",
|
||||
);
|
||||
let fields = CeoPostsFieldNames::default();
|
||||
|
||||
// 2024-01-01 03:00 UTC == 2023-12-31 22:00 EST -> listing 101 (UTC date would give 102).
|
||||
let t_late_eve = utc_ms(2024, 1, 1, 3, 0, 0);
|
||||
// 2024-01-01 06:00 UTC == 2024-01-01 01:00 EST -> listing 102.
|
||||
let t_new_year = utc_ms(2024, 1, 1, 6, 0, 0);
|
||||
// Posted 2023-12-31 23:30 EST, edited after the switch: mapping must follow posted date.
|
||||
let t_edit_posted = utc_ms(2024, 1, 1, 4, 30, 0);
|
||||
let t_edit_at = utc_ms(2024, 1, 2, 15, 0, 0);
|
||||
// Edit earlier than posting (clock skew): known_at clamps to posted_at.
|
||||
let t_skew_posted = utc_ms(2024, 3, 5, 15, 0, 0);
|
||||
let t_skew_edit = utc_ms(2024, 3, 5, 14, 0, 0);
|
||||
|
||||
let inputs: Vec<(&str, i64, Option<i64>, Option<&str>)> = vec![
|
||||
("sp-eve", t_late_eve, None, Some("1")),
|
||||
("sp-ny", t_new_year, None, None),
|
||||
("sp-edit", t_edit_posted, Some(t_edit_at), None),
|
||||
("sp-skew", t_skew_posted, Some(t_skew_edit), Some("0")),
|
||||
];
|
||||
let mut bytes = Vec::new();
|
||||
let mut baseline_posts = Vec::new();
|
||||
for (spiel, ts, ed, bot) in &inputs {
|
||||
bytes.extend_from_slice(&make_post_doc(
|
||||
&fields,
|
||||
"CEO_RY",
|
||||
spiel,
|
||||
*ts,
|
||||
*ed,
|
||||
*bot,
|
||||
Some("u"),
|
||||
spiel,
|
||||
));
|
||||
baseline_posts.push(CeoPost {
|
||||
key: "CEO_RY".to_string(),
|
||||
spiel_id: spiel.to_string(),
|
||||
timestamp_ms: *ts,
|
||||
edited_ms: *ed,
|
||||
bot: bot.map(str::to_string),
|
||||
user_id: Some("u".to_string()),
|
||||
text: spiel.to_string(),
|
||||
});
|
||||
}
|
||||
let bson_path = dir.path().join("posts.bson");
|
||||
write_bytes(&bson_path, &bytes);
|
||||
let out_dir = dir.path().join("out");
|
||||
|
||||
let report = run_curate_posts(&default_args(
|
||||
bson_path,
|
||||
map_path.clone(),
|
||||
out_dir.clone(),
|
||||
CeoPostsTimestampUnit::Millis,
|
||||
))
|
||||
.expect("run");
|
||||
assert_report_invariants(&report);
|
||||
assert_eq!(report.curate_stats.builder.edited, 2);
|
||||
assert_eq!(report.curate_stats.builder.edited_before_posted, 1);
|
||||
assert_eq!(report.write_stats.rows_written, 4);
|
||||
|
||||
let mapping = ProviderMappingTable::from_rows(vec![
|
||||
ProviderMappingRow {
|
||||
source: "ceo".to_string(),
|
||||
source_id: "CEO_RY".to_string(),
|
||||
valid_from: day(2020, 1, 1),
|
||||
valid_to: Some(day(2023, 12, 31)),
|
||||
listing_id: 101,
|
||||
},
|
||||
ProviderMappingRow {
|
||||
source: "ceo".to_string(),
|
||||
source_id: "CEO_RY".to_string(),
|
||||
valid_from: day(2024, 1, 1),
|
||||
valid_to: None,
|
||||
listing_id: 102,
|
||||
},
|
||||
])
|
||||
.unwrap();
|
||||
let baseline = curate_posts(baseline_posts, "ceo", &mapping).expect("baseline");
|
||||
let rows = read_all_event_partitions(&out_dir);
|
||||
assert_eq!(rows, baseline.rows);
|
||||
assert_eq!(report.curate_stats, baseline.stats);
|
||||
|
||||
let find = |spiel: &str| {
|
||||
rows.iter()
|
||||
.find(|r| r.payload_json.contains(spiel))
|
||||
.unwrap_or_else(|| panic!("row {spiel}"))
|
||||
};
|
||||
assert_eq!(find("sp-eve").listing_id, 101);
|
||||
assert_eq!(find("sp-ny").listing_id, 102);
|
||||
let edit = find("sp-edit");
|
||||
assert_eq!(edit.listing_id, 101);
|
||||
assert_eq!(edit.ts_micros, t_edit_posted * 1_000);
|
||||
assert_eq!(edit.known_at_micros, t_edit_at * 1_000);
|
||||
let skew = find("sp-skew");
|
||||
assert_eq!(skew.ts_micros, t_skew_posted * 1_000);
|
||||
assert_eq!(skew.known_at_micros, t_skew_posted * 1_000);
|
||||
// No row may become visible before it was posted.
|
||||
assert!(rows.iter().all(|r| r.known_at_micros >= r.ts_micros));
|
||||
// All four posts land in the UTC-2024 partition only.
|
||||
assert_eq!(sorted_names(&out_dir.join("events")), vec!["year=2024"]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue