Skip to content

Commit

Permalink
Improved tests and included rocrate validator
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenBrinckmann committed Nov 19, 2024
1 parent 76b8798 commit 8fc7087
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest rocrate jsonschema
pip install pytest rocrate jsonschema rocrateValidator
- name: Test with pytest
run: |
pytest --tb=no -s
Expand Down
4 changes: 3 additions & 1 deletion tests/test_00_pypi_rocrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from rocrate.rocrate import ROCrate

LABEL = 'pypi_rocrate'
verbose = False

class Test_1(unittest.TestCase):
"""
Expand Down Expand Up @@ -43,7 +44,8 @@ def test_main(self):
tempPath= [i for i in dirpath.iterdir() if i.is_dir()][0]
crate = ROCrate(tempPath)
for e in crate.get_entities():
print(f' {e.id}: {e.type}')
if verbose:
print(f' {e.id}: {e.type}')
if fileName not in logJson:
logJson[fileName] = {LABEL:True}
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_01_params_metadata_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_main(self):
# runtime global variables
METADATA_FILE = 'ro-crate-metadata.json'
OUTPUT_INFO = False
OUTPUT_COUNTS = True
OUTPUT_COUNTS = False
KNOWN_KEYS = DATASET_MANDATORY+DATASET_SUGGESTED+FILE_MANDATORY+FILE_SUGGESTED+['@id', '@type']
LABEL = 'params_metadata_json'

Expand Down
65 changes: 65 additions & 0 deletions tests/test_03_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/python3
"""
Validate if rocrate of pypi can open and parse it. This is a test if we follow general ro-crate guidelines.
https://pypi.org/project/rocrate/
"""
import os
import json
import tempfile
import unittest
from pathlib import Path
from zipfile import ZIP_DEFLATED
from zipfile import ZipFile
import rocrate_validator
from rocrate_validator.errors import ValidationError
import rocrate_validator.services as rvs

LABEL = 'validator'
METADATA_FILE = 'ro-crate-metadata.json'

class Test_1(unittest.TestCase):
"""
derived class for this test
"""
def test_main(self):
"""
main function
"""
# log-file
if Path('tests/logging.json').exists():
logJson = json.load(open('tests/logging.json'))
else:
logJson = {}

for root, _, files in os.walk(".", topdown=False):
for name in files:
if not name.endswith('.eln'):
continue
fileName = os.path.join(root, name)
print(f'\n\nTry to parse: {fileName}')
with ZipFile(fileName, 'r', compression=ZIP_DEFLATED) as elnFile:
dirpath = Path(tempfile.mkdtemp())/Path(fileName).parent.name
dirpath.mkdir(parents=True, exist_ok=True)
elnFile.extractall(dirpath)
rocrate_dir= [i for i in dirpath.iterdir() if i.is_dir()][0]
result = rvs.validate({
"profiles_path": rocrate_validator.utils.get_profiles_path(),
"profile_identifier": "ro-crate",
"requirement_severity": rocrate_validator.models.Severity.REQUIRED.name,
"requirement_severity_only": False,
"inherit_profiles": True,
"verbose": True,
"data_path": rocrate_dir,
"ontology_path": None,
"abort_on_first": False
})
result_dict = result.to_dict()
if result_dict['issues'] == [] and result_dict['passed']:
success = True
else:
print(f'{fileName} is not valid')
print(result_dict)
success = False
logJson[fileName] = logJson.get(fileName,{}) | {LABEL: success}
json.dump(logJson, open('tests/logging.json', 'w'))
assert success
9 changes: 5 additions & 4 deletions tests/test_99_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import unittest

COLUMNS = ['params_metadata_json', 'pypi_rocrate','schema']
COLUMNS = ['pypi_rocrate','validator','schema','params_metadata_json']
HEADER = "## Results of verification\nautomatically created\n\n"


Expand All @@ -31,9 +31,10 @@ def test_main(self):
resultStr = ' | '.join([':white_check_mark:' if result[col] else ':x:' for col in COLUMNS])
output.write(f'| {software} | {individualFileName} | {resultStr} |\n')
output.write("\n\nDefinition of tests\n")
output.write("- **pypi_rocrate**: tests if eln-file can be opened by pypi's rocrate; aka if eln file conforms to rocrate convention.\n")
output.write("- **params_metadata_json**: tests if the conventions of the consortium are fulfilled, aka parameters exist and are consistent with convention.\n")
output.write("- **schema**: tests if the conventions of the consortium are fulfilled using a schema description.\n")
output.write("- **pypi_rocrate**: tests if eln-file can be opened by pypi's rocrate; if eln file can be easily opened by that library.\n")
output.write("- **validator**: tests if the ro-crate conventions fulfilled using pypi's rocrateValidator.\n")
output.write("- **schema**: tests if the conventions of the ELN-consortium are fulfilled using a schema description.\n")
output.write("- **params_metadata_json**: tests if the conventions of the ELN-consortium are fulfilled, aka parameters exist and are consistent with convention.\n")
output.close()
print('Created logging markdown')
else:
Expand Down

0 comments on commit 8fc7087

Please sign in to comment.