def test_list_metadata_formats_as_object_return_object_and_ok_status(self, mock_metadata_format, mock_transform): # Arrange mock_metadata_format.return_value = [], status.HTTP_200_OK mock_transform.return_value = [OaiHarvesterMetadataFormat(), OaiHarvesterMetadataFormat()] # Act data, status_code = oai_verbs_api.list_metadata_formats_as_object("") # Assert self.assertTrue(all(isinstance(item, OaiHarvesterMetadataFormat) for item in data)) self.assertEquals(status_code, status.HTTP_200_OK)
def test_harvest_records_returns_errors_if_not_HTTP_200_OK( self, mock_list_records): """ Args: mock_list_records: Returns: """ # Arrange resumption_token = None content = OaiPmhMessage.get_message_labelled("Error") status_code = status.HTTP_500_INTERNAL_SERVER_ERROR mock_list_records.return_value = ( Response(content, status=status_code), resumption_token, ) expected_error = [{"status_code": status_code, "error": "Error"}] registry = Mock(spec=OaiRegistry()) registry.url = "dummy_url" metadata_format = Mock(spec=OaiHarvesterMetadataFormat()) metadata_format.metadata_prefix = "oai_dummy" last_update = registry_all_sets = None # Act result = oai_registry_api._harvest_records(registry, metadata_format, last_update, registry_all_sets) # Assert self.assertEquals(result, expected_error)
def _create_oai_harvester_metadata_format(): """ Get an OaiHarvesterMetadataFormat object. Returns: OaiHarvesterMetadataFormat instance. """ oai_harvester_metadata_format = OaiHarvesterMetadataFormat() _set_oai_harvester_metadata_format_fields(oai_harvester_metadata_format) return oai_harvester_metadata_format
def transform_dict_metadata_format_to_oai_harvester_metadata_format(data): """ Transforms a dict to a list of OaiHarvesterMetadataFormat object. Args: data: Data to transform. Returns: List of OaiHarvesterMetadataFormat instances. """ return [ OaiHarvesterMetadataFormat(metadata_prefix=obj['metadataPrefix'], metadata_namespace=obj['metadataNamespace'], schema=obj['schema'], raw=raw_xml_to_dict(obj['raw'])) for obj in data ]
def _set_oai_record_fields(oai_record): """Set OaiRecord fields. Args: oai_record: Returns: OaiRecord with assigned fields. """ oai_record.identifier = "oai:test/id.0006" oai_record.last_modification_date = datetime.datetime.now() oai_record.deleted = False oai_record.harvester_sets = [OaiHarvesterSet(), OaiHarvesterSet()] oai_record.harvester_metadata_format = OaiHarvesterMetadataFormat() oai_record.registry = OaiRegistry() oai_record.xml_content = "<test><message>Hello</message></test>" return oai_record
def test_upsert_creates_if_does_not_exist(self, mock_convert_file): """ Test upsert create """ # Arrange oai_record = OaiPmhMock.mock_oai_first_record() metadata_format = OaiHarvesterMetadataFormat() metadata_format.id = ObjectId() self.fixture.insert_registry(insert_related_collections=False) mock_convert_file.return_value = None # Act oai_registry_api._upsert_record_for_registry(oai_record, metadata_format, self.fixture.registry) # Assert record_in_database = oai_record_api.\ get_by_identifier_and_metadata_format(oai_record.identifier, metadata_format) self.assertEquals(record_in_database, oai_record)
def test_harvest_records_returns_errors_if_transform_raises( self, mock_list_records, mock_transform_operations): """ Args: mock_list_records: mock_transform_operations: Returns: """ # Arrange resumption_token = None content = [] status_code = status.HTTP_200_OK mock_list_records.return_value = ( Response(content, status=status_code), resumption_token, ) error_message = "Error" expected_error = [{ "status_code": status.HTTP_400_BAD_REQUEST, "error": error_message }] registry = Mock(spec=OaiRegistry()) registry.url = "dummy_url" metadata_format = Mock(spec=OaiHarvesterMetadataFormat()) metadata_format.metadata_prefix = "oai_dummy" last_update = registry_all_sets = None mock_transform_operations.side_effect = Exception(error_message) # Act result = oai_registry_api._harvest_records(registry, metadata_format, last_update, registry_all_sets) # Assert self.assertEquals(result, expected_error)