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

46 statements  

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

1"""Tests the datasets module.""" 

2 

3from __future__ import annotations 

4 

5import pathlib 

6 

7import amos 

8import pandas as pd 

9import pytest 

10 

11import courtpy 

12from courtpy import datasets 

13 

14 

15def table() -> pd.DataFrame: 

16 """Returns a small table of cases, labeled by their ids.""" 

17 return pd.DataFrame({ 

18 'year': [2019, 2020], 

19 'published': [True, None], 

20 'panel_judges': [['LYNCH', 'HOWARD'], []]}, index = pd.Index(['1', '2'], name = 'case_id')) 

21 

22 

23def test_each_kind_has_its_key() -> None: 

24 keys = { 

25 courtpy.Cases: 'case_id', courtpy.Dockets: 'case_docket', courtpy.Votes: 'vote_id', courtpy.Judges: 'judge_nid', 

26 courtpy.Courts: 'court_num', courtpy.CaseTypes: 'case_type', courtpy.PartyTypes: 'party_type'} 

27 for kind, key in keys.items(): 

28 assert issubclass(kind, amos.Dataset) and kind.key == key 

29 

30 

31def test_adopt_and_create() -> None: 

32 plain = amos.Dataset(table().reset_index(), label = 'year', seed = 7) 

33 plain.record('something') 

34 cases = courtpy.Cases.adopt(plain) 

35 assert isinstance(cases, courtpy.Cases) 

36 # It keeps everything, and its key labels the rows. 

37 assert (cases.label, cases.seed, cases.history) == ('year', 7, plain.history) 

38 assert list(cases.data.index) == ['1', '2'] and cases.data.index.name == 'case_id' 

39 assert courtpy.Cases.adopt(cases) is cases 

40 votes = courtpy.Votes.adopt(cases) 

41 assert isinstance(votes, courtpy.Votes) and votes.data is cases.data 

42 created = courtpy.Cases.create(table(), label = 'year') 

43 assert isinstance(created, courtpy.Cases) and created.label == 'year' 

44 

45 

46def test_save_and_load(tmp_path: pathlib.Path) -> None: 

47 cases = courtpy.Cases.create(table()) 

48 paths = cases.save(tmp_path / 'nested' / 'cases.csv') 

49 assert [p.name for p in paths] == ['cases.joblib', 'cases.csv'] 

50 assert sorted(p.name for p in (tmp_path / 'nested').iterdir()) == ['cases.csv', 'cases.joblib'] 

51 assert cases.history[-1]['technique'] == 'save_data' 

52 # Lists are saved as text, as the rules write them, and the table's 

53 # labels and types come back. 

54 loaded = courtpy.Cases.load(tmp_path / 'nested' / 'cases.csv') 

55 assert list(loaded.data.index) == ['1', '2'] 

56 assert list(loaded.data['panel_judges'].fillna('')) == ['LYNCH; HOWARD', ''] 

57 assert loaded.data['published'].dtype == 'boolean' 

58 # The joblib file holds the whole dataset, of its kind. 

59 whole = courtpy.Cases.load(tmp_path / 'nested' / 'cases.joblib') 

60 assert whole.history[-1]['technique'] == 'save_data' 

61 assert courtpy.CourtData.load(tmp_path / 'nested' / 'cases.joblib').__class__ is courtpy.Cases 

62 # Saving to a joblib file saves no table. 

63 assert [p.name for p in cases.save(tmp_path / 'only.joblib')] == ['only.joblib'] 

64 with pytest.raises(FileNotFoundError, match = 'no file of Votes'): 

65 courtpy.Votes.load(tmp_path / 'missing.csv') 

66 with pytest.raises(TypeError, match = 'courtpy.Cases.load'): 

67 courtpy.Judges.load(tmp_path / 'only.joblib') 

68 

69 

70def test_join_lists_leaves_other_tables_alone() -> None: 

71 data = pd.DataFrame({'x': [1, 2]}) 

72 assert datasets._join_lists(data) is data