Coverage for tests/conftest.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-22 22:47 +0000

1"""Invented judges in the layout of the FJC's files, and a fake FJC website.""" 

2 

3from __future__ import annotations 

4 

5import io 

6import pathlib 

7from typing import Any 

8 

9import judicial_common_space as jcs 

10import pandas as pd 

11import pytest 

12 

13from fjc_biographies import downloader, options 

14 

15FIRST = 'U.S. Court of Appeals for the First Circuit' 

16NINTH = 'U.S. Court of Appeals for the Ninth Circuit' 

17APPEALS = 'U.S. Court of Appeals' 

18DISTRICT = 'U.S. District Court' 

19# Invented judges, in the layout of the FJC's file of federal judicial 

20# service: nid, sequence, name, court type, court, president, party, ABA 

21# rating, recess appointment, vote type, ayes/nays, commission, senior 

22# status, and termination. 

23SERVICE = [ 

24 (101, 1, 'Lynch, Mary Ann', APPEALS, FIRST, 'William J. Clinton', 'Democratic', 'Well Qualified', '', 'Voice', '', '1995-03-17', '', ''), 

25 (102, 1, 'Howard, John Q.', APPEALS, FIRST, 'George W. Bush', 'Republican', 'Qualified', '', 'Roll Call', ' 99/1', '2002-04-23', '', ''), 

26 (103, 1, 'Thompson, Rita Mae', APPEALS, FIRST, 'Barack Obama', 'Democratic', 'Exceptionally Well Qualified', '', 'Roll Call', ' 98/0', '2010-03-17', '', ''), 

27 (104, 1, 'Smith, Milton Dale, Jr.', APPEALS, NINTH, 'George W. Bush', 'Republican', 'Well Qualified', '', 'Voice', '', '2006-05-18', '', ''), 

28 (105, 1, 'Smith, N[orman] Roy', APPEALS, NINTH, 'George W. Bush', 'Republican', '', '', 'Roll Call', ' 94/0', '2007-03-19', '', ''), 

29 (106, 1, 'Woods, Dana Paul', DISTRICT, 'U.S. District Court for the District of Massachusetts', 'Ronald Reagan', 'Republican', 'Not Qualified', '', 'Voice', '', '1986-06-16', '2015-06-01', ''), 

30 (107, 2, 'Stone, Nora H.', APPEALS, FIRST, 'George H.W. Bush', 'Republican', 'Well Qualified', '', 'Voice', '', '1992-04-30', '2001-04-16', ''), 

31 (107, 1, 'Stone, Nora H.', DISTRICT, 'U.S. District Court for the District of New Hampshire', 'George H.W. Bush', 'Republican', 'Qualified', '', 'Voice', '', '1990-04-27', '', '1992-04-30'), 

32 (108, 1, 'Fields, R[ay] Lanier III', APPEALS, 'U.S. Court of Appeals for the Fifth Circuit', 'Jimmy Carter', 'Democratic', 'Well Qualified', '', 'Roll Call', ' 60/30', '1979-07-13', '', '1981-10-01'), 

33 (108, 2, 'Fields, R[ay] Lanier III', APPEALS, 'U.S. Court of Appeals for the Eleventh Circuit', 'None (reassignment)', 'None (reassignment)', '', '', '', '', '1981-10-01', '', ''), 

34 (109, 1, 'Rake, Jed Saul', DISTRICT, 'U.S. District Court for the Southern District of New York', 'William J. Clinton', 'Democratic', 'Well Qualified', '', 'Voice', '', '1996-03-01', '', ''), 

35 (110, 1, 'Poser, Richard Allen', APPEALS, 'U.S. Court of Appeals for the Seventh Circuit', 'Ronald Reagan', 'Republican', 'Qualified', '', 'Voice', '', '1981-12-01', '', '2017-09-02'), 

36 (111, 1, 'Gray, Roger L.', APPEALS, 'U.S. Court of Appeals for the Fourth Circuit', 'William J. Clinton', 'Democratic', '', '2000-12-27', 'Unknown', '', '', '', '2001-07-20'), 

37 (112, 1, 'Marsh, John', 'Supreme Court', 'Supreme Court of the United States', 'John Adams', 'Federalist', '', '', 'Voice', '', '1801-01-31', '', '1835-07-06'), 

38 (113, 1, 'Ward, Ellen', APPEALS, 'U.S. Court of Appeals for the District of Columbia Circuit', 'Barack Obama', 'Democratic', 'Well Qualified', '', 'Roll Call', ' 53/44', '2013-12-12', '', ''), 

39 (113, 2, 'Ward, Ellen', APPEALS, 'U.S. Court of Appeals for the Federal Circuit', 'None (reassignment)', 'None (reassignment)', '', '', '', '', '2020-01-02', '', '')] 

40SERVICE_COLUMNS = [ 

41 'nid', 'Sequence', 'Judge Name', 'Court Type', 'Court Name', 

42 'Appointing President', 'Party of Appointing President', 'ABA Rating', 

43 'Recess Appointment Date', 'Senate Vote Type', 'Ayes/Nays', 

44 'Commission Date', 'Senior Status Date', 'Termination Date'] 

45DEMOGRAPHICS = [ 

46 (101, 'Lynch', 'Mary', 'Ann', '', '1946', 'Female', 'White'), 

47 (102, 'Howard', 'John', 'Q.', '', '1955', 'Male', 'White'), 

48 (103, 'Thompson', 'Rita', 'Mae', '', '1951', 'Female', 'African American'), 

49 (104, 'Smith', 'Milton', 'Dale', 'Jr.', '1942', 'Male', 'White'), 

50 (105, 'Smith', 'N[orman]', 'Roy', '', '1949', 'Male', 'Hispanic/White'), 

51 (106, 'Woods', 'Dana', 'Paul', '', '1947', 'Male', 'White'), 

52 (107, 'Stone', 'Nora', 'H.', '', '1931', 'Female', 'White'), 

53 (108, 'Fields', 'R[ay]', 'Lanier', 'III', '1936', 'Male', 'White'), 

54 (109, 'Rake', 'Jed', 'Saul', '', '1943', 'Male', 'White'), 

55 (110, 'Poser', 'Richard', 'Allen', '', '1939', 'Male', 'White'), 

56 (111, 'Gray', 'Roger', 'L.', '', '1953', 'Male', 'African American'), 

57 (112, 'Marsh', 'John', ' ', '', '1755', '', ''), 

58 (113, 'Ward', 'Ellen', '', '', '1970', 'Declined to Report', 'White')] 

59DEMOGRAPHICS_COLUMNS = [ 

60 'nid', 'Last Name', 'First Name', 'Middle Name', 'Suffix', 'Birth Year', 

61 'Gender', 'Race or Ethnicity'] 

62CAREER = [ 

63 (101, 1, 'Law clerk, Hon. A. Judge, U.S. District Court, District of Rhode Island, 1971-1973'), 

64 (101, 2, 'Assistant attorney general, State of Massachusetts, 1973-1974'), 

65 (102, 1, 'Assistant U.S. attorney, District of New Hampshire, 1981-1987'), 

66 (103, 1, 'Assistant public defender, Rhode Island, 1976-1979'), 

67 (104, 1, 'Law clerk, Hon. B. Justice, Supreme Court of the United States, 1970-1971'), 

68 (104, 2, 'Professor, University of the West School of Law, 1980-1990'), 

69 (105, 1, 'Reporter of decisions, Supreme Court of the United States, 1990-1991'), 

70 (105, 2, 'Law clerk, Hon. C. Judge, U.S. District Court, District of Idaho, 1977-1978'), 

71 (113, 1, 'Deputy Solicitor General, U.S. Department of Justice, 2009-2013')] 

72CAREER_COLUMNS = ['nid', 'Sequence', 'Professional Career'] 

73 

74 

75def tables() -> dict[str, pd.DataFrame]: 

76 """Returns the files of invented judges, as they are read.""" 

77 names = {row[0]: row[2] for row in SERVICE} 

78 # judicial-common-space reads a few more of the FJC's columns. 

79 demographics = pd.DataFrame(DEMOGRAPHICS, columns = DEMOGRAPHICS_COLUMNS).assign( 

80 jid = lambda table: table['nid'], **{'Birth State': ''}) 

81 service = pd.DataFrame(SERVICE, columns = SERVICE_COLUMNS).assign(**{ 

82 'Appointment Title': 'Judge', 'Nomination Date': '', 'Confirmation Date': ''}) 

83 return { 

84 'demographics': demographics, 

85 'service': service, 

86 'other_service': pd.DataFrame( 

87 [(101, 1, names[101], 'U.S. Magistrate Judge', 'U.S. Magistrate Judge, District of Maine, 1990-1995', '', '')], 

88 columns = ['nid', 'Sequence', 'Judge Name', 'Type', 'Other Federal Judicial Service', '', ' ']), 

89 'education': pd.DataFrame( 

90 [(101, 1, names[101], 'Harvard Law School', 'J.D.', '1971'), 

91 (102, 1, names[102], 'Yale Law School', '', '')], 

92 columns = ['nid', 'Sequence', 'Judge Name', 'School', 'Degree', 'Degree Year']), 

93 'career': pd.DataFrame(CAREER, columns = CAREER_COLUMNS), 

94 'other_nominations': pd.DataFrame( 

95 [(103, 1, names[103], 'Nominated to U.S. Court of Appeals for the First Circuit, May 1, 2008; no Senate vote')], 

96 columns = ['nid', 'Sequence', 'Judge Name', 'Other Nominations/Recess Appointments'])} 

97 

98 

99def files() -> dict[str, bytes]: 

100 """Returns the contents of the FJC's files of invented judges, by name.""" 

101 return { 

102 options._CATEGORIES[category]: table.to_csv(index = False, quoting = 1).encode() 

103 for category, table in tables().items()} 

104 

105 

106@pytest.fixture 

107def requests(monkeypatch: pytest.MonkeyPatch) -> list[str]: 

108 """Replaces web requests with a fake FJC website and records them.""" 

109 requested: list[str] = [] 

110 served = files() 

111 

112 def fake_open(url: str) -> io.BytesIO: 

113 requested.append(url) 

114 return io.BytesIO(served[url.rsplit('/', 1)[-1]]) 

115 

116 monkeypatch.setattr(downloader, '_open', fake_open) 

117 return requested 

118 

119 

120@pytest.fixture(autouse = True) 

121def data_folder(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path) -> pathlib.Path: 

122 """Keeps the tests' files out of the real data folder.""" 

123 folder = tmp_path / 'data' 

124 monkeypatch.setenv(options._ENV_DATA, str(folder)) 

125 monkeypatch.setenv('JUDICIAL_COMMON_SPACE_DATA_DIR', str(tmp_path / 'scores')) 

126 return folder 

127 

128 

129@pytest.fixture(autouse = True) 

130def no_score_downloads(monkeypatch: pytest.MonkeyPatch) -> list[tuple[Any, Any, bool]]: 

131 """Refuses judicial-common-space's downloads, and records them.""" 

132 requested: list[tuple[Any, Any, bool]] = [] 

133 

134 def refuse(kinds: Any = None, folder: Any = None, *, overwrite: bool = False, **kwargs: Any) -> None: 

135 requested.append((kinds, folder, overwrite)) 

136 message = f'the tests do not download {kinds}' 

137 raise OSError(message) 

138 

139 monkeypatch.setattr(jcs.sources, 'download', refuse) 

140 return requested