Exemple #1
0
    def test_write_cache_spec(self):
        '''
        Round-trip test for writing spec and reading it back in
        '''

        with File(self.path) as fil:
            with HDF5IO(self.path, manager=self.manager, file=fil,
                        mode='a') as io:
                io.write(self.container)
        with File(self.path) as f:
            self.assertIn('specifications', f)

        ns_catalog = NamespaceCatalog(NWBGroupSpec, NWBDatasetSpec,
                                      NWBNamespace)
        HDF5IO.load_namespaces(ns_catalog, self.path, namespaces=['core'])
        original_ns = self.manager.namespace_catalog.get_namespace('core')
        cached_ns = ns_catalog.get_namespace('core')
        self.maxDiff = None
        for key in ('author', 'contact', 'doc', 'full_name', 'name'):
            with self.subTest(namespace_field=key):
                self.assertEqual(original_ns[key], cached_ns[key])
        for dt in original_ns.get_registered_types():
            with self.subTest(neurodata_type=dt):
                original_spec = original_ns.get_spec(dt)
                cached_spec = cached_ns.get_spec(dt)
                with self.subTest(test='data_type spec read back in'):
                    self.assertIsNotNone(cached_spec)
                with self.subTest(test='cached spec preserved original spec'):
                    self.assertDictEqual(original_spec, cached_spec)
    def test_load_namespace_unversioned_version(self):
        """Test that reading a namespace file with version=unversioned string works but raises a warning."""
        # create namespace with version key (remove it later)
        ns_dict = {
            'doc': 'a test namespace',
            'name': 'test_ns',
            'schema': [
                {'source': self.specs_path}
            ],
            'version': '0.0.1'
        }
        namespace = SpecNamespace.build_namespace(**ns_dict)
        namespace['version'] = str(SpecNamespace.UNVERSIONED)  # work around lack of setter to remove version key

        # write the namespace to file without version key
        to_dump = {'namespaces': [namespace]}
        with open(self.namespace_path, 'w') as tmp:
            yaml.safe_dump(json.loads(json.dumps(to_dump)), tmp, default_flow_style=False)

        # load the namespace from file
        ns_catalog = NamespaceCatalog()
        msg = "Loaded namespace 'test_ns' is unversioned. Please notify the extension author."
        with self.assertWarnsWith(UserWarning, msg):
            ns_catalog.load_namespaces(self.namespace_path)

        self.assertEqual(ns_catalog.get_namespace('test_ns').version, SpecNamespace.UNVERSIONED)
Exemple #3
0
    def test_load_namespace_none_version(self):
        """Test that reading a namespace file without a version works but raises a warning."""
        # create namespace with version key (remove it later)
        ns_dict = {
            'doc': 'a test namespace',
            'name': 'test_ns',
            'schema': [{
                'source': self.specs_path
            }],
            'version': '0.0.1'
        }
        namespace = SpecNamespace.build_namespace(**ns_dict)
        namespace[
            'version'] = None  # work around lack of setter to remove version key

        # write the namespace to file without version key
        to_dump = {'namespaces': [namespace]}
        with open(self.namespace_path, 'w') as tmp:
            yaml_obj = yaml.YAML(typ='safe', pure=True)
            yaml_obj.default_flow_style = False
            yaml_obj.dump(json.loads(json.dumps(to_dump)), tmp)

        # load the namespace from file
        ns_catalog = NamespaceCatalog()
        msg = (
            "Loaded namespace 'test_ns' is missing the required key 'version'. Version will be set to "
            "'%s'. Please notify the extension author." %
            SpecNamespace.UNVERSIONED)
        with self.assertWarnsWith(UserWarning, msg):
            ns_catalog.load_namespaces(self.namespace_path)

        self.assertEqual(
            ns_catalog.get_namespace('test_ns').version,
            SpecNamespace.UNVERSIONED)
Exemple #4
0
 def _check_spec(self):
     ns_catalog = NamespaceCatalog()
     HDF5IO.load_namespaces(ns_catalog, self.path)
     self.maxDiff = None
     for namespace in self.manager.namespace_catalog.namespaces:
         with self.subTest(namespace=namespace):
             original_ns = self.manager.namespace_catalog.get_namespace(namespace)
             cached_ns = ns_catalog.get_namespace(namespace)
             ns_fields_to_check = list(original_ns.keys())
             ns_fields_to_check.remove('schema')  # schema fields will not match, so reset
             for ns_field in ns_fields_to_check:
                 with self.subTest(namespace_field=ns_field):
                     self.assertEqual(original_ns[ns_field], cached_ns[ns_field])
             for dt in original_ns.get_registered_types():
                 with self.subTest(data_type=dt):
                     original_spec = original_ns.get_spec(dt)
                     cached_spec = cached_ns.get_spec(dt)
                     with self.subTest('Data type spec is read back in'):
                         self.assertIsNotNone(cached_spec)
                     with self.subTest('Cached spec matches original spec'):
                         self.assertDictEqual(original_spec, cached_spec)
Exemple #5
0
class TestCustomSpecClasses(TestCase):
    def setUp(self):  # noqa: C901
        self.ns_catalog = NamespaceCatalog(CustomGroupSpec, CustomDatasetSpec,
                                           CustomSpecNamespace)
        hdmf_typemap = get_type_map()
        self.ns_catalog.merge(hdmf_typemap.namespace_catalog)

    def test_constructor_getters(self):
        self.assertEqual(self.ns_catalog.dataset_spec_cls, CustomDatasetSpec)
        self.assertEqual(self.ns_catalog.group_spec_cls, CustomGroupSpec)
        self.assertEqual(self.ns_catalog.spec_namespace_cls,
                         CustomSpecNamespace)

    def test_load_namespaces(self):
        namespace_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), 'test.namespace.yaml')
        namespace_deps = self.ns_catalog.load_namespaces(namespace_path)

        # test that the dependencies are correct, including dependencies of the dependencies
        expected = set([
            'Data', 'Container', 'DynamicTable', 'ElementIdentifiers',
            'VectorData'
        ])
        self.assertSetEqual(set(namespace_deps['test']['hdmf-common']),
                            expected)

        # test that the types are loaded
        types = self.ns_catalog.get_types('test.base.yaml')
        expected = ('TestData', 'TestContainer', 'TestTable')
        self.assertTupleEqual(types, expected)

        # test that the namespace is correct and the types_key is updated for test ns
        test_namespace = self.ns_catalog.get_namespace('test')
        expected = {
            'doc':
            'Test namespace',
            'schema': [{
                'namespace': 'hdmf-common',
                'my_data_types': ['Data', 'DynamicTable', 'Container']
            }, {
                'doc': 'This source module contains base data types.',
                'source': 'test.base.yaml',
                'title': 'Base data types'
            }],
            'name':
            'test',
            'full_name':
            'Test',
            'version':
            '0.1.0',
            'author': ['Test test'],
            'contact': ['*****@*****.**']
        }
        self.assertDictEqual(test_namespace, expected)

        # test that the def_key is updated for test ns
        test_data_spec = self.ns_catalog.get_spec('test', 'TestData')
        self.assertTrue('my_data_type_def' in test_data_spec)
        self.assertTrue('my_data_type_inc' in test_data_spec)

        # test that the def_key is maintained for hdmf-common
        data_spec = self.ns_catalog.get_spec('hdmf-common', 'Data')
        self.assertTrue('data_type_def' in data_spec)

    def test_load_namespaces_ext(self):
        namespace_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), 'test.namespace.yaml')
        self.ns_catalog.load_namespaces(namespace_path)

        ext_namespace_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'test-ext.namespace.yaml')
        ext_namespace_deps = self.ns_catalog.load_namespaces(
            ext_namespace_path)

        # test that the dependencies are correct, including dependencies of the dependencies
        expected_deps = set([
            'TestData', 'TestContainer', 'TestTable', 'Container', 'Data',
            'DynamicTable', 'ElementIdentifiers', 'VectorData'
        ])
        self.assertSetEqual(set(ext_namespace_deps['test-ext']['test']),
                            expected_deps)

    def test_load_namespaces_bad_path(self):
        namespace_path = 'test.namespace.yaml'
        msg = "namespace file 'test.namespace.yaml' not found"
        with self.assertRaisesWith(IOError, msg):
            self.ns_catalog.load_namespaces(namespace_path)

    def test_load_namespaces_twice(self):
        namespace_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), 'test.namespace.yaml')
        namespace_deps1 = self.ns_catalog.load_namespaces(namespace_path)
        namespace_deps2 = self.ns_catalog.load_namespaces(namespace_path)
        self.assertDictEqual(namespace_deps1, namespace_deps2)