|
2 | 2 | from unittest.mock import MagicMock, patch |
3 | 3 | from DIRAC.Resources.Catalog.RucioFileCatalogClient import RucioFileCatalogClient |
4 | 4 |
|
5 | | -class TestRucioFileCatalogClient(unittest.TestCase): |
6 | 5 |
|
| 6 | +class TestRucioFileCatalogClient(unittest.TestCase): |
7 | 7 | def setUp(self): |
8 | | - self.patcher = patch.object(RucioFileCatalogClient, 'client', new_callable=MagicMock) |
| 8 | + self.patcher = patch.object(RucioFileCatalogClient, "client", new_callable=MagicMock) |
9 | 9 | self.client = RucioFileCatalogClient() |
10 | | - self.client.scopes = ['test_scope'] |
| 10 | + self.client.scopes = ["test_scope"] |
11 | 11 | self.patcher.start() |
12 | 12 |
|
13 | 13 | def tearDown(self): |
14 | 14 | self.patcher.stop() |
15 | 15 |
|
16 | 16 | def test_transform_DIRAC_operator_to_Rucio(self): |
17 | | - DIRAC_dict = { |
18 | | - 'key1': 'value1', |
19 | | - 'key2': {'>': 10}, |
20 | | - 'key3': {'=': 10} |
21 | | - } |
22 | | - expected_output = { |
23 | | - 'key1': 'value1', |
24 | | - 'key2.gt': 10, |
25 | | - 'key3': 10 |
26 | | - } |
| 17 | + DIRAC_dict = {"key1": "value1", "key2": {">": 10}, "key3": {"=": 10}} |
| 18 | + expected_output = {"key1": "value1", "key2.gt": 10, "key3": 10} |
27 | 19 | result = self.client._RucioFileCatalogClient__transform_DIRAC_operator_to_Rucio(DIRAC_dict) |
28 | 20 | self.assertEqual(result, expected_output) |
29 | 21 |
|
30 | 22 | def test_transform_dict_with_in_operateur_2steps(self): |
31 | 23 | DIRAC_dict_with_in_operator_list = [ |
32 | | - {'particle': {'in': ['proton', 'electron']}, 'site': {'in': ['LaPalma', 'paranal']}, 'configuration_id': {'=': 14}} |
| 24 | + { |
| 25 | + "particle": {"in": ["proton", "electron"]}, |
| 26 | + "site": {"in": ["LaPalma", "paranal"]}, |
| 27 | + "configuration_id": {"=": 14}, |
| 28 | + } |
33 | 29 | ] |
34 | 30 | expected_intermediate_output = [ |
35 | | - {'particle': 'proton', 'site': {'in': ['LaPalma', 'paranal']}, 'configuration_id': {'=': 14}}, |
36 | | - {'particle': 'electron', 'site': {'in': ['LaPalma', 'paranal']}, 'configuration_id': {'=': 14}} |
| 31 | + {"particle": "proton", "site": {"in": ["LaPalma", "paranal"]}, "configuration_id": {"=": 14}}, |
| 32 | + {"particle": "electron", "site": {"in": ["LaPalma", "paranal"]}, "configuration_id": {"=": 14}}, |
37 | 33 | ] |
38 | 34 | expected_final_output = [ |
39 | | - {'particle': 'proton', 'site': 'LaPalma', 'configuration_id': {'=': 14}}, |
40 | | - {'particle': 'proton', 'site': 'paranal', 'configuration_id': {'=': 14}}, |
41 | | - {'particle': 'electron', 'site': 'LaPalma', 'configuration_id': {'=': 14}}, |
42 | | - {'particle': 'electron', 'site': 'paranal', 'configuration_id': {'=': 14}} |
| 35 | + {"particle": "proton", "site": "LaPalma", "configuration_id": {"=": 14}}, |
| 36 | + {"particle": "proton", "site": "paranal", "configuration_id": {"=": 14}}, |
| 37 | + {"particle": "electron", "site": "LaPalma", "configuration_id": {"=": 14}}, |
| 38 | + {"particle": "electron", "site": "paranal", "configuration_id": {"=": 14}}, |
43 | 39 | ] |
44 | | - result_intermediate, _ = self.client._RucioFileCatalogClient__transform_dict_with_in_operateur(DIRAC_dict_with_in_operator_list) |
| 40 | + result_intermediate, _ = self.client._RucioFileCatalogClient__transform_dict_with_in_operateur( |
| 41 | + DIRAC_dict_with_in_operator_list |
| 42 | + ) |
45 | 43 | self.assertEqual(result_intermediate, expected_intermediate_output) |
46 | | - result_final, _ = self.client._RucioFileCatalogClient__transform_dict_with_in_operateur( result_intermediate) |
| 44 | + result_final, _ = self.client._RucioFileCatalogClient__transform_dict_with_in_operateur(result_intermediate) |
47 | 45 | self.assertEqual(result_final, expected_final_output) |
48 | | - |
| 46 | + |
49 | 47 | def test_transform_DIRAC_operator_to_Rucio_simple_key_value(self): |
50 | | - input_dict = {'key1': 'value1', 'key2': 'value2'} |
51 | | - expected_output = {'key1': 'value1', 'key2': 'value2'} |
| 48 | + input_dict = {"key1": "value1", "key2": "value2"} |
| 49 | + expected_output = {"key1": "value1", "key2": "value2"} |
52 | 50 | result = self.client._RucioFileCatalogClient__transform_DIRAC_operator_to_Rucio(input_dict) |
53 | | - self.assertEqual(result,expected_output) |
| 51 | + self.assertEqual(result, expected_output) |
54 | 52 |
|
55 | 53 | def test_transform_DIRAC_operator_to_Rucio_nested_dict_with_operators_gl(self): |
56 | | - input_dict ={'start' : {'>=': 10}, 'end' : {'>': 5}, 'pointingZ' : {'>=': 0.1} , 'organization' : 'ViaCorp' , 'data_levels' : 'DL3'} |
57 | | - expected_output = {'start.gte': 10, 'end.gt': 5, 'pointingZ.gte' : 0.1 ,'organization': 'ViaCorp', 'data_levels' :'DL3'} |
| 54 | + input_dict = { |
| 55 | + "start": {">=": 10}, |
| 56 | + "end": {">": 5}, |
| 57 | + "pointingZ": {">=": 0.1}, |
| 58 | + "organization": "ViaCorp", |
| 59 | + "data_levels": "DL3", |
| 60 | + } |
| 61 | + expected_output = { |
| 62 | + "start.gte": 10, |
| 63 | + "end.gt": 5, |
| 64 | + "pointingZ.gte": 0.1, |
| 65 | + "organization": "ViaCorp", |
| 66 | + "data_levels": "DL3", |
| 67 | + } |
58 | 68 | result = self.client._RucioFileCatalogClient__transform_DIRAC_operator_to_Rucio(input_dict) |
59 | | - self.assertEqual(result,expected_output) |
| 69 | + self.assertEqual(result, expected_output) |
60 | 70 |
|
61 | 71 | def test_transform_DIRAC_operator_to_Rucio_nested_dict_with_operators_equals(self): |
62 | | - input_dict = {'start' : {'=': 10}, 'pointingZ' : {'=': 0.1} , 'organization' : 'ViaCorp' , 'data_levels' : 'DL3'} |
63 | | - expected_output = {'start': 10, 'pointingZ' : 0.1 ,'organization': 'ViaCorp', 'data_levels' :'DL3'} |
| 72 | + input_dict = {"start": {"=": 10}, "pointingZ": {"=": 0.1}, "organization": "ViaCorp", "data_levels": "DL3"} |
| 73 | + expected_output = {"start": 10, "pointingZ": 0.1, "organization": "ViaCorp", "data_levels": "DL3"} |
64 | 74 | result = self.client._RucioFileCatalogClient__transform_DIRAC_operator_to_Rucio(input_dict) |
65 | | - assert result == expected_output |
| 75 | + assert result == expected_output |
66 | 76 |
|
67 | 77 | def test_transform_DIRAC_operator_to_Rucio_mixed_dict(self): |
68 | | - input_dict = {'key1': 'value1', 'key2': {'>': 10}, 'key3': {'=': 10}} |
69 | | - expected_output = {'key1': 'value1', 'key2.gt': 10, 'key3': 10} |
| 78 | + input_dict = {"key1": "value1", "key2": {">": 10}, "key3": {"=": 10}} |
| 79 | + expected_output = {"key1": "value1", "key2.gt": 10, "key3": 10} |
70 | 80 | result = self.client._RucioFileCatalogClient__transform_DIRAC_operator_to_Rucio(input_dict) |
71 | | - assert result == expected_output |
| 81 | + assert result == expected_output |
72 | 82 |
|
73 | 83 | def test_transform_DIRAC_operator_to_Rucio_in_operator(self): |
74 | | - input_dict = [{'analysis_prog': {'in': ['ctapipe-merge', 'ctapipe-process', 'ctapipe-apply-models']}, 'key1': 'value1','key3': {'=': 10},'key4': {'<': 5} }] |
75 | | - expected_intermediate = [{'key1': 'value1', 'key3': 10, 'key4.lt': 5, 'analysis_prog': 'ctapipe-merge'}, {'key1': 'value1', 'key3': 10, 'key4.lt': 5, 'analysis_prog': 'ctapipe-process'}, {'key1': 'value1', 'key3': 10, 'key4.lt': 5, 'analysis_prog': 'ctapipe-apply-models'}] |
76 | | - result_interm = self.client._RucioFileCatalogClient__transform_DIRAC_filter_dict_to_Rucio_filter_dict(input_dict) |
77 | | - assert result_interm == expected_intermediate |
78 | | - |
79 | | - |
| 84 | + input_dict = [ |
| 85 | + { |
| 86 | + "analysis_prog": {"in": ["ctapipe-merge", "ctapipe-process", "ctapipe-apply-models"]}, |
| 87 | + "key1": "value1", |
| 88 | + "key3": {"=": 10}, |
| 89 | + "key4": {"<": 5}, |
| 90 | + } |
| 91 | + ] |
| 92 | + expected_intermediate = [ |
| 93 | + {"key1": "value1", "key3": 10, "key4.lt": 5, "analysis_prog": "ctapipe-merge"}, |
| 94 | + {"key1": "value1", "key3": 10, "key4.lt": 5, "analysis_prog": "ctapipe-process"}, |
| 95 | + {"key1": "value1", "key3": 10, "key4.lt": 5, "analysis_prog": "ctapipe-apply-models"}, |
| 96 | + ] |
| 97 | + result_interm = self.client._RucioFileCatalogClient__transform_DIRAC_filter_dict_to_Rucio_filter_dict( |
| 98 | + input_dict |
| 99 | + ) |
| 100 | + assert result_interm == expected_intermediate |
| 101 | + |
80 | 102 | def test_transform_DIRAC_operator_to_Rucio_2timesin_operator(self): |
81 | | - input_dict = [{ 'particle': {'in': ['proton','electron']},'site': {'in': [ "LaPalma", 'paranal'] } }] |
82 | | - expected = [{'particle': 'proton', 'site': 'LaPalma'}, {'particle': 'proton', 'site': 'paranal'}, {'particle': 'electron', 'site': 'LaPalma'}, {'particle': 'electron', 'site': 'paranal'}] |
| 103 | + input_dict = [{"particle": {"in": ["proton", "electron"]}, "site": {"in": ["LaPalma", "paranal"]}}] |
| 104 | + expected = [ |
| 105 | + {"particle": "proton", "site": "LaPalma"}, |
| 106 | + {"particle": "proton", "site": "paranal"}, |
| 107 | + {"particle": "electron", "site": "LaPalma"}, |
| 108 | + {"particle": "electron", "site": "paranal"}, |
| 109 | + ] |
83 | 110 | result = self.client._RucioFileCatalogClient__transform_DIRAC_filter_dict_to_Rucio_filter_dict(input_dict) |
84 | | - assert result == expected |
| 111 | + assert result == expected |
85 | 112 |
|
86 | 113 | def test_2timesin_mix_operator(self): |
87 | | - input_dict = [{ |
88 | | - 'particle': {'in': ['proton','electron']},'site': {'in': [ "LaPalma", 'paranal']},'configuration_id': {'=': 14} } ] |
89 | | - expected = [{'particle': 'proton', 'site': 'LaPalma', 'configuration_id': 14}, {'particle': 'proton', 'site': 'paranal', 'configuration_id': 14}, {'particle': 'electron', 'site': 'LaPalma', 'configuration_id': 14}, {'particle': 'electron', 'site': 'paranal', 'configuration_id': 14}] |
| 114 | + input_dict = [ |
| 115 | + { |
| 116 | + "particle": {"in": ["proton", "electron"]}, |
| 117 | + "site": {"in": ["LaPalma", "paranal"]}, |
| 118 | + "configuration_id": {"=": 14}, |
| 119 | + } |
| 120 | + ] |
| 121 | + expected = [ |
| 122 | + {"particle": "proton", "site": "LaPalma", "configuration_id": 14}, |
| 123 | + {"particle": "proton", "site": "paranal", "configuration_id": 14}, |
| 124 | + {"particle": "electron", "site": "LaPalma", "configuration_id": 14}, |
| 125 | + {"particle": "electron", "site": "paranal", "configuration_id": 14}, |
| 126 | + ] |
90 | 127 | result = self.client._RucioFileCatalogClient__transform_DIRAC_filter_dict_to_Rucio_filter_dict(input_dict) |
91 | | - assert result == expected |
92 | | - |
93 | | - input_dict = [{ |
94 | | - 'particle': {'in': ['proton','electron']},'configuration_id': {'=': 14},'site': {'in': [ "LaPalma", 'paranal']} } ] |
95 | | - expected = [{'particle': 'proton', 'configuration_id': 14, 'site': 'LaPalma'}, {'particle': 'proton', 'configuration_id': 14, 'site': 'paranal'}, {'particle': 'electron', 'configuration_id': 14, 'site': 'LaPalma'}, {'particle': 'electron', 'configuration_id': 14, 'site': 'paranal'}] |
| 128 | + assert result == expected |
| 129 | + |
| 130 | + input_dict = [ |
| 131 | + { |
| 132 | + "particle": {"in": ["proton", "electron"]}, |
| 133 | + "configuration_id": {"=": 14}, |
| 134 | + "site": {"in": ["LaPalma", "paranal"]}, |
| 135 | + } |
| 136 | + ] |
| 137 | + expected = [ |
| 138 | + {"particle": "proton", "configuration_id": 14, "site": "LaPalma"}, |
| 139 | + {"particle": "proton", "configuration_id": 14, "site": "paranal"}, |
| 140 | + {"particle": "electron", "configuration_id": 14, "site": "LaPalma"}, |
| 141 | + {"particle": "electron", "configuration_id": 14, "site": "paranal"}, |
| 142 | + ] |
96 | 143 | result = self.client._RucioFileCatalogClient__transform_DIRAC_filter_dict_to_Rucio_filter_dict(input_dict) |
97 | | - assert result == expected |
98 | | - |
99 | | - |
| 144 | + assert result == expected |
100 | 145 |
|
101 | 146 | def test_transform_DIRAC_filter_dict_to_Rucio_filter_dict(self): |
102 | 147 | DIRAC_filter_dict_list = [ |
103 | | - {'particle': {'in': ['proton', 'electron']}, 'configuration_id': {'=': 14}, 'site': {'in': ['LaPalma', 'paranal']}} |
| 148 | + { |
| 149 | + "particle": {"in": ["proton", "electron"]}, |
| 150 | + "configuration_id": {"=": 14}, |
| 151 | + "site": {"in": ["LaPalma", "paranal"]}, |
| 152 | + } |
104 | 153 | ] |
105 | 154 | expected_output = [ |
106 | | - {'particle': 'proton', 'configuration_id': 14, 'site': 'LaPalma'}, |
107 | | - {'particle': 'proton', 'configuration_id': 14, 'site': 'paranal'}, |
108 | | - {'particle': 'electron', 'configuration_id': 14, 'site': 'LaPalma'}, |
109 | | - {'particle': 'electron', 'configuration_id': 14, 'site': 'paranal'} |
| 155 | + {"particle": "proton", "configuration_id": 14, "site": "LaPalma"}, |
| 156 | + {"particle": "proton", "configuration_id": 14, "site": "paranal"}, |
| 157 | + {"particle": "electron", "configuration_id": 14, "site": "LaPalma"}, |
| 158 | + {"particle": "electron", "configuration_id": 14, "site": "paranal"}, |
110 | 159 | ] |
111 | | - result = self.client._RucioFileCatalogClient__transform_DIRAC_filter_dict_to_Rucio_filter_dict(DIRAC_filter_dict_list) |
| 160 | + result = self.client._RucioFileCatalogClient__transform_DIRAC_filter_dict_to_Rucio_filter_dict( |
| 161 | + DIRAC_filter_dict_list |
| 162 | + ) |
112 | 163 | self.assertEqual(result, expected_output) |
113 | 164 |
|
114 | 165 | def test_findFilesByMetadata(self): |
115 | | - self.client.client.list_dids.return_value = ['did1', 'did2'] |
116 | | - metadataFilterDict = {'key1': 'value1'} |
| 166 | + self.client.client.list_dids.return_value = ["did1", "did2"] |
| 167 | + metadataFilterDict = {"key1": "value1"} |
117 | 168 | result = self.client.findFilesByMetadata(metadataFilterDict) |
118 | | - self.assertTrue(result['OK']) |
119 | | - self.assertEqual(result['Value'], ['did1', 'did2']) |
| 169 | + self.assertTrue(result["OK"]) |
| 170 | + self.assertEqual(result["Value"], ["did1", "did2"]) |
120 | 171 |
|
121 | 172 | def test_findFilesByMetadata_with_error(self): |
122 | | - self.client.client.list_dids.side_effect = Exception('Test error') |
123 | | - metadataFilterDict = {'key1': 'value1'} |
| 173 | + self.client.client.list_dids.side_effect = Exception("Test error") |
| 174 | + metadataFilterDict = {"key1": "value1"} |
124 | 175 | result = self.client.findFilesByMetadata(metadataFilterDict) |
125 | | - self.assertFalse(result['OK']) |
126 | | - self.assertIn('Test error', result['Message']) |
| 176 | + self.assertFalse(result["OK"]) |
| 177 | + self.assertIn("Test error", result["Message"]) |
| 178 | + |
127 | 179 |
|
128 | | -if __name__ == '__main__': |
| 180 | +if __name__ == "__main__": |
129 | 181 | unittest.main() |
0 commit comments