66
77"""Tests for Validator"""
88
9+ import copy
910import tempfile
1011import unittest
1112from pathlib import Path
1819from .testdata import TEST_FLOAT32_DATA , TEST_METADATA
1920
2021
21- def test_valid_data ():
22- """ensure the default metadata is OK"""
23- SigMFFile (TEST_METADATA ).validate ()
22+ class NominalCases (unittest .TestCase ):
23+ """Cases where the validator should succeed."""
24+
25+ def test_nominal (self ):
26+ """nominal case should pass"""
27+ SigMFFile (copy .deepcopy (TEST_METADATA )).validate ()
2428
2529
2630class CommandLineValidator (unittest .TestCase ):
@@ -32,7 +36,7 @@ def setUp(self):
3236 self .tmp_path = tmp_path = Path (self .tmp_dir .name )
3337 junk_path = tmp_path / "junk"
3438 TEST_FLOAT32_DATA .tofile (junk_path )
35- some_meta = SigMFFile (TEST_METADATA , data_file = junk_path )
39+ some_meta = SigMFFile (copy . deepcopy ( TEST_METADATA ) , data_file = junk_path )
3640 some_meta .tofile (tmp_path / "a" )
3741 some_meta .tofile (tmp_path / "b" )
3842 some_meta .tofile (tmp_path / "c" , toarchive = True )
@@ -75,13 +79,14 @@ class FailingCases(unittest.TestCase):
7579 """Cases where the validator should raise an exception."""
7680
7781 def setUp (self ):
78- self .metadata = dict (TEST_METADATA )
82+ self .metadata = copy . deepcopy (TEST_METADATA )
7983
8084 def test_no_version (self ):
81- """core:version must be present"""
82- del self .metadata [SigMFFile .GLOBAL_KEY ][SigMFFile .VERSION_KEY ]
85+ """version key must be present"""
86+ meta = SigMFFile (copy .deepcopy (self .metadata ))
87+ del meta ._metadata [SigMFFile .GLOBAL_KEY ][SigMFFile .VERSION_KEY ]
8388 with self .assertRaises (ValidationError ):
84- SigMFFile ( self . metadata ) .validate ()
89+ meta .validate ()
8590
8691 def test_extra_top_level_key (self ):
8792 """no extra keys allowed on the top level"""
@@ -128,3 +133,29 @@ def test_invalid_hash(self):
128133 self .metadata [SigMFFile .GLOBAL_KEY ][SigMFFile .HASH_KEY ] = "derp"
129134 with self .assertRaises (sigmf .error .SigMFFileError ):
130135 SigMFFile (metadata = self .metadata , data_file = temp_file .name )
136+
137+
138+ class CheckNamespace (unittest .TestCase ):
139+ """Cases where namespace issues are involved"""
140+
141+ def setUp (self ):
142+ self .metadata = copy .deepcopy (TEST_METADATA )
143+
144+ def test_undeclared_namespace (self ):
145+ """unknown namespace should raise a warning"""
146+ self .metadata [SigMFFile .GLOBAL_KEY ]["other_namespace:key" ] = 0
147+ with self .assertWarns (Warning ):
148+ SigMFFile (self .metadata ).validate ()
149+
150+ def test_declared_namespace (self ):
151+ """known namespace should not raise a warning"""
152+ self .metadata [SigMFFile .GLOBAL_KEY ]["other_namespace:key" ] = 0
153+ # define other_namespace
154+ self .metadata [SigMFFile .GLOBAL_KEY ][SigMFFile .EXTENSIONS_KEY ] = [
155+ {
156+ "name" : "other_namespace" ,
157+ "version" : "0.0.1" ,
158+ "optional" : False ,
159+ }
160+ ]
161+ SigMFFile (self .metadata ).validate ()
0 commit comments