def test_09_remove_permanent(self): # Separate an incoming Request from its corresponding PublicAPC, leaving no owners, thus deleting the record source = RequestFixtureFactory.example() req = Request(source) req.owner = "test" # create a record with 2 distinct apcs from different owners source2 = PublicAPCFixtureFactory.example() pub = PublicAPC(source2) pub.remove_apcs_by_owner("abcdefg") # clear the existing apc record apc_record = PublicAPCFixtureFactory.apc_record() del apc_record["ref"] # do this so that the ref gets created correctly later pub.add_apc_for_owner("test", apc_record) # add a new, known one pub.save(blocking=True) # now request the removal PublicApi.remove(req) time.sleep(2) dao = PublicAPC() pub2 = dao.pull(pub.id) assert pub2 is None
def test_06_publish_update(self): # Publish an update to an existing PublicAPC merge_source = PublicAPCFixtureFactory.record_merge_source() merge_target = PublicAPCFixtureFactory.record_merge_target() apc_record = PublicAPCFixtureFactory.apc_record() result = PublicAPCFixtureFactory.record_merge_result() del merge_source["jm:apc"] del merge_target["jm:apc"] del result["jm:apc"] first = deepcopy(apc_record) second = deepcopy(apc_record) third = deepcopy(apc_record) first["organisation_name"] = "First" del first["ref"] second["organisation_name"] = "Second" del second["ref"] third["organisation_name"] = "Third" del third["ref"] req = Request() req.record = merge_source req.add_apc_record(first) req.owner = "11111" pub = PublicAPC() pub.record = merge_target pub.add_apc_for_owner("22222", second) pub.add_apc_for_owner("11111", third) pub.save(blocking=True) PublicApi.publish(req) dao = PublicAPC() pub2 = dao.pull(pub.id) # first check that the apcs are as we would expect one = pub2.get_apcs_by_owner("11111") two = pub2.get_apcs_by_owner("22222") assert len(one) == 1 assert len(two) == 1 assert one[0]["organisation_name"] == "First" assert two[0]["organisation_name"] == "Second" # now check that the metadata merge proceeded correctly record = pub2.record del record["jm:apc"] assert record == result
def pull(cls, id, account=None): """ Pull a record with the given id, in the context of the supplied account (response content may vary slightly if the user owns the record) The id may be one of 2 things, in order of preference: 1. The DOI 2. The public id :param id: :param account: :return: """ # if this is a DOI, do the DOI queries if id.startswith("10."): pub = PublicApi.find_public_record_by_identifier("doi", id) if pub is not None: return ApiRequest._make_from_public(pub, account) else: dao = PublicAPC() pub = dao.pull(id) if pub is not None: return ApiRequest._make_from_public(pub, account) return None
def test_07_save_delete(self): # Work through acycle of saves and deletes to observe the outputs source = RequestFixtureFactory.record() acc = MonitorUKAccount() acc.save(blocking=True) req = ApiRequest(source, account=acc) req.save() dao = Request() req2 = dao.pull(req.request.id) assert req2 is not None assert req2.owner == acc.id assert req2.record == source assert req2.action == "update" # now publish the request PublicApi.publish(req2) time.sleep(2) # now pull the object as identified by its API identifier (which should be the DOI) source2 = deepcopy(source) source2["dc:title"] = "An update" next = ApiRequest.pull(req.id, account=acc) next.update(source2) next.save() # now, at this point we should have 2 request objects in the index. One for the # original save, and one for the new save req3 = dao.pull(next.request.id) assert req3 is not None assert req3.owner == acc.id assert req3.record == source2 assert req3.action == "update" # now issue a delete on the same record next.delete() # by now we should have 3 request objects in the index, 2 for the above updates # and one for the delete request req4 = dao.pull(next.request.id) assert req4 is not None assert req4.owner == acc.id assert req4.record == source2 assert req4.action == "delete"
def test_07_separate_records(self): # Separate an incoming Request from its corresponding PublicAPC apc_record = PublicAPCFixtureFactory.apc_record() req = Request() req.owner = "test" pub = PublicAPC() pub.add_apc_for_owner("test", apc_record) pub.add_apc_for_owner("test", apc_record) assert len(pub.apc_records) == 2 PublicApi.separate_records(req, pub) assert not pub.has_apcs()
def test_03_publish_new(self): # Publish a new request source = RequestFixtureFactory.example() req = Request(source) pub = PublicApi.publish(req) dao = PublicAPC() pub2 = dao.pull(pub.id) assert pub2 is not None
def test_05_enhance_metadata(self): # Enhance the bibliographic metadata on a PublicAPC record merge_source = PublicAPCFixtureFactory.record_merge_source() merge_target = PublicAPCFixtureFactory.record_merge_target() result = PublicAPCFixtureFactory.record_merge_result() source = PublicAPC() source.record = merge_source source.set_apc_ref("22222", "bbbbb") target = PublicAPC() target.record = merge_target target.set_apc_ref("11111", "aaaaa") PublicApi.enhance_metadata(source, target) assert target.record == result assert target.get_apc_refs("11111") == ["aaaaa"] assert target.get_apc_refs("22222") == []
def test_12_publish_enhancement(self): # Publish an Enhancement where no public record exists source = EnhancementFixtureFactory.example() en = Enhancement(source) pub = PublicApi.publish(en) # check that no public record was saved assert pub.id is None dao = PublicAPC() pubs = [p for p in dao.iterall()] assert len(pubs) == 0
def test_08_remove_separate(self): # Separate an incoming Request from its corresponding PublicAPC, leaving only one owner behind source = RequestFixtureFactory.example() req = Request(source) req.owner = "test" # create a record with 2 distinct apcs from different owners source2 = PublicAPCFixtureFactory.example() apc_record = PublicAPCFixtureFactory.apc_record() del apc_record["ref"] # do this so that the ref gets created correctly later pub = PublicAPC(source2) pub.add_apc_for_owner("test", apc_record) pub.save(blocking=True) # now request the removal PublicApi.remove(req) time.sleep(2) dao = PublicAPC() pub2 = dao.pull(pub.id) assert len(pub2.get_apcs_by_owner("test")) == 0 assert len(pub2.get_apcs_by_owner("abcdefg")) == 1
def test_04_merge_public_apcs(self): # Merge two PublicAPC records together source_source = PublicAPCFixtureFactory.example() target_source = PublicAPCFixtureFactory.example() # first try a merge with no apc records (this shouldn't ever happen in normal operation) ss1 = deepcopy(source_source) del ss1["record"]["jm:apc"] source1 = PublicAPC(ss1) ts1 = deepcopy(target_source) del ts1["record"]["jm:apc"] target1 = PublicAPC(ts1) result = PublicApi.merge_public_apcs(source1, target1) assert len(result.apc_records) == 0 # next try a merge with only apc records in the source (again, shouldn't really happen in real life) ss2 = deepcopy(source_source) source2 = PublicAPC(ss2) ts2 = deepcopy(target_source) del ts2["record"]["jm:apc"] target2 = PublicAPC(ts2) result = PublicApi.merge_public_apcs(source2, target2) assert len(result.apc_records) == 1 # next try a merge with only apc records in the target (also shouldn't happen in real life) ss3 = deepcopy(source_source) source3 = PublicAPC(ss3) ts3 = deepcopy(target_source) del ts3["record"]["jm:apc"] target3 = PublicAPC(ts3) result = PublicApi.merge_public_apcs(source3, target3) assert len(result.apc_records) == 1 # finally try a merge with the following criteria: # replacements apcs and new apcs in the source record # existing apcs and other apcs in the target record apc_record = PublicAPCFixtureFactory.apc_record() first = deepcopy(apc_record) first["ref"] = "aaaaa" second = deepcopy(apc_record) second["ref"] = "bbbbb" ss4 = deepcopy(source_source) del ss4["record"]["jm:apc"] source4 = PublicAPC(ss4) source4.add_apc_for_owner("11111", first) source4.add_apc_for_owner("11111", second) third = deepcopy(apc_record) third["ref"] = "ccccc" fourth = deepcopy(apc_record) fourth["ref"] = "ddddd" ts4 = deepcopy(target_source) del ts4["record"]["jm:apc"] target4 = PublicAPC(ts4) target4.add_apc_for_owner("11111", third) target4.add_apc_for_owner("22222", fourth) result = PublicApi.merge_public_apcs(source4, target4) assert len(result.apc_records) == 3 ones = result.get_apcs_by_owner("11111") assert len(ones) == 2 refs = [o.get("ref") for o in ones] assert "aaaaa" in refs assert "bbbbb" in refs assert "ccccc" not in refs assert "ddddd" not in refs twos = result.get_apcs_by_owner("22222") assert len(twos) == 1 refs = [o.get("ref") for o in twos] assert "aaaaa" not in refs assert "bbbbb" not in refs assert "ccccc" not in refs assert "ddddd" in refs
def test_02_find_public_record(self): # Find a public record with a variety of identifiers source = PublicAPCFixtureFactory.example() pub = PublicAPC(source) pub.save(blocking=True) # document to form the basis of the queries source2 = RequestFixtureFactory.example() # create sources with one of each kind of identifier, then look them up using the # find_public_record and find_public_record_by_identifier methods pid = deepcopy(source2) del pid["record"]["dc:identifier"] req = Request(pid) req.public_id = pub.id pub1 = PublicApi.find_public_record(req) assert pub1 is not None doi = deepcopy(source2) doi["record"]["dc:identifier"] = [{"type": "doi", "id": "10.1234/me"}] req = Request(doi) pub1 = PublicApi.find_public_record(req) assert pub1 is not None pub11 = PublicApi.find_public_record_by_identifier("doi", "10.1234/me") assert pub11 is not None pmid = deepcopy(source2) pmid["record"]["dc:identifier"] = [{"type": "pmid", "id": "87654321"}] req = Request(pmid) pub1 = PublicApi.find_public_record(req) assert pub1 is not None pub11 = PublicApi.find_public_record_by_identifier("pmid", "87654321") assert pub11 is not None pmcid = deepcopy(source2) pmcid["record"]["dc:identifier"] = [{"type": "pmcid", "id": "PMC1234"}] req = Request(pmcid) pub1 = PublicApi.find_public_record(req) assert pub1 is not None pub11 = PublicApi.find_public_record_by_identifier("pmcid", "PMC1234") assert pub11 is not None url = deepcopy(source2) url["record"]["dc:identifier"] = [{"type": "url", "id": "http://example.com/whatever"}] req = Request(url) pub1 = PublicApi.find_public_record(req) assert pub1 is not None pub11 = PublicApi.find_public_record_by_identifier("url", "http://example.com/whatever") assert pub11 is not None # finally, ensure that you don't get a match when you shouldn't null = deepcopy(source2) null["record"]["dc:identifier"] = [{"type": "doi", "id": "10.1234/another"}] req = Request(null) pub1 = PublicApi.find_public_record(req) assert pub1 is None pub11 = PublicApi.find_public_record_by_identifier("doi", "10.1234/another") assert pub11 is None