| 1 |
from abc import ABCMeta, abstractmethod |
| 2 |
from datetime import datetime, date |
| 3 |
from unittest import mock, skip |
| 4 |
|
| 5 |
import numpy as np |
| 6 |
from dateutil.tz import tzlocal |
| 7 |
from hdmf.build import GroupBuilder, DatasetBuilder, LinkBuilder, ReferenceBuilder, TypeMap, BuildManager |
| 8 |
from hdmf.spec import (GroupSpec, AttributeSpec, DatasetSpec, SpecCatalog, SpecNamespace, |
| 9 |
LinkSpec, RefSpec, NamespaceCatalog, DtypeSpec) |
| 10 |
from hdmf.spec.spec import ONE_OR_MANY, ZERO_OR_MANY, ZERO_OR_ONE |
| 11 |
from hdmf.testing import TestCase, remove_test_file |
| 12 |
from hdmf.validate import ValidatorMap |
| 13 |
from hdmf.validate.errors import (DtypeError, MissingError, ExpectedArrayError, MissingDataType, |
| 14 |
IncorrectQuantityError, IllegalLinkError) |
| 15 |
from hdmf.backends.hdf5 import HDF5IO |
| 16 |
|
| 17 |
CORE_NAMESPACE = 'test_core' |
| 18 |
|
| 19 |
|
| 20 |
class ValidatorTestBase(TestCase, metaclass=ABCMeta): |
| 21 |
|
| 22 |
def setUp(self): |
| 23 |
spec_catalog = SpecCatalog() |
| 24 |
for spec in self.getSpecs(): |
| 25 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 26 |
self.namespace = SpecNamespace( |
| 27 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 28 |
self.vmap = ValidatorMap(self.namespace) |
| 29 |
|
| 30 |
@abstractmethod |
| 31 |
def getSpecs(self): |
| 32 |
pass |
| 33 |
|
| 34 |
def assertValidationError(self, error, type_, name=None, reason=None): |
| 35 |
"""Assert that a validation Error matches expectations""" |
| 36 |
self.assertIsInstance(error, type_) |
| 37 |
if name is not None: |
| 38 |
self.assertEqual(error.name, name) |
| 39 |
if reason is not None: |
| 40 |
self.assertEqual(error.reason, reason) |
| 41 |
|
| 42 |
|
| 43 |
class TestEmptySpec(ValidatorTestBase): |
| 44 |
|
| 45 |
def getSpecs(self): |
| 46 |
return (GroupSpec('A test group specification with a data type', data_type_def='Bar'),) |
| 47 |
|
| 48 |
def test_valid(self): |
| 49 |
builder = GroupBuilder('my_bar', attributes={'data_type': 'Bar'}) |
| 50 |
validator = self.vmap.get_validator('Bar') |
| 51 |
result = validator.validate(builder) |
| 52 |
self.assertEqual(len(result), 0) |
| 53 |
|
| 54 |
def test_invalid_missing_req_type(self): |
| 55 |
builder = GroupBuilder('my_bar') |
| 56 |
err_msg = r"builder must have data type defined with attribute '[A-Za-z_]+'" |
| 57 |
with self.assertRaisesRegex(ValueError, err_msg): |
| 58 |
self.vmap.validate(builder) |
| 59 |
|
| 60 |
|
| 61 |
class TestBasicSpec(ValidatorTestBase): |
| 62 |
|
| 63 |
def getSpecs(self): |
| 64 |
ret = GroupSpec('A test group specification with a data type', |
| 65 |
data_type_def='Bar', |
| 66 |
datasets=[DatasetSpec('an example dataset', 'int', name='data', |
| 67 |
attributes=[AttributeSpec( |
| 68 |
'attr2', 'an example integer attribute', 'int')])], |
| 69 |
attributes=[AttributeSpec('attr1', 'an example string attribute', 'text')]) |
| 70 |
return (ret,) |
| 71 |
|
| 72 |
def test_invalid_missing(self): |
| 73 |
builder = GroupBuilder('my_bar', attributes={'data_type': 'Bar'}) |
| 74 |
validator = self.vmap.get_validator('Bar') |
| 75 |
result = validator.validate(builder) |
| 76 |
self.assertEqual(len(result), 2) |
| 77 |
self.assertValidationError(result[0], MissingError, name='Bar/attr1') |
| 78 |
self.assertValidationError(result[1], MissingError, name='Bar/data') |
| 79 |
|
| 80 |
def test_invalid_incorrect_type_get_validator(self): |
| 81 |
builder = GroupBuilder('my_bar', attributes={'data_type': 'Bar', 'attr1': 10}) |
| 82 |
validator = self.vmap.get_validator('Bar') |
| 83 |
result = validator.validate(builder) |
| 84 |
self.assertEqual(len(result), 2) |
| 85 |
self.assertValidationError(result[0], DtypeError, name='Bar/attr1') |
| 86 |
self.assertValidationError(result[1], MissingError, name='Bar/data') |
| 87 |
|
| 88 |
def test_invalid_incorrect_type_validate(self): |
| 89 |
builder = GroupBuilder('my_bar', attributes={'data_type': 'Bar', 'attr1': 10}) |
| 90 |
result = self.vmap.validate(builder) |
| 91 |
self.assertEqual(len(result), 2) |
| 92 |
self.assertValidationError(result[0], DtypeError, name='Bar/attr1') |
| 93 |
self.assertValidationError(result[1], MissingError, name='Bar/data') |
| 94 |
|
| 95 |
def test_valid(self): |
| 96 |
builder = GroupBuilder('my_bar', |
| 97 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 98 |
datasets=[DatasetBuilder('data', 100, attributes={'attr2': 10})]) |
| 99 |
validator = self.vmap.get_validator('Bar') |
| 100 |
result = validator.validate(builder) |
| 101 |
self.assertEqual(len(result), 0) |
| 102 |
|
| 103 |
|
| 104 |
class TestDateTimeInSpec(ValidatorTestBase): |
| 105 |
|
| 106 |
def getSpecs(self): |
| 107 |
ret = GroupSpec( |
| 108 |
'A test group specification with a data type', |
| 109 |
data_type_def='Bar', |
| 110 |
datasets=[ |
| 111 |
DatasetSpec( |
| 112 |
'an example dataset', |
| 113 |
'int', |
| 114 |
name='data', |
| 115 |
attributes=[AttributeSpec('attr2', 'an example integer attribute', 'int')] |
| 116 |
), |
| 117 |
DatasetSpec('an example time dataset', 'isodatetime', name='datetime'), |
| 118 |
DatasetSpec('an example time dataset', 'isodatetime', name='date', quantity='?'), |
| 119 |
DatasetSpec('an array of times', 'isodatetime', name='time_array', dims=('num_times',), shape=(None,)), |
| 120 |
DatasetSpec( |
| 121 |
doc='an array with compound dtype that includes an isodatetime', |
| 122 |
dtype=[ |
| 123 |
DtypeSpec('x', doc='x', dtype='int'), |
| 124 |
DtypeSpec('y', doc='y', dtype='isodatetime'), |
| 125 |
], |
| 126 |
name='cpd_array', |
| 127 |
dims=('num_times',), |
| 128 |
shape=(None,), |
| 129 |
quantity="?", |
| 130 |
), |
| 131 |
], |
| 132 |
attributes=[AttributeSpec('attr1', 'an example string attribute', 'text')]) |
| 133 |
return ret, |
| 134 |
|
| 135 |
def test_valid_isodatetime(self): |
| 136 |
builder = GroupBuilder( |
| 137 |
'my_bar', |
| 138 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 139 |
datasets=[ |
| 140 |
DatasetBuilder('data', 100, attributes={'attr2': 10}), |
| 141 |
DatasetBuilder('datetime', datetime(2017, 5, 1, 12, 0, 0)), |
| 142 |
DatasetBuilder('date', date(2017, 5, 1)), |
| 143 |
DatasetBuilder('time_array', [datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())]), |
| 144 |
DatasetBuilder( |
| 145 |
name='cpd_array', |
| 146 |
data=[(1, datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal()))], |
| 147 |
dtype=[ |
| 148 |
DtypeSpec('x', doc='x', dtype='int'), |
| 149 |
DtypeSpec('y', doc='y', dtype='isodatetime'), |
| 150 |
], |
| 151 |
), |
| 152 |
] |
| 153 |
) |
| 154 |
validator = self.vmap.get_validator('Bar') |
| 155 |
result = validator.validate(builder) |
| 156 |
self.assertEqual(len(result), 0) |
| 157 |
|
| 158 |
def test_invalid_isodatetime(self): |
| 159 |
builder = GroupBuilder( |
| 160 |
'my_bar', |
| 161 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 162 |
datasets=[ |
| 163 |
DatasetBuilder('data', 100, attributes={'attr2': 10}), |
| 164 |
DatasetBuilder('datetime', 100), |
| 165 |
DatasetBuilder('time_array', [datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())]), |
| 166 |
] |
| 167 |
) |
| 168 |
validator = self.vmap.get_validator('Bar') |
| 169 |
result = validator.validate(builder) |
| 170 |
self.assertEqual(len(result), 1) |
| 171 |
self.assertValidationError(result[0], DtypeError, name='Bar/datetime') |
| 172 |
|
| 173 |
def test_invalid_isodatetime_array(self): |
| 174 |
builder = GroupBuilder( |
| 175 |
'my_bar', |
| 176 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 177 |
datasets=[ |
| 178 |
DatasetBuilder('data', 100, attributes={'attr2': 10}), |
| 179 |
DatasetBuilder('datetime', datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())), |
| 180 |
DatasetBuilder('time_array', datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())), |
| 181 |
], |
| 182 |
) |
| 183 |
validator = self.vmap.get_validator('Bar') |
| 184 |
result = validator.validate(builder) |
| 185 |
self.assertEqual(len(result), 1) |
| 186 |
self.assertValidationError(result[0], ExpectedArrayError, name='Bar/time_array') |
| 187 |
|
| 188 |
def test_invalid_cpd_isodatetime_array(self): |
| 189 |
builder = GroupBuilder( |
| 190 |
'my_bar', |
| 191 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 192 |
datasets=[ |
| 193 |
DatasetBuilder('data', 100, attributes={'attr2': 10}), |
| 194 |
DatasetBuilder('datetime', datetime(2017, 5, 1, 12, 0, 0)), |
| 195 |
DatasetBuilder('date', date(2017, 5, 1)), |
| 196 |
DatasetBuilder('time_array', [datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())]), |
| 197 |
DatasetBuilder( |
| 198 |
name='cpd_array', |
| 199 |
data=[(1, "wrong")], |
| 200 |
dtype=[ |
| 201 |
DtypeSpec('x', doc='x', dtype='int'), |
| 202 |
DtypeSpec('y', doc='y', dtype='isodatetime'), |
| 203 |
], |
| 204 |
), |
| 205 |
], |
| 206 |
) |
| 207 |
validator = self.vmap.get_validator('Bar') |
| 208 |
result = validator.validate(builder) |
| 209 |
self.assertEqual(len(result), 1) |
| 210 |
self.assertValidationError(result[0], DtypeError, name='Bar/cpd_array') |
| 211 |
|
| 212 |
|
| 213 |
class TestNestedTypes(ValidatorTestBase): |
| 214 |
|
| 215 |
def getSpecs(self): |
| 216 |
baz = DatasetSpec('A dataset with a data type', 'int', data_type_def='Baz', |
| 217 |
attributes=[AttributeSpec('attr2', 'an example integer attribute', 'int')]) |
| 218 |
bar = GroupSpec('A test group specification with a data type', |
| 219 |
data_type_def='Bar', |
| 220 |
datasets=[DatasetSpec('an example dataset', data_type_inc='Baz')], |
| 221 |
attributes=[AttributeSpec('attr1', 'an example string attribute', 'text')]) |
| 222 |
foo = GroupSpec('A test group that contains a data type', |
| 223 |
data_type_def='Foo', |
| 224 |
groups=[GroupSpec('A Bar group for Foos', name='my_bar', data_type_inc='Bar')], |
| 225 |
attributes=[AttributeSpec('foo_attr', 'a string attribute specified as text', 'text', |
| 226 |
required=False)]) |
| 227 |
|
| 228 |
return (bar, foo, baz) |
| 229 |
|
| 230 |
def test_invalid_missing_named_req_group(self): |
| 231 |
"""Test that a MissingDataType is returned when a required named nested data type is missing.""" |
| 232 |
foo_builder = GroupBuilder('my_foo', attributes={'data_type': 'Foo', |
| 233 |
'foo_attr': 'example Foo object'}) |
| 234 |
results = self.vmap.validate(foo_builder) |
| 235 |
self.assertEqual(len(results), 1) |
| 236 |
self.assertValidationError(results[0], MissingDataType, name='Foo', |
| 237 |
reason='missing data type Bar (my_bar)') |
| 238 |
|
| 239 |
def test_invalid_wrong_name_req_type(self): |
| 240 |
"""Test that a MissingDataType is returned when a required nested data type is given the wrong name.""" |
| 241 |
bar_builder = GroupBuilder('bad_bar_name', |
| 242 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 243 |
datasets=[DatasetBuilder('data', 100, attributes={'attr2': 10})]) |
| 244 |
|
| 245 |
foo_builder = GroupBuilder('my_foo', |
| 246 |
attributes={'data_type': 'Foo', 'foo_attr': 'example Foo object'}, |
| 247 |
groups=[bar_builder]) |
| 248 |
|
| 249 |
results = self.vmap.validate(foo_builder) |
| 250 |
self.assertEqual(len(results), 1) |
| 251 |
self.assertValidationError(results[0], MissingDataType, name='Foo') |
| 252 |
self.assertEqual(results[0].data_type, 'Bar') |
| 253 |
|
| 254 |
def test_invalid_missing_unnamed_req_group(self): |
| 255 |
"""Test that a MissingDataType is returned when a required unnamed nested data type is missing.""" |
| 256 |
bar_builder = GroupBuilder('my_bar', |
| 257 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}) |
| 258 |
|
| 259 |
foo_builder = GroupBuilder('my_foo', |
| 260 |
attributes={'data_type': 'Foo', 'foo_attr': 'example Foo object'}, |
| 261 |
groups=[bar_builder]) |
| 262 |
|
| 263 |
results = self.vmap.validate(foo_builder) |
| 264 |
self.assertEqual(len(results), 1) |
| 265 |
self.assertValidationError(results[0], MissingDataType, name='Bar', |
| 266 |
reason='missing data type Baz') |
| 267 |
|
| 268 |
def test_valid(self): |
| 269 |
"""Test that no errors are returned when nested data types are correctly built.""" |
| 270 |
bar_builder = GroupBuilder('my_bar', |
| 271 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 272 |
datasets=[DatasetBuilder('data', 100, attributes={'data_type': 'Baz', 'attr2': 10})]) |
| 273 |
|
| 274 |
foo_builder = GroupBuilder('my_foo', |
| 275 |
attributes={'data_type': 'Foo', 'foo_attr': 'example Foo object'}, |
| 276 |
groups=[bar_builder]) |
| 277 |
|
| 278 |
results = self.vmap.validate(foo_builder) |
| 279 |
self.assertEqual(len(results), 0) |
| 280 |
|
| 281 |
def test_valid_wo_opt_attr(self): |
| 282 |
""""Test that no errors are returned when an optional attribute is omitted from a group.""" |
| 283 |
bar_builder = GroupBuilder('my_bar', |
| 284 |
attributes={'data_type': 'Bar', 'attr1': 'a string attribute'}, |
| 285 |
datasets=[DatasetBuilder('data', 100, attributes={'data_type': 'Baz', 'attr2': 10})]) |
| 286 |
foo_builder = GroupBuilder('my_foo', |
| 287 |
attributes={'data_type': 'Foo'}, |
| 288 |
groups=[bar_builder]) |
| 289 |
|
| 290 |
results = self.vmap.validate(foo_builder) |
| 291 |
self.assertEqual(len(results), 0) |
| 292 |
|
| 293 |
|
| 294 |
class TestQuantityValidation(TestCase): |
| 295 |
|
| 296 |
def create_test_specs(self, q_groups, q_datasets, q_links): |
| 297 |
bar = GroupSpec('A test group', data_type_def='Bar') |
| 298 |
baz = DatasetSpec('A test dataset', 'int', data_type_def='Baz') |
| 299 |
qux = GroupSpec('A group to link', data_type_def='Qux') |
| 300 |
foo = GroupSpec('A group containing a quantity of tests and datasets', |
| 301 |
data_type_def='Foo', |
| 302 |
groups=[GroupSpec('A bar', data_type_inc='Bar', quantity=q_groups)], |
| 303 |
datasets=[DatasetSpec('A baz', data_type_inc='Baz', quantity=q_datasets)], |
| 304 |
links=[LinkSpec('A qux', target_type='Qux', quantity=q_links)],) |
| 305 |
return (bar, foo, baz, qux) |
| 306 |
|
| 307 |
def configure_specs(self, specs): |
| 308 |
spec_catalog = SpecCatalog() |
| 309 |
for spec in specs: |
| 310 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 311 |
self.namespace = SpecNamespace( |
| 312 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 313 |
self.vmap = ValidatorMap(self.namespace) |
| 314 |
|
| 315 |
def get_test_builder(self, n_groups, n_datasets, n_links): |
| 316 |
child_groups = [GroupBuilder(f'bar_{n}', attributes={'data_type': 'Bar'}) for n in range(n_groups)] |
| 317 |
child_datasets = [DatasetBuilder(f'baz_{n}', n, attributes={'data_type': 'Baz'}) for n in range(n_datasets)] |
| 318 |
child_links = [LinkBuilder(GroupBuilder(f'qux_{n}', attributes={'data_type': 'Qux'}), f'qux_{n}_link') |
| 319 |
for n in range(n_links)] |
| 320 |
return GroupBuilder('my_foo', attributes={'data_type': 'Foo'}, |
| 321 |
groups=child_groups, datasets=child_datasets, links=child_links) |
| 322 |
|
| 323 |
def test_valid_zero_or_many(self): |
| 324 |
""""Verify that groups/datasets/links with ZERO_OR_MANY and a valid quantity correctly pass validation""" |
| 325 |
specs = self.create_test_specs(q_groups=ZERO_OR_MANY, q_datasets=ZERO_OR_MANY, q_links=ZERO_OR_MANY) |
| 326 |
self.configure_specs(specs) |
| 327 |
for n in [0, 1, 2, 5]: |
| 328 |
with self.subTest(quantity=n): |
| 329 |
builder = self.get_test_builder(n_groups=n, n_datasets=n, n_links=n) |
| 330 |
results = self.vmap.validate(builder) |
| 331 |
self.assertEqual(len(results), 0) |
| 332 |
|
| 333 |
def test_valid_one_or_many(self): |
| 334 |
""""Verify that groups/datasets/links with ONE_OR_MANY and a valid quantity correctly pass validation""" |
| 335 |
specs = self.create_test_specs(q_groups=ONE_OR_MANY, q_datasets=ONE_OR_MANY, q_links=ONE_OR_MANY) |
| 336 |
self.configure_specs(specs) |
| 337 |
for n in [1, 2, 5]: |
| 338 |
with self.subTest(quantity=n): |
| 339 |
builder = self.get_test_builder(n_groups=n, n_datasets=n, n_links=n) |
| 340 |
results = self.vmap.validate(builder) |
| 341 |
self.assertEqual(len(results), 0) |
| 342 |
|
| 343 |
def test_valid_zero_or_one(self): |
| 344 |
""""Verify that groups/datasets/links with ZERO_OR_ONE and a valid quantity correctly pass validation""" |
| 345 |
specs = self.create_test_specs(q_groups=ZERO_OR_ONE, q_datasets=ZERO_OR_ONE, q_links=ZERO_OR_ONE) |
| 346 |
self.configure_specs(specs) |
| 347 |
for n in [0, 1]: |
| 348 |
with self.subTest(quantity=n): |
| 349 |
builder = self.get_test_builder(n_groups=n, n_datasets=n, n_links=n) |
| 350 |
results = self.vmap.validate(builder) |
| 351 |
self.assertEqual(len(results), 0) |
| 352 |
|
| 353 |
def test_valid_fixed_quantity(self): |
| 354 |
""""Verify that groups/datasets/links with a correct fixed quantity correctly pass validation""" |
| 355 |
self.configure_specs(self.create_test_specs(q_groups=2, q_datasets=3, q_links=5)) |
| 356 |
builder = self.get_test_builder(n_groups=2, n_datasets=3, n_links=5) |
| 357 |
results = self.vmap.validate(builder) |
| 358 |
self.assertEqual(len(results), 0) |
| 359 |
|
| 360 |
def test_missing_one_or_many_should_not_return_incorrect_quantity_error(self): |
| 361 |
"""Verify that missing ONE_OR_MANY groups/datasets/links should not return an IncorrectQuantityError |
| 362 |
|
| 363 |
NOTE: a MissingDataType error should be returned instead |
| 364 |
""" |
| 365 |
specs = self.create_test_specs(q_groups=ONE_OR_MANY, q_datasets=ONE_OR_MANY, q_links=ONE_OR_MANY) |
| 366 |
self.configure_specs(specs) |
| 367 |
builder = self.get_test_builder(n_groups=0, n_datasets=0, n_links=0) |
| 368 |
results = self.vmap.validate(builder) |
| 369 |
self.assertFalse(any(isinstance(e, IncorrectQuantityError) for e in results)) |
| 370 |
|
| 371 |
def test_missing_fixed_quantity_should_not_return_incorrect_quantity_error(self): |
| 372 |
"""Verify that missing groups/datasets/links should not return an IncorrectQuantityError""" |
| 373 |
self.configure_specs(self.create_test_specs(q_groups=5, q_datasets=3, q_links=2)) |
| 374 |
builder = self.get_test_builder(0, 0, 0) |
| 375 |
results = self.vmap.validate(builder) |
| 376 |
self.assertFalse(any(isinstance(e, IncorrectQuantityError) for e in results)) |
| 377 |
|
| 378 |
def test_incorrect_fixed_quantity_should_return_incorrect_quantity_error(self): |
| 379 |
"""Verify that an incorrect quantity of groups/datasets/links should return an IncorrectQuantityError""" |
| 380 |
self.configure_specs(self.create_test_specs(q_groups=5, q_datasets=5, q_links=5)) |
| 381 |
for n in [1, 2, 10]: |
| 382 |
with self.subTest(quantity=n): |
| 383 |
builder = self.get_test_builder(n_groups=n, n_datasets=n, n_links=n) |
| 384 |
results = self.vmap.validate(builder) |
| 385 |
self.assertEqual(len(results), 3) |
| 386 |
self.assertTrue(all(isinstance(e, IncorrectQuantityError) for e in results)) |
| 387 |
|
| 388 |
def test_incorrect_zero_or_one_quantity_should_return_incorrect_quantity_error(self): |
| 389 |
"""Verify that an incorrect ZERO_OR_ONE quantity of groups/datasets/links should return |
| 390 |
an IncorrectQuantityError |
| 391 |
""" |
| 392 |
specs = self.create_test_specs(q_groups=ZERO_OR_ONE, q_datasets=ZERO_OR_ONE, q_links=ZERO_OR_ONE) |
| 393 |
self.configure_specs(specs) |
| 394 |
builder = self.get_test_builder(n_groups=2, n_datasets=2, n_links=2) |
| 395 |
results = self.vmap.validate(builder) |
| 396 |
self.assertEqual(len(results), 3) |
| 397 |
self.assertTrue(all(isinstance(e, IncorrectQuantityError) for e in results)) |
| 398 |
|
| 399 |
def test_incorrect_quantity_error_message(self): |
| 400 |
"""Verify that an IncorrectQuantityError includes the expected information in the message""" |
| 401 |
specs = self.create_test_specs(q_groups=2, q_datasets=ZERO_OR_MANY, q_links=ZERO_OR_MANY) |
| 402 |
self.configure_specs(specs) |
| 403 |
builder = self.get_test_builder(n_groups=7, n_datasets=0, n_links=0) |
| 404 |
results = self.vmap.validate(builder) |
| 405 |
self.assertEqual(len(results), 1) |
| 406 |
self.assertIsInstance(results[0], IncorrectQuantityError) |
| 407 |
message = str(results[0]) |
| 408 |
self.assertTrue('expected a quantity of 2' in message) |
| 409 |
self.assertTrue('received 7' in message) |
| 410 |
|
| 411 |
|
| 412 |
class TestDtypeValidation(TestCase): |
| 413 |
|
| 414 |
def set_up_spec(self, dtype): |
| 415 |
spec_catalog = SpecCatalog() |
| 416 |
spec = GroupSpec('A test group specification with a data type', |
| 417 |
data_type_def='Bar', |
| 418 |
datasets=[DatasetSpec('an example dataset', dtype, name='data')], |
| 419 |
attributes=[AttributeSpec('attr1', 'an example attribute', dtype)]) |
| 420 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 421 |
self.namespace = SpecNamespace( |
| 422 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 423 |
self.vmap = ValidatorMap(self.namespace) |
| 424 |
|
| 425 |
def test_ascii_for_utf8(self): |
| 426 |
"""Test that validator allows ASCII data where UTF8 is specified.""" |
| 427 |
self.set_up_spec('text') |
| 428 |
value = b'an ascii string' |
| 429 |
bar_builder = GroupBuilder('my_bar', |
| 430 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 431 |
datasets=[DatasetBuilder('data', value)]) |
| 432 |
results = self.vmap.validate(bar_builder) |
| 433 |
self.assertEqual(len(results), 0) |
| 434 |
|
| 435 |
def test_utf8_for_ascii(self): |
| 436 |
"""Test that validator does not allow UTF8 where ASCII is specified.""" |
| 437 |
self.set_up_spec('bytes') |
| 438 |
value = 'a utf8 string' |
| 439 |
bar_builder = GroupBuilder('my_bar', |
| 440 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 441 |
datasets=[DatasetBuilder('data', value)]) |
| 442 |
results = self.vmap.validate(bar_builder) |
| 443 |
result_strings = set([str(s) for s in results]) |
| 444 |
expected_errors = {"Bar/attr1 (my_bar.attr1): incorrect type - expected 'bytes', got 'utf'", |
| 445 |
"Bar/data (my_bar/data): incorrect type - expected 'bytes', got 'utf'"} |
| 446 |
self.assertEqual(result_strings, expected_errors) |
| 447 |
|
| 448 |
def test_int64_for_int8(self): |
| 449 |
"""Test that validator allows int64 data where int8 is specified.""" |
| 450 |
self.set_up_spec('int8') |
| 451 |
value = np.int64(1) |
| 452 |
bar_builder = GroupBuilder('my_bar', |
| 453 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 454 |
datasets=[DatasetBuilder('data', value)]) |
| 455 |
results = self.vmap.validate(bar_builder) |
| 456 |
self.assertEqual(len(results), 0) |
| 457 |
|
| 458 |
def test_int8_for_int64(self): |
| 459 |
"""Test that validator does not allow int8 data where int64 is specified.""" |
| 460 |
self.set_up_spec('int64') |
| 461 |
value = np.int8(1) |
| 462 |
bar_builder = GroupBuilder('my_bar', |
| 463 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 464 |
datasets=[DatasetBuilder('data', value)]) |
| 465 |
results = self.vmap.validate(bar_builder) |
| 466 |
result_strings = set([str(s) for s in results]) |
| 467 |
expected_errors = {"Bar/attr1 (my_bar.attr1): incorrect type - expected 'int64', got 'int8'", |
| 468 |
"Bar/data (my_bar/data): incorrect type - expected 'int64', got 'int8'"} |
| 469 |
self.assertEqual(result_strings, expected_errors) |
| 470 |
|
| 471 |
def test_int64_for_numeric(self): |
| 472 |
"""Test that validator allows int64 data where numeric is specified.""" |
| 473 |
self.set_up_spec('numeric') |
| 474 |
value = np.int64(1) |
| 475 |
bar_builder = GroupBuilder('my_bar', |
| 476 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 477 |
datasets=[DatasetBuilder('data', value)]) |
| 478 |
results = self.vmap.validate(bar_builder) |
| 479 |
self.assertEqual(len(results), 0) |
| 480 |
|
| 481 |
def test_bool_for_numeric(self): |
| 482 |
"""Test that validator does not allow bool data where numeric is specified.""" |
| 483 |
self.set_up_spec('numeric') |
| 484 |
value = True |
| 485 |
bar_builder = GroupBuilder('my_bar', |
| 486 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 487 |
datasets=[DatasetBuilder('data', value)]) |
| 488 |
results = self.vmap.validate(bar_builder) |
| 489 |
result_strings = set([str(s) for s in results]) |
| 490 |
expected_errors = {"Bar/attr1 (my_bar.attr1): incorrect type - expected 'numeric', got 'bool'", |
| 491 |
"Bar/data (my_bar/data): incorrect type - expected 'numeric', got 'bool'"} |
| 492 |
self.assertEqual(result_strings, expected_errors) |
| 493 |
|
| 494 |
def test_np_bool_for_bool(self): |
| 495 |
"""Test that validator allows np.bool_ data where bool is specified.""" |
| 496 |
self.set_up_spec('bool') |
| 497 |
value = np.bool_(True) |
| 498 |
bar_builder = GroupBuilder('my_bar', |
| 499 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 500 |
datasets=[DatasetBuilder('data', value)]) |
| 501 |
results = self.vmap.validate(bar_builder) |
| 502 |
self.assertEqual(len(results), 0) |
| 503 |
|
| 504 |
def test_scalar_compound_dtype(self): |
| 505 |
"""Test that validator allows scalar compound dtype data where a compound dtype is specified.""" |
| 506 |
spec_catalog = SpecCatalog() |
| 507 |
dtype = [DtypeSpec('x', doc='x', dtype='int'), DtypeSpec('y', doc='y', dtype='float')] |
| 508 |
spec = GroupSpec('A test group specification with a data type', |
| 509 |
data_type_def='Bar', |
| 510 |
datasets=[DatasetSpec('an example dataset', dtype, name='data',)], |
| 511 |
attributes=[AttributeSpec('attr1', 'an example attribute', 'text',)]) |
| 512 |
spec_catalog.register_spec(spec, 'test2.yaml') |
| 513 |
self.namespace = SpecNamespace( |
| 514 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test2.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 515 |
self.vmap = ValidatorMap(self.namespace) |
| 516 |
|
| 517 |
value = np.array((1, 2.2), dtype=[('x', 'int'), ('y', 'float')]) |
| 518 |
bar_builder = GroupBuilder('my_bar', |
| 519 |
attributes={'data_type': 'Bar', 'attr1': 'test'}, |
| 520 |
datasets=[DatasetBuilder(name='data', |
| 521 |
data=value, |
| 522 |
dtype=[DtypeSpec('x', doc='x', dtype='int'), |
| 523 |
DtypeSpec('y', doc='y', dtype='float'),],),]) |
| 524 |
results = self.vmap.validate(bar_builder) |
| 525 |
self.assertEqual(len(results), 0) |
| 526 |
|
| 527 |
class Test1DArrayValidation(TestCase): |
| 528 |
|
| 529 |
def set_up_spec(self, dtype): |
| 530 |
spec_catalog = SpecCatalog() |
| 531 |
spec = GroupSpec('A test group specification with a data type', |
| 532 |
data_type_def='Bar', |
| 533 |
datasets=[DatasetSpec('an example dataset', dtype, name='data', shape=(None, ))], |
| 534 |
attributes=[AttributeSpec('attr1', 'an example attribute', dtype, shape=(None, ))]) |
| 535 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 536 |
self.namespace = SpecNamespace( |
| 537 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 538 |
self.vmap = ValidatorMap(self.namespace) |
| 539 |
|
| 540 |
def test_scalar(self): |
| 541 |
"""Test that validator does not allow a scalar where an array is specified.""" |
| 542 |
self.set_up_spec('text') |
| 543 |
value = 'a string' |
| 544 |
bar_builder = GroupBuilder('my_bar', |
| 545 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 546 |
datasets=[DatasetBuilder('data', value)]) |
| 547 |
results = self.vmap.validate(bar_builder) |
| 548 |
result_strings = set([str(s) for s in results]) |
| 549 |
expected_errors = {("Bar/attr1 (my_bar.attr1): incorrect shape - expected an array of shape '(None,)', " |
| 550 |
"got non-array data 'a string'"), |
| 551 |
("Bar/data (my_bar/data): incorrect shape - expected an array of shape '(None,)', " |
| 552 |
"got non-array data 'a string'")} |
| 553 |
self.assertEqual(result_strings, expected_errors) |
| 554 |
|
| 555 |
def test_empty_list(self): |
| 556 |
"""Test that validator allows an empty list where an array is specified.""" |
| 557 |
self.set_up_spec('text') |
| 558 |
value = [] |
| 559 |
bar_builder = GroupBuilder('my_bar', |
| 560 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 561 |
datasets=[DatasetBuilder('data', value)]) |
| 562 |
results = self.vmap.validate(bar_builder) |
| 563 |
self.assertEqual(len(results), 0) |
| 564 |
|
| 565 |
def test_empty_nparray(self): |
| 566 |
"""Test that validator allows an empty numpy array where an array is specified.""" |
| 567 |
self.set_up_spec('text') |
| 568 |
value = np.array([]) # note: dtype is float64 |
| 569 |
bar_builder = GroupBuilder('my_bar', |
| 570 |
attributes={'data_type': 'Bar', 'attr1': value}, |
| 571 |
datasets=[DatasetBuilder('data', value)]) |
| 572 |
results = self.vmap.validate(bar_builder) |
| 573 |
self.assertEqual(len(results), 0) |
| 574 |
|
| 575 |
# TODO test shape validation more completely |
| 576 |
|
| 577 |
|
| 578 |
class TestStringDatetime(TestCase): |
| 579 |
|
| 580 |
def test_str_coincidental_isodatetime(self): |
| 581 |
"""Test validation of a text spec allows a string that coincidentally matches the isodatetime format.""" |
| 582 |
spec_catalog = SpecCatalog() |
| 583 |
spec = GroupSpec( |
| 584 |
doc='A test group specification with a data type', |
| 585 |
data_type_def='Bar', |
| 586 |
datasets=[ |
| 587 |
DatasetSpec(doc='an example scalar dataset', dtype="text", name='data1'), |
| 588 |
DatasetSpec(doc='an example 1D dataset', dtype="text", name='data2', shape=(None, )), |
| 589 |
DatasetSpec( |
| 590 |
doc='an example 1D compound dtype dataset', |
| 591 |
dtype=[ |
| 592 |
DtypeSpec('x', doc='x', dtype='int'), |
| 593 |
DtypeSpec('y', doc='y', dtype='text'), |
| 594 |
], |
| 595 |
name='data3', |
| 596 |
shape=(None, ), |
| 597 |
), |
| 598 |
], |
| 599 |
attributes=[ |
| 600 |
AttributeSpec(name='attr1', doc='an example scalar attribute', dtype="text"), |
| 601 |
AttributeSpec(name='attr2', doc='an example 1D attribute', dtype="text", shape=(None, )), |
| 602 |
] |
| 603 |
) |
| 604 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 605 |
namespace = SpecNamespace( |
| 606 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog |
| 607 |
) |
| 608 |
vmap = ValidatorMap(namespace) |
| 609 |
|
| 610 |
bar_builder = GroupBuilder( |
| 611 |
name='my_bar', |
| 612 |
attributes={'data_type': 'Bar', 'attr1': "2023-01-01", 'attr2': ["2023-01-01"]}, |
| 613 |
datasets=[ |
| 614 |
DatasetBuilder(name='data1', data="2023-01-01"), |
| 615 |
DatasetBuilder(name='data2', data=["2023-01-01"]), |
| 616 |
DatasetBuilder( |
| 617 |
name='data3', |
| 618 |
data=[(1, "2023-01-01")], |
| 619 |
dtype=[ |
| 620 |
DtypeSpec('x', doc='x', dtype='int'), |
| 621 |
DtypeSpec('y', doc='y', dtype='text'), |
| 622 |
], |
| 623 |
), |
| 624 |
], |
| 625 |
) |
| 626 |
results = vmap.validate(bar_builder) |
| 627 |
self.assertEqual(len(results), 0) |
| 628 |
|
| 629 |
|
| 630 |
class TestLinkable(TestCase): |
| 631 |
|
| 632 |
def set_up_spec(self): |
| 633 |
spec_catalog = SpecCatalog() |
| 634 |
typed_dataset_spec = DatasetSpec('A typed dataset', data_type_def='Foo') |
| 635 |
typed_group_spec = GroupSpec('A typed group', data_type_def='Bar') |
| 636 |
spec = GroupSpec('A test group specification with a data type', |
| 637 |
data_type_def='Baz', |
| 638 |
datasets=[ |
| 639 |
DatasetSpec('A linkable child dataset', name='untyped_linkable_ds', |
| 640 |
linkable=True, quantity=ZERO_OR_ONE), |
| 641 |
DatasetSpec('A non-linkable child dataset', name='untyped_nonlinkable_ds', |
| 642 |
linkable=False, quantity=ZERO_OR_ONE), |
| 643 |
DatasetSpec('A linkable child dataset', data_type_inc='Foo', |
| 644 |
name='typed_linkable_ds', linkable=True, quantity=ZERO_OR_ONE), |
| 645 |
DatasetSpec('A non-linkable child dataset', data_type_inc='Foo', |
| 646 |
name='typed_nonlinkable_ds', linkable=False, quantity=ZERO_OR_ONE), |
| 647 |
], |
| 648 |
groups=[ |
| 649 |
GroupSpec('A linkable child group', name='untyped_linkable_group', |
| 650 |
linkable=True, quantity=ZERO_OR_ONE), |
| 651 |
GroupSpec('A non-linkable child group', name='untyped_nonlinkable_group', |
| 652 |
linkable=False, quantity=ZERO_OR_ONE), |
| 653 |
GroupSpec('A linkable child group', data_type_inc='Bar', |
| 654 |
name='typed_linkable_group', linkable=True, quantity=ZERO_OR_ONE), |
| 655 |
GroupSpec('A non-linkable child group', data_type_inc='Bar', |
| 656 |
name='typed_nonlinkable_group', linkable=False, quantity=ZERO_OR_ONE), |
| 657 |
]) |
| 658 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 659 |
spec_catalog.register_spec(typed_dataset_spec, 'test.yaml') |
| 660 |
spec_catalog.register_spec(typed_group_spec, 'test.yaml') |
| 661 |
self.namespace = SpecNamespace( |
| 662 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 663 |
self.vmap = ValidatorMap(self.namespace) |
| 664 |
|
| 665 |
def validate_linkability(self, link, expect_error): |
| 666 |
"""Execute a linkability test and assert whether or not an IllegalLinkError is returned""" |
| 667 |
self.set_up_spec() |
| 668 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, links=[link]) |
| 669 |
result = self.vmap.validate(builder) |
| 670 |
if expect_error: |
| 671 |
self.assertEqual(len(result), 1) |
| 672 |
self.assertIsInstance(result[0], IllegalLinkError) |
| 673 |
else: |
| 674 |
self.assertEqual(len(result), 0) |
| 675 |
|
| 676 |
def test_untyped_linkable_dataset_accepts_link(self): |
| 677 |
"""Test that the validator accepts a link when the spec has an untyped linkable dataset""" |
| 678 |
link = LinkBuilder(name='untyped_linkable_ds', builder=DatasetBuilder('foo')) |
| 679 |
self.validate_linkability(link, expect_error=False) |
| 680 |
|
| 681 |
def test_untyped_nonlinkable_dataset_does_not_accept_link(self): |
| 682 |
"""Test that the validator returns an IllegalLinkError when the spec has an untyped non-linkable dataset""" |
| 683 |
link = LinkBuilder(name='untyped_nonlinkable_ds', builder=DatasetBuilder('foo')) |
| 684 |
self.validate_linkability(link, expect_error=True) |
| 685 |
|
| 686 |
def test_typed_linkable_dataset_accepts_link(self): |
| 687 |
"""Test that the validator accepts a link when the spec has a typed linkable dataset""" |
| 688 |
link = LinkBuilder(name='typed_linkable_ds', |
| 689 |
builder=DatasetBuilder('foo', attributes={'data_type': 'Foo'})) |
| 690 |
self.validate_linkability(link, expect_error=False) |
| 691 |
|
| 692 |
def test_typed_nonlinkable_dataset_does_not_accept_link(self): |
| 693 |
"""Test that the validator returns an IllegalLinkError when the spec has a typed non-linkable dataset""" |
| 694 |
link = LinkBuilder(name='typed_nonlinkable_ds', |
| 695 |
builder=DatasetBuilder('foo', attributes={'data_type': 'Foo'})) |
| 696 |
self.validate_linkability(link, expect_error=True) |
| 697 |
|
| 698 |
def test_untyped_linkable_group_accepts_link(self): |
| 699 |
"""Test that the validator accepts a link when the spec has an untyped linkable group""" |
| 700 |
link = LinkBuilder(name='untyped_linkable_group', builder=GroupBuilder('foo')) |
| 701 |
self.validate_linkability(link, expect_error=False) |
| 702 |
|
| 703 |
def test_untyped_nonlinkable_group_does_not_accept_link(self): |
| 704 |
"""Test that the validator returns an IllegalLinkError when the spec has an untyped non-linkable group""" |
| 705 |
link = LinkBuilder(name='untyped_nonlinkable_group', builder=GroupBuilder('foo')) |
| 706 |
self.validate_linkability(link, expect_error=True) |
| 707 |
|
| 708 |
def test_typed_linkable_group_accepts_link(self): |
| 709 |
"""Test that the validator accepts a link when the spec has a typed linkable group""" |
| 710 |
link = LinkBuilder(name='typed_linkable_group', |
| 711 |
builder=GroupBuilder('foo', attributes={'data_type': 'Bar'})) |
| 712 |
self.validate_linkability(link, expect_error=False) |
| 713 |
|
| 714 |
def test_typed_nonlinkable_group_does_not_accept_link(self): |
| 715 |
"""Test that the validator returns an IllegalLinkError when the spec has a typed non-linkable group""" |
| 716 |
link = LinkBuilder(name='typed_nonlinkable_group', |
| 717 |
builder=GroupBuilder('foo', attributes={'data_type': 'Bar'})) |
| 718 |
self.validate_linkability(link, expect_error=True) |
| 719 |
|
| 720 |
@mock.patch("hdmf.validate.validator.DatasetValidator.validate") |
| 721 |
def test_should_not_validate_illegally_linked_objects(self, mock_validator): |
| 722 |
"""Test that an illegally linked child dataset is not validated |
| 723 |
|
| 724 |
Note: this behavior is expected to change in the future: |
| 725 |
https://github.com/hdmf-dev/hdmf/issues/516 |
| 726 |
""" |
| 727 |
self.set_up_spec() |
| 728 |
typed_link = LinkBuilder(name='typed_nonlinkable_ds', |
| 729 |
builder=DatasetBuilder('foo', attributes={'data_type': 'Foo'})) |
| 730 |
untyped_link = LinkBuilder(name='untyped_nonlinkable_ds', builder=DatasetBuilder('foo')) |
| 731 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, links=[typed_link, untyped_link]) |
| 732 |
_ = self.vmap.validate(builder) |
| 733 |
assert not mock_validator.called |
| 734 |
|
| 735 |
|
| 736 |
class TestMultipleNamedChildrenOfSameType(TestCase): |
| 737 |
"""When a group has multiple named children of the same type (such as X, Y, |
| 738 |
and Z VectorData), they all need to be validated. |
| 739 |
""" |
| 740 |
|
| 741 |
def set_up_spec(self): |
| 742 |
spec_catalog = SpecCatalog() |
| 743 |
dataset_spec = DatasetSpec('A dataset', data_type_def='Foo') |
| 744 |
group_spec = GroupSpec('A group', data_type_def='Bar') |
| 745 |
spec = GroupSpec('A test group specification with a data type', |
| 746 |
data_type_def='Baz', |
| 747 |
datasets=[ |
| 748 |
DatasetSpec('Child Dataset A', name='a', data_type_inc='Foo'), |
| 749 |
DatasetSpec('Child Dataset B', name='b', data_type_inc='Foo'), |
| 750 |
], |
| 751 |
groups=[ |
| 752 |
GroupSpec('Child Group X', name='x', data_type_inc='Bar'), |
| 753 |
GroupSpec('Child Group Y', name='y', data_type_inc='Bar'), |
| 754 |
]) |
| 755 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 756 |
spec_catalog.register_spec(dataset_spec, 'test.yaml') |
| 757 |
spec_catalog.register_spec(group_spec, 'test.yaml') |
| 758 |
self.namespace = SpecNamespace( |
| 759 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 760 |
self.vmap = ValidatorMap(self.namespace) |
| 761 |
|
| 762 |
def validate_multiple_children(self, dataset_names, group_names): |
| 763 |
"""Utility function to validate a builder with the specified named dataset and group children""" |
| 764 |
self.set_up_spec() |
| 765 |
datasets = [DatasetBuilder(ds, attributes={'data_type': 'Foo'}) for ds in dataset_names] |
| 766 |
groups = [GroupBuilder(gr, attributes={'data_type': 'Bar'}) for gr in group_names] |
| 767 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, |
| 768 |
datasets=datasets, groups=groups) |
| 769 |
return self.vmap.validate(builder) |
| 770 |
|
| 771 |
def test_missing_first_dataset_should_return_error(self): |
| 772 |
"""Test that the validator returns a MissingDataType error if the first dataset is missing""" |
| 773 |
result = self.validate_multiple_children(['b'], ['x', 'y']) |
| 774 |
self.assertEqual(len(result), 1) |
| 775 |
self.assertIsInstance(result[0], MissingDataType) |
| 776 |
|
| 777 |
def test_missing_last_dataset_should_return_error(self): |
| 778 |
"""Test that the validator returns a MissingDataType error if the last dataset is missing""" |
| 779 |
result = self.validate_multiple_children(['a'], ['x', 'y']) |
| 780 |
self.assertEqual(len(result), 1) |
| 781 |
self.assertIsInstance(result[0], MissingDataType) |
| 782 |
|
| 783 |
def test_missing_first_group_should_return_error(self): |
| 784 |
"""Test that the validator returns a MissingDataType error if the first group is missing""" |
| 785 |
result = self.validate_multiple_children(['a', 'b'], ['y']) |
| 786 |
self.assertEqual(len(result), 1) |
| 787 |
self.assertIsInstance(result[0], MissingDataType) |
| 788 |
|
| 789 |
def test_missing_last_group_should_return_error(self): |
| 790 |
"""Test that the validator returns a MissingDataType error if the last group is missing""" |
| 791 |
result = self.validate_multiple_children(['a', 'b'], ['x']) |
| 792 |
self.assertEqual(len(result), 1) |
| 793 |
self.assertIsInstance(result[0], MissingDataType) |
| 794 |
|
| 795 |
def test_no_errors_when_all_children_satisfied(self): |
| 796 |
"""Test that the validator does not return an error if all child specs are satisfied""" |
| 797 |
result = self.validate_multiple_children(['a', 'b'], ['x', 'y']) |
| 798 |
self.assertEqual(len(result), 0) |
| 799 |
|
| 800 |
|
| 801 |
class TestLinkAndChildMatchingDataType(TestCase): |
| 802 |
"""If a link and a child dataset/group have the same specified data type, |
| 803 |
both the link and the child need to be validated |
| 804 |
""" |
| 805 |
|
| 806 |
def set_up_spec(self): |
| 807 |
spec_catalog = SpecCatalog() |
| 808 |
dataset_spec = DatasetSpec('A dataset', data_type_def='Foo') |
| 809 |
group_spec = GroupSpec('A group', data_type_def='Bar') |
| 810 |
spec = GroupSpec('A test group specification with a data type', |
| 811 |
data_type_def='Baz', |
| 812 |
datasets=[ |
| 813 |
DatasetSpec('Child Dataset', name='dataset', data_type_inc='Foo'), |
| 814 |
], |
| 815 |
groups=[ |
| 816 |
GroupSpec('Child Group', name='group', data_type_inc='Bar'), |
| 817 |
], |
| 818 |
links=[ |
| 819 |
LinkSpec('Linked Dataset', name='dataset_link', target_type='Foo'), |
| 820 |
LinkSpec('Linked Dataset', name='group_link', target_type='Bar') |
| 821 |
]) |
| 822 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 823 |
spec_catalog.register_spec(dataset_spec, 'test.yaml') |
| 824 |
spec_catalog.register_spec(group_spec, 'test.yaml') |
| 825 |
self.namespace = SpecNamespace( |
| 826 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 827 |
self.vmap = ValidatorMap(self.namespace) |
| 828 |
|
| 829 |
def validate_matching_link_data_type_case(self, datasets, groups, links): |
| 830 |
"""Execute validation against a group builder using the provided group |
| 831 |
children and verify that a MissingDataType error is returned |
| 832 |
""" |
| 833 |
self.set_up_spec() |
| 834 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, |
| 835 |
datasets=datasets, groups=groups, links=links) |
| 836 |
result = self.vmap.validate(builder) |
| 837 |
self.assertEqual(len(result), 1) |
| 838 |
self.assertIsInstance(result[0], MissingDataType) |
| 839 |
|
| 840 |
def test_error_on_missing_child_dataset(self): |
| 841 |
"""Test that a MissingDataType is returned when the child dataset is missing""" |
| 842 |
datasets = [] |
| 843 |
groups = [GroupBuilder('group', attributes={'data_type': 'Bar'})] |
| 844 |
links = [ |
| 845 |
LinkBuilder(name='dataset_link', builder=DatasetBuilder('foo', attributes={'data_type': 'Foo'})), |
| 846 |
LinkBuilder(name='group_link', builder=GroupBuilder('bar', attributes={'data_type': 'Bar'})) |
| 847 |
] |
| 848 |
self.validate_matching_link_data_type_case(datasets, groups, links) |
| 849 |
|
| 850 |
def test_error_on_missing_linked_dataset(self): |
| 851 |
"""Test that a MissingDataType is returned when the linked dataset is missing""" |
| 852 |
datasets = [DatasetBuilder('dataset', attributes={'data_type': 'Foo'})] |
| 853 |
groups = [GroupBuilder('group', attributes={'data_type': 'Bar'})] |
| 854 |
links = [ |
| 855 |
LinkBuilder(name='group_link', builder=GroupBuilder('bar', attributes={'data_type': 'Bar'})) |
| 856 |
] |
| 857 |
self.validate_matching_link_data_type_case(datasets, groups, links) |
| 858 |
|
| 859 |
def test_error_on_missing_group(self): |
| 860 |
"""Test that a MissingDataType is returned when the child group is missing""" |
| 861 |
self.set_up_spec() |
| 862 |
datasets = [DatasetBuilder('dataset', attributes={'data_type': 'Foo'})] |
| 863 |
groups = [] |
| 864 |
links = [ |
| 865 |
LinkBuilder(name='dataset_link', builder=DatasetBuilder('foo', attributes={'data_type': 'Foo'})), |
| 866 |
LinkBuilder(name='group_link', builder=GroupBuilder('bar', attributes={'data_type': 'Bar'})) |
| 867 |
] |
| 868 |
self.validate_matching_link_data_type_case(datasets, groups, links) |
| 869 |
|
| 870 |
def test_error_on_missing_linked_group(self): |
| 871 |
"""Test that a MissingDataType is returned when the linked group is missing""" |
| 872 |
self.set_up_spec() |
| 873 |
datasets = [DatasetBuilder('dataset', attributes={'data_type': 'Foo'})] |
| 874 |
groups = [GroupBuilder('group', attributes={'data_type': 'Bar'})] |
| 875 |
links = [ |
| 876 |
LinkBuilder(name='dataset_link', builder=DatasetBuilder('foo', attributes={'data_type': 'Foo'})) |
| 877 |
] |
| 878 |
self.validate_matching_link_data_type_case(datasets, groups, links) |
| 879 |
|
| 880 |
|
| 881 |
class TestMultipleChildrenAtDifferentLevelsOfInheritance(TestCase): |
| 882 |
"""When multiple children can satisfy multiple specs due to data_type |
| 883 |
inheritance, the validation needs to carefully match builders against specs |
| 884 |
""" |
| 885 |
|
| 886 |
def set_up_spec(self): |
| 887 |
spec_catalog = SpecCatalog() |
| 888 |
dataset_spec = DatasetSpec('A dataset', data_type_def='Foo') |
| 889 |
sub_dataset_spec = DatasetSpec('An Inheriting Dataset', |
| 890 |
data_type_def='Bar', data_type_inc='Foo') |
| 891 |
spec = GroupSpec('A test group specification with a data type', |
| 892 |
data_type_def='Baz', |
| 893 |
datasets=[ |
| 894 |
DatasetSpec('Child Dataset', data_type_inc='Foo'), |
| 895 |
DatasetSpec('Child Dataset', data_type_inc='Bar'), |
| 896 |
]) |
| 897 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 898 |
spec_catalog.register_spec(dataset_spec, 'test.yaml') |
| 899 |
spec_catalog.register_spec(sub_dataset_spec, 'test.yaml') |
| 900 |
self.namespace = SpecNamespace( |
| 901 |
'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 902 |
self.vmap = ValidatorMap(self.namespace) |
| 903 |
|
| 904 |
def test_error_returned_when_child_at_highest_level_missing(self): |
| 905 |
"""Test that a MissingDataType error is returned when the dataset at |
| 906 |
the highest level of the inheritance hierarchy is missing |
| 907 |
""" |
| 908 |
self.set_up_spec() |
| 909 |
datasets = [ |
| 910 |
DatasetBuilder('bar', attributes={'data_type': 'Bar'}) |
| 911 |
] |
| 912 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, datasets=datasets) |
| 913 |
result = self.vmap.validate(builder) |
| 914 |
self.assertEqual(len(result), 1) |
| 915 |
self.assertIsInstance(result[0], MissingDataType) |
| 916 |
|
| 917 |
def test_error_returned_when_child_at_lowest_level_missing(self): |
| 918 |
"""Test that a MissingDataType error is returned when the dataset at |
| 919 |
the lowest level of the inheritance hierarchy is missing |
| 920 |
""" |
| 921 |
self.set_up_spec() |
| 922 |
datasets = [ |
| 923 |
DatasetBuilder('foo', attributes={'data_type': 'Foo'}) |
| 924 |
] |
| 925 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, datasets=datasets) |
| 926 |
result = self.vmap.validate(builder) |
| 927 |
self.assertEqual(len(result), 1) |
| 928 |
self.assertIsInstance(result[0], MissingDataType) |
| 929 |
|
| 930 |
def test_both_levels_of_hierarchy_validated(self): |
| 931 |
"""Test that when both required children at separate levels of |
| 932 |
inheritance hierarchy are present, both child specs are satisfied |
| 933 |
""" |
| 934 |
self.set_up_spec() |
| 935 |
datasets = [ |
| 936 |
DatasetBuilder('foo', attributes={'data_type': 'Foo'}), |
| 937 |
DatasetBuilder('bar', attributes={'data_type': 'Bar'}) |
| 938 |
] |
| 939 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, datasets=datasets) |
| 940 |
result = self.vmap.validate(builder) |
| 941 |
self.assertEqual(len(result), 0) |
| 942 |
|
| 943 |
@skip("Functionality not yet supported") |
| 944 |
def test_both_levels_of_hierarchy_validated_inverted_order(self): |
| 945 |
"""Test that when both required children at separate levels of |
| 946 |
inheritance hierarchy are present, both child specs are satisfied. |
| 947 |
This should work no matter what the order of the builders. |
| 948 |
""" |
| 949 |
self.set_up_spec() |
| 950 |
datasets = [ |
| 951 |
DatasetBuilder('bar', attributes={'data_type': 'Bar'}), |
| 952 |
DatasetBuilder('foo', attributes={'data_type': 'Foo'}) |
| 953 |
] |
| 954 |
builder = GroupBuilder('my_baz', attributes={'data_type': 'Baz'}, datasets=datasets) |
| 955 |
result = self.vmap.validate(builder) |
| 956 |
self.assertEqual(len(result), 0) |
| 957 |
|
| 958 |
|
| 959 |
class TestExtendedIncDataTypes(TestCase): |
| 960 |
"""Test validation against specs where a data type is included via data_type_inc |
| 961 |
and modified by adding new fields or constraining existing fields but is not |
| 962 |
defined as a new type via data_type_inc. |
| 963 |
|
| 964 |
For the purpose of this test class: we are calling a data type which is nested |
| 965 |
inside a group an "inner" data type. When an inner data type inherits from a data type |
| 966 |
via data_type_inc and has fields that are either added or modified from the base |
| 967 |
data type, we are labeling that data type as an "extension". When the inner data |
| 968 |
type extension does not define a new data type via data_type_def we say that it is |
| 969 |
an "anonymous extension". |
| 970 |
|
| 971 |
Anonymous data type extensions should be avoided in for new specs, but |
| 972 |
it does occur in existing nwb |
| 973 |
specs, so we need to allow and validate against it. |
| 974 |
One example is the `Units.spike_times` dataset attached to Units in the `core` |
| 975 |
nwb namespace, which extends `VectorData` via neurodata_type_inc but adds a new |
| 976 |
attribute named `resolution` without defining a new data type via neurodata_type_def. |
| 977 |
""" |
| 978 |
|
| 979 |
def setup_spec(self): |
| 980 |
"""Prepare a set of specs for tests which includes an anonymous data type extension""" |
| 981 |
spec_catalog = SpecCatalog() |
| 982 |
attr_foo = AttributeSpec(name='foo', doc='an attribute', dtype='text') |
| 983 |
attr_bar = AttributeSpec(name='bar', doc='an attribute', dtype='numeric') |
| 984 |
d1_spec = DatasetSpec(doc='type D1', data_type_def='D1', dtype='numeric', |
| 985 |
attributes=[attr_foo]) |
| 986 |
d2_spec = DatasetSpec(doc='type D2', data_type_def='D2', data_type_inc=d1_spec) |
| 987 |
g1_spec = GroupSpec(doc='type G1', data_type_def='G1', |
| 988 |
datasets=[DatasetSpec(doc='D1 extension', data_type_inc=d1_spec, |
| 989 |
attributes=[attr_foo, attr_bar])]) |
| 990 |
for spec in [d1_spec, d2_spec, g1_spec]: |
| 991 |
spec_catalog.register_spec(spec, 'test.yaml') |
| 992 |
self.namespace = SpecNamespace('a test namespace', CORE_NAMESPACE, |
| 993 |
[{'source': 'test.yaml'}], version='0.1.0', catalog=spec_catalog) |
| 994 |
self.vmap = ValidatorMap(self.namespace) |
| 995 |
|
| 996 |
def test_missing_additional_attribute_on_anonymous_data_type_extension(self): |
| 997 |
"""Verify that a MissingError is returned when a required attribute from an |
| 998 |
anonymous extension is not present |
| 999 |
""" |
| 1000 |
self.setup_spec() |
| 1001 |
dataset = DatasetBuilder('test_d1', 42.0, attributes={'data_type': 'D1', 'foo': 'xyz'}) |
| 1002 |
builder = GroupBuilder('test_g1', attributes={'data_type': 'G1'}, datasets=[dataset]) |
| 1003 |
result = self.vmap.validate(builder) |
| 1004 |
self.assertEqual(len(result), 1) |
| 1005 |
error = result[0] |
| 1006 |
self.assertIsInstance(error, MissingError) |
| 1007 |
self.assertTrue('G1/D1/bar' in str(error)) |
| 1008 |
|
| 1009 |
def test_validate_child_type_against_anonymous_data_type_extension(self): |
| 1010 |
"""Verify that a MissingError is returned when a required attribute from an |
| 1011 |
anonymous extension is not present on a data type which inherits from the data |
| 1012 |
type included in the anonymous extension. |
| 1013 |
""" |
| 1014 |
self.setup_spec() |
| 1015 |
dataset = DatasetBuilder('test_d2', 42.0, attributes={'data_type': 'D2', 'foo': 'xyz'}) |
| 1016 |
builder = GroupBuilder('test_g1', attributes={'data_type': 'G1'}, datasets=[dataset]) |
| 1017 |
result = self.vmap.validate(builder) |
| 1018 |
self.assertEqual(len(result), 1) |
| 1019 |
error = result[0] |
| 1020 |
self.assertIsInstance(error, MissingError) |
| 1021 |
self.assertTrue('G1/D1/bar' in str(error)) |
| 1022 |
|
| 1023 |
def test_redundant_attribute_in_spec(self): |
| 1024 |
"""Test that only one MissingError is returned when an attribute is missing |
| 1025 |
which is redundantly defined in both a base data type and an inner data type |
| 1026 |
""" |
| 1027 |
self.setup_spec() |
| 1028 |
dataset = DatasetBuilder('test_d2', 42.0, attributes={'data_type': 'D2', 'bar': 5}) |
| 1029 |
builder = GroupBuilder('test_g1', attributes={'data_type': 'G1'}, datasets=[dataset]) |
| 1030 |
result = self.vmap.validate(builder) |
| 1031 |
self.assertEqual(len(result), 1) |
| 1032 |
|
| 1033 |
|
| 1034 |
class TestReferenceDatasetsRoundTrip(ValidatorTestBase): |
| 1035 |
"""Test that no errors occur when when datasets containing references either in an |
| 1036 |
array or as part of a compound type are written out to file, read back in, and |
| 1037 |
then validated. |
| 1038 |
|
| 1039 |
In order to support lazy reading on loading, datasets containing references are |
| 1040 |
wrapped in lazy-loading ReferenceResolver objects. These tests verify that the |
| 1041 |
validator can work with these ReferenceResolver objects. |
| 1042 |
""" |
| 1043 |
|
| 1044 |
def setUp(self): |
| 1045 |
self.filename = 'test_ref_dataset.h5' |
| 1046 |
super().setUp() |
| 1047 |
|
| 1048 |
def tearDown(self): |
| 1049 |
remove_test_file(self.filename) |
| 1050 |
super().tearDown() |
| 1051 |
|
| 1052 |
def getSpecs(self): |
| 1053 |
qux_spec = DatasetSpec( |
| 1054 |
doc='a simple scalar dataset', |
| 1055 |
data_type_def='Qux', |
| 1056 |
dtype='int', |
| 1057 |
shape=None |
| 1058 |
) |
| 1059 |
baz_spec = DatasetSpec( |
| 1060 |
doc='a dataset with a compound datatype that includes a reference', |
| 1061 |
data_type_def='Baz', |
| 1062 |
dtype=[ |
| 1063 |
DtypeSpec('x', doc='x-value', dtype='int'), |
| 1064 |
DtypeSpec('y', doc='y-ref', dtype=RefSpec('Qux', reftype='object')) |
| 1065 |
], |
| 1066 |
shape=None |
| 1067 |
) |
| 1068 |
bar_spec = DatasetSpec( |
| 1069 |
doc='a dataset of an array of references', |
| 1070 |
dtype=RefSpec('Qux', reftype='object'), |
| 1071 |
data_type_def='Bar', |
| 1072 |
shape=(None,) |
| 1073 |
) |
| 1074 |
foo_spec = GroupSpec( |
| 1075 |
doc='a base group for containing test datasets', |
| 1076 |
data_type_def='Foo', |
| 1077 |
datasets=[ |
| 1078 |
DatasetSpec(doc='optional Bar', data_type_inc=bar_spec, quantity=ZERO_OR_ONE), |
| 1079 |
DatasetSpec(doc='optional Baz', data_type_inc=baz_spec, quantity=ZERO_OR_ONE), |
| 1080 |
DatasetSpec(doc='multiple qux', data_type_inc=qux_spec, quantity=ONE_OR_MANY) |
| 1081 |
] |
| 1082 |
) |
| 1083 |
return (foo_spec, bar_spec, baz_spec, qux_spec) |
| 1084 |
|
| 1085 |
def runBuilderRoundTrip(self, builder): |
| 1086 |
"""Executes a round-trip test for a builder |
| 1087 |
|
| 1088 |
1. First writes the builder to file, |
| 1089 |
2. next reads a new builder from disk |
| 1090 |
3. and finally runs the builder through the validator. |
| 1091 |
The test is successful if there are no validation errors.""" |
| 1092 |
ns_catalog = NamespaceCatalog() |
| 1093 |
ns_catalog.add_namespace(self.namespace.name, self.namespace) |
| 1094 |
typemap = TypeMap(ns_catalog) |
| 1095 |
self.manager = BuildManager(typemap) |
| 1096 |
|
| 1097 |
with HDF5IO(self.filename, manager=self.manager, mode='w') as write_io: |
| 1098 |
write_io.write_builder(builder) |
| 1099 |
|
| 1100 |
with HDF5IO(self.filename, manager=self.manager, mode='r') as read_io: |
| 1101 |
read_builder = read_io.read_builder() |
| 1102 |
errors = self.vmap.validate(read_builder) |
| 1103 |
self.assertEqual(len(errors), 0, errors) |
| 1104 |
|
| 1105 |
def test_round_trip_validation_of_reference_dataset_array(self): |
| 1106 |
"""Verify that a dataset builder containing an array of references passes |
| 1107 |
validation after a round trip""" |
| 1108 |
qux1 = DatasetBuilder('q1', 5, attributes={'data_type': 'Qux'}) |
| 1109 |
qux2 = DatasetBuilder('q2', 10, attributes={'data_type': 'Qux'}) |
| 1110 |
bar = DatasetBuilder( |
| 1111 |
name='bar', |
| 1112 |
data=[ReferenceBuilder(qux1), ReferenceBuilder(qux2)], |
| 1113 |
attributes={'data_type': 'Bar'}, |
| 1114 |
dtype='object' |
| 1115 |
) |
| 1116 |
foo = GroupBuilder( |
| 1117 |
name='foo', |
| 1118 |
datasets=[bar, qux1, qux2], |
| 1119 |
attributes={'data_type': 'Foo'} |
| 1120 |
) |
| 1121 |
self.runBuilderRoundTrip(foo) |
| 1122 |
|
| 1123 |
def test_round_trip_validation_of_compound_dtype_with_reference(self): |
| 1124 |
"""Verify that a dataset builder containing data with a compound dtype |
| 1125 |
containing a reference passes validation after a round trip""" |
| 1126 |
qux1 = DatasetBuilder('q1', 5, attributes={'data_type': 'Qux'}) |
| 1127 |
qux2 = DatasetBuilder('q2', 10, attributes={'data_type': 'Qux'}) |
| 1128 |
baz = DatasetBuilder( |
| 1129 |
name='baz', |
| 1130 |
data=[(10, ReferenceBuilder(qux1))], |
| 1131 |
dtype=[ |
| 1132 |
DtypeSpec('x', doc='x-value', dtype='int'), |
| 1133 |
DtypeSpec('y', doc='y-ref', dtype=RefSpec('Qux', reftype='object')) |
| 1134 |
], |
| 1135 |
attributes={'data_type': 'Baz'} |
| 1136 |
) |
| 1137 |
foo = GroupBuilder( |
| 1138 |
name='foo', |
| 1139 |
datasets=[baz, qux1, qux2], |
| 1140 |
attributes={'data_type': 'Foo'} |
| 1141 |
) |
| 1142 |
self.runBuilderRoundTrip(foo) |
| 1143 |
|
| 1144 |
|
| 1145 |
class TestEmptyDataRoundTrip(ValidatorTestBase): |
| 1146 |
""" |
| 1147 |
Test the special case of empty string datasets and attributes during validation |
| 1148 |
""" |
| 1149 |
def setUp(self): |
| 1150 |
self.filename = 'test_ref_dataset.h5' |
| 1151 |
super().setUp() |
| 1152 |
|
| 1153 |
def tearDown(self): |
| 1154 |
remove_test_file(self.filename) |
| 1155 |
super().tearDown() |
| 1156 |
|
| 1157 |
def getSpecs(self): |
| 1158 |
ret = GroupSpec('A test group specification with a data type', |
| 1159 |
data_type_def='Bar', |
| 1160 |
datasets=[DatasetSpec(name='data', |
| 1161 |
doc='an example dataset', |
| 1162 |
dtype='text', |
| 1163 |
attributes=[AttributeSpec( |
| 1164 |
name='attr2', |
| 1165 |
doc='an example integer attribute', |
| 1166 |
dtype='int', |
| 1167 |
shape=(None,))]), |
| 1168 |
DatasetSpec(name='dataInt', |
| 1169 |
doc='an example int dataset', |
| 1170 |
dtype='int', |
| 1171 |
attributes=[]) |
| 1172 |
], |
| 1173 |
attributes=[AttributeSpec(name='attr1', |
| 1174 |
doc='an example string attribute', |
| 1175 |
dtype='text', |
| 1176 |
shape=(None,))]) |
| 1177 |
return (ret,) |
| 1178 |
|
| 1179 |
def runBuilderRoundTrip(self, builder): |
| 1180 |
"""Executes a round-trip test for a builder |
| 1181 |
|
| 1182 |
1. First writes the builder to file, |
| 1183 |
2. next reads a new builder from disk |
| 1184 |
3. and finally runs the builder through the validator. |
| 1185 |
The test is successful if there are no validation errors.""" |
| 1186 |
ns_catalog = NamespaceCatalog() |
| 1187 |
ns_catalog.add_namespace(self.namespace.name, self.namespace) |
| 1188 |
typemap = TypeMap(ns_catalog) |
| 1189 |
self.manager = BuildManager(typemap) |
| 1190 |
|
| 1191 |
with HDF5IO(self.filename, manager=self.manager, mode='w') as write_io: |
| 1192 |
write_io.write_builder(builder) |
| 1193 |
|
| 1194 |
with HDF5IO(self.filename, manager=self.manager, mode='r') as read_io: |
| 1195 |
read_builder = read_io.read_builder() |
| 1196 |
errors = self.vmap.validate(read_builder) |
| 1197 |
self.assertEqual(len(errors), 0, errors) |
| 1198 |
|
| 1199 |
def test_empty_string_attribute(self): |
| 1200 |
"""Verify that we can determine dtype for empty string attribute during validation""" |
| 1201 |
builder = GroupBuilder('my_bar', |
| 1202 |
attributes={'data_type': 'Bar', 'attr1': []}, # <-- Empty string attribute |
| 1203 |
datasets=[DatasetBuilder(name='data', data=['text1', 'text2'], |
| 1204 |
attributes={'attr2': [10, ]}), |
| 1205 |
DatasetBuilder(name='dataInt', data=[5, ]) |
| 1206 |
]) |
| 1207 |
self.runBuilderRoundTrip(builder) |
| 1208 |
|
| 1209 |
def test_empty_string_dataset(self): |
| 1210 |
"""Verify that we can determine dtype for empty string dataset during validation""" |
| 1211 |
builder = GroupBuilder('my_bar', |
| 1212 |
attributes={'data_type': 'Bar', 'attr1': ['text1', 'text2']}, |
| 1213 |
datasets=[DatasetBuilder(name='data', # <-- Empty string dataset |
| 1214 |
data=[], |
| 1215 |
dtype='text', |
| 1216 |
attributes={'attr2': [10, ]}), |
| 1217 |
DatasetBuilder(name='dataInt', data=[5, ]) |
| 1218 |
]) |
| 1219 |
self.runBuilderRoundTrip(builder) |
| 1220 |
|
| 1221 |
def test_empty_int_attribute(self): |
| 1222 |
"""Verify that we can determine dtype for empty integer attribute during validation""" |
| 1223 |
builder = GroupBuilder('my_bar', |
| 1224 |
attributes={'data_type': 'Bar', 'attr1': ['text1', 'text2']}, |
| 1225 |
datasets=[DatasetBuilder(name='data', data=['text1', 'text2'], |
| 1226 |
attributes={'attr2': []} # <-- Empty integer attribute |
| 1227 |
), |
| 1228 |
DatasetBuilder(name='dataInt', data=[5, ]) |
| 1229 |
]) |
| 1230 |
self.runBuilderRoundTrip(builder) |
| 1231 |
|
| 1232 |
def test_empty_int_dataset(self): |
| 1233 |
"""Verify that a dataset builder containing an array of references passes |
| 1234 |
validation after a round trip""" |
| 1235 |
builder = GroupBuilder('my_bar', |
| 1236 |
attributes={'data_type': 'Bar', 'attr1': ['text1', 'text2']}, |
| 1237 |
datasets=[DatasetBuilder(name='data', data=['text1', 'text2'], |
| 1238 |
attributes={'attr2': [10, ]}), |
| 1239 |
DatasetBuilder(name='dataInt', data=[], dtype='int') # <-- Empty int dataset |
| 1240 |
]) |
| 1241 |
self.runBuilderRoundTrip(builder) |
| 1242 |
|
| 1243 |
|
| 1244 |
class TestValidateSubspec(ValidatorTestBase): |
| 1245 |
"""When a subtype satisfies a subspec, the validator should also validate |
| 1246 |
that the subtype satisfies its spec. |
| 1247 |
""" |
| 1248 |
|
| 1249 |
def getSpecs(self): |
| 1250 |
dataset_spec = DatasetSpec('A dataset', data_type_def='Foo') |
| 1251 |
sub_dataset_spec = DatasetSpec( |
| 1252 |
doc='A subtype of Foo', |
| 1253 |
data_type_def='Bar', |
| 1254 |
data_type_inc='Foo', |
| 1255 |
attributes=[ |
| 1256 |
AttributeSpec(name='attr1', doc='an example attribute', dtype='text') |
| 1257 |
], |
| 1258 |
) |
| 1259 |
spec = GroupSpec( |
| 1260 |
doc='A group that contains a Foo', |
| 1261 |
data_type_def='Baz', |
| 1262 |
datasets=[ |
| 1263 |
DatasetSpec(doc='Child Dataset', data_type_inc='Foo'), |
| 1264 |
]) |
| 1265 |
return (spec, dataset_spec, sub_dataset_spec) |
| 1266 |
|
| 1267 |
def test_validate_subtype(self): |
| 1268 |
"""Test that when spec A contains dataset B, and C is a subtype of B, using a C builder is valid. |
| 1269 |
""" |
| 1270 |
builder = GroupBuilder( |
| 1271 |
name='my_baz', |
| 1272 |
attributes={'data_type': 'Baz'}, |
| 1273 |
datasets=[ |
| 1274 |
DatasetBuilder(name='bar', attributes={'data_type': 'Bar', 'attr1': 'value'}) |
| 1275 |
], |
| 1276 |
) |
| 1277 |
result = self.vmap.validate(builder) |
| 1278 |
self.assertEqual(len(result), 0) |
| 1279 |
|
| 1280 |
def test_validate_subtype_error(self): |
| 1281 |
"""Test that when spec A contains dataset B, and C is a subtype of B, using a C builder validates |
| 1282 |
against spec C. |
| 1283 |
""" |
| 1284 |
builder = GroupBuilder( |
| 1285 |
name='my_baz', |
| 1286 |
attributes={'data_type': 'Baz'}, |
| 1287 |
datasets=[ |
| 1288 |
DatasetBuilder(name='bar', attributes={'data_type': 'Bar'}) |
| 1289 |
], |
| 1290 |
) |
| 1291 |
result = self.vmap.validate(builder) |
| 1292 |
self.assertEqual(len(result), 1) |
| 1293 |
self.assertValidationError(result[0], MissingError, name='Bar/attr1') |