Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
CaosDB Crawler
Commits
74f4e7ef
Commit
74f4e7ef
authored
3 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: implementation of multi macro and additional tests
parent
7d5e3e88
No related branches found
No related tags found
2 merge requests
!53
Release 0.1
,
!25
F macros
Pipeline
#23202
passed with warnings
3 years ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caoscrawler/macros/macro_yaml_object.py
+6
-1
6 additions, 1 deletion
src/caoscrawler/macros/macro_yaml_object.py
unittests/test_macros.py
+96
-3
96 additions, 3 deletions
unittests/test_macros.py
with
102 additions
and
4 deletions
src/caoscrawler/macros/macro_yaml_object.py
+
6
−
1
View file @
74f4e7ef
...
...
@@ -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
This diff is collapsed.
Click to expand it.
unittests/test_macros.py
+
96
−
3
View file @
74f4e7ef
...
...
@@ -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
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment