Skip to content

Commit 46849aa

Browse files
committed
[property] Refactor merge strict policy
1 parent 5da4fb8 commit 46849aa

2 files changed

Lines changed: 50 additions & 41 deletions

File tree

odml/property.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -376,42 +376,49 @@ def clone(self):
376376
def merge_check(self, source, strict=True):
377377
"""
378378
Checks whether a source Property can be merged with self as destination and
379-
raises a ValueError if any of the attributes definition, uncertainty, reference
380-
or value_origin and potentially dtype differ in source and destination.
379+
raises a ValueError if the values of source and destination are not compatible.
380+
With parameter *strict=True* a ValueError is also raised, if any of the
381+
attributes unit, definition, uncertainty, reference or value_origin and dtype
382+
differ in source and destination.
381383
382384
:param source: an odML Property.
383-
:param strict: If strict is True the dtypes of self and other must be identical.
385+
:param strict: If True, the attributes dtype, unit, uncertainty, definition,
386+
reference and value_origin of source and destination
387+
must be identical.
384388
"""
385389
if not isinstance(source, BaseProperty):
386390
raise ValueError("odml.Property.merge: odML Property required.")
387391

388-
if strict and self.dtype != source.dtype:
392+
# Catch unmerge-able values at this point to avoid
393+
# failing Section tree merges which cannot easily be rolled back.
394+
new_value = self._convert_value_input(source.value)
395+
if not self._validate_values(new_value):
396+
raise ValueError("odml.Property.merge: passed value(s) cannot "
397+
"be converted to data type '%s'!" % self._dtype)
398+
if not strict:
399+
return
400+
401+
if (self.dtype is not None and source.dtype is not None and
402+
self.dtype != source.dtype):
389403
raise ValueError("odml.Property.merge: src and dest dtypes do not match!")
390-
else:
391-
# Catch unmerge-able values also at this point to avoid
392-
# failing Section tree merges which cannot easily be rolled back.
393-
new_value = self._convert_value_input(source.value)
394-
if not self._validate_values(new_value):
395-
raise ValueError("odml.Property.merge: passed value(s) cannot "
396-
"be converted to data type '%s'!" % self._dtype)
397404

398405
if self.unit is not None and source.unit is not None and self.unit != source.unit:
399406
raise ValueError("odml.Property.merge: "
400407
"src and dest units (%s, %s) do not match!" %
401408
(source.unit, self.unit))
402409

410+
if (self.uncertainty is not None and source.uncertainty is not None and
411+
self.uncertainty != source.uncertainty):
412+
raise ValueError("odml.Property.merge: "
413+
"src and dest uncertainty both set and do not match!")
414+
403415
if self.definition is not None and source.definition is not None:
404416
self_def = ''.join(map(str.strip, self.definition.split())).lower()
405417
other_def = ''.join(map(str.strip, source.definition.split())).lower()
406418
if self_def != other_def:
407419
raise ValueError("odml.Property.merge: "
408420
"src and dest definitions do not match!")
409421

410-
if (self.uncertainty is not None and source.uncertainty is not None and
411-
self.uncertainty != source.uncertainty):
412-
raise ValueError("odml.Property.merge: "
413-
"src and dest uncertainty both set and do not match!")
414-
415422
if self.reference is not None and source.reference is not None:
416423
self_ref = ''.join(map(str.strip, self.reference.lower().split()))
417424
other_ref = ''.join(map(str.strip, source.reference.lower().split()))

test/test_property.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -435,85 +435,87 @@ def test_merge_check(self):
435435
source = Property(name="source", dtype="string")
436436
destination = Property(name="destination", dtype="string")
437437

438-
destination.merge_check(source, True)
438+
destination.merge_check(source)
439439
source.dtype = "int"
440440
with self.assertRaises(ValueError):
441-
destination.merge_check(source, True)
441+
destination.merge_check(source)
442+
442443
destination.merge_check(source, False)
443444

444445
# Test value check
445446
source = Property(name="source", value=[1, 2, 3])
446447
destination = Property(name="destination", value=[4, 5, 6])
447-
destination.merge_check(source, True)
448+
destination.merge_check(source)
448449

449450
# Test value convertable
450451
source = Property(name="source", value=["7", "8"])
451452
with self.assertRaises(ValueError):
452-
destination.merge_check(source, True)
453+
destination.merge_check(source)
454+
453455
destination.merge_check(source, False)
454456

455457
# Test value not convertable
456458
source = Property(name="source", value=["nine", "ten"])
457459
with self.assertRaises(ValueError):
458-
destination.merge_check(source, True)
460+
destination.merge_check(source)
459461
with self.assertRaises(ValueError):
460462
destination.merge_check(source, False)
461463

462464
# Test unit check
463465
source = Property(name="source", unit="Hz")
464466
destination = Property(name="destination", unit="Hz")
465467

466-
destination.merge_check(source, True)
468+
destination.merge_check(source)
467469
source.unit = "s"
468470
with self.assertRaises(ValueError):
469-
destination.merge_check(source, True)
470-
with self.assertRaises(ValueError):
471-
destination.merge_check(source, False)
471+
destination.merge_check(source)
472+
473+
destination.merge_check(source, False)
472474

473475
# Test uncertainty check
474476
source = Property(name="source", uncertainty=0.0)
475477
destination = Property(name="destination", uncertainty=0.0)
476478

477-
destination.merge_check(source, True)
479+
destination.merge_check(source)
478480
source.uncertainty = 10.0
479481
with self.assertRaises(ValueError):
480-
destination.merge_check(source, True)
481-
with self.assertRaises(ValueError):
482-
destination.merge_check(source, False)
482+
destination.merge_check(source)
483+
484+
destination.merge_check(source, False)
483485

484486
# Test definition check
485487
source = Property(name="source", definition="Freude\t schoener\nGoetterfunken\n")
486488
destination = Property(name="destination",
487489
definition="FREUDE schoener GOETTERfunken")
488490

489-
destination.merge_check(source, True)
491+
destination.merge_check(source)
490492
source.definition = "Freunde schoender Goetterfunken"
491493
with self.assertRaises(ValueError):
492-
destination.merge_check(source, True)
493-
with self.assertRaises(ValueError):
494-
destination.merge_check(source, False)
494+
destination.merge_check(source)
495+
496+
destination.merge_check(source, False)
495497

496498
# Test reference check
497499
source = Property(name="source", reference="portal.g-node.org")
498500
destination = Property(name="destination", reference="portal.g-node.org")
499501

500-
destination.merge_check(source, True)
502+
destination.merge_check(source)
501503
source.reference = "portal.g-node.org/odml/terminologies/v1.1"
502504
with self.assertRaises(ValueError):
503-
destination.merge_check(source, True)
504-
with self.assertRaises(ValueError):
505-
destination.merge_check(source, False)
505+
destination.merge_check(source)
506+
507+
destination.merge_check(source, False)
506508

507509
# Test value origin check
508510
source = Property(name="source", value_origin="file")
509511
destination = Property(name="destination", value_origin="file")
510512

511-
destination.merge_check(source, True)
513+
destination.merge_check(source)
512514
source.value_origin = "other file"
513515
with self.assertRaises(ValueError):
514-
destination.merge_check(source, True)
515-
with self.assertRaises(ValueError):
516-
destination.merge_check(source, False)
516+
destination.merge_check(source)
517+
518+
destination.merge_check(source, False)
517519

518520
def test_merge(self):
519521
p_dst = Property("p1", value=[1, 2, 3], unit="Hz", definition="Freude\t schoener\nGoetterfunken\n",

0 commit comments

Comments
 (0)