File size: 6,349 Bytes
48cf766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""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]}"