From abb64f382634aeaa5a53fbcae31d5d65f1e6da0b Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Tue, 12 Nov 2024 17:07:03 +0100 Subject: [PATCH] fix(merge): handle iterables in sets union correctly --- src/ontoweaver/merge.py | 3 ++- tests/test_merge.py | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/test_merge.py diff --git a/src/ontoweaver/merge.py b/src/ontoweaver/merge.py index 12d0c56..e0f6c28 100644 --- a/src/ontoweaver/merge.py +++ b/src/ontoweaver/merge.py @@ -133,6 +133,7 @@ class Append(DictryMerger): def __init__(self, sep = None): self.sep = sep + self.merged = {} def reset(self): self.merged = {} @@ -144,7 +145,7 @@ def set(self, merged) -> None: if type(v) == set: e.union(v) else: - self.merged[k] = set(v).union(e) + self.merged[k] = set((v,)).union(e) def merge(self, key, lhs: dict[str,str], rhs: dict[str,str]): self.set(lhs) diff --git a/tests/test_merge.py b/tests/test_merge.py new file mode 100644 index 0000000..e102283 --- /dev/null +++ b/tests/test_merge.py @@ -0,0 +1,42 @@ +import logging + +import ontoweaver + +def test_append(): + + sep = ";" + merge = ontoweaver.merge.dictry.Append(sep) + + k = ontoweaver.base.Node() + + merge(k, {"p1":"x"},{"p2":"y"} ) + assert( merge.get() == {"p1":"x", "p2":"y"} ) + + merge.reset() + merge(k, {"p1":"x"},{} ) + assert( merge.get() == {"p1":"x"} ) + + merge.reset() + merge(k, {"p1":"x", "p2":"y"},{} ) + assert( merge.get() == {"p1":"x", "p2":"y"} ) + + merge.reset() + merge(k, {"p1":"x"},{"p1":"y"} ) + assert( "y" in merge.get()["p1"].split(sep) ) + assert( "x" in merge.get()["p1"].split(sep) ) + + merge.reset() + merge(k, {"p1":"abcd"},{"p1":"efgh"} ) + m = merge.get() + assert( "abcd" in m["p1"].split(sep) ) + assert( "efgh" in m["p1"].split(sep) ) + + merge.reset() + merge(k, {"p1":"[abcd]"},{"p1":"[efgh]"} ) + m = merge.get() + assert( "[abcd]" in m["p1"].split(sep) ) + assert( "[efgh]" in m["p1"].split(sep) ) + + +if __name__ == "__main__": + test_append()