Coverage for tests/sources/test_lexis.py: 100%

73 statements  

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

1"""Tests the lexis module and parsing Lexis-Nexis cases.""" 

2 

3from __future__ import annotations 

4 

5import pathlib 

6 

7import pandas as pd 

8 

9import courtpy 

10from courtpy import lexis 

11 

12 

13def test_split(lexis_folder: pathlib.Path) -> None: 

14 files = sorted(lexis_folder.glob('*.txt')) 

15 assert [f.name for f in files] == ['lexis_batch_00001.txt', 'lexis_batch_00002.txt'] 

16 text = files[0].read_text(encoding = 'utf-8') 

17 assert text.startswith('UNITED STATES OF AMERICA') 

18 assert '[101]' not in text and '*' not in text 

19 

20 

21def test_clean() -> None: 

22 text = 'Signal: Caution\nAs of: Jan 1\n\nA *12 case [3] here \r\n next' 

23 assert lexis.clean(text) == 'A 12 case here\nnext' 

24 

25 

26def test_read_case(lexis_folder: pathlib.Path) -> None: 

27 case = lexis.read_case(lexis_folder / 'lexis_batch_00001.txt') 

28 assert case.id == 'lexis_batch_00001' 

29 assert case.sections['text'].startswith('\nUNITED STATES') 

30 assert case.metadata == {'source': 'lexis_nexis', 'file': 'lexis_batch_00001.txt'} 

31 

32 

33def test_parse(lexis_folder: pathlib.Path) -> None: 

34 table = courtpy.parse(lexis_folder, source = 'lexis_nexis') 

35 doe = table.loc['lexis_batch_00001'] 

36 assert doe['party1'] == 'UNITED STATES OF AMERICA, Plaintiff, Appellee,' 

37 assert doe['party2'] == 'JOHN DOE, Defendant, Appellant.' 

38 assert doe['court_num'] == 1 and doe['court_name'] == 'first_circuit' 

39 assert doe['lower_court_num'] == 1026 

40 assert doe['lower_court_name'] == 'district_of_massachusetts' 

41 assert doe['docket_number'] == 'No. 08-1234' 

42 assert doe['docket_numbers'] == '08-1234' 

43 assert doe['date_argued'] == '2009-03-03' 

44 assert doe['date_decided'] == '2009-06-01' 

45 assert doe['date_filed'] == '2009-06-01' 

46 assert doe['year'] == 2009 

47 assert doe['published'] 

48 assert doe['panel_judges'] == 'TORRUELLA; LYNCH; HOWARD' 

49 assert doe['panel_size'] == 3 

50 assert doe['author'] == 'LYNCH' 

51 assert doe['dissenting'] == 'HOWARD' and doe['dissents'] == 1 

52 assert doe['concurrences'] == 0 

53 assert doe['counsel_us_attorney'] and doe['counsel_public_defender'] 

54 assert doe['party1_united_states'] and doe['party2_appellant'] 

55 assert doe['criminal_firearm'] and doe['constitutional_amend4'] and doe['standard_de_novo'] 

56 assert doe['opinion_reverse'] and not doe['disposition_reverse'] 

57 assert doe['citations_case'] == '400 F.3d 10' 

58 assert doe['word_count'] > 50 

59 assert 'opinion' not in table.columns and 'header' not in table.columns 

60 acme = table.loc['lexis_batch_00002'] 

61 assert acme['court_num'] == 9 and acme['court_name'] == 'ninth_circuit' 

62 assert pd.isna(acme['lower_court_num']) and pd.isna(acme['lower_court_name']) 

63 assert acme['party2_agencies'] == 'National Labor Relations Board' 

64 assert acme['history_agencies'] == 'National Labor Relations Board' 

65 assert pd.isna(acme['party1_agencies']) 

66 assert acme['disposition'] == 'DISPOSITION: PETITION DENIED.' 

67 assert acme['disposition_deny'] 

68 assert acme['notice_unpublished'] and not acme['published'] 

69 assert acme['date_submitted'] == '2008-01-10' and acme['date_filed'] == '2008-02-02' 

70 assert acme['panel_judges'] == 'SCHROEDER; OSCANNLAIN; BYBEE' 

71 assert acme['party1_petitioner'] and acme['party2_respondent'] 

72 assert acme['civil_labor'] 

73 

74 

75def test_keep_text(lexis_folder: pathlib.Path) -> None: 

76 table = courtpy.parse(lexis_folder, source = 'lexis_nexis', keep_text = True, limit = 1) 

77 assert len(table) == 1 

78 assert table.iloc[0]['opinion'].startswith('LYNCH, Circuit Judge.') 

79 assert 'COUNSEL:' in table.iloc[0]['header'] 

80 assert table.iloc[0]['counsel'].startswith('COUNSEL:') 

81 assert 'text' not in table.columns 

82 

83 

84def test_parse_all_uses_the_lexis_header_rules(lexis_folder: pathlib.Path) -> None: 

85 table = courtpy.parse(lexis_folder, source = 'lexis_nexis', keep_text = True) 

86 again = courtpy.parsers.ParseAll().parse( 

87 table.drop(columns = ['court_num', 'court_name', 'party2_agencies', 'notice_unpublished'])) 

88 assert again['court_num'].tolist() == [1, 9] 

89 assert again['court_name'].tolist() == ['first_circuit', 'ninth_circuit'] 

90 assert again.loc['lexis_batch_00002', 'party2_agencies'] == 'National Labor Relations Board' 

91 assert again['notice_unpublished'].tolist() == [False, True]