Coverage for test / file_manager_test.py: 100%

58 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-25 18:06 +0000

1# SPDX-FileCopyrightText: 2026 Arcangelo Massari <arcangelo.massari@unibo.it> 

2# 

3# SPDX-License-Identifier: ISC 

4 

5import json 

6import os 

7import tempfile 

8 

9from oc_ds_converter.lib.file_manager import init_cache 

10 

11 

12class TestInitCache: 

13 def test_none_filepath(self) -> None: 

14 result = init_cache(None) 

15 assert result == set() 

16 

17 def test_nonexistent_file(self) -> None: 

18 result = init_cache("/nonexistent/path/cache.json") 

19 assert result == set() 

20 

21 def test_empty_cache_file(self) -> None: 

22 with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: 

23 json.dump({}, f) 

24 f.flush() 

25 result = init_cache(f.name) 

26 os.unlink(f.name) 

27 assert result == set() 

28 

29 def test_cache_with_data(self) -> None: 

30 cache_data = { 

31 "citing": ["file1.json", "file2.json", "file3.json"], 

32 "cited": ["file2.json", "file3.json", "file4.json"] 

33 } 

34 with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: 

35 json.dump(cache_data, f) 

36 f.flush() 

37 result = init_cache(f.name) 

38 os.unlink(f.name) 

39 assert result == {"file2.json", "file3.json"} 

40 

41 def test_cache_no_intersection(self) -> None: 

42 cache_data = { 

43 "citing": ["file1.json", "file2.json"], 

44 "cited": ["file3.json", "file4.json"] 

45 } 

46 with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: 

47 json.dump(cache_data, f) 

48 f.flush() 

49 result = init_cache(f.name) 

50 os.unlink(f.name) 

51 assert result == set() 

52 

53 def test_cache_empty_lists(self) -> None: 

54 cache_data = {"citing": [], "cited": []} 

55 with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: 

56 json.dump(cache_data, f) 

57 f.flush() 

58 result = init_cache(f.name) 

59 os.unlink(f.name) 

60 assert result == set() 

61 

62 def test_cache_only_citing_key(self) -> None: 

63 cache_data = {"citing": ["file1.json", "file2.json"]} 

64 with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: 

65 json.dump(cache_data, f) 

66 f.flush() 

67 result = init_cache(f.name) 

68 os.unlink(f.name) 

69 assert result == set() 

70 

71 def test_cache_only_cited_key(self) -> None: 

72 cache_data = {"cited": ["file1.json", "file2.json"]} 

73 with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: 

74 json.dump(cache_data, f) 

75 f.flush() 

76 result = init_cache(f.name) 

77 os.unlink(f.name) 

78 assert result == set()