Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_project_serialization():
"""Test project serialization with JSON-LD context."""
with freeze_time('2017-03-01T08:00:00.000000+00:00') as frozen_time:
project = Project(name='demo')
assert project.name == 'demo'
assert project.created == frozen_time().replace(tzinfo=timezone.utc)
assert project.updated == frozen_time().replace(tzinfo=timezone.utc)
data = asjsonld(project)
assert 'schema:Project' in data['@type']
assert 'prov:Location' in data['@type']
context = data['@context']
assert 'schema:name' == context['name']
assert Person._jsonld_context == context['creator']['@context']
assert 'schema:dateUpdated' == context['updated']
assert 'schema:dateCreated' == context['created']
assert 'schema:schemaVersion' == context['version']
def test_inheritance():
"""Test type and context inheritance."""
types = {'prov:Activity', 'wfprov:ProcessRun'}
context_keys = {'prov', 'wfprov', '@version'}
@jsonld.s(type='prov:Activity', context={'prov': 'A'})
class Activity:
"""Define an activity."""
@jsonld.s(type='wfprov:ProcessRun', context={'wfprov': 'B'})
class ProcessRun(Activity):
"""Define a process execution based on an activity."""
data = jsonld.asjsonld(ProcessRun())
assert set(data['@type']) == types
assert set(data['@context'].keys()) == context_keys
types = {'prov:Activity', 'wfprov:ProcessRun', 'wfprov:WorkflowRun'}
@jsonld.s(type='wfprov:WorkflowRun')
class WorkflowRun(ProcessRun):
"""Define a workflow run."""
data = jsonld.asjsonld(WorkflowRun())
assert set(data['@type']) == types
assert set(data['@context'].keys()) == context_keys
@jsonld.s(type='wfprov:ProcessRun', context={'wfprov': 'B'})
class ProcessRun(Activity):
"""Define a process execution based on an activity."""
data = jsonld.asjsonld(ProcessRun())
assert set(data['@type']) == types
assert set(data['@context'].keys()) == context_keys
types = {'prov:Activity', 'wfprov:ProcessRun', 'wfprov:WorkflowRun'}
@jsonld.s(type='wfprov:WorkflowRun')
class WorkflowRun(ProcessRun):
"""Define a workflow run."""
data = jsonld.asjsonld(WorkflowRun())
assert set(data['@type']) == types
assert set(data['@context'].keys()) == context_keys
def _jsonld(graph, format, *args, **kwargs):
"""Return formatted graph in JSON-LD ``format`` function."""
import json
from renku.core.compat import pyld
from renku.core.models.jsonld import asjsonld
output = getattr(pyld.jsonld, format)([
asjsonld(action) for action in graph.activities.values()
])
return json.dumps(output, indent=2)
def jsonld(client, datasets):
"""Format datasets as JSON-LD."""
data = [
asjsonld(
dataset,
basedir=os.path.relpath(
'.', start=str(dataset.__reference__.parent)
)
) for dataset in datasets
]
return dumps(data, indent=2)