def test_02_request(self): # Check we can instantiate and work with a Request # first make a blank one req = Request() # now make one around the fixture source = RequestFixtureFactory.example() req = Request(source) # make one with a broken source broken = {"whatever" : "broken"} with self.assertRaises(dataobj.DataStructureException): req = Request(broken) # now make one bit by bit req = Request() req.record = source.get("record") req.owner = "test1" req.action = "update" req.public_id = "abcdefg" assert req.owner == "test1" assert req.action == "update" assert req.public_id == "abcdefg" # now make it broken req = Request() with self.assertRaises(dataobj.DataStructureException): req.record = {"random" : "stuff"}
def test_10_request_refs(self): # Check that APC refs are handled correctly by Reuqests # first check that refs are stripped automatically on construction source = RequestFixtureFactory.example() source["record"]["jm:apc"][0]["ref"] = "1234567890" req = Request(source) assert "ref" not in req.apc_records[0] # now do it again, setting the record explicitly source = RequestFixtureFactory.example() record = source.get("record") record["jm:apc"][0]["ref"] = "123456789" req = Request() req.record = record assert "ref" not in req.apc_records[0]
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_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
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_request_dao(self): # Check the DAO methods on the Request object dao = Request() source = RequestFixtureFactory.example() req = Request(source) req.owner = "test1" req.action = "update" req.public_id = "abcdefg" req.save(blocking=True) req2 = dao.pull(req.id) assert req2 is not None # check successful queries for identifiers res = dao.find_by_identifier("doi", "10.1234/me", "test1") assert len(res) == 1 res = dao.find_by_identifier("pmcid", "PMC1234", "test1") assert len(res) == 1 res = dao.find_by_identifier("pmid", "87654321", "test1") assert len(res) == 1 res = dao.find_by_identifier("url", "http://example.com/whatever", "test1") assert len(res) == 1 # check unsuccessful ones res = dao.find_by_identifier("doi", "10.1234/you", "test1") assert len(res) == 0 res = dao.find_by_identifier("pmcid", "PMC5678", "test1") assert len(res) == 0 res = dao.find_by_identifier("pmid", "123456789", "test1") assert len(res) == 0 res = dao.find_by_identifier("url", "http://example.com/this", "test1") assert len(res) == 0 # and check using the wrong owner res = dao.find_by_identifier("doi", "10.1234/me", "test2") assert len(res) == 0 res = dao.find_by_identifier("pmcid", "PMC1234", "test2") assert len(res) == 0 res = dao.find_by_identifier("pmid", "87654321", "test2") assert len(res) == 0 res = dao.find_by_identifier("url", "http://example.com/whatever", "test2") assert len(res) == 0
def test_10_find_request(self): # Find a Request through a number of routes source = RequestFixtureFactory.example() req = Request(source) req.save(blocking=True) time.sleep(2) source = RequestFixtureFactory.example() req1 = Request(source) req1.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_request_by_identifier method result = RequestApi.find_request_by_identifier("doi", "10.1234/me", "abcdefghij") assert result is not None assert result.created_date == req1.created_date result = RequestApi.find_request_by_identifier("pmid", "87654321", "abcdefghij") assert result is not None assert result.created_date == req1.created_date result = RequestApi.find_request_by_identifier("pmcid", "PMC1234", "abcdefghij") assert result is not None assert result.created_date == req1.created_date result = RequestApi.find_request_by_identifier("url", "http://example.com/whatever", "abcdefghij") assert result is not None assert result.created_date == req1.created_date # finally, ensure that you don't get a match when you shouldn't result = RequestApi.find_request_by_identifier("doi", "10.1234/another", "abcdefghij") assert result is None result = RequestApi.find_request_by_identifier("doi", "10.1234/me", "test") assert result is None
def test_07_request2public(self): # Check the conversion of a Request to a PublicAPC source = RequestFixtureFactory.example() req = Request(source) pub = req.make_public_apc() assert pub is not None assert pub.record is not None assert len(pub.apc_records) == 1 setrefs = [] for apc in pub.apc_records: assert apc.get("ref") is not None setrefs.append(apc.get("ref")) assert len(setrefs) == 1 refs = pub.get_apc_refs(req.owner) assert len(refs) == 1 assert refs[0] == setrefs[0]
def test_03_pull_request(self): # Pull a Request through the ApiRequest object acc = MonitorUKAccount() acc.id = "abcdefghij" acc.save(blocking=True) # first make a request which contains that doi req_source = RequestFixtureFactory.example() req = Request(req_source) req.save(blocking=True) # you can't pull a request object, so just show that that's true... # pull by doi should fail result = ApiRequest.pull("10.1234/me", account=acc) assert result is None # pull by request id should fail result = ApiRequest.pull(req.id, account=acc) assert result is None
def test_04_record_methods(self): # Check all the Record methods work classes = [Request, PublicAPC] source = RequestFixtureFactory.example() record = source.get("record") additional_apcs = deepcopy(record.get("jm:apc")) + deepcopy(record.get("jm:apc")) for c in classes: inst = c() assert isinstance(inst, RecordMethods) inst.record = deepcopy(record) assert inst.doi == "10.1234/me" assert inst.pmid == "87654321" assert inst.pmcid == "PMC1234" assert inst.url == "http://example.com/whatever" assert len(inst.apc_records) == 1 inst.apc_records = additional_apcs assert len(inst.apc_records) == 2 # now try adding and removing apc records withref = deepcopy(record.get("jm:apc")[0]) withref["ref"] = "myref" inst.add_apc_record(withref) assert len(inst.apc_records) == 3 inst.remove_apc_by_ref("myref") assert len(inst.apc_records) == 2 for apc in inst.apc_records: assert "ref" not in apc assert inst.has_apcs() is True inst.apc_records = [] assert inst.has_apcs() is False
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_11_process_requests_cycle(self): # Run through the process of processing a Request into a PublicAPC source = RequestFixtureFactory.example() if "id" in source: del source["id"] pub_dao = PublicAPC() wfs_dao = WorkflowState() # first make a record for the first time first = deepcopy(source) del first["record"]["dc:title"] req = Request(first) req.owner = "test" req.action = "update" req.save(blocking=True) # run the job WorkflowApi.process_requests() time.sleep(2) # first check that a public record was made pubs = pub_dao.find_by_doi("10.1234/me") assert len(pubs) == 1 assert pubs[0].record.get("dc:title") is None # check that the workflow state was created wfs = wfs_dao.pull("requests") assert wfs is not None assert wfs.last_request == req.created_date assert wfs.already_processed == [req.id] # now run an update with a different date second = deepcopy(source) second["record"]["dc:title"] = "Update" second["created_date"] = "2002-01-01T00:00:00Z" req2 = Request(second) req2.owner = "test" req2.action = "update" req2.save(blocking=True) # run the job again WorkflowApi.process_requests() time.sleep(2) # check the public record was updated pubs = pub_dao.find_by_doi("10.1234/me") assert len(pubs) == 1 assert pubs[0].record.get("dc:title") == "Update" # check that the workflow state was updated wfs = wfs_dao.pull("requests") assert wfs is not None assert wfs.last_request == req2.created_date assert wfs.already_processed == [req2.id] # now run an update with the same date, to observe the difference in the workflow state third = deepcopy(source) third["record"]["dc:title"] = "Update 2" third["created_date"] = "2002-01-01T00:00:00Z" req3 = Request(third) req3.owner = "test" req3.action = "update" req3.save(blocking=True) # run the job again WorkflowApi.process_requests() time.sleep(2) # check the public record was updated pubs = pub_dao.find_by_doi("10.1234/me") assert len(pubs) == 1 assert ( pubs[0].record.get("dc:title") == "Update 2" ) # should have been updated, as there are only apc contributions from one source # check that the workflow state was updated wfs = wfs_dao.pull("requests") assert wfs is not None assert wfs.last_request == req3.created_date assert wfs.already_processed == [req2.id, req3.id] # processed records should have been appended # finally issue a delete request fourth = deepcopy(source) fourth["created_date"] = "2003-01-01T00:00:00Z" req4 = Request(fourth) req4.owner = "test" req4.action = "delete" req4.save(blocking=True) # run the job again WorkflowApi.process_requests() time.sleep(2) # check the public record was updated pubs = pub_dao.find_by_doi("10.1234/me") assert len(pubs) == 0 # check that the workflow state was updated wfs = wfs_dao.pull("requests") assert wfs is not None assert wfs.last_request == req4.created_date assert wfs.already_processed == [req4.id] # processed records should have been appended