| 1 |
import datetime |
| 2 |
import os |
| 3 |
|
| 4 |
from hdmf.spec.namespace import SpecNamespace, NamespaceCatalog |
| 5 |
from hdmf.spec.spec import GroupSpec |
| 6 |
from hdmf.spec.write import NamespaceBuilder, YAMLSpecWriter, export_spec |
| 7 |
from hdmf.testing import TestCase |
| 8 |
|
| 9 |
|
| 10 |
class TestSpec(TestCase): |
| 11 |
|
| 12 |
def setUp(self): |
| 13 |
# create a builder for the namespace |
| 14 |
self.ns_name = "mylab" |
| 15 |
self.date = datetime.datetime.now() |
| 16 |
|
| 17 |
self.ns_builder = NamespaceBuilder(doc="mydoc", |
| 18 |
name=self.ns_name, |
| 19 |
full_name="My Laboratory", |
| 20 |
version="0.0.1", |
| 21 |
author="foo", |
| 22 |
contact="[email protected]", |
| 23 |
namespace_cls=SpecNamespace, |
| 24 |
date=self.date) |
| 25 |
|
| 26 |
# create extensions |
| 27 |
ext1 = GroupSpec('A custom DataSeries interface', |
| 28 |
attributes=[], |
| 29 |
datasets=[], |
| 30 |
groups=[], |
| 31 |
data_type_inc=None, |
| 32 |
data_type_def='MyDataSeries') |
| 33 |
|
| 34 |
ext2 = GroupSpec('An extension of a DataSeries interface', |
| 35 |
attributes=[], |
| 36 |
datasets=[], |
| 37 |
groups=[], |
| 38 |
data_type_inc='MyDataSeries', |
| 39 |
data_type_def='MyExtendedMyDataSeries') |
| 40 |
|
| 41 |
ext2.add_dataset(doc='test', |
| 42 |
dtype='float', |
| 43 |
name='testdata') |
| 44 |
|
| 45 |
self.data_types = [ext1, ext2] |
| 46 |
|
| 47 |
# add the extension |
| 48 |
self.ext_source_path = 'mylab.extensions.yaml' |
| 49 |
self.namespace_path = 'mylab.namespace.yaml' |
| 50 |
|
| 51 |
def _test_extensions_file(self): |
| 52 |
with open(self.ext_source_path, 'r') as file: |
| 53 |
match_str = """groups: |
| 54 |
- data_type_def: MyDataSeries |
| 55 |
doc: A custom DataSeries interface |
| 56 |
- data_type_def: MyExtendedMyDataSeries |
| 57 |
data_type_inc: MyDataSeries |
| 58 |
doc: An extension of a DataSeries interface |
| 59 |
datasets: |
| 60 |
- name: testdata |
| 61 |
dtype: float |
| 62 |
doc: test |
| 63 |
""" |
| 64 |
nsstr = file.read() |
| 65 |
self.assertEqual(nsstr, match_str) |
| 66 |
|
| 67 |
def _test_namespace_file(self): |
| 68 |
with open(self.namespace_path, 'r') as file: |
| 69 |
match_str = """namespaces: |
| 70 |
- author: foo |
| 71 |
contact: [email protected] |
| 72 |
date: '%s' |
| 73 |
doc: mydoc |
| 74 |
full_name: My Laboratory |
| 75 |
name: mylab |
| 76 |
schema: |
| 77 |
- doc: Extensions for my lab |
| 78 |
source: mylab.extensions.yaml |
| 79 |
title: Extensions for my lab |
| 80 |
version: 0.0.1 |
| 81 |
""" % self.date.isoformat() # noqa: E122 |
| 82 |
nsstr = file.read() |
| 83 |
self.assertEqual(nsstr, match_str) |
| 84 |
|
| 85 |
|
| 86 |
class TestNamespaceBuilder(TestSpec): |
| 87 |
NS_NAME = 'test_ns' |
| 88 |
|
| 89 |
def setUp(self): |
| 90 |
super().setUp() |
| 91 |
for data_type in self.data_types: |
| 92 |
self.ns_builder.add_spec(source=self.ext_source_path, spec=data_type) |
| 93 |
self.ns_builder.add_source(source=self.ext_source_path, |
| 94 |
doc='Extensions for my lab', |
| 95 |
title='My lab extensions') |
| 96 |
self.ns_builder.export(self.namespace_path) |
| 97 |
|
| 98 |
def tearDown(self): |
| 99 |
if os.path.exists(self.ext_source_path): |
| 100 |
os.remove(self.ext_source_path) |
| 101 |
if os.path.exists(self.namespace_path): |
| 102 |
os.remove(self.namespace_path) |
| 103 |
|
| 104 |
def test_export_namespace(self): |
| 105 |
self._test_namespace_file() |
| 106 |
self._test_extensions_file() |
| 107 |
|
| 108 |
def test_read_namespace(self): |
| 109 |
ns_catalog = NamespaceCatalog() |
| 110 |
ns_catalog.load_namespaces(self.namespace_path, resolve=True) |
| 111 |
loaded_ns = ns_catalog.get_namespace(self.ns_name) |
| 112 |
self.assertEqual(loaded_ns.doc, "mydoc") |
| 113 |
self.assertEqual(loaded_ns.author, "foo") |
| 114 |
self.assertEqual(loaded_ns.contact, "[email protected]") |
| 115 |
self.assertEqual(loaded_ns.full_name, "My Laboratory") |
| 116 |
self.assertEqual(loaded_ns.name, "mylab") |
| 117 |
self.assertEqual(loaded_ns.date, self.date.isoformat()) |
| 118 |
self.assertDictEqual(loaded_ns.schema[0], {'doc': 'Extensions for my lab', |
| 119 |
'source': 'mylab.extensions.yaml', |
| 120 |
'title': 'Extensions for my lab'}) |
| 121 |
self.assertEqual(loaded_ns.version, "0.0.1") |
| 122 |
|
| 123 |
def test_get_source_files(self): |
| 124 |
ns_catalog = NamespaceCatalog() |
| 125 |
ns_catalog.load_namespaces(self.namespace_path, resolve=True) |
| 126 |
loaded_ns = ns_catalog.get_namespace(self.ns_name) |
| 127 |
self.assertListEqual(loaded_ns.get_source_files(), ['mylab.extensions.yaml']) |
| 128 |
|
| 129 |
def test_get_source_description(self): |
| 130 |
ns_catalog = NamespaceCatalog() |
| 131 |
ns_catalog.load_namespaces(self.namespace_path, resolve=True) |
| 132 |
loaded_ns = ns_catalog.get_namespace(self.ns_name) |
| 133 |
descr = loaded_ns.get_source_description('mylab.extensions.yaml') |
| 134 |
self.assertDictEqual(descr, {'doc': 'Extensions for my lab', |
| 135 |
'source': 'mylab.extensions.yaml', |
| 136 |
'title': 'Extensions for my lab'}) |
| 137 |
|
| 138 |
def test_missing_version(self): |
| 139 |
"""Test that creating a namespace builder without a version raises an error.""" |
| 140 |
msg = "Namespace '%s' missing key 'version'. Please specify a version for the extension." % self.ns_name |
| 141 |
with self.assertRaisesWith(ValueError, msg): |
| 142 |
self.ns_builder = NamespaceBuilder(doc="mydoc", |
| 143 |
name=self.ns_name, |
| 144 |
full_name="My Laboratory", |
| 145 |
author="foo", |
| 146 |
contact="[email protected]", |
| 147 |
namespace_cls=SpecNamespace, |
| 148 |
date=self.date) |
| 149 |
|
| 150 |
|
| 151 |
class TestYAMLSpecWrite(TestSpec): |
| 152 |
|
| 153 |
def setUp(self): |
| 154 |
super().setUp() |
| 155 |
for data_type in self.data_types: |
| 156 |
self.ns_builder.add_spec(source=self.ext_source_path, spec=data_type) |
| 157 |
self.ns_builder.add_source(source=self.ext_source_path, |
| 158 |
doc='Extensions for my lab', |
| 159 |
title='My lab extensions') |
| 160 |
|
| 161 |
def tearDown(self): |
| 162 |
if os.path.exists(self.ext_source_path): |
| 163 |
os.remove(self.ext_source_path) |
| 164 |
if os.path.exists(self.namespace_path): |
| 165 |
os.remove(self.namespace_path) |
| 166 |
|
| 167 |
def test_init(self): |
| 168 |
temp = YAMLSpecWriter('.') |
| 169 |
self.assertEqual(temp._YAMLSpecWriter__outdir, '.') |
| 170 |
|
| 171 |
def test_write_namespace(self): |
| 172 |
temp = YAMLSpecWriter() |
| 173 |
self.ns_builder.export(self.namespace_path, writer=temp) |
| 174 |
self._test_namespace_file() |
| 175 |
self._test_extensions_file() |
| 176 |
|
| 177 |
def test_get_name(self): |
| 178 |
self.assertEqual(self.ns_name, self.ns_builder.name) |
| 179 |
|
| 180 |
|
| 181 |
class TestExportSpec(TestSpec): |
| 182 |
|
| 183 |
def test_export(self): |
| 184 |
"""Test that export_spec writes the correct files.""" |
| 185 |
export_spec(self.ns_builder, self.data_types, '.') |
| 186 |
self._test_namespace_file() |
| 187 |
self._test_extensions_file() |
| 188 |
|
| 189 |
def tearDown(self): |
| 190 |
if os.path.exists(self.ext_source_path): |
| 191 |
os.remove(self.ext_source_path) |
| 192 |
if os.path.exists(self.namespace_path): |
| 193 |
os.remove(self.namespace_path) |
| 194 |
|
| 195 |
def _test_namespace_file(self): |
| 196 |
with open(self.namespace_path, 'r') as file: |
| 197 |
match_str = """namespaces: |
| 198 |
- author: foo |
| 199 |
contact: [email protected] |
| 200 |
date: '%s' |
| 201 |
doc: mydoc |
| 202 |
full_name: My Laboratory |
| 203 |
name: mylab |
| 204 |
schema: |
| 205 |
- source: mylab.extensions.yaml |
| 206 |
version: 0.0.1 |
| 207 |
""" % self.date.isoformat() # noqa: E122 |
| 208 |
nsstr = file.read() |
| 209 |
self.assertEqual(nsstr, match_str) |
| 210 |
|
| 211 |
def test_missing_data_types(self): |
| 212 |
"""Test that calling export_spec on a namespace builder without data types raises a warning.""" |
| 213 |
with self.assertWarnsWith(UserWarning, 'No data types specified. Exiting.'): |
| 214 |
export_spec(self.ns_builder, [], '.') |