Skip to content

Commit 4a10c2c

Browse files
authored
Merge pull request #263 from mpsonntag/propertyTest
Fixes and Section/Property tests LGTM
2 parents 7041e7f + 7410cc0 commit 4a10c2c

7 files changed

Lines changed: 256 additions & 192 deletions

File tree

odml/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def get_terminology_equivalent(self):
3535

3636
def __eq__(self, obj):
3737
"""
38-
Do a deep comparison of this object and its odml properties
38+
Do a deep comparison of this object and its odml properties.
39+
The 'id' attribute of an object is excluded, since it is
40+
unique within a document.
3941
"""
4042
# cannot compare totally different stuff
4143
if not isinstance(obj, _baseobj):
@@ -45,7 +47,9 @@ def __eq__(self, obj):
4547
return False
4648

4749
for key in self._format:
48-
if getattr(self, key) != getattr(obj, key):
50+
if key == "id":
51+
continue
52+
elif getattr(self, key) != getattr(obj, key):
4953
return False
5054

5155
return True

odml/property.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,22 @@ def remove(self, value):
329329

330330
def get_path(self):
331331
"""
332-
Return the absolute path to this object
332+
Return the absolute path to this object.
333333
"""
334+
if not self.parent:
335+
return "/"
336+
334337
return self.parent.get_path() + ":" + self.name
335338

336339
def clone(self):
337340
"""
338-
Clone this object to copy it independently
339-
to another document
341+
Clone this object to copy it independently to another document.
340342
"""
341343
obj = super(BaseProperty, self).clone()
342-
obj._section = None
344+
obj._parent = None
343345
obj.value = self._value
346+
obj._id = str(uuid.uuid4())
347+
344348
return obj
345349

346350
def merge(self, other, strict=True):
@@ -408,9 +412,10 @@ def get_merged_equivalent(self):
408412
Return the merged object (i.e. if the section is linked to another one,
409413
return the corresponding property of the linked section) or None
410414
"""
411-
if self._parent._merged is None:
415+
if self.parent is None or self.parent._merged is None:
412416
return None
413-
return self._parent._merged.contains(self)
417+
418+
return self.parent._merged.contains(self)
414419

415420
@inherit_docstring
416421
def get_terminology_equivalent(self):

odml/section.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ def clone(self, children=True):
307307
to another document
308308
"""
309309
obj = super(BaseSection, self).clone(children)
310+
obj._id = str(uuid.uuid4())
310311

311312
obj._props = base.SmartList()
312313
if children:

test/test_odMLMetadata.xml

Lines changed: 0 additions & 180 deletions
This file was deleted.

test/test_property.py

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,17 @@ def test_parent(self):
187187
def test_dtype(self):
188188
pass
189189

190-
def test_path(self):
191-
pass
190+
def test_get_path(self):
191+
doc = Document()
192+
sec = Section(name="parent", parent=doc)
193+
194+
# Check root path for a detached Property.
195+
prop = Property(name="prop")
196+
self.assertEqual("/", prop.get_path())
197+
198+
# Check absolute path of Property in a Document.
199+
prop.parent = sec
200+
self.assertEqual("/%s:%s" % (sec.name, prop.name), prop.get_path())
192201

193202
def test_value_origin(self):
194203
p = Property("P")
@@ -199,12 +208,19 @@ def test_value_origin(self):
199208
self.assertEqual(p.value_origin, None)
200209

201210
def test_set_id(self):
211+
p = Property(name="P")
212+
self.assertIsNotNone(p.id)
213+
202214
p = Property("P", id="79b613eb-a256-46bf-84f6-207df465b8f7")
203215
self.assertEqual(p.id, "79b613eb-a256-46bf-84f6-207df465b8f7")
204216

205-
Property("P", id="id")
217+
p = Property("P", id="id")
206218
self.assertNotEqual(p.id, "id")
207219

220+
# Make sure id cannot be reset programmatically.
221+
with self.assertRaises(AttributeError):
222+
p.id = "someId"
223+
208224
def test_merge(self):
209225
p_dst = Property("p1", value=[1, 2, 3], unit="Hz", definition="Freude\t schoener\nGoetterfunken\n",
210226
reference="portal.g-node.org", uncertainty=0.0)
@@ -255,6 +271,48 @@ def test_merge(self):
255271
int_p.merge(double_p, strict=False)
256272
self.assertEqual(len(int_p), 2)
257273

274+
def test_clone(self):
275+
sec = Section(name="parent")
276+
277+
# Check different id.
278+
prop = Property(name="original")
279+
clone_prop = prop.clone()
280+
self.assertNotEqual(prop.id, clone_prop.id)
281+
282+
# Check parent removal in clone.
283+
prop.parent = sec
284+
clone_prop = prop.clone()
285+
self.assertIsNotNone(prop.parent)
286+
self.assertIsNone(clone_prop.parent)
287+
288+
def test_get_merged_equivalent(self):
289+
sec = Section(name="parent")
290+
mersec = Section(name="merged_section")
291+
merprop_other = Property(name="other_prop", value="other")
292+
merprop = Property(name="prop", value=[1, 2, 3])
293+
294+
# Check None on unset parent.
295+
prop = Property(name="prop")
296+
self.assertIsNone(prop.get_merged_equivalent())
297+
298+
# Check None on parent without merged Section.
299+
prop.parent = sec
300+
self.assertIsNone(prop.get_merged_equivalent())
301+
302+
# Check None on parent with merged Section but no attached Property.
303+
sec.merge(mersec)
304+
self.assertIsNone(prop.get_merged_equivalent())
305+
306+
# Check None on parent with merged Section and unequal Property.
307+
merprop_other.parent = mersec
308+
self.assertIsNone(prop.get_merged_equivalent())
309+
310+
# Check receiving merged equivalent Property.
311+
merprop.parent = mersec
312+
self.assertIsNotNone(prop.get_merged_equivalent())
313+
self.assertEqual(prop.get_merged_equivalent(), merprop)
314+
315+
258316
if __name__ == "__main__":
259317
print("TestProperty")
260318
tp = TestProperty()

0 commit comments

Comments
 (0)