"""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