Spaces:
Running
Running
Commit
·
fbb1d85
1
Parent(s):
bdf49c6
Added validate_zip tests
Browse files- validation.py +110 -47
validation.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
|
|
| 1 |
import json
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
from zipfile import ZipFile
|
| 4 |
from typing import List, Dict, Any, Union
|
|
@@ -92,53 +94,114 @@ def validate_json_file_structure(file_path: Path, fields: List[str]):
|
|
| 92 |
raise ValueError(f'Invalid `{file_path.name}` format, fields: {fields} are required in each entry')
|
| 93 |
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
if __name__ == '__main__':
|
| 144 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import json
|
| 3 |
+
import unittest
|
| 4 |
from pathlib import Path
|
| 5 |
from zipfile import ZipFile
|
| 6 |
from typing import List, Dict, Any, Union
|
|
|
|
| 94 |
raise ValueError(f'Invalid `{file_path.name}` format, fields: {fields} are required in each entry')
|
| 95 |
|
| 96 |
|
| 97 |
+
####################################################################################################
|
| 98 |
+
# Tests
|
| 99 |
+
####################################################################################################
|
| 100 |
+
|
| 101 |
+
class TestValidateZip(unittest.TestCase):
|
| 102 |
+
DATA_SAMPLES = 10
|
| 103 |
+
|
| 104 |
+
@classmethod
|
| 105 |
+
def setUpClass(cls):
|
| 106 |
+
cls.valid_data = [{'session_id': 'session_id', 'words': 'words', 'speaker': 'speaker',
|
| 107 |
+
'start_time': 0.0, 'end_time': 1.0} for _ in range(cls.DATA_SAMPLES)]
|
| 108 |
+
cls.invalid_data = [{'session_id': 'session_id', 'words': 'words',
|
| 109 |
+
'start_time': 0.0} for _ in range(cls.DATA_SAMPLES)]
|
| 110 |
+
|
| 111 |
+
def setUp(self):
|
| 112 |
+
self.temp_dir = TemporaryDirectory()
|
| 113 |
+
self.submission_zip = Path(self.temp_dir.name) / 'submission.zip'
|
| 114 |
+
|
| 115 |
+
def create_test_data(self, submission_track: str, data: List[Dict[str, Any]], json_file_names: List[str],
|
| 116 |
+
parent_zip_dir: str = None):
|
| 117 |
+
submission_dir = Path(self.temp_dir.name) / submission_track
|
| 118 |
+
os.makedirs(submission_dir, exist_ok=True)
|
| 119 |
+
with ZipFile(self.submission_zip, 'w') as submission_zip_file:
|
| 120 |
+
for json_file_name in json_file_names:
|
| 121 |
+
if parent_zip_dir:
|
| 122 |
+
json_file_name = str(Path(parent_zip_dir) / json_file_name)
|
| 123 |
+
submission_zip_file.writestr(json_file_name, json.dumps(data))
|
| 124 |
+
return submission_track, self.submission_zip
|
| 125 |
+
|
| 126 |
+
def tearDown(self):
|
| 127 |
+
self.temp_dir.cleanup()
|
| 128 |
+
|
| 129 |
+
def test_NOTSOFAR_SC_valid_data_tcp(self):
|
| 130 |
+
self.assertEqual(validate_zip(*self.create_test_data(
|
| 131 |
+
'NOTSOFAR-SC', self.valid_data, ['tcp_wer_hyp.json'])), None)
|
| 132 |
+
|
| 133 |
+
def test_NOTSOFAR_SC_valid_data_tcp_and_tcorc(self):
|
| 134 |
+
self.assertEqual(validate_zip(*self.create_test_data(
|
| 135 |
+
'NOTSOFAR-SC', self.valid_data, ['tcp_wer_hyp.json', 'tc_orc_wer_ref.json'])), None)
|
| 136 |
+
|
| 137 |
+
def test_NOTSOFAR_SC_missing_tcp_file(self):
|
| 138 |
+
with self.assertRaises(ValueError):
|
| 139 |
+
validate_zip(*self.create_test_data(
|
| 140 |
+
'NOTSOFAR-SC', self.valid_data, ['tc_orc_wer_ref.json']))
|
| 141 |
+
|
| 142 |
+
def test_NOTSOFAR_SC_invalid_data(self):
|
| 143 |
+
with self.assertRaises(ValueError):
|
| 144 |
+
validate_zip(*self.create_test_data(
|
| 145 |
+
'NOTSOFAR-SC', self.invalid_data, ['tcp_wer_hyp.json']))
|
| 146 |
+
|
| 147 |
+
def test_NOTSOFAR_MC_valid_data_tcp(self):
|
| 148 |
+
self.assertEqual(validate_zip(*self.create_test_data(
|
| 149 |
+
'NOTSOFAR-MC', self.valid_data, ['tcp_wer_hyp.json'])), None)
|
| 150 |
+
|
| 151 |
+
def test_NOTSOFAR_MC_valid_data_tcp_and_tcorc(self):
|
| 152 |
+
self.assertEqual(validate_zip(*self.create_test_data(
|
| 153 |
+
'NOTSOFAR-MC', self.valid_data, ['tcp_wer_hyp.json', 'tc_orc_wer_ref.json'])), None)
|
| 154 |
+
|
| 155 |
+
def test_NOTSOFAR_MC_missing_tcp_file(self):
|
| 156 |
+
with self.assertRaises(ValueError):
|
| 157 |
+
validate_zip(*self.create_test_data(
|
| 158 |
+
'NOTSOFAR-MC', self.valid_data, ['tc_orc_wer_ref.json']))
|
| 159 |
+
|
| 160 |
+
def test_NOTSOFAR_MC_invalid_data(self):
|
| 161 |
+
with self.assertRaises(ValueError):
|
| 162 |
+
validate_zip(*self.create_test_data(
|
| 163 |
+
'NOTSOFAR-MC', self.invalid_data, ['tcp_wer_hyp.json']))
|
| 164 |
+
|
| 165 |
+
def test_DASR_Constrained_LM_valid_data(self):
|
| 166 |
+
self.assertEqual(validate_zip(*self.create_test_data('DASR-Constrained-LM', self.valid_data,
|
| 167 |
+
['chime6.json', 'dipco.json', 'mixer6.json',
|
| 168 |
+
'notsofar1.json'], 'dev')), None)
|
| 169 |
+
|
| 170 |
+
def test_DASR_Constrained_LM_invalid_data(self):
|
| 171 |
+
with self.assertRaises(ValueError):
|
| 172 |
+
validate_zip(*self.create_test_data('DASR-Constrained-LM', self.invalid_data,
|
| 173 |
+
['chime6.json', 'dipco.json', 'mixer6.json', 'notsofar1.json'], 'dev'))
|
| 174 |
+
|
| 175 |
+
def test_DASR_Constrained_LM_missing_dev_dir(self):
|
| 176 |
+
with self.assertRaises(ValueError):
|
| 177 |
+
validate_zip(*self.create_test_data('DASR-Constrained-LM', self.valid_data,
|
| 178 |
+
['chime6.json', 'dipco.json', 'mixer6.json', 'notsofar1.json']))
|
| 179 |
+
|
| 180 |
+
def test_DASR_Constrained_LM_missing_json_file(self):
|
| 181 |
+
with self.assertRaises(ValueError):
|
| 182 |
+
validate_zip(*self.create_test_data('DASR-Constrained-LM', self.valid_data,
|
| 183 |
+
['chime6.json', 'dipco.json', 'mixer6.json'], 'dev'))
|
| 184 |
+
|
| 185 |
+
def test_DASR_Unconstrained_LM_valid_data(self):
|
| 186 |
+
self.assertEqual(validate_zip(*self.create_test_data('DASR-Unconstrained-LM', self.valid_data,
|
| 187 |
+
['chime6.json', 'dipco.json', 'mixer6.json',
|
| 188 |
+
'notsofar1.json'], 'dev')), None)
|
| 189 |
+
|
| 190 |
+
def test_DASR_Unconstrained_LM_invalid_data(self):
|
| 191 |
+
with self.assertRaises(ValueError):
|
| 192 |
+
validate_zip(*self.create_test_data('DASR-Unconstrained-LM', self.invalid_data,
|
| 193 |
+
['chime6.json', 'dipco.json', 'mixer6.json', 'notsofar1.json'], 'dev'))
|
| 194 |
+
|
| 195 |
+
def test_DASR_Unconstrained_LM_missing_dev_dir(self):
|
| 196 |
+
with self.assertRaises(ValueError):
|
| 197 |
+
validate_zip(*self.create_test_data('DASR-Unconstrained-LM', self.valid_data,
|
| 198 |
+
['chime6.json', 'dipco.json', 'mixer6.json', 'notsofar1.json']))
|
| 199 |
+
|
| 200 |
+
def test_DASR_Unconstrained_LM_missing_json_file(self):
|
| 201 |
+
with self.assertRaises(ValueError):
|
| 202 |
+
validate_zip(*self.create_test_data('DASR-Unconstrained-LM', self.valid_data,
|
| 203 |
+
['chime6.json', 'dipco.json', 'mixer6.json'], 'dev'))
|
| 204 |
|
| 205 |
|
| 206 |
if __name__ == '__main__':
|
| 207 |
+
unittest.main()
|