From 74f4e7efa681b7a361084f316b5c7cc24a7c7de7 Mon Sep 17 00:00:00 2001
From: Alexander Schlemmer <alexander@mail-schlemmer.de>
Date: Fri, 20 May 2022 14:59:39 +0200
Subject: [PATCH] ENH: implementation of multi macro and additional tests

---
 src/caoscrawler/macros/macro_yaml_object.py |  7 +-
 unittests/test_macros.py                    | 99 ++++++++++++++++++++-
 2 files changed, 102 insertions(+), 4 deletions(-)

diff --git a/src/caoscrawler/macros/macro_yaml_object.py b/src/caoscrawler/macros/macro_yaml_object.py
index 5c776f03..056152c9 100644
--- a/src/caoscrawler/macros/macro_yaml_object.py
+++ b/src/caoscrawler/macros/macro_yaml_object.py
@@ -134,4 +134,9 @@ def multimacro_constructor(loader, node):
     It can be registered in pyaml using:
     yaml.SafeLoader.add_constructor("!multimacro", multimacro_constructor)
     """
-    pass
+
+    res = dict()
+    for val in node.value:
+        res.update(macro_constructor(loader, val))
+
+    return res
diff --git a/unittests/test_macros.py b/unittests/test_macros.py
index b6dbc16b..dfb4bd0a 100644
--- a/unittests/test_macros.py
+++ b/unittests/test_macros.py
@@ -22,7 +22,10 @@
 # ** end header
 #
 
-from caoscrawler.macros import defmacro_constructor, macro_constructor, multimacro_constructor
+from caoscrawler.macros import (defmacro_constructor,
+                                macro_constructor,
+                                multimacro_constructor)
+from caoscrawler.macros.macro_yaml_object import macro_store
 import yaml
 import pytest
 
@@ -32,7 +35,11 @@ def register_macros():
     yaml.SafeLoader.add_constructor("!macro", macro_constructor)
     yaml.SafeLoader.add_constructor("!multimacro", multimacro_constructor)
 
-def test_macros(register_macros):
+@pytest.fixture
+def macro_store_reset():
+    macro_store.clear()
+
+def test_macros(register_macros, macro_store_reset):
     dat = yaml.load("""
 defs:
 - !defmacro
@@ -58,7 +65,7 @@ testnode:
     assert "expanded_bla" not in dat["testnode"]["obl"]
     assert "bla" not in dat["testnode"]["obl"]["expanded_yea"]
 
-def test_macro_list_replacment(register_macros):
+def test_macro_list_replacment(register_macros, macro_store_reset):
     dat = yaml.load("""
 defs:
 - !defmacro
@@ -84,3 +91,89 @@ testnode:
     assert len(dat["testnode"]["obl"]["expanded_yea"]["blubb"]) == 2
     assert dat["testnode"]["obl"]["expanded_yea"]["blubb"][0] == "ok4"
     assert dat["testnode"]["obl"]["expanded_yea"]["blubb"][1]["yea"] == "$variable"
+
+def test_multi_macros(register_macros, macro_store_reset):
+    dat = yaml.load("""
+defs:
+- !defmacro
+  name: test_one
+  params: {}
+  definition:
+    replaced1: ok
+- !defmacro
+  name: test_two
+  params: {}
+  definition:
+    replaced2: ok
+    replaced3: ok
+
+testnode:
+    obl: !multimacro
+    - name: test_one
+    - name: test_two
+""", Loader=yaml.SafeLoader)
+    print(yaml.dump(dat))
+    assert dat["testnode"]["obl"]["replaced1"] == "ok"
+    assert dat["testnode"]["obl"]["replaced2"] == "ok"
+    assert dat["testnode"]["obl"]["replaced3"] == "ok"
+
+@pytest.mark.xfail
+def test_multi_macros_toplevel(register_macros, macro_store_reset):
+    """
+    See: https://gitlab.indiscale.com/caosdb/src/caosdb-crawler/-/issues/23
+    """
+    dat = yaml.load("""
+defs:
+- !defmacro
+  name: test_one
+  params: {}
+  definition:
+    replaced1: ok
+- !defmacro
+  name: test_two
+  params: {}
+  definition:
+    replaced2: ok
+    replaced3: ok
+
+testnode: !multimacro
+    - name: test_one
+    - name: test_two
+""", Loader=yaml.SafeLoader)
+    assert dat["testnode"]["replaced1"] == "ok"
+    assert dat["testnode"]["replaced2"] == "ok"
+    assert dat["testnode"]["replaced3"] == "ok"
+
+
+@pytest.mark.xfail
+def test_replace_arbitrary_objects(register_macros, macro_store_reset):
+    """
+    See: https://gitlab.indiscale.com/caosdb/src/caosdb-crawler/-/issues/24
+    """
+    dat = yaml.load("""
+defs:
+- !defmacro
+  name: test
+  params:
+    b: 25
+    testvar_list:
+    - a
+    - $b
+    testvar_dict:
+      t1: a
+      t2: $b
+  definition:
+    replaced1:
+      $b: ok
+      c: $testvar_dict
+      d: $testvar_list
+
+testnode:
+    obl: !macro
+      name: test
+""", Loader=yaml.SafeLoader)
+    print(yaml.dump(dat))
+    assert dat["testnode"]["obl"]["replaced1"]["c"]["t1"] == "a"
+    assert dat["testnode"]["obl"]["replaced1"]["c"]["t2"] == "25"
+    assert dat["testnode"]["obl"]["replaced1"]["d"][0] == "a"
+    assert dat["testnode"]["obl"]["replaced1"]["d"][1] == "25"
-- 
GitLab