1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
import subprocess
import re
import sys
from unittest.mock import patch
from io import StringIO
import warnings
from pynwb.testing import TestCase
from pynwb import validate, NWBHDF5IO
# NOTE we use "coverage run -m pynwb.validate" instead of "python -m pynwb.validate"
# so that we can both test pynwb.validate and compute code coverage from that test.
# NOTE we also use "coverage run -p" which will generate a .coverage file with the
# machine name, process id, and a random number appended to the filename to
# simplify collecting and merging coverage data from multiple subprocesses. if "-p"
# is not used, then each "coverage run" will overwrite the .coverage file from a
# previous "coverage run".
# NOTE we run "coverage" as "{sys.executable} -m coverage" to 1. make sure to use
# the same python version, and on Debian systems executable is "python3-coverage", not
# just "coverage".
# NOTE the run_coverage.yml GitHub Action runs "python -m coverage combine" to
# combine the individual coverage reports into one .coverage file.
def run_coverage(extra_args: list[str]):
return subprocess.run(
[sys.executable, "-m", "coverage", "run", "-p", "-m", "pynwb.validate"]
+ extra_args,
capture_output=True
)
class TestValidateCLI(TestCase):
# 1.0.2_nwbfile.nwb has no cached specifications
# 1.0.3_nwbfile.nwb has cached "core" specification
# 1.1.2_nwbfile.nwb has cached "core" and "hdmf-common" specifications
def test_validate_file_no_cache(self):
"""Test that validating a file with no cached spec against the core namespace succeeds."""
result = run_coverage(["tests/back_compat/1.0.2_nwbfile.nwb"])
stderr_regex = re.compile(
r"The file tests/back_compat/1\.0\.2_nwbfile\.nwb has no cached namespace information\. "
r"Falling back to PyNWB namespace information\.\s*"
)
self.assertRegex(result.stderr.decode('utf-8'), stderr_regex)
stdout_regex = re.compile(
r"Validating tests/back_compat/1\.0\.2_nwbfile\.nwb against PyNWB namespace information using namespace "
r"'core'\.\s* - no errors found\.\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
def test_validate_file_no_cache_bad_ns(self):
"""Test that validating a file with no cached spec against a specified, unknown namespace fails."""
result = run_coverage(["tests/back_compat/1.0.2_nwbfile.nwb", "--ns", "notfound"])
stderr_regex = re.compile(
r"The file tests/back_compat/1\.0\.2_nwbfile\.nwb has no cached namespace information\. "
r"Falling back to PyNWB namespace information\.\s*"
r"The namespace 'notfound' could not be found in PyNWB namespace information as only "
r"\['core'\] is present\.\s*"
)
self.assertRegex(result.stderr.decode('utf-8'), stderr_regex)
self.assertEqual(result.stdout.decode('utf-8'), '')
def test_validate_file_cached(self):
"""Test that validating a file with cached spec against its cached namespace succeeds."""
result = run_coverage(["tests/back_compat/1.1.2_nwbfile.nwb"])
self.assertEqual(result.stderr.decode('utf-8'), '')
stdout_regex = re.compile(
r"Validating tests/back_compat/1\.1\.2_nwbfile\.nwb against cached namespace information using namespace "
r"'core'\.\s* - no errors found\.\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
def test_validate_file_cached_bad_ns(self):
"""Test that validating a file with cached spec against a specified, unknown namespace fails."""
result = run_coverage(["tests/back_compat/1.1.2_nwbfile.nwb", "--ns", "notfound"])
stderr_regex = re.compile(
r"The namespace 'notfound' could not be found in cached namespace information as only "
r"\['core'\] is present\.\s*"
)
self.assertRegex(result.stderr.decode('utf-8'), stderr_regex)
self.assertEqual(result.stdout.decode('utf-8'), '')
def test_validate_file_cached_extension(self):
"""Test that validating a file with cached spec against the cached namespaces succeeds."""
result = run_coverage(["tests/back_compat/2.1.0_nwbfile_with_extension.nwb"])
self.assertEqual(result.stderr.decode('utf-8'), '')
stdout_regex = re.compile(
r"Validating tests/back_compat/2\.1\.0_nwbfile_with_extension\.nwb against cached namespace information "
r"using namespace 'ndx-testextension'\.\s* - no errors found\.\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
def test_validate_file_cached_extension_pass_ns(self):
"""Test that validating a file with cached spec against the extension namespace succeeds."""
result = run_coverage(["tests/back_compat/2.1.0_nwbfile_with_extension.nwb", "--ns", "ndx-testextension"])
self.assertEqual(result.stderr.decode('utf-8'), '')
stdout_regex = re.compile(
r"Validating tests/back_compat/2\.1\.0_nwbfile_with_extension\.nwb against cached namespace information "
r"using namespace 'ndx-testextension'\.\s* - no errors found\.\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
def test_validate_file_cached_core(self):
"""Test that validating a file with cached spec against the core namespace succeeds."""
result = run_coverage(["tests/back_compat/2.1.0_nwbfile_with_extension.nwb", "--ns", "core"])
stdout_regex = re.compile(
r"The namespace 'core' is included by the namespace 'ndx-testextension'. "
r"Please validate against that namespace instead\.\s*"
)
self.assertRegex(result.stderr.decode('utf-8'), stdout_regex)
def test_validate_file_cached_hdmf_common(self):
"""Test that validating a file with cached spec against the hdmf-common namespace fails."""
result = run_coverage(["tests/back_compat/1.1.2_nwbfile.nwb", "--ns", "hdmf-common"])
stderr_regex = re.compile(
r"The namespace 'hdmf-common' is included by the namespace 'core'\. Please validate against that "
r"namespace instead\.\s*",
)
self.assertRegex(result.stderr.decode('utf-8'), stderr_regex)
def test_validate_file_cached_ignore(self):
"""Test that validating a file with cached spec against the core namespace succeeds."""
result = run_coverage(["tests/back_compat/1.1.2_nwbfile.nwb", "--no-cached-namespace"])
self.assertEqual(result.stderr.decode('utf-8'), '')
stdout_regex = re.compile(
r"Validating tests/back_compat/1\.1\.2_nwbfile\.nwb against PyNWB namespace information using namespace "
r"'core'\.\s* - no errors found\.\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
def test_validate_file_invalid(self):
"""Test that validating an invalid file outputs errors."""
result = run_coverage(["tests/back_compat/1.0.2_str_experimenter.nwb", "--no-cached-namespace"])
stderr_regex = re.compile(
r" - found the following errors:\s*"
r"root/general/experimenter \(general/experimenter\): incorrect shape - expected an array of shape "
r"'\[None\]', got non-array data 'one experimenter'\s*"
)
self.assertRegex(result.stderr.decode('utf-8'), stderr_regex)
stdout_regex = re.compile(
r"Validating tests/back_compat/1\.0\.2_str_experimenter\.nwb against PyNWB namespace information using "
r"namespace 'core'\.\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
def test_validate_file_list_namespaces_core(self):
"""Test listing namespaces from a file"""
result = run_coverage(["tests/back_compat/1.1.2_nwbfile.nwb", "--list-namespaces"])
self.assertEqual(result.stderr.decode('utf-8'), '')
stdout_regex = re.compile(r"core\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
def test_validate_file_list_namespaces_extension(self):
"""Test listing namespaces from a file with an extension"""
result = run_coverage(["tests/back_compat/2.1.0_nwbfile_with_extension.nwb", "--list-namespaces"])
self.assertEqual(result.stderr.decode('utf-8'), '')
stdout_regex = re.compile(r"ndx-testextension\s*")
self.assertRegex(result.stdout.decode('utf-8'), stdout_regex)
class TestValidateFunction(TestCase):
# 1.0.2_nwbfile.nwb has no cached specifications
# 1.0.3_nwbfile.nwb has cached "core" specification
# 1.1.2_nwbfile.nwb has cached "core" and "hdmf-common" specificaitions
def get_io(self, path):
"""Get an NWBHDF5IO object for the given path, ignoring the warning about ignoring cached namespaces."""
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=r"Ignoring cached namespace .*",
category=UserWarning,
)
return NWBHDF5IO(str(path), 'r')
def test_validate_io_no_cache(self):
"""Test that validating a file with no cached spec against the core namespace succeeds."""
with self.get_io('tests/back_compat/1.0.2_nwbfile.nwb') as io:
errors = validate(io)
self.assertEqual(errors, [])
def test_validate_io_no_cache_bad_ns(self):
"""Test that validating a file with no cached spec against a specified, unknown namespace fails."""
with self.get_io('tests/back_compat/1.0.2_nwbfile.nwb') as io:
with self.assertRaisesWith(KeyError, "\"'notfound' not a namespace\""):
validate(io, 'notfound')
def test_validate_io_cached(self):
"""Test that validating a file with cached spec against its cached namespace succeeds."""
with self.get_io('tests/back_compat/1.1.2_nwbfile.nwb') as io:
errors = validate(io)
self.assertEqual(errors, [])
def test_validate_io_cached_extension(self):
"""Test that validating a file with cached spec against its cached namespaces succeeds."""
with self.get_io('tests/back_compat/2.1.0_nwbfile_with_extension.nwb') as io:
errors = validate(io)
self.assertEqual(errors, [])
def test_validate_io_cached_extension_pass_ns(self):
"""Test that validating a file with cached extension spec against the extension namespace succeeds."""
with self.get_io('tests/back_compat/2.1.0_nwbfile_with_extension.nwb') as io:
errors = validate(io, 'ndx-testextension')
self.assertEqual(errors, [])
def test_validate_io_cached_core_with_io(self):
"""
For back-compatability, test that validating a file with cached extension spec against the core
namespace succeeds when using the `io` + `namespace` keywords.
"""
with self.get_io(path='tests/back_compat/2.1.0_nwbfile_with_extension.nwb') as io:
results = validate(io=io, namespace="core")
self.assertEqual(results, [])
def test_validate_file_cached_extension(self):
"""
Test that validating a file with cached extension spec against the core
namespace raises an error with the new CLI-mimicing paths keyword.
"""
nwbfile_path = "tests/back_compat/2.1.0_nwbfile_with_extension.nwb"
with patch("sys.stderr", new=StringIO()) as fake_err:
with patch("sys.stdout", new=StringIO()) as fake_out:
results, status = validate(paths=[nwbfile_path], namespace="core", verbose=True)
self.assertEqual(results, [])
self.assertEqual(status, 1)
self.assertEqual(
fake_err.getvalue(),
(
"The namespace 'core' is included by the namespace 'ndx-testextension'. "
"Please validate against that namespace instead.\n"
)
)
self.assertEqual(fake_out.getvalue(), "")
def test_validate_file_cached_core(self):
"""
Test that validating a file with cached core spec with verbose=False.
"""
nwbfile_path = "tests/back_compat/1.1.2_nwbfile.nwb"
with patch("sys.stderr", new=StringIO()) as fake_err:
with patch("sys.stdout", new=StringIO()) as fake_out:
results, status = validate(paths=[nwbfile_path], namespace="core")
self.assertEqual(results, [])
self.assertEqual(status, 0)
self.assertEqual(fake_err.getvalue(), "")
self.assertEqual(fake_out.getvalue(), "")
def test_validate_file_cached_no_cache_bad_ns(self):
"""
Test that validating a file with no cached namespace, a namespace that is not found, and verbose=False.
"""
nwbfile_path = "tests/back_compat/1.0.2_nwbfile.nwb"
with patch("sys.stderr", new=StringIO()) as fake_err:
with patch("sys.stdout", new=StringIO()) as fake_out:
results, status = validate(paths=[nwbfile_path], namespace="notfound")
self.assertEqual(results, [])
self.assertEqual(status, 1)
stderr_regex = (
r"The namespace 'notfound' could not be found in PyNWB namespace information as only "
r"\['core'\] is present.\n"
)
self.assertRegex(fake_err.getvalue(), stderr_regex)
self.assertEqual(fake_out.getvalue(), "")
def test_validate_io_cached_bad_ns(self):
"""Test that validating a file with cached spec against a specified, unknown namespace fails."""
with self.get_io('tests/back_compat/1.1.2_nwbfile.nwb') as io:
with self.assertRaisesWith(KeyError, "\"'notfound' not a namespace\""):
validate(io, 'notfound')
def test_validate_io_cached_hdmf_common(self):
"""Test that validating a file with cached spec against the hdmf-common namespace fails."""
with self.get_io('tests/back_compat/1.1.2_nwbfile.nwb') as io:
# TODO this error should not be different from the error when using the validate script above
msg = "builder must have data type defined with attribute 'data_type'"
with self.assertRaisesWith(ValueError, msg):
validate(io, 'hdmf-common')
|