Repository navigation
Expand file tree
/
Copy pathtest_sec_segments.py
More file actions
150 lines (130 loc) · 6.82 KB
/
Copy pathtest_sec_segments.py
File metadata and controls
150 lines (130 loc) · 6.82 KB
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
import unittest
from unittest.mock import Mock
import requests
from sec_segments import (
Company,
Filing,
Report,
SecClient,
UnsupportedFiling,
_parse_number,
parse_companies,
parse_filing_summary,
parse_latest_10k,
parse_segment_table,
rank_segment_reports,
search_companies,
segment_shares,
)
def table_html(metric="Revenue", total=300, first=100, second=200, unit="$ in Millions"):
return f"""
<table>
<thead>
<tr><th>Segment detail - USD ($)</th><th colspan="3">12 Months Ended</th></tr>
<tr><th>{unit}</th><th>Dec. 31, 2025</th><th>Dec. 31, 2024</th><th>Dec. 31, 2023</th></tr>
</thead>
<tbody>
<tr><td>Segment Reporting Information [Line Items]</td><td></td><td></td><td></td></tr>
<tr><td>Total revenues</td><td>{total}</td><td>{total - 30}</td><td>{total - 60}</td></tr>
<tr><td>First Segment</td><td></td><td></td><td></td></tr>
<tr><td>Segment Reporting Information [Line Items]</td><td></td><td></td><td></td></tr>
<tr><td>{metric}</td><td>{first}</td><td>{first - 10}</td><td>{first - 20}</td></tr>
<tr><td>Second Segment</td><td></td><td></td><td></td></tr>
<tr><td>Segment Reporting Information [Line Items]</td><td></td><td></td><td></td></tr>
<tr><td>{metric}</td><td>{second}</td><td>{second - 20}</td><td>{second - 40}</td></tr>
</tbody>
</table>
"""
class SecSegmentsTest(unittest.TestCase):
def setUp(self):
self.company = Company("TEST", "Test Corporation", 1234)
self.filing = Filing("0000001234-26-000001", "2026-02-01", "2025-12-31", "test.htm", 1234)
def parse(self, html):
return parse_segment_table(html, self.company, self.filing, "Segment detail", "https://example.test/R1.htm")
def test_company_lookup_prefers_exact_ticker_and_handles_names(self):
companies = parse_companies(
{
"0": {"ticker": "MSFT", "title": "MICROSOFT CORP", "cik_str": 789019},
"1": {"ticker": "MSF.B", "title": "MICROSOFT OTHER", "cik_str": 2},
"bad": {"ticker": "BROKEN"},
}
)
self.assertEqual(search_companies(companies, "msft")[0].cik, 789019)
self.assertEqual(search_companies(companies, "Microsoft Corp")[0].ticker, "MSFT")
self.assertEqual(len(search_companies(companies, "Microsoft")), 2)
self.assertEqual(search_companies(companies, ""), [])
def test_latest_10k_and_archive_urls(self):
payload = {
"filings": {"recent": {
"form": ["10-Q", "10-K"],
"accessionNumber": ["q", "0000001234-26-000001"],
"filingDate": ["2026-05-01", "2026-02-01"],
"reportDate": ["2026-03-31", "2025-12-31"],
"primaryDocument": ["q.htm", "test.htm"],
}}
}
filing = parse_latest_10k(payload, 1234)
self.assertEqual(filing.accession_compact, "000000123426000001")
self.assertTrue(filing.filing_url.endswith("/000000123426000001/test.htm"))
with self.assertRaises(UnsupportedFiling):
parse_latest_10k({"filings": {"recent": {"form": []}}}, 1234)
def test_filing_summary_ranking_excludes_wrong_tables(self):
xml = """<FilingSummary><MyReports>
<Report><ShortName>Segment Information - Disaggregation of Revenue (Details)</ShortName><HtmlFileName>R1.htm</HtmlFileName></Report>
<Report><ShortName>Segment Information - Reportable Segments and Reconciliation (Details)</ShortName><HtmlFileName>R2.htm</HtmlFileName></Report>
<Report><ShortName>Unearned Revenue by Segment (Detail)</ShortName><HtmlFileName>R3.htm</HtmlFileName></Report>
</MyReports></FilingSummary>"""
ranked = rank_segment_reports(parse_filing_summary(xml))
self.assertEqual([report.html_file for report in ranked], ["R2.htm"])
with self.assertRaises(UnsupportedFiling):
parse_filing_summary("<broken")
def test_parser_supports_revenue_net_sales_and_contract_revenue(self):
for metric in ("Revenue", "Net sales", "Revenue from contract with customers"):
with self.subTest(metric=metric):
result = self.parse(table_html(metric=metric))
self.assertEqual(list(result.values.columns), ["First Segment", "Second Segment"])
self.assertEqual(result.values.loc["2025"].sum(), 300)
self.assertEqual(result.consolidated.loc["2025"], 300)
self.assertEqual(result.unit, "USD in millions")
def test_parser_preserves_reconciliation_and_multiple_units(self):
result = self.parse(table_html(total=299, unit="$ in Thousands"))
self.assertEqual(result.difference.loc["2025"], -1)
self.assertEqual(result.unit, "USD in thousands")
self.assertEqual(_parse_number("(1,234)"), -1234)
self.assertEqual(_parse_number("—"), 0)
def test_one_segment_and_missing_table_are_unsupported(self):
one_segment = table_html().replace(
"<tr><td>Second Segment</td><td></td><td></td><td></td></tr>\n <tr><td>Segment Reporting Information [Line Items]</td><td></td><td></td><td></td></tr>\n <tr><td>Revenue</td><td>200</td><td>180</td><td>160</td></tr>",
"",
)
with self.assertRaises(UnsupportedFiling):
self.parse(one_segment)
with self.assertRaises(UnsupportedFiling):
self.parse("<html><p>No table</p></html>")
def test_client_uses_bounded_retries_and_timeouts(self):
client = SecClient("Test test@example.com")
retries = client.session.get_adapter("https://").max_retries
self.assertEqual(retries.total, 3)
self.assertIn(429, retries.status_forcelist)
session = Mock()
session.headers = {}
session.get.side_effect = requests.Timeout
with self.assertRaises(requests.Timeout):
SecClient("Test test@example.com", session)._get("https://example.test")
session.get.assert_called_once_with("https://example.test", timeout=(5, 30))
def test_segment_shares_are_percentages(self):
result = self.parse(table_html())
shares = segment_shares(result.values, "2025")
self.assertAlmostEqual(shares["Share"].sum(), 1)
self.assertEqual(shares.set_index("Segment").loc["First Segment", "Share"], 1 / 3)
invalid = result.values.copy()
invalid.loc["2025", "First Segment"] = -1
with self.assertRaises(ValueError):
segment_shares(invalid, "2025")
zero_segment = result.values.copy()
zero_segment.loc["2025", "First Segment"] = 0
shares = segment_shares(zero_segment, "2025")
self.assertEqual(shares["Segment"].tolist(), ["Second Segment"])
self.assertEqual(shares["Share"].tolist(), [1])
if __name__ == "__main__":
unittest.main()