Datasets:
Download tests/test_taxonomy.py from ZipLime/industry-classification: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/datasets/ZipLime/industry-classification/resolve/main/tests/test_taxonomy.py
- Command line
-
hf download hf://datasets/ZipLime/industry-classification/tests/test_taxonomy.py
-
curl -L -o test_taxonomy.py https://huggingface.co/datasets/ZipLime/industry-classification/resolve/main/tests/test_taxonomy.py
3.93 kB
| """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 | |
| 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 | |