Skip to content
Snippets Groups Projects
Commit d34f0bfa authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-cfood-scalars' into 'dev'

Scalars in cfood definitions

See merge request !45
parents 8f624c26 5935c5f9
No related branches found
No related tags found
2 merge requests!53Release 0.1,!45Scalars in cfood definitions
Pipeline #29051 passed
...@@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* FIX: #35 Parent cannot be set from value * FIX: #35 Parent cannot be set from value
* [#6](https://gitlab.com/caosdb/caosdb-crawler/-/issues/6): Fixed many type * [#6](https://gitlab.com/caosdb/caosdb-crawler/-/issues/6): Fixed many type
hints to be compatible to python 3.8 hints to be compatible to python 3.8
* [#9](https://gitlab.com/caosdb/caosdb-crawler/-/issues/9): Sclaras of types
different than string can now be given in cfood definitions
### Security ### Security
...@@ -156,15 +156,21 @@ def handle_value(value: Union[dict, str, list], values: GeneralStore): ...@@ -156,15 +156,21 @@ def handle_value(value: Union[dict, str, list], values: GeneralStore):
propvalue = value propvalue = value
# variables replacement: # variables replacement:
propvalue = [replace_variables(i, values) for i in propvalue] propvalue = list()
for element in value:
# Do the element-wise replacement only, when its type is string:
if type(element) == str:
propvalue.append(replace_variables(element, values))
else:
propvalue.append(element)
return (propvalue, collection_mode) return (propvalue, collection_mode)
else: else:
# value is another simple type # value is another simple type
# collection_mode = "single" collection_mode = "single"
# propvalue = value["value"] propvalue = value
# return (propvalue, collection_mode) # Return it immediately, otherwise variable substitution would be done and fail:
raise RuntimeError() return (propvalue, collection_mode)
propvalue = replace_variables(propvalue, values) propvalue = replace_variables(propvalue, values)
return (propvalue, collection_mode) return (propvalue, collection_mode)
......
# This is a test cfood for:
# https://gitlab.com/caosdb/caosdb-crawler/-/issues/9
Data: # name of the converter
type: Directory
match: (.*)
subtree:
DataAnalysis: # name of the converter
type: Directory
match: DataAnalysis
records:
RecordThatGetsParentsLater:
someId: 23 # <- this scalar causes problems
#!/bin/python
# Tests for:
# https://gitlab.com/caosdb/caosdb-crawler/-/issues/9
# A. Schlemmer, 06/2021
import pytest
# The main function that is affected by this issue:
from caoscrawler.converters import handle_value
from caoscrawler.crawl import Crawler
# We need the store for the above function
from caoscrawler.stores import GeneralStore
from test_tool import dircheckstr, rfp
@pytest.fixture
def crawler():
crawler = Crawler(debug=True)
crawler.crawl_directory(rfp("test_directories", "examples_article"),
rfp("cfoods_scalar.yml"))
return crawler
def test_handle_value():
# Note that we will need this store only, if we also want to test variables substitution:
store = GeneralStore()
# This one should work:
assert handle_value("bla", store) == ("bla", "single")
# These failed:
assert handle_value(4, store) == (4, "single")
assert handle_value(4.2, store) == (4.2, "single")
assert handle_value(True, store) == (True, "single")
# List test:
assert handle_value([4, 3, 2], store) == ([4, 3, 2], "single")
def test_record_structure_generation(crawler):
subd = crawler.debug_tree[dircheckstr("DataAnalysis")]
assert len(subd) == 2
# variables store on Data Analysis node of debug tree
assert len(subd[0]) == 3
assert "Data" in subd[0]
assert "DataAnalysis" in subd[0]
assert "RecordThatGetsParentsLater" in subd[0]
prop = subd[0]["RecordThatGetsParentsLater"].get_property("someId")
assert type(prop.value) == int
assert prop.value == 23
# record store on Data Analysis node of debug tree
assert len(subd[1]) == 1
prop2 = subd[1]["RecordThatGetsParentsLater"].get_property("someId")
assert prop == prop2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment