Coverage for tests/test_download.py: 100%

40 statements  

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

1"""Tests for downloading the FJC's files.""" 

2 

3from __future__ import annotations 

4 

5import pathlib 

6 

7import pytest 

8 

9import fjc_biographies 

10from fjc_biographies import downloader, options 

11 

12 

13def test_download(requests: list[str], tmp_path: pathlib.Path) -> None: 

14 paths = fjc_biographies.download(['service', 'demographics'], tmp_path) 

15 assert list(paths) == ['service', 'demographics'] 

16 assert paths['service'] == tmp_path / 'raw' / 'federal-judicial-service.csv' 

17 assert requests == [ 

18 'https://www.fjc.gov/sites/default/files/history/federal-judicial-service.csv', 

19 'https://www.fjc.gov/sites/default/files/history/demographics.csv'] 

20 assert paths['service'].read_text(encoding = 'utf-8').startswith('"nid"') 

21 # Files that are there are not downloaded again unless asked. 

22 fjc_biographies.download('service', tmp_path) 

23 assert len(requests) == 2 

24 fjc_biographies.download('service', tmp_path, overwrite = True) 

25 assert len(requests) == 3 

26 

27 

28def test_download_everything(requests: list[str], data_folder: pathlib.Path) -> None: 

29 paths = fjc_biographies.download() 

30 assert list(paths) == list(options._CATEGORIES) 

31 assert [path.name for path in paths.values()] == list(options._CATEGORIES.values()) 

32 # The default folder is the data folder. 

33 assert paths['service'].parent == data_folder / 'raw' 

34 assert len(requests) == 6 

35 

36 

37def test_download_unknown_category(requests: list[str]) -> None: 

38 with pytest.raises(ValueError, match = 'not categories'): 

39 fjc_biographies.download(['service', 'judges']) 

40 assert requests == [] 

41 

42 

43def test_failed_download_leaves_no_file(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path) -> None: 

44 def refuse(url: str) -> None: 

45 raise OSError(f'cannot reach {url}') 

46 

47 monkeypatch.setattr(downloader, '_open', refuse) 

48 with pytest.raises(OSError, match = 'cannot reach'): 

49 fjc_biographies.download('service', tmp_path) 

50 assert list((tmp_path / 'raw').iterdir()) == [] 

51 

52 

53def test_only_https() -> None: 

54 with pytest.raises(ValueError, match = 'https'): 

55 downloader._open('http://www.fjc.gov/sites/default/files/history/judges.csv') 

56 

57 

58def test_data_folder(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path) -> None: 

59 monkeypatch.setenv(options._ENV_DATA, str(tmp_path / 'named')) 

60 assert fjc_biographies.data_folder() == tmp_path / 'named' 

61 # Run from its repository, the data folder is beside the code projects. 

62 monkeypatch.delenv(options._ENV_DATA) 

63 assert fjc_biographies.data_folder() == options._DATA_FOLDER