def test_properties(): crate = ROCrate() crate_name = "new crate" crate.name = crate_name assert crate.name == crate_name crate_description = "this is a new crate" crate.description = crate_description assert crate.description == crate_description assert crate.datePublished == crate.root_dataset.datePublished assert isinstance(crate.root_dataset.datePublished, datetime.datetime) assert isinstance(crate.root_dataset["datePublished"], str) crate_datePublished = datetime.datetime.now().astimezone().replace( microsecond=0) crate.datePublished = crate_datePublished assert crate.datePublished == crate_datePublished new_person = crate.add(Person(crate, '#001', {'name': 'Lee Ritenour'})) crate.creator = new_person assert crate.creator is new_person assert isinstance(crate.creator, Person) assert crate.creator['name'] == 'Lee Ritenour' assert crate.creator.type == 'Person' new_person2 = crate.add(Person(crate, '#002', {'name': 'Lee Ritenour'})) crate.creator = [new_person, new_person2] assert isinstance(crate.creator, list) assert crate.creator[0] is new_person assert crate.creator[1] is new_person2
def test_contextual_entities(): crate = ROCrate() new_person = crate.add(Person(crate, '#joe', {'name': 'Joe Pesci'})) person_dereference = crate.dereference('#joe') assert person_dereference is new_person assert person_dereference.type == 'Person' person_prop = person_dereference.properties() assert person_prop['@type'] == 'Person' assert person_prop['name'] == 'Joe Pesci' assert not new_person.datePublished id_ = "https://orcid.org/0000-0002-1825-0097" new_person = crate.add(Person(crate, id_, {'name': 'Josiah Carberry'})) person_dereference = crate.dereference(id_) assert person_dereference is new_person
def test_delete(test_data_dir): crate = ROCrate() # fundamental entities with pytest.raises(ValueError): crate.delete(crate.root_dataset) with pytest.raises(ValueError): crate.delete(crate.metadata) # preview preview = crate.add(Preview(crate)) assert preview in crate.default_entities crate.delete(preview) assert preview not in crate.default_entities assert crate.preview is None # data entities file1 = crate.add_file(test_data_dir / "sample_file.txt") file2 = crate.add_file(test_data_dir / "sample_cwl_wf.cwl") for f in file1, file2: assert f in crate.root_dataset["hasPart"] assert f in crate.data_entities crate.delete(file1) assert file1 not in crate.data_entities assert file1 not in crate.root_dataset["hasPart"] crate.delete(file2) assert file2 not in crate.data_entities assert crate.root_dataset["hasPart"] is None crate.delete(file2) # no-op # contextual entities john = crate.add(Person(crate, '#john', {'name': 'John Doe'})) assert john in crate.contextual_entities crate.delete(john) assert john not in crate.contextual_entities crate.delete(john) # no-op
def test_update(test_data_dir, tmpdir, helpers): crate = ROCrate() assert not crate.root_dataset["hasPart"] wf_path = test_data_dir / "test_galaxy_wf.ga" john = crate.add(Person(crate, '#john', {'name': 'John Doe'})) file_ = crate.add_file(wf_path) assert crate.root_dataset["hasPart"] == [file_] file_["author"] = john assert isinstance(file_, File) assert crate.mainEntity is None wf = crate.add_workflow(wf_path, main=True, lang="galaxy") assert isinstance(wf, ComputationalWorkflow) assert wf["author"] is None assert crate.mainEntity is wf assert crate.dereference(john.id) is john assert crate.dereference(file_.id) is wf assert crate.root_dataset["hasPart"] == [wf] assert crate.root_dataset.properties()["hasPart"] == [{ "@id": "test_galaxy_wf.ga" }] out_path = tmpdir / "ro_crate_out" out_path.mkdir() crate.write_crate(out_path) json_entities = helpers.read_json_entities(out_path) helpers.check_wf_crate(json_entities, wf.id)
def handle_creator(ga_json, crate, workflow): try: gh_creators = ga_json["creator"] except KeyError: return ro_creators = [] for c in gh_creators: is_person = c.get("class").lower() not in { "organisation", "organization" } id_ = c.get("identifier") if is_person else c.get("url") name = c.get("name") if not id_ and not name: continue properties = {"name": name} if name else {} if is_person: creator = Person(crate, identifier=id_, properties=properties) else: # no explicit Organization in ro-crate-py model yet properties["@type"] = "Organization" creator = Entity(crate, identifier=id_, properties=properties) ro_creators.append(creator) if ro_creators: crate.add(*ro_creators) workflow["creator"] = ro_creators
def test_uuid(): crate = ROCrate() new_person = crate.add(Person(crate, properties={"name": "No Identifier"})) jsonld = new_person.as_jsonld() assert "Person" == jsonld["@type"] assert jsonld["@id"].startswith("#") # Check it made a valid UUIDv4 u = uuid.UUID(jsonld["@id"][1:]) assert 4 == u.version
def test_file_writing(test_data_dir, tmpdir, helpers, gen_preview, to_zip): crate = ROCrate(gen_preview=gen_preview) crate_name = 'Test crate' crate.name = crate_name creator_id = '001' creator_name = 'Lee Ritenour' new_person = Person(crate, creator_id, {'name': creator_name}) crate.add(new_person) crate.creator = new_person sample_file_id = 'sample_file.txt' sample_file2_id = 'a/b/sample_file2.csv' test_dir_id = 'test_add_dir/' data_entity_ids = [sample_file_id, sample_file2_id, test_dir_id] file_subdir_id = 'sample_file_subdir.txt' sample_file = test_data_dir / sample_file_id file_returned = crate.add_file(sample_file) assert file_returned.id == sample_file_id file_returned_subdir = crate.add_file(sample_file, sample_file2_id) assert file_returned_subdir.id == sample_file2_id test_dir_path = test_data_dir / test_dir_id test_dir_entity = crate.add_directory(test_dir_path, test_dir_id) assert isinstance(test_dir_entity, Dataset) out_path = tmpdir / 'ro_crate_out' if to_zip: zip_path = tmpdir / 'ro_crate_out.crate.zip' crate.write_zip(zip_path) with zipfile.ZipFile(zip_path, "r") as zf: zf.extractall(out_path) else: out_path.mkdir() crate.write_crate(out_path) metadata_path = out_path / helpers.METADATA_FILE_NAME assert metadata_path.exists() preview_path = out_path / helpers.PREVIEW_FILE_NAME if gen_preview: assert preview_path.exists() else: assert not preview_path.exists() file1 = out_path / sample_file_id file2 = out_path / sample_file2_id file_subdir = out_path / test_dir_id / file_subdir_id assert file1.exists() with open(sample_file) as f1, open(file1) as f2: sample_file_content = f1.read() assert sample_file_content == f2.read() assert file2.exists() with open(file2) as f: assert sample_file_content == f.read() assert file_subdir.exists() with open(test_dir_path / file_subdir_id) as f1, open(file_subdir) as f2: assert f1.read() == f2.read() json_entities = helpers.read_json_entities(out_path) helpers.check_crate(json_entities, data_entity_ids=data_entity_ids) root = json_entities["./"] assert root["name"] == crate_name assert "datePublished" in root formatted_creator_id = f"#{creator_id}" assert root["creator"] == {"@id": formatted_creator_id} assert formatted_creator_id in json_entities assert json_entities[formatted_creator_id]["name"] == creator_name if gen_preview: assert helpers.PREVIEW_FILE_NAME in json_entities