99import tempfile
1010import unittest
1111from pathlib import Path
12- from typing import cast
12+
13+ import numpy as np
1314
1415import sigmf
1516from sigmf .convert .blue import blue_to_sigmf
1617
18+ from .test_convert_wav import _validate_ncd
1719from .testdata import NONSIGMF_ENV , NONSIGMF_REPO
1820
1921
20- class TestBlueConverter (unittest .TestCase ):
22+ class TestBlueWithNonSigMFRepo (unittest .TestCase ):
2123 """BLUE converter tests using external files"""
2224
2325 def setUp (self ) -> None :
@@ -28,93 +30,57 @@ def setUp(self) -> None:
2830 # skip test if environment variable not set
2931 self .skipTest (f"Set { NONSIGMF_ENV } environment variable to path with BLUE files to run test." )
3032
31- # look for blue files in blue/ directory
33+ # glob all files in blue/ directory
3234 blue_dir = NONSIGMF_REPO / "blue"
33- self .bluefiles = []
35+ self .blue_paths = []
3436 if blue_dir .exists ():
3537 for ext in ["*.cdif" , "*.tmp" ]:
36- self .bluefiles .extend (blue_dir .glob (f"**/{ ext } " ))
37-
38- if not self .bluefiles :
38+ self .blue_paths .extend (blue_dir .glob (f"**/{ ext } " ))
39+ if not self .blue_paths :
3940 self .fail (f"No BLUE files (*.cdif, *.tmp) found in { blue_dir } ." )
4041
4142 def tearDown (self ) -> None :
4243 """clean up temporary directory"""
4344 self .tmp_dir .cleanup ()
4445
45- def _validate_ncd_structure (self , meta , expected_file ):
46- """validate basic NCD structure"""
47- self .assertEqual (meta .data_file , expected_file , "NCD should point to original file" )
48- self .assertIn ("core:trailing_bytes" , meta ._metadata ["global" ])
49- captures = meta .get_captures ()
50- self .assertGreater (len (captures ), 0 , "Should have at least one capture" )
51- self .assertIn ("core:header_bytes" , captures [0 ])
52-
53- # validate SigMF spec compliance: NCDs must not have metadata_only field
54- global_meta = meta ._metadata ["global" ]
55- has_dataset = "core:dataset" in global_meta
56- has_metadata_only = "core:metadata_only" in global_meta
57-
58- self .assertTrue (has_dataset , "NCD should have core:dataset field" )
59- self .assertFalse (has_metadata_only , "NCD should NOT have core:metadata_only field (spec violation)" )
60-
61- return captures
62-
63- def _validate_auto_detection (self , file_path ):
64- """validate auto-detection works and returns valid NCD"""
65- meta_auto_raw = sigmf .fromfile (file_path )
66- # auto-detection should return SigMFFile, not SigMFCollection
67- self .assertIsInstance (meta_auto_raw , sigmf .SigMFFile )
68- meta_auto = cast (sigmf .SigMFFile , meta_auto_raw )
69- # data_file might be Path or str, so convert both for comparison
70- self .assertEqual (str (meta_auto .data_file ), str (file_path ))
71- self .assertIn ("core:trailing_bytes" , meta_auto ._metadata ["global" ])
72- return meta_auto
73-
74- def test_blue_to_sigmf_pair (self ):
46+ def test_sigmf_pair (self ):
7547 """test standard blue to sigmf conversion with file pairs"""
76- for bluefile in self .bluefiles :
77- sigmf_path = self .tmp_path / bluefile .stem
78- meta = blue_to_sigmf (blue_path = bluefile , out_path = sigmf_path )
79- if not meta .get_global_field ("core:metadata_only" ):
80- meta .read_samples (count = 10 )
48+ for blue_path in self .blue_paths :
49+ sigmf_path = self .tmp_path / blue_path .stem
50+ meta = blue_to_sigmf (blue_path = blue_path , out_path = sigmf_path )
8151 self .assertIsInstance (meta , sigmf .SigMFFile )
52+ # FIXME: REPLACE BELOW WITH BELOW COMMENTED AFTER PR #121 MERGED
53+ if not meta .get_global_field ("core:metadata_only" ):
54+ _ = meta .read_samples (count = 10 )
55+ # check sample read consistency
56+ # np.testing.assert_array_equal(meta.read_samples(count=10), meta[0:10])
8257
83- def test_blue_to_sigmf_archive (self ):
58+ def test_sigmf_archive (self ):
8459 """test blue to sigmf conversion with archive output"""
85- for bluefile in self .bluefiles :
86- sigmf_path = self .tmp_path / f"{ bluefile .stem } _archive"
87- meta = blue_to_sigmf (blue_path = bluefile , out_path = str (sigmf_path ), create_archive = True )
88- self .assertIsInstance (meta , sigmf .SigMFFile )
89-
90- def test_blue_to_sigmf_ncd (self ):
91- """test blue to sigmf conversion as Non-Conforming Dataset"""
92- for bluefile in self .bluefiles :
93- meta = blue_to_sigmf (blue_path = str (bluefile ), create_ncd = True )
94-
95- # validate basic NCD structure
96- self ._validate_ncd_structure (meta , bluefile )
97-
98- # verify this is metadata-only (no separate data file created)
99- self .assertIsInstance (meta .data_buffer , type (meta .data_buffer ))
60+ for blue_path in self .blue_paths :
61+ sigmf_path = self .tmp_path / f"{ blue_path .stem } _archive"
62+ meta = blue_to_sigmf (blue_path = blue_path , out_path = sigmf_path , create_archive = True )
63+ # now read newly created archive
64+ arc_meta = sigmf .fromfile (sigmf_path )
65+ self .assertIsInstance (arc_meta , sigmf .SigMFFile )
66+ # FIXME: REPLACE BELOW WITH BELOW COMMENTED AFTER PR #121 MERGED
67+ if not arc_meta .get_global_field ("core:metadata_only" ):
68+ _ = arc_meta .read_samples (count = 10 )
69+ # check sample read consistency
70+ # np.testing.assert_array_equal(meta.read_samples(count=10), meta[0:10])
71+
72+ def test_create_ncd (self ):
73+ """test direct NCD conversion"""
74+ for blue_path in self .blue_paths :
75+ meta = blue_to_sigmf (blue_path = blue_path )
76+ _validate_ncd (self , meta , blue_path )
10077
10178 # test that data can be read if not metadata-only
10279 if not meta .get_global_field ("core:metadata_only" ):
10380 _ = meta .read_samples (count = 10 )
10481
105- def test_blue_auto_detection (self ):
106- """test automatic BLUE detection through fromfile()"""
107- for bluefile in self .bluefiles :
108- # validate auto-detection works
109- self ._validate_auto_detection (bluefile )
110-
111- def test_blue_directory_files_ncd (self ):
112- """test NCD conversion"""
113- for blue_file in self .bluefiles :
114- meta = blue_to_sigmf (blue_path = str (blue_file ), create_ncd = True )
115-
116- # validate basic NCD structure
117- self ._validate_ncd_structure (meta , blue_file )
118-
119- # validate auto-detection also works
120- self ._validate_auto_detection (blue_file )
82+ def test_autodetect_ncd (self ):
83+ """test automatic NCD conversion"""
84+ for blue_path in self .blue_paths :
85+ meta = sigmf .fromfile (blue_path )
86+ _validate_ncd (self , meta , blue_path )
0 commit comments