def test_service_writer_duplicate(lang_type, lang_data, service, identity): writer = ServiceWriter(service, identity) _ = writer.write(stream_entry=StreamEntry(lang_data)) with pytest.raises(WriterError) as err: writer.write(stream_entry=StreamEntry(lang_data)) expected_error = [f"Vocabulary entry already exists: {lang_data}"] assert expected_error in err.value.args
def test_names_service_writer_duplicate(app, es_clear, name_full_data): writer = NamesServiceWriter("names", system_identity) _ = writer.write(stream_entry=StreamEntry(name_full_data)) Name.index.refresh() # refresh index to make changes live with pytest.raises(WriterError) as err: writer.write(stream_entry=StreamEntry(name_full_data)) expected_error = [f"Vocabulary entry already exists: {name_full_data}"] assert expected_error in err.value.args
def test_funders_service_writer_duplicate(app, es_clear, funder_full_data): writer = FundersServiceWriter("funders", system_identity) funder_rec = writer.write(stream_entry=StreamEntry(funder_full_data)) Funder.index.refresh() # refresh index to make changes live with pytest.raises(WriterError) as err: writer.write(stream_entry=StreamEntry(funder_full_data)) expected_error = [f"Vocabulary entry already exists: {funder_full_data}"] assert expected_error in err.value.args # not-ideal cleanup funder_rec.entry._record.delete(force=True)
def test_service_writer_update_existing(lang_type, lang_data, service, identity): # create vocabulary writer = ServiceWriter(service, identity, update=True) lang = writer.write(stream_entry=StreamEntry(lang_data)) # update vocabulary updated_lang = deepcopy(lang_data) updated_lang["description"]["en"] = "Updated english description" updated_lang["tags"].append("updated") # check changes vocabulary _ = writer.write(stream_entry=StreamEntry(updated_lang)) record = service.read(identity, ("languages", lang.entry.id)) record = record.to_dict() assert dict(record, **updated_lang) == record
def dict_award_entry_ec(): """Full award data.""" return StreamEntry({ "acronym": "TS", "code": "129123", "enddate": "2025-12-31", "funding": [{ "funding_stream": { "description": "Test stream", "id": "TST::test::test", }, "jurisdiction": "GR", "name": "Test Name", "shortName": "TST" }], "h2020programme": [], "id": "40|corda__h2020::000000000000000000", "openaccessmandatefordataset": False, "openaccessmandateforpublications": False, "startdate": "2008-04-01", "subject": ["Oceanography"], "title": "Test title", "websiteurl": "https://test.com" })
def dict_xml_entry(): return StreamEntry({ 'orcid-identifier': { 'uri': 'https://orcid.org/0000-0001-8135-3489', 'path': '0000-0001-8135-3489', 'host': 'orcid.org' }, 'person': { 'name': { 'given-names': 'Lars Holm', 'family-name': 'Nielsen', '@visibility': 'public', '@path': '0000-0001-8135-3489' }, 'external-identifiers': { '@path': '/0000-0001-8135-3489/external-identifiers' }, '@path': '/0000-0001-8135-3489/person' }, 'activities-summary': { 'employments': { 'affiliation-group': { 'employment-summary': { 'organization': { 'name': 'CERN' } } }, '@path': '/0000-0001-8135-3489/employments' }, '@path': '/0000-0001-8135-3489/activities' }, '@path': '/0000-0001-8135-3489' })
def dict_award_entry(): return StreamEntry({ "acronym": "TA", "code": "0751743", "enddate": "2010-09-30", "funding": [{ "funding_stream": { "description": "Directorate for Geosciences - Division of " "Ocean Sciences", "id": "NSF::GEO/OAD::GEO/OCE", }, "jurisdiction": "US", "name": "National Science Foundation", "shortName": "NSF" }], "h2020programme": [], "id": "40|nsf_________::3eb1b4f6d6e251a19f9fdeed2aab88d8", "openaccessmandatefordataset": False, "openaccessmandateforpublications": False, "startdate": "2008-04-01", "subject": ["Oceanography"], "title": "Test title", "websiteurl": "https://test.com" })
def test_service_writer_non_existing(lang_type, lang_data, service, identity): writer = ServiceWriter(service, identity) lang = writer.write(stream_entry=StreamEntry(lang_data)) record = service.read(identity, ("languages", lang.entry.id)) record = record.to_dict() assert dict(record, **lang_data) == record
def test_funders_service_writer_create(app, es_clear, funder_full_data): writer = FundersServiceWriter("funders", system_identity) funder_rec = writer.write(StreamEntry(funder_full_data)) funder_dict = funder_rec.entry.to_dict() assert dict(funder_dict, **funder_full_data) == funder_dict # not-ideal cleanup funder_rec.entry._record.delete(force=True)
def test_names_service_writer_update_existing(app, es_clear, name_full_data, names_service): # create vocabulary writer = NamesServiceWriter("names", system_identity, update=True) name = writer.write(stream_entry=StreamEntry(name_full_data)) Name.index.refresh() # refresh index to make changes live # update vocabulary updated_name = deepcopy(name_full_data) updated_name["given_name"] = "Pablo" updated_name["family_name"] = "Panero" # check changes vocabulary _ = writer.write(stream_entry=StreamEntry(updated_name)) record = names_service.read(system_identity, name.entry.id) record = record.to_dict() # needed while the writer resolves from ES assert _.entry.id == name.entry.id assert dict(record, **updated_name) == record
def test_funders_service_writer_update_existing(app, es_clear, funder_full_data, service): # create vocabulary writer = FundersServiceWriter("funders", system_identity, update=True) orig_funder_rec = writer.write(stream_entry=StreamEntry(funder_full_data)) Funder.index.refresh() # refresh index to make changes live # update vocabulary updated_funder = deepcopy(funder_full_data) updated_funder["name"] = "Updated Name" # check changes vocabulary _ = writer.write(stream_entry=StreamEntry(updated_funder)) funder_rec = service.read(system_identity, orig_funder_rec.entry.id) funder_dict = funder_rec.to_dict() # needed while the writer resolves from ES assert _.entry.id == orig_funder_rec.entry.id assert dict(funder_dict, **updated_funder) == funder_dict # not-ideal cleanup funder_rec._record.delete(force=True)
def test_awards_service_writer_create(app, es_clear, example_funder_ec, award_full_data): awards_writer = AwardsServiceWriter("awards", system_identity) award_rec = awards_writer.write(StreamEntry(award_full_data)) award_dict = award_rec.entry.to_dict() award_full_data["funder"]["name"] = example_funder_ec["name"] assert dict(award_dict, **award_full_data) == award_dict # not-ideal cleanup award_rec.entry._record.delete(force=True)
def test_awards_funder_id_not_exist_no_funders(app, es_clear, award_full_data_invalid_id): awards_writer = AwardsServiceWriter("awards", system_identity) with pytest.raises(WriterError) as err: awards_writer.write(StreamEntry(award_full_data_invalid_id)) expected_error = [{ 'InvalidRelationValue': 'Invalid value {funder_id}.'.format( funder_id=award_full_data_invalid_id.get('funder').get('id')) }] assert expected_error in err.value.args
def test_awards_service_writer_update_existing(app, es_clear, example_funder_ec, award_full_data, service): # create vocabulary writer = AwardsServiceWriter("awards", system_identity, update=True) orig_award_rec = writer.write(stream_entry=StreamEntry(award_full_data)) Award.index.refresh() # refresh index to make changes live # update vocabulary updated_award = deepcopy(award_full_data) updated_award["title"] = {"en": "New Test title"} # check changes vocabulary _ = writer.write(stream_entry=StreamEntry(updated_award)) award_rec = service.read(system_identity, orig_award_rec.entry.id) award_dict = award_rec.to_dict() updated_award["funder"]["name"] = example_funder_ec["name"] # needed while the writer resolves from ES assert _.entry.id == orig_award_rec.entry.id assert dict(award_dict, **updated_award) == award_dict # not-ideal cleanup award_rec._record.delete(force=True)
def test_names_service_writer_update_non_existing(app, es_clear, name_full_data, names_service): # vocabulary item not created, call update directly updated_name = deepcopy(name_full_data) updated_name["given_name"] = "Pablo" updated_name["family_name"] = "Panero" # check changes vocabulary writer = NamesServiceWriter("names", system_identity, update=True) name = writer.write(stream_entry=StreamEntry(updated_name)) record = names_service.read(system_identity, name.entry.id) record = record.to_dict() assert dict(record, **updated_name) == record
def test_bad_xml_transformer(): bytes_xml_entry = StreamEntry( bytes( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' '<field_one:field_one>value</field_one:field_one>\n' '<multi_field:multi_field>\n' ' <some:some>value</some:some>\n' ' <another:another>value too</another:another>\n' '</multi_field:multi_field>\n', encoding="raw_unicode_escape")) transformer = XMLTransformer() with pytest.raises(TransformerError): transformer.apply(bytes_xml_entry)
def test_xml_transformer(expected_from_xml): bytes_xml_entry = StreamEntry( bytes( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' '<record:record>\n' ' <field_one:field_one>value</field_one:field_one>\n' ' <multi_field:multi_field>\n' ' <some:some>value</some:some>\n' ' <another:another>value too</another:another>\n' ' </multi_field:multi_field>\n' '</record:record>\n', encoding="raw_unicode_escape")) transformer = XMLTransformer() assert expected_from_xml == transformer.apply(bytes_xml_entry).entry
def test_funders_service_writer_update_non_existing(app, es_clear, funder_full_data, service): # vocabulary item not created, call update directly updated_funder = deepcopy(funder_full_data) updated_funder["name"] = "New name" # check changes vocabulary writer = FundersServiceWriter("funders", system_identity, update=True) funder_rec = writer.write(stream_entry=StreamEntry(updated_funder)) funder_rec = service.read(system_identity, funder_rec.entry.id) funder_dict = funder_rec.to_dict() assert dict(funder_dict, **updated_funder) == funder_dict # not-ideal cleanup funder_rec._record.delete(force=True)
def test_awards_service_writer_update_non_existing(app, es_clear, example_funder_ec, award_full_data, service): # vocabulary item not created, call update directly updated_award = deepcopy(award_full_data) updated_award["title"] = {"en": "New Test title"} # check changes vocabulary writer = AwardsServiceWriter("awards", system_identity, update=True) award_rec = writer.write(stream_entry=StreamEntry(updated_award)) award_rec = service.read(system_identity, award_rec.entry.id) award_dict = award_rec.to_dict() updated_award["funder"]["name"] = example_funder_ec["name"] assert dict(award_dict, **updated_award) == award_dict # not-ideal cleanup award_rec._record.delete(force=True)
def dict_ror_entry(): return StreamEntry({ "id": "https://ror.org/0aaaaaa11", "name": "Funder", "types": ["Facility"], "links": ["http://test.com"], "aliases": [], "acronyms": ["FND"], "status": "active", "wikipedia_url": "https://en.wikipedia.org/wiki/FUNDER_TEST", "labels": [{ "label": "Funder", "iso639": "en" }, { "label": "Geldgeber", "iso639": "de" }], "email_address": None, "ip_addresses": [], "established": 1954, "country": { "country_code": "GR", "country_name": "Greece" }, "relationships": [], "external_ids": { "ISNI": { "preferred": None, "all": ["0000 0001 2156 142X"] }, "GRID": { "preferred": "grid.9132.9", "all": "grid.9132.9" }, "FundRef": { "preferred": "000000000000", "all": [] } } })
def test_yaml_writer(): filepath = Path('writer_test.yaml') test_output = [{ "key_one": [{ "inner_one": 1 }] }, { "key_two": [{ "inner_two": "two" }] }] writer = YamlWriter(filepath=filepath) for output in test_output: writer.write(stream_entry=StreamEntry(output)) with open(filepath) as file: assert yaml.safe_load(file) == test_output filepath.unlink()
def test_names_service_writer_create(app, es_clear, name_full_data): writer = NamesServiceWriter("names", system_identity) record = writer.write(StreamEntry(name_full_data)) record = record.entry.to_dict() assert dict(record, **name_full_data) == record