Coverage for tests/test_examples.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-22 15:51 +0000

1"""Runs the example study on synthetic cases.""" 

2 

3from __future__ import annotations 

4 

5import pathlib 

6import random 

7 

8import chrisjen 

9from conftest import make_cluster, make_opinion, write_case 

10 

11import courtpy 

12 

13EXAMPLES = pathlib.Path(__file__).parents[1] / 'examples' 

14OUTCOMES = ('Affirmed.', 'Reversed and remanded.', 'Vacated.', 'Affirmed in part.') 

15PARTIES = ( 

16 'UNITED STATES of America, Plaintiff-Appellee, v. John DOE, Defendant-Appellant', 

17 'Mary SMITH, Plaintiff-Appellant, v. ACME CORP., Defendant-Appellee', 

18 'Pat LEE, Petitioner, v. Merrick GARLAND, Attorney General, Respondent') 

19ISSUES = ( 

20 'The sentencing guidelines range was correct.', 

21 'He argues that the search violated the Fourth Amendment.', 

22 'Our review is de novo.', 

23 'She appeared pro se and raised a section 1983 claim.', 

24 'The district court did not abuse its discretion.') 

25 

26 

27def test_federal_appeals_example(tmp_path: pathlib.Path) -> None: 

28 rng = random.Random(7) 

29 folder = tmp_path / 'data' / 'courtlistener' 

30 for number in range(1, 81): 

31 court = rng.choice(['ca1', 'ca2', 'ca9']) 

32 text = ' '.join(rng.sample(ISSUES, 2)) 

33 write_case( 

34 folder, 

35 make_cluster( 

36 number, case_name_full = rng.choice(PARTIES), 

37 disposition = rng.choice(OUTCOMES), 

38 date_filed = f'2019-{rng.randint(1, 12):02d}-15'), 

39 [make_opinion(number * 10, number, html_with_citations = f'<p>{text}</p>')], 

40 court = court) 

41 idea = chrisjen.Idea.create(EXAMPLES / 'federal_appeals.ini') 

42 # The cases are in the folder already, so they are loaded, not downloaded. 

43 idea['wrangler']['techniques'][0] = 'load_courtlistener' 

44 idea['load_courtlistener_parameters'] = idea.contents.pop('download_courtlistener_parameters') 

45 project = courtpy.Project.create(idea, clerk = tmp_path) 

46 result = project.result 

47 assert result.label == 'outcome_reversal' 

48 assert [e['technique'] for e in result.history][:4] == [ 

49 'load_courtlistener', 'code_parties', 'code_case_type', 'code_outcome'] 

50 assert (tmp_path / 'data' / 'cases.csv').is_file() 

51 assert set(result.tables) >= {'summarize', 'label_balance', 'analyst_comparison', 'scorecard'} 

52 assert 'panel_judges' not in result.data.columns 

53 assert 0 <= result.metrics['roc_auc'] <= 1 

54 folder = project.export() 

55 assert (folder / 'scorecard.csv').is_file()