Coverage for tests/core/test_utilities.py: 100%

76 statements  

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

1"""Tests the utilities module.""" 

2 

3from __future__ import annotations 

4 

5import datetime 

6import pathlib 

7import zipfile 

8 

9import nagata 

10import pandas as pd 

11import pytest 

12from conftest import Site 

13 

14import courtpy 

15from courtpy import utilities 

16 

17 

18@pytest.mark.parametrize(('text', 'expected'), [ 

19 ('March 3, 2009, Argued', '2009-03-03'), 

20 ('Decided: Sept. 5 2010', '2010-09-05'), 

21 ('filed 2020-01-31', '2020-01-31'), 

22 ('February 30, 2010', None), 

23 ('no date here', None), 

24 (None, None)]) 

25def test_parse_date(text: str | None, expected: str | None) -> None: 

26 assert utilities.parse_date(text) == expected 

27 

28 

29def test_to_bool() -> None: 

30 assert utilities.to_bool('TRUE') and utilities.to_bool('yes') and utilities.to_bool(1) 

31 assert not utilities.to_bool('FALSE') and not utilities.to_bool('') and not utilities.to_bool(None) 

32 with pytest.raises(ValueError, match = 'not TRUE or FALSE'): 

33 utilities.to_bool('perhaps') 

34 

35 

36def test_listify_and_normalize() -> None: 

37 assert utilities.listify('a, b,,c ') == ['a', 'b', 'c'] 

38 assert utilities.listify(None) == [] 

39 assert utilities.listify(['x', ' y']) == ['x', 'y'] 

40 assert utilities.listify(7) == ['7'] 

41 assert utilities.normalize(' a\n\tb c ') == 'a b c' 

42 assert utilities.normalize(None) == '' 

43 

44 

45def test_expand_courts() -> None: 

46 courts = utilities.expand_courts('federal_appellate, scotus, ca1') 

47 assert courts[:2] == ['ca1', 'ca2'] and courts[-1] == 'scotus' and len(courts) == 14 

48 geographic = utilities.expand_courts('federal_geographic_circuits') 

49 assert len(geographic) == 12 and 'cadc' in geographic and 'cafc' not in geographic 

50 with pytest.raises(ValueError, match = 'name at least one court'): 

51 utilities.expand_courts('') 

52 

53 

54def test_folders_follow_environment(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: 

55 monkeypatch.setenv(courtpy.options._ENV_CONFIG, str(tmp_path / 'c')) 

56 monkeypatch.setenv(courtpy.options._ENV_DATA, str(tmp_path / 'd')) 

57 assert utilities.config_folder() == tmp_path / 'c' 

58 assert utilities.data_folder() == tmp_path / 'd' 

59 monkeypatch.delenv(courtpy.options._ENV_CONFIG) 

60 assert utilities.config_folder().name == 'courtpy' 

61 # Otherwise, the data folder is "courts" in the amos workspace's data. 

62 monkeypatch.delenv(courtpy.options._ENV_DATA) 

63 monkeypatch.setenv('AMOS_WORKSPACE', str(tmp_path / 'w')) 

64 assert utilities.data_folder() == tmp_path / 'w' / 'data' / 'courts' 

65 

66 

67def test_open_session(monkeypatch: pytest.MonkeyPatch) -> None: 

68 # Without a session, one that identifies courtpy is made. 

69 site = Site({}) 

70 monkeypatch.setattr(utilities.requests, 'Session', lambda: site) 

71 assert utilities.open_session() is site and utilities.open_session(site) is site 

72 assert site.headers == {'User-Agent': courtpy.options._USER_AGENT} 

73 

74 

75def test_read_csv(tmp_path: pathlib.Path) -> None: 

76 table = pd.DataFrame({'name': ['Peña', 'Lynch'], 'nid': [1, 2]}) 

77 for encoding in ('utf-8', 'utf-8-sig', 'cp1252'): 

78 path = tmp_path / f'{encoding}.csv' 

79 table.to_csv(path, index = False, encoding = encoding) 

80 assert utilities.read_csv(path).equals(table) 

81 assert list(utilities.read_csv(path, dtype = str)['nid']) == ['1', '2'] 

82 # A zip archive of one file is read as that file. 

83 with zipfile.ZipFile(tmp_path / 'table.csv.zip', 'w') as archive: 

84 archive.writestr('table.csv', table.to_csv(index = False).encode('cp1252')) 

85 assert utilities.read_csv(tmp_path / 'table.csv.zip').equals(table) 

86 

87 

88def test_locate(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: 

89 monkeypatch.chdir(tmp_path) 

90 clerk = nagata.FileManager(root_folder = tmp_path, input_folder = 'data') 

91 (tmp_path / 'here.csv').write_text('a\n1\n', encoding = 'utf-8') 

92 # A file in the current folder is used, and otherwise the input folder. 

93 assert utilities.locate('here.csv', clerk) == pathlib.Path('here.csv') 

94 assert utilities.locate('there.csv', clerk) == pathlib.Path(clerk.input_folder) / 'there.csv' 

95 assert utilities.locate(tmp_path / 'there.csv', clerk) == tmp_path / 'there.csv' 

96 assert utilities.locate('there.csv') == pathlib.Path('there.csv') 

97 

98 

99@pytest.mark.parametrize(('value', 'expected'), [ 

100 (None, True), (False, True), ('', True), (' None ', True), ('FALSE', True), ('no', True), 

101 ('0', True), ('bulk', False), ('code_outcome', False), (True, False)]) 

102def test_is_none_setting(value: object, expected: bool) -> None: 

103 assert utilities.is_none_setting(value) is expected 

104 

105 

106def test_to_date_and_to_integer() -> None: 

107 assert utilities.to_date('2019-01-01') == '2019-01-01' 

108 assert utilities.to_date(datetime.datetime(2019, 1, 1, 5)) == '2019-01-01' 

109 assert utilities.to_date('') is None and utilities.to_date(None) is None 

110 assert utilities.to_integer('500') == 500 and utilities.to_integer(7) == 7 

111 assert utilities.to_integer('') is None 

112 with pytest.raises(ValueError): 

113 utilities.to_integer('many')