Coverage for tests/test_analysis_files.py: 100%

71 statements  

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

1import pytest 

2 

3from bbconf.exceptions import AnalysisFileNotFoundError 

4from bbconf.models.base_models import AnalysisFileArtifact 

5 

6from .conftest import SERVICE_UNAVAILABLE 

7from .utils import ContextManagerDBTesting 

8 

9UPLOAD_TARGET = "bbconf.config_parser.bedbaseconfig.BedBaseConfig.upload_s3" 

10DELETE_TARGET = "bbconf.config_parser.bedbaseconfig.BedBaseConfig.delete_s3" 

11 

12 

13def _artifact(**overrides) -> AnalysisFileArtifact: 

14 values = dict( 

15 path="/local/openSignalMatrix_hg38.txt.gz", 

16 name="openSignalMatrix", 

17 file_type="openSignalMatrix", 

18 genome="hg38", 

19 description="Open signal matrix for hg38", 

20 tags=["reference", "hg38"], 

21 file_size=12345, 

22 checksum="a" * 64, 

23 ) 

24 values.update(overrides) 

25 return AnalysisFileArtifact(**values) 

26 

27 

28@pytest.mark.skipif(SERVICE_UNAVAILABLE, reason="Database is not available") 

29class Test_AnalysisFile_Agent: 

30 def test_add(self, bbagent_obj, mocker): 

31 upload_mock = mocker.patch(UPLOAD_TARGET, return_value=True) 

32 with ContextManagerDBTesting(config=bbagent_obj.config, add_data=False): 

33 result = bbagent_obj.analysis_files.add(_artifact()) 

34 

35 assert upload_mock.called 

36 assert result.count == 1 

37 row = result.results[0] 

38 assert row.id is not None 

39 assert row.name == "openSignalMatrix" 

40 assert row.genome == "hg38" 

41 assert row.tags == ["reference", "hg38"] 

42 assert row.checksum == "a" * 64 

43 assert row.file_path == "analysis_files/openSignalMatrix_hg38.txt.gz" 

44 

45 def test_list_and_filters(self, bbagent_obj, mocker): 

46 mocker.patch(UPLOAD_TARGET, return_value=True) 

47 with ContextManagerDBTesting(config=bbagent_obj.config, add_data=False): 

48 bbagent_obj.analysis_files.add( 

49 [ 

50 _artifact(), 

51 _artifact( 

52 path="/local/openSignalMatrix_mm10.txt.gz", 

53 genome="mm10", 

54 tags=["reference", "mm10"], 

55 checksum="b" * 64, 

56 ), 

57 _artifact( 

58 path="/local/some_model.pt", 

59 name="some_model", 

60 file_type="model", 

61 genome=None, 

62 tags=["model"], 

63 checksum="c" * 64, 

64 ), 

65 ] 

66 ) 

67 

68 assert bbagent_obj.analysis_files.list().count == 3 

69 assert ( 

70 bbagent_obj.analysis_files.list(file_type="openSignalMatrix").count == 2 

71 ) 

72 assert bbagent_obj.analysis_files.list(genome="mm10").count == 1 

73 assert bbagent_obj.analysis_files.list(tag="model").count == 1 

74 assert bbagent_obj.analysis_files.list(genome="hg19").count == 0 

75 

76 def test_get_and_get_by_name(self, bbagent_obj, mocker): 

77 mocker.patch(UPLOAD_TARGET, return_value=True) 

78 with ContextManagerDBTesting(config=bbagent_obj.config, add_data=False): 

79 added = bbagent_obj.analysis_files.add(_artifact()).results[0] 

80 

81 by_id = bbagent_obj.analysis_files.get(added.id) 

82 assert by_id.name == "openSignalMatrix" 

83 

84 by_name = bbagent_obj.analysis_files.get_by_name( 

85 "openSignalMatrix", genome="hg38" 

86 ) 

87 assert by_name.id == added.id 

88 

89 by_filename = bbagent_obj.analysis_files.get_by_filename( 

90 "openSignalMatrix_hg38.txt.gz" 

91 ) 

92 assert by_filename.id == added.id 

93 

94 def test_get_missing_raises(self, bbagent_obj): 

95 with ContextManagerDBTesting(config=bbagent_obj.config, add_data=False): 

96 with pytest.raises(AnalysisFileNotFoundError): 

97 bbagent_obj.analysis_files.get(999999) 

98 with pytest.raises(AnalysisFileNotFoundError): 

99 bbagent_obj.analysis_files.get_by_name("does-not-exist") 

100 

101 def test_delete(self, bbagent_obj, mocker): 

102 mocker.patch(UPLOAD_TARGET, return_value=True) 

103 delete_mock = mocker.patch(DELETE_TARGET, return_value=True) 

104 with ContextManagerDBTesting(config=bbagent_obj.config, add_data=False): 

105 added = bbagent_obj.analysis_files.add(_artifact()).results[0] 

106 

107 bbagent_obj.analysis_files.delete(added.id) 

108 assert delete_mock.called 

109 assert bbagent_obj.analysis_files.list().count == 0 

110 with pytest.raises(AnalysisFileNotFoundError): 

111 bbagent_obj.analysis_files.get(added.id) 

112 

113 def test_delete_by_checksum(self, bbagent_obj, mocker): 

114 mocker.patch(UPLOAD_TARGET, return_value=True) 

115 delete_mock = mocker.patch(DELETE_TARGET, return_value=True) 

116 with ContextManagerDBTesting(config=bbagent_obj.config, add_data=False): 

117 bbagent_obj.analysis_files.add(_artifact()) 

118 

119 bbagent_obj.analysis_files.delete_by_checksum("a" * 64) 

120 assert delete_mock.called 

121 assert bbagent_obj.analysis_files.list().count == 0 

122 with pytest.raises(AnalysisFileNotFoundError): 

123 bbagent_obj.analysis_files.delete_by_checksum("a" * 64)