Coverage for tests/references/test_courts.py: 100%
50 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-22 15:51 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-22 15:51 +0000
1"""Tests the courts module."""
3from __future__ import annotations
5import pandas as pd
6import pytest
8from courtpy import courts
11def test_federal_courts() -> None:
12 table = courts.federal_courts()
13 assert list(table.columns) == [
14 'court_num', 'court', 'feature_name', 'level', 'circuit_num', 'courtlistener_name',
15 'fjc_name', 'opinion_name']
16 assert table['court'].is_unique
17 assert set(table['level']) == {'high', 'appellate', 'trial', 'specialty'}
18 appeals = table[table['courtlistener_name'].str.fullmatch(r'ca\d+|cadc|cafc', na = False)]
19 assert sorted(appeals['court_num']) == list(range(1, 14))
20 # Every trial court has a circuit, and every current one an id.
21 districts = table[table['level'] == 'trial']
22 assert districts['circuit_num'].notna().all()
23 by_id = table.dropna(subset = ['courtlistener_name']).set_index('courtlistener_name')
24 assert by_id.loc['nysd', 'circuit_num'] == 2
25 assert by_id.loc['dcd', 'circuit_num'] == 12
26 assert by_id.loc['cand', 'circuit_num'] == 9
27 assert by_id.loc['scotus', 'court'] == 'scotus'
28 assert by_id.loc['scotus', 'feature_name'] == 'SCOTUS'
29 assert by_id.loc['bap9', 'court_num'] == 110
30 assert by_id.loc['nysd', 'feature_name'] == 'Southern District of New York'
31 assert by_id.loc['txsd', 'opinion_name'].startswith('SOUTHERN DISTRICT OF TEXAS|')
32 # A copy is returned each time.
33 table.loc[0, 'court'] = 'changed'
34 assert courts.federal_courts().loc[0, 'court'] != 'changed'
37def test_court_table() -> None:
38 assert courts.court_table('federal').equals(courts.federal_courts())
39 with pytest.raises(FileNotFoundError, match = 'nowhere'):
40 courts.court_table('nowhere')
43def test_courtlistener_lookups() -> None:
44 numbers = courts.court_numbers_by_courtlistener_name()
45 names = courts.court_names_by_courtlistener_name()
46 assert (numbers['ca1'], names['ca1']) == (1, 'first_circuit')
47 assert (numbers['scotus'], names['scotus']) == (99, 'scotus')
48 assert (numbers['mad'], names['mad']) == (1026, 'district_of_massachusetts')
49 assert 'nowhere' not in numbers
50 with pytest.raises(TypeError):
51 numbers['ca1'] = 2 # type: ignore[index]
54def test_circuits() -> None:
55 cases = pd.DataFrame({
56 'court': ['ca5', 'NYSD', None, 'scotus', 'nowhere'],
57 'court_num': pd.array([5, pd.NA, 12, 99, pd.NA], dtype = 'Int64')})
58 assert courts.circuits(cases).tolist() == [5, 2, 12, pd.NA, pd.NA]
59 assert courts.circuits(cases[['court_num']]).tolist() == [5, pd.NA, 12, pd.NA, pd.NA]
62def test_court_labels() -> None:
63 cases = pd.DataFrame({
64 'court_name': ['tenth_circuit', None, 'dc_circuit', None, None],
65 'court_num': [10, 9, 12, 1026, None]})
66 labels = courts.court_labels(cases)
67 assert labels.tolist()[:4] == [
68 'Tenth Circuit', 'Ninth Circuit', 'D.C. Circuit', 'District of Massachusetts']
69 assert pd.isna(labels.iloc[4])
70 # The categories are the courts of the cases, in the table's order.
71 assert list(labels.cat.categories) == [
72 'Ninth Circuit', 'Tenth Circuit', 'D.C. Circuit', 'District of Massachusetts']
73 assert courts.court_labels(cases[['court_num']]).tolist()[:3] == [
74 'Tenth Circuit', 'Ninth Circuit', 'D.C. Circuit']
75 with pytest.raises(KeyError, match = 'labeled'):
76 courts.court_labels(pd.DataFrame({'year': [2008]}))