From efc184dd5df329cd9e7e5c428bb2e1a597bf0927 Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Fri, 5 Apr 2024 21:15:59 +0000 Subject: [PATCH] Validate that global core:version is present. --- sigmf/sigmffile.py | 5 +---- tests/conftest.py | 1 + tests/test_archivereader.py | 1 + tests/test_validation.py | 6 ++++++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sigmf/sigmffile.py b/sigmf/sigmffile.py index 9e72d01..91a31b7 100644 --- a/sigmf/sigmffile.py +++ b/sigmf/sigmffile.py @@ -174,6 +174,7 @@ def __init__(self, metadata=None, data_file=None, global_info=None, skip_checksu if metadata is None: self._metadata = {self.GLOBAL_KEY:{}, self.CAPTURE_KEY:[], self.ANNOTATION_KEY:[]} self._metadata[self.GLOBAL_KEY][self.NUM_CHANNELS_KEY] = 1 + self._metadata[self.GLOBAL_KEY][self.VERSION_KEY] = "1.0.0" elif isinstance(metadata, dict): self._metadata = metadata else: @@ -183,8 +184,6 @@ def __init__(self, metadata=None, data_file=None, global_info=None, skip_checksu if data_file is not None: self.set_data_file(data_file, skip_checksum=skip_checksum, map_readonly=map_readonly) - self._metadata[self.GLOBAL_KEY][self.VERSION_KEY] = '1.0.0' - def __len__(self): return self._memmap.shape[0] @@ -724,8 +723,6 @@ def __init__(self, metafiles=None, metadata=None, skip_checksums=False): if not self.skip_checksums: self.verify_stream_hashes() - self._metadata[self.COLLECTION_KEY][self.VERSION_KEY] = '1.0.0' - def __len__(self): ''' the length of a collection is the number of streams diff --git a/tests/conftest.py b/tests/conftest.py index d25ad06..f8f6aa4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -40,6 +40,7 @@ def test_sigmffile(test_data_file): """If pytest uses this signature, will return valid SigMF file.""" sigf = SigMFFile() sigf.set_global_field("core:datatype", "rf32_le") + sigf.set_global_field("core:version", "1.0.0") sigf.add_annotation(start_index=0, length=len(TEST_FLOAT32_DATA)) sigf.add_capture(start_index=0) sigf.set_data_file(test_data_file.name) diff --git a/tests/test_archivereader.py b/tests/test_archivereader.py index 657d633..1818285 100644 --- a/tests/test_archivereader.py +++ b/tests/test_archivereader.py @@ -42,6 +42,7 @@ def test_access_data_without_untar(self): global_info={ SigMFFile.DATATYPE_KEY: f"{complex_prefix}{key}_le", SigMFFile.NUM_CHANNELS_KEY: num_channels, + SigMFFile.VERSION_KEY: "1.0.0", }, ) temp_meta.tofile(temp_archive, toarchive=True) diff --git a/tests/test_validation.py b/tests/test_validation.py index c2c267b..ce427a1 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -39,6 +39,12 @@ class FailingCases(unittest.TestCase): def setUp(self): self.metadata = dict(TEST_METADATA) + def test_no_version(self): + '''core:version must be present''' + del self.metadata[SigMFFile.GLOBAL_KEY][SigMFFile.VERSION_KEY] + with self.assertRaises(ValidationError): + SigMFFile(self.metadata).validate() + def test_extra_top_level_key(self): '''no extra keys allowed on the top level''' self.metadata['extra'] = 0