def test_get_source_description(self): ns_catalog = NamespaceCatalog() ns_catalog.load_namespaces(self.namespace_path, resolve=True) loaded_ns = ns_catalog.get_namespace(self.ns_name) descr = loaded_ns.get_source_description('mylab.extensions.yaml') self.assertDictEqual(descr, {'doc': 'Extensions for my lab', 'source': 'mylab.extensions.yaml', 'title': 'Extensions for my lab'})
def test_read_namespace(self): ns_catalog = NamespaceCatalog() ns_catalog.load_namespaces(self.namespace_path, resolve=True) loaded_ns = ns_catalog.get_namespace(self.ns_name) self.assertEqual(loaded_ns.doc, "mydoc") self.assertEqual(loaded_ns.author, "foo") self.assertEqual(loaded_ns.contact, "*****@*****.**") self.assertEqual(loaded_ns.full_name, "My Laboratory") self.assertEqual(loaded_ns.name, "mylab") self.assertEqual(loaded_ns.date, self.date.isoformat()) self.assertDictEqual(loaded_ns.schema[0], {'doc': 'Extensions for my lab', 'source': 'mylab.extensions.yaml', 'title': 'Extensions for my lab'}) self.assertEqual(loaded_ns.version, "0.0.1")
def test_cache_spec(self): self.test_temp_file = tempfile.NamedTemporaryFile() self.test_temp_file.close() # On Windows h5py cannot truncate an open file in write mode. # The temp file will be closed before h5py truncates it # and will be removed during the tearDown step. self.io = HDF5IO(self.test_temp_file.name, manager=self.manager, mode='w') # Setup all the data we need foo1 = Foo('foo1', [0, 1, 2, 3, 4], "I am foo1", 17, 3.14) foo2 = Foo('foo2', [5, 6, 7, 8, 9], "I am foo2", 34, 6.28) foobucket = FooBucket('test_bucket', [foo1, foo2]) foofile = FooFile('test_foofile', [foobucket]) # Write the first file self.io.write(foofile, cache_spec=True) self.io.close() ns_catalog = NamespaceCatalog() HDF5IO.load_namespaces(ns_catalog, self.test_temp_file.name) self.assertEqual(ns_catalog.namespaces, ('test_core',)) source_types = self.__get_types(self.io.manager.namespace_catalog) read_types = self.__get_types(ns_catalog) self.assertSetEqual(source_types, read_types)
def test_get_source_files(self): ns_catalog = NamespaceCatalog() ns_catalog.load_namespaces(self.namespace_path, resolve=True) loaded_ns = ns_catalog.get_namespace(self.ns_name) self.assertListEqual(loaded_ns.get_source_files(), ['mylab.extensions.yaml'])
def _get_manager(): foo_spec = GroupSpec('A test group specification with a data type', data_type_def='Foo', datasets=[DatasetSpec('an example dataset', 'int', name='my_data', attributes=[AttributeSpec('attr2', 'an example integer attribute', 'int')])], attributes=[AttributeSpec('attr1', 'an example string attribute', 'text')]) tmp_spec = GroupSpec('A subgroup for Foos', name='foo_holder', groups=[GroupSpec('the Foos in this bucket', data_type_inc='Foo', quantity=ZERO_OR_MANY)]) bucket_spec = GroupSpec('A test group specification for a data type containing data type', data_type_def='FooBucket', groups=[tmp_spec]) class BucketMapper(ObjectMapper): def __init__(self, spec): super(BucketMapper, self).__init__(spec) foo_spec = spec.get_group('foo_holder').get_data_type('Foo') self.map_spec('foos', foo_spec) file_spec = GroupSpec("A file of Foos contained in FooBuckets", name='root', data_type_def='FooFile', groups=[GroupSpec('Holds the FooBuckets', name='buckets', groups=[GroupSpec("One ore more FooBuckets", data_type_inc='FooBucket', quantity=ONE_OR_MANY)])]) class FileMapper(ObjectMapper): def __init__(self, spec): super(FileMapper, self).__init__(spec) bucket_spec = spec.get_group('buckets').get_data_type('FooBucket') self.map_spec('buckets', bucket_spec) spec_catalog = SpecCatalog() spec_catalog.register_spec(foo_spec, 'test.yaml') spec_catalog.register_spec(bucket_spec, 'test.yaml') spec_catalog.register_spec(file_spec, 'test.yaml') namespace = SpecNamespace( 'a test namespace', CORE_NAMESPACE, [{'source': 'test.yaml'}], catalog=spec_catalog) namespace_catalog = NamespaceCatalog() namespace_catalog.add_namespace(CORE_NAMESPACE, namespace) type_map = TypeMap(namespace_catalog) type_map.register_container_type(CORE_NAMESPACE, 'Foo', Foo) type_map.register_container_type(CORE_NAMESPACE, 'FooBucket', FooBucket) type_map.register_container_type(CORE_NAMESPACE, 'FooFile', FooFile) type_map.register_map(FooBucket, BucketMapper) type_map.register_map(FooFile, FileMapper) manager = BuildManager(type_map) return manager