Skip to content
Snippets Groups Projects
Commit 66173ba1 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

TST: added unit tests for macro expansion during cfood loading

parent ecabc99f
Branches
Tags
2 merge requests!53Release 0.1,!25F macros
Pipeline #28570 failed
...@@ -235,7 +235,7 @@ class Crawler(object): ...@@ -235,7 +235,7 @@ class Crawler(object):
if len(crawler_definitions) == 1: if len(crawler_definitions) == 1:
# Simple case, just one document: # Simple case, just one document:
crawler_definition = crawler_definitions[0] crawler_definition = crawler_definitions[0]
elif len(crawler_definition) == 2: elif len(crawler_definitions) == 2:
crawler_definition = crawler_definitions[1] crawler_definition = crawler_definitions[1]
else: else:
raise RuntimeError("Crawler definition must not contain more than two documents.") raise RuntimeError("Crawler definition must not contain more than two documents.")
......
...@@ -24,6 +24,10 @@ ...@@ -24,6 +24,10 @@
from caoscrawler.macros import defmacro_constructor, macro_constructor from caoscrawler.macros import defmacro_constructor, macro_constructor
from caoscrawler.macros.macro_yaml_object import macro_store from caoscrawler.macros.macro_yaml_object import macro_store
from caoscrawler.crawl import Crawler
from tempfile import NamedTemporaryFile
import yaml import yaml
import pytest import pytest
...@@ -142,6 +146,70 @@ testnode: !macro ...@@ -142,6 +146,70 @@ testnode: !macro
assert dat["testnode"]["replaced3"] == "ok" assert dat["testnode"]["replaced3"] == "ok"
def temp_file_load(txt: str):
"""
Create a temporary file with txt and load the crawler
definition using load_definition from Crawler.
"""
definition = None
with NamedTemporaryFile() as f:
f.write(txt.encode())
f.flush()
c = Crawler()
definition = c.load_definition(f.name)
return definition
def test_load_definition(register_macros, macro_store_reset):
txt = """
extroot:
type: Directory
match: extroot
subtree:
SimulationData:
type: Directory
match: SimulationData
"""
# Check whether simple cfoods can be loaded:
cfood = temp_file_load(txt)
assert cfood["extroot"]["subtree"]["SimulationData"]["match"] == "SimulationData"
cfood = temp_file_load("""
---
metadata:
macros:
- !defmacro
name: test_one
params: {}
definition:
replaced1: ok
- !defmacro
name: test_two
params:
match_name: null
definition:
type: Directory
match: $match_name
---
extroot:
type: Directory
match: extroot
subtree:
SimulationData:
type: Directory
match: SimulationData
extroot2: !macro # test top level macro
test_one:
extroot3:
subtree:
SimulationData: !macro
test_two:
match_name: SimulationData
""")
assert cfood["extroot"]["subtree"]["SimulationData"]["match"] == "SimulationData"
assert cfood["extroot2"]["replaced1"] == "ok"
assert cfood["extroot3"]["subtree"]["SimulationData"]["match"] == "SimulationData"
@pytest.mark.xfail @pytest.mark.xfail
def test_replace_arbitrary_objects(register_macros, macro_store_reset): def test_replace_arbitrary_objects(register_macros, macro_store_reset):
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment