financial-model-builder-v2 / tests /test_data_processor.py
bdschi1's picture
deploy
48cf766
Raw History Blame Contribute Delete
6.35 kB
"""Tests for data processor — sign normalization and instant_start shift."""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from data_processor import DataProcessor
def _make_facts(concepts_data):
"""Build a minimal company_facts dict for testing."""
return {
"entityName": "TestCo",
"cik": 12345,
"facts": {"us-gaap": concepts_data, "dei": {}},
}
def _make_submissions(fye="1231"):
return {"fiscalYearEnd": fye, "sic": "1234", "sicDescription": "Test"}
def _usd_entry(val, start, end, fy, fp, form="10-K", filed="2024-03-01"):
return {
"val": val, "start": start, "end": end,
"fy": fy, "fp": fp, "form": form, "filed": filed,
"units": "USD",
}
def _instant_entry(val, end, fy, fp, form="10-K", filed="2024-03-01"):
return {
"val": val, "end": end,
"fy": fy, "fp": fp, "form": form, "filed": filed,
"units": "USD",
}
def test_sign_normalization_is_cogs():
"""COGS (IS-2000) with sign:-1 should be stored as negative."""
facts = _make_facts({
"CostOfRevenue": {
"units": {"USD": [
_usd_entry(60_000_000, "2023-01-01", "2023-12-31", 2023, "FY"),
]}
}
})
proc = DataProcessor(facts, _make_submissions())
result = proc.extract_annual_data()
cogs = result["is_data"].get("IS-2000", {})
assert 2023 in cogs, "IS-2000 should have FY2023 data"
assert cogs[2023] < 0, f"COGS should be negative after sign normalization, got {cogs[2023]}"
assert cogs[2023] == -60.0, f"Expected -60.0, got {cogs[2023]}"
def test_sign_normalization_revenue_stays_positive():
"""Revenue (IS-1000) has no sign field — should stay positive."""
facts = _make_facts({
"Revenues": {
"units": {"USD": [
_usd_entry(100_000_000, "2023-01-01", "2023-12-31", 2023, "FY"),
]}
}
})
proc = DataProcessor(facts, _make_submissions())
result = proc.extract_annual_data()
rev = result["is_data"].get("IS-1000", {})
assert 2023 in rev
assert rev[2023] == 100.0, f"Revenue should be positive 100.0, got {rev[2023]}"
def test_sign_normalization_bs_treasury_stock():
"""Treasury Stock (BS-6300) with sign:-1 should be stored as negative."""
facts = _make_facts({
"TreasuryStockValue": {
"units": {"USD": [
_instant_entry(50_000_000, "2023-12-31", 2023, "FY"),
]}
}
})
proc = DataProcessor(facts, _make_submissions())
result = proc.extract_annual_data()
ts = result["bs_data"].get("BS-6300", {})
assert 2023 in ts
assert ts[2023] < 0, f"Treasury stock should be negative, got {ts[2023]}"
def test_sign_normalization_cf_capex():
"""CapEx (CF-4100) with sign:-1 should be stored as negative."""
facts = _make_facts({
"PaymentsToAcquirePropertyPlantAndEquipment": {
"units": {"USD": [
_usd_entry(10_000_000, "2023-01-01", "2023-12-31", 2023, "FY"),
]}
}
})
proc = DataProcessor(facts, _make_submissions())
result = proc.extract_annual_data()
capex = result["cf_data"].get("CF-4100", {})
assert 2023 in capex
assert capex[2023] < 0, f"CapEx should be negative, got {capex[2023]}"
def test_instant_start_shift():
"""Beginning Cash (CF-6200, instant_start) should shift fy+1."""
facts = _make_facts({
"CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalentsIncludingDisposalGroupAndDiscontinuedOperations": {
"units": {"USD": [
_instant_entry(25_000_000, "2022-12-31", 2022, "FY"),
_instant_entry(30_000_000, "2023-12-31", 2023, "FY"),
]}
},
# Need some duration item to establish the year range
"NetIncomeLoss": {
"units": {"USD": [
_usd_entry(5_000_000, "2023-01-01", "2023-12-31", 2023, "FY"),
]}
},
})
proc = DataProcessor(facts, _make_submissions())
result = proc.extract_annual_data()
beg_cash = result["cf_data"].get("CF-6200", {})
# FY2022 ending balance -> beginning of FY2023
assert 2023 in beg_cash, f"Beginning cash should map to FY2023, got keys: {list(beg_cash.keys())}"
assert beg_cash[2023] == 25.0
# FY2023 ending balance -> beginning of FY2024
assert 2024 in beg_cash, f"Beginning cash should also map to FY2024"
assert beg_cash[2024] == 30.0
def test_instant_end_no_shift():
"""Ending Cash (CF-6300, instant_end) should NOT shift fy."""
facts = _make_facts({
"CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalentsIncludingDisposalGroupAndDiscontinuedOperations": {
"units": {"USD": [
_instant_entry(30_000_000, "2023-12-31", 2023, "FY"),
]}
},
})
proc = DataProcessor(facts, _make_submissions())
result = proc.extract_annual_data()
end_cash = result["cf_data"].get("CF-6300", {})
assert 2023 in end_cash, f"Ending cash should map to FY2023"
assert end_cash[2023] == 30.0
def test_non_december_fye():
"""Companies with non-Dec FYE should correctly map fiscal years."""
# Apple-like: FYE September (month 9)
facts = _make_facts({
"Revenues": {
"units": {"USD": [
_usd_entry(100_000_000, "2022-10-01", "2023-09-30", 2023, "FY"),
]}
}
})
proc = DataProcessor(facts, _make_submissions(fye="0930"))
result = proc.extract_annual_data()
rev = result["is_data"].get("IS-1000", {})
assert 2023 in rev, f"Revenue should map to FY2023 for Sep FYE"
def test_quarterly_sign_normalization():
"""Quarterly extraction should also apply sign normalization."""
facts = _make_facts({
"CostOfRevenue": {
"units": {"USD": [
_usd_entry(15_000_000, "2023-01-01", "2023-03-31", 2023, "Q1", "10-Q"),
]}
}
})
proc = DataProcessor(facts, _make_submissions())
result = proc.extract_quarterly_data()
cogs = result["is_data"].get("IS-2000", {})
q1_key = "Q1 2023"
assert q1_key in cogs, f"Expected quarterly COGS for Q1 2023"
assert cogs[q1_key] < 0, f"Quarterly COGS should be negative, got {cogs[q1_key]}"