| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import os |
| 10 |
import time |
| 11 |
import unittest |
| 12 |
import nixio as nix |
| 13 |
from .tmp import TempDir |
| 14 |
|
| 15 |
|
| 16 |
class TestSections(unittest.TestCase): |
| 17 |
|
| 18 |
def setUp(self): |
| 19 |
self.tmpdir = TempDir("sectiontest") |
| 20 |
self.testfilename = os.path.join(self.tmpdir.path, "sectiontest.nix") |
| 21 |
self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite) |
| 22 |
self.section = self.file.create_section("test section", |
| 23 |
"recordingsession") |
| 24 |
self.other = self.file.create_section("other section", |
| 25 |
"recordingsession") |
| 26 |
|
| 27 |
def tearDown(self): |
| 28 |
del self.file.sections[self.section.id] |
| 29 |
del self.file.sections[self.other.id] |
| 30 |
self.file.close() |
| 31 |
self.tmpdir.cleanup() |
| 32 |
|
| 33 |
def test_section_eq(self): |
| 34 |
assert self.section == self.section |
| 35 |
assert not self.section == self.other |
| 36 |
assert self.section is not None |
| 37 |
|
| 38 |
def test_section_id(self): |
| 39 |
assert self.section.id is not None |
| 40 |
|
| 41 |
|
| 42 |
oid = "4a6e8483-0a9a-464d-bdd9-b39818334bcd" |
| 43 |
sec = self.file.create_section("assign id", "sec type", oid) |
| 44 |
assert sec.id == oid |
| 45 |
|
| 46 |
nonid = "I am not a proper uuid" |
| 47 |
sec = self.file.create_section("invalid id", "sec type", nonid) |
| 48 |
assert sec.id != nonid |
| 49 |
|
| 50 |
|
| 51 |
oid = "4a6e8483-0a9a-464d-bdd9-b39818334bcd" |
| 52 |
sub_sec = sec.create_section("assign id", "sec type", oid) |
| 53 |
assert sub_sec.id == oid |
| 54 |
|
| 55 |
nonid = "I am not a proper uuid" |
| 56 |
sub_sec = sec.create_section("invalid id", "sec type", nonid) |
| 57 |
assert sub_sec.id != nonid |
| 58 |
|
| 59 |
def test_section_name(self): |
| 60 |
assert self.section.name is not None |
| 61 |
|
| 62 |
def test_section_type(self): |
| 63 |
def set_none(): |
| 64 |
self.section.type = None |
| 65 |
|
| 66 |
assert self.section.type is not None |
| 67 |
self.assertRaises(Exception, set_none) |
| 68 |
|
| 69 |
self.section.type = "foo type" |
| 70 |
assert self.section.type == "foo type" |
| 71 |
|
| 72 |
def test_section_definition(self): |
| 73 |
assert self.section.definition is None |
| 74 |
|
| 75 |
self.section.definition = "definition" |
| 76 |
assert self.section.definition == "definition" |
| 77 |
|
| 78 |
self.section.definition = None |
| 79 |
assert self.section.definition is None |
| 80 |
|
| 81 |
def test_section_repository(self): |
| 82 |
assert self.section.repository is None |
| 83 |
|
| 84 |
self.section.repository = "repository" |
| 85 |
assert self.section.repository == "repository" |
| 86 |
|
| 87 |
self.section.repository = None |
| 88 |
assert self.section.repository is None |
| 89 |
|
| 90 |
def test_property_reference(self): |
| 91 |
assert self.section.reference is None |
| 92 |
|
| 93 |
self.section.reference = "reference" |
| 94 |
assert self.section.reference == "reference" |
| 95 |
|
| 96 |
self.section.reference = None |
| 97 |
assert self.section.reference is None |
| 98 |
self.section.reference = None |
| 99 |
|
| 100 |
def test_section_sections(self): |
| 101 |
assert len(self.section.sections) == 0 |
| 102 |
|
| 103 |
child = self.section.create_section("test section", "electrode") |
| 104 |
assert child.parent == self.section |
| 105 |
|
| 106 |
assert len(self.section.sections) == 1 |
| 107 |
|
| 108 |
assert child in self.section.sections |
| 109 |
assert child.id in self.section.sections |
| 110 |
assert "notexist" not in self.section.sections |
| 111 |
|
| 112 |
assert child.id == self.section.sections[0].id |
| 113 |
assert child.id == self.section.sections[-1].id |
| 114 |
|
| 115 |
del self.section.sections[0] |
| 116 |
|
| 117 |
assert len(self.section.sections) == 0 |
| 118 |
|
| 119 |
self.section['easy subsection'] = nix.S('electrode') |
| 120 |
subject = self.section['subject'] = nix.S('subject') |
| 121 |
|
| 122 |
assert self.section['subject'] == subject |
| 123 |
assert self.section['subject'].id == subject.id |
| 124 |
assert('easy subsection' in |
| 125 |
[v.name for k, v in self.section.sections.items()]) |
| 126 |
assert 'easy subsection' in self.section.sections |
| 127 |
assert self.section['easy subsection'].name == 'easy subsection' |
| 128 |
|
| 129 |
def test_section_find_sections(self): |
| 130 |
for i in range(2): |
| 131 |
self.section.create_section("level1-p0-s" + str(i), "dummy") |
| 132 |
for i in range(2): |
| 133 |
self.section.sections[0].create_section("level2-p1-s" + str(i), |
| 134 |
"dummy") |
| 135 |
for i in range(2): |
| 136 |
self.section.sections[1].create_section("level2-p2-s" + str(i), |
| 137 |
"dummy") |
| 138 |
for i in range(2): |
| 139 |
self.section.sections[0].sections[0].create_section( |
| 140 |
"level3-p1-s" + str(i), "dummy" |
| 141 |
) |
| 142 |
|
| 143 |
assert len(self.section.find_sections()) == 9 |
| 144 |
assert len(self.section.find_sections(limit=1)) == 3 |
| 145 |
assert(len(self.section.find_sections( |
| 146 |
filtr=lambda x: "level2-p1-s" in x.name)) == 2 |
| 147 |
) |
| 148 |
assert(len(self.section.find_sections( |
| 149 |
filtr=lambda x: "level2-p1-s" in x.name, limit=1)) == 0 |
| 150 |
) |
| 151 |
|
| 152 |
assert len(self.section.find_related()) == 3 |
| 153 |
assert len(self.section.sections[0].find_related()) == 5 |
| 154 |
|
| 155 |
def test_section_properties(self): |
| 156 |
assert len(self.section) == 0 |
| 157 |
|
| 158 |
prop = self.section.create_property("test prop", nix.DataType.String) |
| 159 |
|
| 160 |
assert len(self.section) == 1 |
| 161 |
|
| 162 |
for prop in self.section: |
| 163 |
assert prop in self.section |
| 164 |
|
| 165 |
assert "test prop" in self.section |
| 166 |
assert "notexist" not in self.section |
| 167 |
assert self.section["test prop"] is not None |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
assert len(self.section.inherited_properties()) == 1 |
| 172 |
|
| 173 |
assert prop in self.section |
| 174 |
assert prop.id in self.section |
| 175 |
assert prop.name in self.section |
| 176 |
assert "notexist" not in self.section |
| 177 |
|
| 178 |
props = dict(self.section.items()) |
| 179 |
assert props["test prop"] == prop |
| 180 |
|
| 181 |
assert prop.id == self.section.props[0].id |
| 182 |
assert prop.id == self.section.props[-1].id |
| 183 |
|
| 184 |
|
| 185 |
self.section['ep_str'] = 'str' |
| 186 |
self.section['ep_int'] = 23 |
| 187 |
self.section['ep_float'] = 42.0 |
| 188 |
self.section['ep_list'] = [1, 2, 3] |
| 189 |
self.section['ep_val'] = 1.0 |
| 190 |
|
| 191 |
self.section['ep_val'] = 2.0 |
| 192 |
|
| 193 |
|
| 194 |
self.section.create_property('cp_str', 'str') |
| 195 |
self.section.create_property('cp_int', 23) |
| 196 |
self.section.create_property('cp_float', 42.0) |
| 197 |
self.section.create_property('cp_list', [1, 2, 3]) |
| 198 |
self.section.create_property('cp_val', 1.0) |
| 199 |
|
| 200 |
self.section.props["cp_str"].values = "anotherstr" |
| 201 |
|
| 202 |
assert all([name in self.section for name in ['ep_str', 'ep_int', 'ep_float']]) |
| 203 |
|
| 204 |
assert self.section['ep_str'] == 'str' |
| 205 |
assert self.section['ep_int'] == 23 |
| 206 |
assert self.section['ep_float'] == 42.0 |
| 207 |
assert self.section['ep_list'] == [1, 2, 3] |
| 208 |
|
| 209 |
def create_hetero_section(): |
| 210 |
self.section['ep_ex'] = [1, 1.0] |
| 211 |
|
| 212 |
self.assertRaises(TypeError, create_hetero_section) |
| 213 |
|
| 214 |
section_ids = [sec.id for sec in self.section] |
| 215 |
for s_id in section_ids: |
| 216 |
del self.section[s_id] |
| 217 |
|
| 218 |
assert len(self.section) == 0 |
| 219 |
|
| 220 |
def test_parent(self): |
| 221 |
self.assertIs(self.section.parent, None) |
| 222 |
child = self.section.create_section("child section", "sect") |
| 223 |
self.assertEqual(self.section, child.parent) |
| 224 |
|
| 225 |
block = self.file.create_block("block", "section parent test") |
| 226 |
mdsection = self.file.create_section("block md", "metadata sect") |
| 227 |
block.metadata = mdsection |
| 228 |
self.assertIs(block.metadata.parent, None) |
| 229 |
|
| 230 |
grp = block.create_group("group", "section parent test") |
| 231 |
grp.metadata = child |
| 232 |
self.assertEqual(grp.metadata.parent, self.section) |
| 233 |
|
| 234 |
|
| 235 |
self.assertEqual(block.groups["group"].metadata.parent, self.section) |
| 236 |
|
| 237 |
def test_inverse_search(self): |
| 238 |
block = self.file.create_block("a block", "block with metadata") |
| 239 |
block.metadata = self.section |
| 240 |
|
| 241 |
otherblock = self.file.create_block("b block", "block with metadata") |
| 242 |
otherblock.metadata = self.other |
| 243 |
|
| 244 |
self.file.create_block("c block", "block without metadata") |
| 245 |
|
| 246 |
self.assertEqual(len(self.section.referring_blocks), 1) |
| 247 |
self.assertEqual(len(self.other.referring_blocks), 1) |
| 248 |
self.assertEqual(self.section.referring_blocks[0], block) |
| 249 |
self.assertEqual(self.other.referring_blocks[0], otherblock) |
| 250 |
|
| 251 |
da_one = block.create_data_array("foo", "data_array", data=range(10)) |
| 252 |
da_one.metadata = self.other |
| 253 |
da_two = block.create_data_array("foobar", "data_array", data=[1]) |
| 254 |
da_two.metadata = self.other |
| 255 |
|
| 256 |
block.create_data_array("nomd", "data_array", data=[9]) |
| 257 |
|
| 258 |
self.assertEqual(len(self.other.referring_data_arrays), 2) |
| 259 |
self.assertIn(da_one, self.other.referring_data_arrays) |
| 260 |
self.assertIn(da_two, self.other.referring_data_arrays) |
| 261 |
|
| 262 |
tag = block.create_tag("tago", "tagtype", [1, 1]) |
| 263 |
tag.metadata = self.section |
| 264 |
|
| 265 |
block.create_tag("tagi", "tagtype", [1, 10]) |
| 266 |
self.assertEqual(len(self.section.referring_tags), 1) |
| 267 |
self.assertEqual(len(self.other.referring_tags), 0) |
| 268 |
self.assertEqual(self.section.referring_tags[0].id, tag.id) |
| 269 |
|
| 270 |
mtag = block.create_multi_tag("MultiTagName", "MultiTagType", da_one) |
| 271 |
mtag.metadata = self.section |
| 272 |
block.create_multi_tag("MtagNOMD", "MultiTagType", da_one) |
| 273 |
self.assertEqual(len(self.section.referring_multi_tags), 1) |
| 274 |
self.assertEqual(len(self.other.referring_multi_tags), 0) |
| 275 |
self.assertEqual(self.section.referring_multi_tags[0].id, mtag.id) |
| 276 |
|
| 277 |
src = block.create_source("sauce", "stype") |
| 278 |
src.metadata = self.other |
| 279 |
block.create_source("nosauce", "stype") |
| 280 |
self.assertEqual(len(self.other.referring_sources), 1) |
| 281 |
self.assertEqual(len(self.section.referring_sources), 0) |
| 282 |
self.assertEqual(self.other.referring_sources[0].id, src.id) |
| 283 |
|
| 284 |
def test_section_link(self): |
| 285 |
self.section.create_property("PropOnSection", "value") |
| 286 |
|
| 287 |
self.assertIn("PropOnSection", self.section) |
| 288 |
self.assertEqual("value", self.section["PropOnSection"]) |
| 289 |
|
| 290 |
self.assertNotIn("PropOnSection", self.other) |
| 291 |
with self.assertRaises(KeyError): |
| 292 |
_ = self.other["PropOnSection"] |
| 293 |
|
| 294 |
self.other.link = self.section |
| 295 |
|
| 296 |
self.assertNotIn("PropOnSection", self.other) |
| 297 |
with self.assertRaises(KeyError): |
| 298 |
_ = self.other["PropOnSection"] |
| 299 |
|
| 300 |
inhpropnames = [p.name for p in self.other.inherited_properties()] |
| 301 |
self.assertIn("PropOnSection", inhpropnames) |
| 302 |
|
| 303 |
|
| 304 |
def test_copy_on_sections(self): |
| 305 |
tarfilename = os.path.join(self.tmpdir.path, "destination.nix") |
| 306 |
tarfile = nix.File.open(tarfilename, nix.FileMode.Overwrite) |
| 307 |
sec1 = self.section |
| 308 |
prop1 = sec1.create_property("prop from origin", |
| 309 |
values_or_dtype=[1, 2, 3]) |
| 310 |
sec2 = sec1.create_section("nested sec", 'test nested') |
| 311 |
tarsec = tarfile.create_section("tar sec", "tar") |
| 312 |
tarsec.create_property(copy_from=prop1) |
| 313 |
assert prop1 == tarsec.props[0] |
| 314 |
tarsec.copy_section(sec1) |
| 315 |
tarsec.copy_section(sec2) |
| 316 |
assert sec1 == tarsec.sections[0] |
| 317 |
assert sec2 == tarsec.sections[1] |
| 318 |
assert sec1.sections[0] == tarsec.sections[1] |
| 319 |
tarfile.close() |
| 320 |
|
| 321 |
def test_timestamp_autoupdate(self): |
| 322 |
section = self.file.create_section("section.time", "test.time") |
| 323 |
|
| 324 |
sectime = section.updated_at |
| 325 |
time.sleep(1) |
| 326 |
section.reference = "whatever" |
| 327 |
self.assertNotEqual(sectime, section.updated_at) |
| 328 |
|
| 329 |
sectime = section.updated_at |
| 330 |
linksec = self.file.create_section("link.section.time", "test.time") |
| 331 |
time.sleep(1) |
| 332 |
section.link = linksec |
| 333 |
self.assertNotEqual(sectime, section.updated_at) |
| 334 |
|
| 335 |
sectime = section.updated_at |
| 336 |
time.sleep(1) |
| 337 |
section.repository = "repo" |
| 338 |
self.assertNotEqual(sectime, section.updated_at) |
| 339 |
|
| 340 |
def test_timestamp_noautoupdate(self): |
| 341 |
self.file.auto_update_timestamps = False |
| 342 |
section = self.file.create_section("section.time", "test.time") |
| 343 |
|
| 344 |
sectime = section.updated_at |
| 345 |
time.sleep(1) |
| 346 |
section.reference = "whatever" |
| 347 |
self.assertEqual(sectime, section.updated_at) |
| 348 |
|
| 349 |
sectime = section.updated_at |
| 350 |
time.sleep(1) |
| 351 |
linksec = self.file.create_section("link.section.time", "test.time") |
| 352 |
section.link = linksec |
| 353 |
self.assertEqual(sectime, section.updated_at) |
| 354 |
|
| 355 |
sectime = section.updated_at |
| 356 |
time.sleep(1) |
| 357 |
section.repository = "repo" |
| 358 |
self.assertEqual(sectime, section.updated_at) |