Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/pip/ci/dependabot-updates/pydan…
Browse files Browse the repository at this point in the history
…tic-gte-1.10.0-and-lt-2.10.2
  • Loading branch information
francescalb authored Nov 28, 2024
2 parents 4ddffbb + 9a47df8 commit a2f9c8a
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 13 deletions.
48 changes: 38 additions & 10 deletions bindings/python/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ function(test_failure name)
set_property(TEST ${name} PROPERTY WILL_FAIL true)
endfunction()

# Check for yaml
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import yaml"
RESULT_VARIABLE missing_yaml
)


# Install
install(
Expand Down Expand Up @@ -83,13 +89,35 @@ test_success(
dlite-validate-empty
dlite-validate Empty.json
)
# Test valid yaml
test_success(
dlite-validate-valid1-yaml
dlite-validate ../tests/entities/ValidYaml1.yaml
)
# Test invalid yaml
test_failure(
dlite-validate-invalid1-yaml
dlite-validate ../tests/entities/InvalidYaml1.yaml
)

# Tests depending on yaml
if(NOT ${missing_yaml})
# Test valid yaml
test_success(
dlite-validate-valid1-yaml
dlite-validate ../tests/entities/ValidYaml1.yaml
)
# Test invalid yaml
test_failure(
dlite-validate-invalid1-yaml
dlite-validate ../tests/entities/InvalidYaml1.yaml
)

# Test invalid yaml with ref-type
test_failure(
dlite-validate-ref-invalid1-yaml
dlite-validate ../tests/entities/Ref_invalid1.json
)

# Test valid yaml with invalid ref-type
test_failure(
dlite-validate-invalidref-yaml
dlite-validate ../tests/entities/ValidYaml_invalidref.yaml
)

# Test invalid yaml
test_failure(
dlite-validate-invalid2-yaml
dlite-validate ../tests/entities/InvalidYaml2.yaml
)
endif()
37 changes: 34 additions & 3 deletions bindings/python/scripts/dlite-validate
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@ def parse(url, driver=None, options="mode=r", id=None):
List of new instances.
"""
import dlite

loc = url.split("#", 1)[0].split("?",1)[0]
if driver is None:
driver = Path(loc).suffix.lstrip('.').lower()


# Validate all instances if `id` is None
if id is None:
try:
uuids = dlite.Instance.get_uuids(driver, url, options=options)
#except dlite.DLiteError:
except dlite.DLiteUnsupportedError:
pass
else:
insts = []
for uuid in uuids:
insts.extend(parse(url, driver=driver, options=options, id=uuid))
return insts

# Validate all instances if `id` is None
if id is None:
Expand Down Expand Up @@ -102,8 +115,9 @@ def main():
parser.add_argument(
'--storage-path', '-p', action='append', default=[],
help='URL or path to storage path to look up metadata. '
'Only needed when validating data instances. '
'This option can be given multiple times.'
'Only needed when validating data instances, or datamodels '
'referring to other datamodels as properties. '
'This option can be given multiple times.'
)
parser.add_argument(
'--show', '-s', action='store_true',
Expand Down Expand Up @@ -140,6 +154,23 @@ def main():
for inst in insts:
if inst.is_meta:
check_dimensions(args.url, inst)
# Check that datamodels for properties that refer to
# a specific datamodel can be loaded.
for prop in inst.properties['properties']:
if prop.type == 'ref':
if prop.ref == 'ref':
print(f'Property "{prop.name}" is of type "ref", the value '
'is ref and not a uri. Check that the property '
'"$ref" is given, the "$" is a strict requirement.')
sys.exit(1)
# Add the path of the original url to storage_path
# since it is likely that referenced data models are
# in the same folder.
dlite.storage_path.append(Path(args.url).resolve().parent)
propmeta = dlite.get_instance(prop.ref)
print(f"Datamodel for property '{prop.name}' was loaded successfully, and "
" not further validated.")


print(f'{args.url}: validated')

Expand Down
8 changes: 8 additions & 0 deletions bindings/python/tests/entities/InvalidYaml2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# An invalid version of datamodel Valid1.yaml
uri: http://onto-ns.com/meta/0.1/InvalidYaml2
description: A datamodel wiht invalid dimension (empty).
dimensions:
properties:
name:
type: str
description: Name of the item.
27 changes: 27 additions & 0 deletions bindings/python/tests/entities/Ref_invalid1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"uri": "http://onto-ns.com/meta/0.2/Ref",
"description": "A datamodel with ref-types...",
"dimensions": {
"nitems": "Number of items.",
"nrefs": "Number of references to Ref datamodels."
},
"properties": {
"item": {
"type": "ref",
"ref": "http://onto-ns.com/meta/0.1/Item",
"description": "Single item."
},
"items": {
"type": "ref",
"$ref": "http://onto-ns.com/meta/0.1/Item",
"shape": ["nitems"],
"description": "List of items."
},
"refs": {
"type": "ref",
"$ref": "http://onto-ns.com/meta/0.1/Ref",
"shape": ["nrefs"],
"description": "List of references to Ref datamodels. This may become very cyclic, but DLite should cope with it..."
}
}
}
9 changes: 9 additions & 0 deletions bindings/python/tests/entities/ValidYaml_invalidref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# An valid version of datamodel Invalid1.yaml
uri: http://onto-ns.com/meta/0.1/ValidYaml1
description: An datamodel for an item with float type and invalid shape name.
dimensions: []
properties:
name:
type: ref
description: Name of the item.
$ref: http://onto-ns.com/meta/0.1/InvalidYaml2
2 changes: 2 additions & 0 deletions src/dlite-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,8 @@ static DLiteInstance *parse_instance(const char *src, jsmntok_t *obj,
if (dlite_property_jscan(src, t, p->name, ptr, p, pdims, 0) < 0)
goto fail;
} else {
if (!ptr)
FAIL1("cannot assign property with NULL destination: %s", p->name);
if (dlite_type_scan(src+t->start, t->end - t->start, ptr, p->type,
p->size, 0) < 0)
goto fail;
Expand Down

0 comments on commit a2f9c8a

Please sign in to comment.