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_dataset_serialization(dataset):
"""Test dataset (de)serialization."""
dataset_metadata = dataset.asjsonld()
dataset = Dataset.from_jsonld(dataset_metadata)
# assert that all attributes found in metadata are set in the instance
assert dataset.created
assert dataset.creator
assert dataset.identifier
assert dataset.name
assert dataset.path
assert dataset._project
# check values
assert str(dataset.created.isoformat()) == dataset_metadata.get('created')
assert dataset.creator[0].email == dataset_metadata.get('creator'
)[0].get('email')
assert dataset.identifier == dataset_metadata.get('identifier')
assert dataset.name == dataset_metadata.get('name')
assert dataset.path == dataset_metadata.get('path')
def test_migration_broken_urls(dataset_metadata):
"""Check that migration of broken dataset file URLs is string."""
dataset = Dataset.from_jsonld(
dataset_metadata,
client=LocalClient('.'),
)
for file_ in dataset.files:
assert isinstance(url_to_string(file_.url), str)
def test_dataset_creator_email(dataset_metadata):
"""Check that creators without an email are assigned a blank node."""
# modify the dataset metadata to change the creator
dataset = Dataset.from_jsonld(
dataset_metadata,
client=LocalClient('.'),
)
dataset.creator[0]._id = 'mailto:None'
dataset_broken = Dataset.from_jsonld(
dataset.asjsonld(), client=LocalClient('.')
)
assert 'mailto:None' not in dataset_broken.creator[0]._id
def test_dataset_doi_metadata(dataset_metadata):
"""Check dataset metadata for correct DOI."""
from renku.core.utils.doi import is_doi
dataset = Dataset.from_jsonld(
dataset_metadata,
client=LocalClient('.'),
)
if is_doi(dataset.identifier):
assert urljoin(
'https://doi.org', dataset.identifier
) == dataset.same_as
assert dataset._id.endswith(
'datasets/{}'.format(quote(dataset.identifier, safe=''))
)
def test_dataset_creator_email(dataset_metadata):
"""Check that creators without an email are assigned a blank node."""
# modify the dataset metadata to change the creator
dataset = Dataset.from_jsonld(
dataset_metadata,
client=LocalClient('.'),
)
dataset.creator[0]._id = 'mailto:None'
dataset_broken = Dataset.from_jsonld(
dataset.asjsonld(), client=LocalClient('.')
)
assert 'mailto:None' not in dataset_broken.creator[0]._id
def as_dataset(self, client):
"""Deserialize `ZenodoRecordSerializer` to `Dataset`."""
files = self.get_files()
metadata = self.get_jsonld()
dataset = Dataset.from_jsonld(metadata, client=client)
serialized_files = []
for file_ in files:
remote_ = file_.remote_url
dataset_file = DatasetFile(
url=remote_,
id=file_.id,
checksum=file_.checksum,
filename=file_.filename,
filesize=file_.filesize,
filetype=file_.type,
path='',
)
serialized_files.append(dataset_file)
dataset.files = serialized_files
def as_dataset(self, client):
"""Deserialize `DataverseRecordSerializer` to `Dataset`."""
files = self.get_files()
dataset = Dataset.from_jsonld(self._json, client=client)
serialized_files = []
for file_ in files:
remote_ = file_.remote_url
dataset_file = DatasetFile(
url=remote_,
id=file_._id if file_._id else file_.name,
filename=file_.name,
filesize=file_.content_size,
filetype=file_.file_format,
path='',
)
serialized_files.append(dataset_file)
dataset.files = serialized_files