def test_register_document_documentsbundle_to_documentsbundle_calls_kernel_connect( self, mk_hooks): """ Verifica se register_document invoca kernel_connect com os parâmetros corretos. """ update_documents_in_bundle("0066-782X-1999-v72-n0", self.payload) mk_hooks.kernel_connect.assert_called_once_with( "/bundles/0066-782X-1999-v72-n0/documents", "PUT", [{ "id": "0034-8910-rsp-48-2-0347", "order": "01" }, { "id": "0034-8910-rsp-48-2-0348", "order": "02" }])
def test_if_register_document_documentsbundle_return_status_code_204_with_correct_params(self, mk_hooks): """ Verifica se ao invocarmos update_documents_in_bundle com o ID do bundle e payload corretos o retorno é o esperado. Status code 204 significa que os documentos foram atualizado com sucesso. """ mk_hooks.kernel_connect.return_value.status_code = requests.codes.no_content payload = [ {"id": "0034-8910-rsp-48-2-0347", "order": "01"}, {"id": "0034-8910-rsp-48-2-0348", "order": "02"}, ] response = update_documents_in_bundle("0066-782X-1999-v72-n0", payload) self.assertEqual(response.status_code, 204)
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")