def test_creates_aop_bundle_if_not_found(self, mk_create_aop_bundle, mk_hooks): bundle_id = "0034-8910-aop" error = requests.exceptions.HTTPError("Bundle not found") error.response = Mock(status_code=http.client.NOT_FOUND) mk_hooks.kernel_connect.side_effect = [error, Mock(spec=requests.Response)] get_or_create_bundle(bundle_id, True) mk_create_aop_bundle.assert_called_once_with(bundle_id)
def test_raises_exception_if_issue_not_found_in_kernel(self, mk_create_aop_bundle, mk_hooks): bundle_id = "0034-8910-rsp-48-2-0347" error = requests.exceptions.HTTPError("Bundle not found") error.response = Mock(status_code=http.client.NOT_FOUND) mk_hooks.kernel_connect.side_effect = error with self.assertRaises(LinkDocumentToDocumentsBundleException) as exc_info: get_or_create_bundle(bundle_id, False) self.assertEqual(str(exc_info.exception), "Bundle not found") self.assertEqual(exc_info.exception.response, error.response) mk_create_aop_bundle.assert_not_called()
def test_returns_kernel_response(self, mk_create_aop_bundle, mk_hooks): bundle_id = "0034-8910-aop" error = requests.exceptions.HTTPError("Bundle not found") error.response = Mock(status_code=http.client.NOT_FOUND) MockResponse = Mock(spec=requests.Response) mk_hooks.kernel_connect.side_effect = [error, MockResponse] result = get_or_create_bundle(bundle_id, True) self.assertEqual(result, MockResponse) mk_hooks.kernel_connect.assert_called_with("/bundles/" + bundle_id, "GET")
def link_documents_to_documentsbundle(sps_package, documents, issn_index_json_path): """ Relaciona documentos com seu fascículos(DocumentsBundle). :param kwargs['sps_package']: Path do pacote SPS com os documentos :param kwargs['documents']: Uma lista de dicionários contento os atributos necessários para a descoberta do fascículo. Exemplo contendo a lista de atributos(mínimo): [ { "scielo_id": "S0034-8910.2014048004923", "issn": "0034-8910", "year": "2014", "volume": "48", "number": "2", "order": "347", }, { "scielo_id": "S0034-8910.2014048004924", "issn": "0034-8910", "year": "2014", "volume": "48", "number": "2", "order": "348", }, { "scielo_id": "S0034-8910.20140078954641", "issn": "1518-8787", "year": "2014", "volume": "02", "number": "2", "order": "978", }, { "scielo_id": "S0034-8910.20140078954641", "issn": "1518-8787", "year": "2014", "volume": "02", "number": "2", "order": "978", "supplement": "1", } ] {"id": "0034-8910-2014-v48-n2", "status":204} Return a list of document linkd or not, something like: [ {'id': 'S0034-8910.2014048004923', 'status': 204}, {'id': 'S0034-8910.20140078954641', 'status': 422}, {'id': 'S0034-8910.20140078923452', 'status': 404}, ] """ Logger.info("link_documents_to_documentsbundle PUT") ret = [] issn_id = '' bundle_id = '' bundle_id_doc = {} executions = [] if documents: Logger.info('Reading ISSN index file %s', issn_index_json_path) with open(issn_index_json_path) as issn_index_file: issn_index_json = issn_index_file.read() issn_index = json.loads(issn_index_json) for doc in documents: try: issn_id = issn_index[doc["issn"]] except KeyError as exc: Logger.info( 'Could not get journal ISSN ID: ISSN id "%s" not found', doc["issn"] ) executions.append( { "pid": doc.get("scielo_id"), "bundle_id": None, "error": 'Could not get journal ISSN ID: ISSN id "%s" not found' % doc["issn"], } ) else: bundle_id = get_bundle_id(issn_id=issn_id, year=doc.get("year"), volume=doc.get("volume", None), number=doc.get("number", None), supplement=doc.get("supplement", None)) bundle_id_doc.setdefault(bundle_id, []) payload_doc = {} payload_doc['id'] = doc.get("scielo_id") payload_doc['order'] = doc.get("order") bundle_id_doc[bundle_id].append(payload_doc) def _update_items_list(new_items: list, current_items: list) -> list: """Retorna uma lista links atualizada a partir dos items atuais e dos novos items.""" items = deepcopy(current_items) for new_item in new_items: for index, current_item in enumerate(items): if new_item["id"] == current_item["id"]: items[index] = new_item break else: items.append(new_item) return items is_aop_bundle = "ahead" in sps_package for bundle_id, new_items in bundle_id_doc.items(): try: conn_response = get_or_create_bundle(bundle_id, is_aop=is_aop_bundle) except LinkDocumentToDocumentsBundleException as exc: ret.append({"id": bundle_id, "status": exc.response.status_code}) Logger.info("Could not get bundle %: Bundle not found", bundle_id) for new_item_relationship in new_items: executions.append( { "pid": new_item_relationship.get("id"), "bundle_id": bundle_id, "failed": True, "error": str(exc) } ) else: current_items = conn_response.json()["items"] payload = _update_items_list(new_items, current_items) Logger.info("Registering bundle_id %s with %s", bundle_id, payload) if DeepDiff(current_items, payload, ignore_order=True): response = update_documents_in_bundle(bundle_id, payload) ret.append({"id": bundle_id, "status": response.status_code}) logging.info( "The bundle %s items list has been updated." % bundle_id ) for new_item_relationship in new_items: executions.append( { "pid": new_item_relationship.get("id"), "bundle_id": bundle_id, } ) else: logging.info( "The bundle %s items does not need to be updated." % bundle_id ) if not is_aop_bundle: try: articles_removed_from_aop = update_aop_bundle_items( issn_id, payload ) except LinkDocumentToDocumentsBundleException as exc: Logger.error(str(exc)) else: executions.extend(articles_removed_from_aop) return (ret, executions) Logger.info("link_documents_to_documentsbundle OUT")
def test_tries_to_get_bundle_from_kernel(self, mk_create_aop_bundle, mk_hooks): bundle_id = "0034-8910-aop" get_or_create_bundle(bundle_id, True) mk_hooks.kernel_connect.assert_called_once_with( "/bundles/" + bundle_id, "GET")
def test_returns_kernel_response(self, mk_create_aop_bundle, mk_hooks): bundle_id = "0034-8910-rsp-48-2-0347" MockResponse = Mock(spec=requests.Response) mk_hooks.kernel_connect.return_value = MockResponse result = get_or_create_bundle(bundle_id, False) self.assertEqual(result, MockResponse)