diff --git a/tools/curate-etl/src/bson_qm_financials_tests.rs b/tools/curate-etl/src/bson_qm_financials_tests.rs index ed2ba301..b563060a 100644 --- a/tools/curate-etl/src/bson_qm_financials_tests.rs +++ b/tools/curate-etl/src/bson_qm_financials_tests.rs @@ -5,7 +5,9 @@ use bson::DateTime as BsonDateTime; use super::*; use crate::events::{QmFiling, KNOWN_AT_SOURCE_FILING_TIMESTAMP}; -use crate::fundamentals::{build_fundamentals_rows, StatutoryLagVenue}; +use crate::fundamentals::{ + as_of, build_fundamentals_rows, build_fundamentals_snapshots, StatutoryLagVenue, +}; use crate::shorts::epoch_days_to_ymd; use crate::split::ymd_to_epoch_days; @@ -811,3 +813,195 @@ fn decoded_observation_feeds_build_fundamentals_rows_with_filing_timestamp() { assert_eq!(row.fields.total_equity, Some(4_500_000.25)); assert_eq!(row.fields.shares_outstanding, Some(100_000_000.0)); } + +#[test] +fn nested_vintages_preserve_the_earlier_snapshot_until_the_amended_filing() { + let fields = QmFinancialsFieldNames { + key: "metadata.identity.code", + period_end: "metadata.period.end", + report_period: "metadata.period.token", + report_date: "metadata.period.vintage", + ..QmFinancialsFieldNames::with_verified_defaults( + "metadata.identity.currency", + Some("IncomeStatement.values.revenue"), + None, + None, + None, + None, + None, + ) + }; + let statement = |vintage: RawBson, revenue: i32| { + let mut identity = RawDocumentBuf::new(); + identity.append(cstr!("code"), "FIXTURE:CA"); + identity.append(cstr!("currency"), "CAD"); + let mut period = RawDocumentBuf::new(); + period.append(cstr!("end"), "2024-06-30"); + period.append(cstr!("token"), "Q2"); + period.append(cstr!("vintage"), vintage); + let mut metadata = RawDocumentBuf::new(); + metadata.append(cstr!("identity"), identity); + metadata.append(cstr!("period"), period); + let mut values = RawDocumentBuf::new(); + values.append(cstr!("revenue"), revenue); + let mut income = RawDocumentBuf::new(); + income.append(cstr!("values"), values); + let mut doc = RawDocumentBuf::new(); + doc.append(cstr!("metadata"), metadata); + doc.append(cstr!("IncomeStatement"), income); + doc + }; + let original = statement(RawBson::String("2024-08-02".to_owned()), 2_100_000); + let amendment = statement(RawBson::String("2024-09-06".to_owned()), 900_000); + let invalid = statement(RawBson::DateTime(BsonDateTime::from_millis(i64::MAX)), 1); + let filing = |timefiled: &str, form_type: &str, id: &str| QmFiling { + key: "FIXTURE:CA", + timefiled: timefiled.to_owned(), + form_type: form_type.to_owned(), + filing_id: Some(id.to_owned()), + url: None, + }; + let original_filing = filing("2024-08-02T16:30:15 EDT", "10-Q", "fixture-original"); + let amended_filing = filing("2024-09-06T16:30:15 EDT", "10-Q/A", "fixture-amended"); + let original_micros = 1_722_630_615_000_000; + let amended_micros = 1_725_654_615_000_000; + let venues = BTreeMap::from([("FIXTURE:CA", StatutoryLagVenue::UsTsx)]); + let decode = |bytes| { + let mut observations = Vec::new(); + let stats = decode_qm_financials_buffer(bytes, &fields, token_period_cfg(), |obs| { + observations.push(obs); + }) + .unwrap(); + assert_conservation(&stats); + (observations, stats) + }; + + let (baseline, _) = decode(original.as_bytes()); + let baseline = build_fundamentals_rows(baseline, [original_filing.clone()], &venues); + let baseline = build_fundamentals_snapshots(&baseline.rows, 1).unwrap(); + let input = [ + amendment.as_bytes(), + invalid.as_bytes(), + original.as_bytes(), + ] + .concat(); + let (observations, stats) = decode(&input); + let full = build_fundamentals_rows(observations, [amended_filing, original_filing], &venues); + let snapshots = build_fundamentals_snapshots(&full.rows, 1).unwrap(); + + assert_eq!( + stats, + BsonQmFinancialsDecodeStats { + docs_in: 3, + rows_out: 2, + skipped_report_date_out_of_range: 1, + ..Default::default() + } + ); + assert_eq!(full.stats.rows_linked_to_filing, 2); + assert_eq!(full.stats.rows_statutory_lag_estimate, 0); + assert_eq!(snapshots.len(), 2); + assert_eq!(as_of(&snapshots, "FIXTURE:CA", original_micros - 1), None); + let prior = as_of(&snapshots, "FIXTURE:CA", amended_micros - 1).unwrap(); + assert_eq!(prior, &baseline[0]); + assert_eq!(prior.effective_known_at_utc_micros, original_micros); + assert_eq!(prior.periods[0].fields.revenue, Some(2_100_000.0)); + let revised = as_of(&snapshots, "FIXTURE:CA", amended_micros).unwrap(); + assert_eq!(revised.effective_known_at_utc_micros, amended_micros); + assert_eq!(revised.periods[0].fields.revenue, Some(900_000.0)); +} + +#[test] +fn distinguishes_null_and_scalar_intermediates_and_matches_period_tokens_verbatim() { + let fields = QmFinancialsFieldNames { + report_date: "disclosure.vintage", + ..QmFinancialsFieldNames::with_verified_defaults( + "currency", + Some("IncomeStatement.revenue"), + None, + None, + None, + None, + None, + ) + }; + let fixture = |token: &str, disclosure: RawBson, income: RawBson| { + let mut doc = RawDocumentBuf::new(); + doc.append(cstr!("qmSearchCode"), " FIXTURE:CA "); + doc.append(cstr!("periodEndDate"), "2024-06-30"); + doc.append(cstr!("reportPeriod"), token); + doc.append(cstr!("currency"), "CAD"); + doc.append(cstr!("disclosure"), disclosure); + doc.append(cstr!("IncomeStatement"), income); + doc + }; + let docs = [ + fixture("Q2", RawBson::Null, RawBson::Null), + fixture("Q2", RawBson::Null, RawBson::Int32(1)), + fixture("Q2", RawBson::Int32(1), RawBson::Null), + fixture(" Q2 ", RawBson::Int32(1), RawBson::Int32(1)), + fixture("q2", RawBson::Null, RawBson::Null), + ]; + let input: Vec = docs + .iter() + .flat_map(|doc| doc.as_bytes().iter().copied()) + .collect(); + let mut rows = Vec::new(); + + let stats = decode_qm_financials_buffer(&input, &fields, token_period_cfg(), |row| { + rows.push(row); + }) + .unwrap(); + + assert_conservation(&stats); + assert_eq!( + stats, + BsonQmFinancialsDecodeStats { + docs_in: 5, + rows_out: 2, + skipped_unmapped_report_period: 2, + skipped_wrong_report_date_type: 1, + nulled_missing_path: 1, + nulled_wrong_type: 1, + ..Default::default() + } + ); + assert_eq!(rows.len(), 2); + for row in rows { + assert_eq!(row.key, " FIXTURE:CA "); + assert_eq!(row.report_date, None); + assert_eq!(row.fields.revenue, None); + } +} + +#[test] +fn validates_unused_nested_documents_at_the_depth_boundary() { + let nest = |levels| { + let mut inner = RawDocumentBuf::new(); + for _ in 1..levels { + let mut parent = RawDocumentBuf::new(); + parent.append(cstr!("nested"), inner); + inner = parent; + } + inner + }; + let mut good = make_valid_doc("AAPL:US"); + good.append(cstr!("extra"), nest(128)); + let mut bad = make_valid_doc("SHOP:CA"); + bad.append(cstr!("extra"), nest(129)); + let input = [good.as_bytes(), bad.as_bytes(), good.as_bytes()].concat(); + let mut seen = Vec::new(); + + let error = + decode_qm_financials_buffer(&input, &full_test_fields(), token_period_cfg(), |row| { + seen.push(row.key); + }) + .unwrap_err(); + + assert_eq!(seen, ["AAPL:US"]); + assert!(matches!( + error, + BsonQmFinancialsError::CorruptBson { offset, .. } + if offset == good.as_bytes().len() as u64 + )); +}