File size: 3,927 Bytes
06a67ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The taxonomy is code, and the things that would break it quietly."""

from __future__ import annotations

import pytest

from recipe.quality import QualityReport, check_taxonomy
from recipe.sic import EDGAR_ONLY_CODES, code_level, division_of, normalize, prefix_of
from recipe.taxonomy import INDUSTRIES, RULES, SECTORS, classify


def test_every_rule_names_a_real_industry_and_sector():
    report = QualityReport()
    check_taxonomy(report)
    assert report.errors == []


def test_every_industry_is_reachable_from_some_rule():
    """An industry no rule points at is a title nothing will ever carry."""
    reachable = {code for _, _, code in RULES}
    # The three catch-alls exist for codes outside EDGAR's current list.
    unreachable = set(INDUSTRIES) - reachable
    assert unreachable <= {"UNCL_OTHER", "FUND_TRUST", "GOVT_PUBLIC"}, unreachable


def test_every_four_digit_code_classifies():
    """No gap in the rules: every code the SEC could invent lands somewhere."""
    for value in range(100, 10_000):
        assignment = classify(f"{value:04d}")
        assert assignment.industry_code in INDUSTRIES
        assert assignment.sector_code in SECTORS


@pytest.mark.parametrize(
    ("sic", "sector", "industry"),
    [
        ("2834", "HLTH", "HLTH_PHARMA"),
        ("2836", "HLTH", "HLTH_BIOTECH"),
        # Managed care files under an insurance code and is not insurance.
        ("6324", "HLTH", "HLTH_MANAGED"),
        ("3674", "TECH", "TECH_SEMI"),
        # Semiconductor equipment hides inside "special industry machinery".
        ("3559", "TECH", "TECH_SEMI"),
        ("7372", "TECH", "TECH_SOFTWARE"),
        ("6022", "FINS", "FINS_BANK"),
        ("6798", "REAL", "REAL_REIT"),
        ("6770", "SHEL", "SHEL_BLANK"),
        ("4911", "UTIL", "UTIL_ELEC"),
        ("1311", "ENER", "ENER_EP"),
        # A roll-up resolves to the same place as the industries beneath it.
        ("2000", "COND", "COND_FOOD"),
        ("2011", "COND", "COND_FOOD"),
        ("8888", "GOVT", "GOVT_SOVEREIGN"),
        ("8880", "UNCL", "UNCL_ADR"),
    ],
)
def test_known_codes(sic, sector, industry):
    assignment = classify(sic)
    assert (assignment.sector_code, assignment.industry_code) == (sector, industry)


def test_narrow_rules_beat_the_ranges_that_contain_them():
    """First match wins, so an exception placed after its range is dead code."""
    for index, (low, high, _) in enumerate(RULES):
        for other_low, other_high, _ in RULES[index + 1:]:
            contained = other_low >= low and other_high <= high
            assert not (contained and (other_low, other_high) != (low, high)), (
                f"range {other_low}-{other_high} sits inside the earlier "
                f"{low}-{high} and can never match"
            )


def test_unclassified_rather_than_raising():
    for value in (None, "", "abc", "12345", "0"):
        assert classify(value).sector_code == "UNCL"


def test_normalize_accepts_what_edgar_actually_writes():
    assert normalize("100") == "0100"
    assert normalize(2834) == "2834"
    assert normalize(" 3571 ") == "3571"
    assert normalize("") is None
    assert normalize("0000") is None
    assert normalize("12345") is None


def test_code_level_reads_the_padding():
    assert code_level("2000") == "major_group"
    assert code_level("2020") == "industry_group"
    assert code_level("2834") == "industry"
    assert prefix_of("2000") == "20"
    assert prefix_of("2020") == "202"
    assert prefix_of("2834") == "2834"


def test_divisions_cover_every_major_group_the_sec_uses():
    assert division_of("2834").letter == "D"
    assert division_of("6022").letter == "H"
    assert division_of("9995") is None


def test_edgar_inventions_have_no_division():
    """88 is Private Households; calling an ADR a household service is worse
    than saying nothing."""
    for code in EDGAR_ONLY_CODES:
        assert division_of(code) is None