def test_add_registry_metadata_format(self, mock_identify, mock_metadata_formats, mock_sets, mock_get): """ Test add registry metadata formats Args: mock_identify: mock_metadata_formats: mock_sets: mock_get: Returns: """ # Arrange mock_identify.return_value = OaiPmhMock.mock_oai_identify( ), status.HTTP_200_OK list_metadata_formats = OaiPmhMock.mock_oai_metadata_format() mock_metadata_formats.return_value = list_metadata_formats, status.HTTP_200_OK mock_sets.return_value = [], status.HTTP_200_OK text = '<test>Hello</test>' mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = text # Act result = oai_registry_api.add_registry_by_url( self.fixture.url, self.fixture.harvest_rate, self.fixture.harvest) # Assert _assert_metadata_format(self, list_metadata_formats, result.id)
def test_add_registry(self, mock_identify, mock_metadata_formats, mock_sets, mock_get): """ Test add registry Args: mock_identify: mock_metadata_formats: mock_sets: mock_get: Returns: """ # Arrange mock_identify.return_value = OaiPmhMock.mock_oai_identify( ), status.HTTP_200_OK mock_metadata_formats.return_value = OaiPmhMock.mock_oai_metadata_format(), \ status.HTTP_200_OK mock_sets.return_value = OaiPmhMock.mock_oai_set(), status.HTTP_200_OK text = '<test>Hello</test>' mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = text # Act result = oai_registry_api.add_registry_by_url( self.fixture.url, self.fixture.harvest_rate, self.fixture.harvest) # Assert self.assertIsInstance(result, OaiRegistry)
def test_add_registry_sets(self, mock_identify, mock_metadata_formats, mock_sets, mock_get): """Test add registry sets Args: mock_identify: mock_metadata_formats: mock_sets: mock_get: Returns: """ # Arrange mock_user = create_mock_user("1", is_superuser=True) mock_request = create_mock_request(user=mock_user) mock_identify.return_value = OaiPmhMock.mock_oai_identify( ), status.HTTP_200_OK mock_metadata_formats.return_value = [], status.HTTP_200_OK list_sets = OaiPmhMock.mock_oai_set() mock_sets.return_value = list_sets, status.HTTP_200_OK text = "<test>Hello</test>" mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = text # Act result = oai_registry_api.add_registry_by_url( self.fixture.url, self.fixture.harvest_rate, self.fixture.harvest, request=mock_request, ) # Assert _assert_set(self, list_sets, result.id)
def test_upsert_creates_if_does_not_exist(self, mock_get): """ Test upsert create Args: mock_get: Returns: """ # Arrange text = '<test>Hello</test>' mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = text oai_harvester_metadata_format = OaiPmhMock.mock_oai_first_metadata_format( ) self.fixture.insert_registry(insert_related_collections=False) # Act oai_registry_api._upsert_metadata_format_for_registry( oai_harvester_metadata_format, self.fixture.registry) # Assert metadata_format_in_database = oai_harvester_metadata_format_api. \ get_by_metadata_prefix_and_registry_id(oai_harvester_metadata_format.metadata_prefix, self.fixture.registry.id) self.assertEquals(oai_harvester_metadata_format, metadata_format_in_database)
def test_harvest_registry_updates_dates(self, mock_convert_file, mock_get): """Test harvest update Args: mock_get: mock_convert_file: Returns: """ # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records( with_resumption_token=False) mock_convert_file.return_value = None # Assert # Registry date self.assertEquals(self.fixture.registry.last_update, None) # Act result = oai_registry_api.harvest_registry(self.fixture.registry) # Assert self.assertEquals(result, []) # Registry date self.assertNotEquals(self.fixture.registry.last_update, None)
def test_harvest_params(self, mock_get): # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records( ) expected_params = { "verb": "ListRecords", "metadataPrefix": self.metadata_prefix, "set": self.set, "from": self.from_, "until": self.until, } # Act oai_verbs_api.list_records( url=self.url, metadata_prefix=self.metadata_prefix, set_h=self.set, from_date=self.from_, until_date=self.until, ) # Assert mock_get.assert_called_with(self.url, expected_params, verify=SSL_CERTIFICATES_DIR)
def test_add_registry_by_url_raises_exception_if_bad_sets_data( self, mock_registry, mock_identify, mock_set, mock_transform): """ Args: mock_registry: mock_identify: mock_set: mock_transform: Returns: """ # Arrange mock_user = create_mock_user("1", is_superuser=True) mock_request = create_mock_request(user=mock_user) mock_registry.return_value = False mock_identify.return_value = OaiPmhMock.mock_oai_identify( ), status.HTTP_200_OK mock_set.return_value = [], status.HTTP_200_OK exception_message = "Bad sets" mock_transform.side_effect = Exception(exception_message) # Act + Assert with self.assertRaises( oai_pmh_exceptions.OAIAPILabelledException) as ex: oai_registry_api.add_registry_by_url(self.url, self.harvest_rate, self.harvest, request=mock_request) self.assertTrue(exception_message in ex.exception.message) self.assertEqual(ex.exception.status_code, status.HTTP_400_BAD_REQUEST)
def test_harvest_by_metadata_formats_and_sets_saves_record( self, mock_convert_file, mock_get): """Test harvest by metadata formats and sets save Args: mock_get: Returns: """ # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records( with_resumption_token=False) metadata_format = [self.fixture.oai_metadata_formats[0]] set_ = [self.fixture.oai_sets[0]] mock_convert_file.return_value = None # Act result = oai_registry_api._harvest_by_metadata_formats_and_sets( self.fixture.registry, metadata_format, set_, self.fixture.oai_sets) # Assert record_in_database = oai_record_api.get_all_by_registry_id( self.fixture.registry.id) self.assertEquals(result, []) self.assertTrue(len(record_in_database) > 0)
def test_update_registry(self, mock_identify, mock_metadata_formats, mock_sets, mock_get): # Arrange identify = OaiPmhMock.mock_oai_identify(version=2) mock_identify.return_value = identify, status.HTTP_200_OK first_metadata_format = OaiPmhMock.mock_oai_metadata_format(version=2) mock_metadata_formats.return_value = first_metadata_format, status.HTTP_200_OK first_set = OaiPmhMock.mock_oai_set(version=2) mock_sets.return_value = first_set, status.HTTP_200_OK text = '<test>Hello</test>' mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = text # Act result = oai_registry_api.update_registry_info(self.fixture.registry) # Assert _assert_identify(self, identify, result.id) _assert_metadata_format(self, first_metadata_format, result.id) _assert_set(self, first_set, result.id)
def test_update_registry_info(self, mock_identify, mock_metadata_formats, mock_sets, mock_get): # Arrange identify = OaiPmhMock.mock_oai_identify(version=2) mock_identify.return_value = identify, status.HTTP_200_OK first_metadata_format = OaiPmhMock.mock_oai_metadata_format(version=2) mock_metadata_formats.return_value = first_metadata_format, status.HTTP_200_OK first_set = OaiPmhMock.mock_oai_set(version=2) mock_sets.return_value = first_set, status.HTTP_200_OK text = '<test>Hello</test>' mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = text # Act response = RequestMock.do_request_patch( rest_oai_registry.InfoRegistry.as_view(), user=create_mock_user('1', is_staff=True), param=self.param) # Assert self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_upsert_creates_if_does_not_exist(self): """ Test upsert create """ # Arrange oai_identify = OaiPmhMock.mock_oai_identify() self.fixture.insert_registry(insert_related_collections=False) # Act oai_registry_api._upsert_identify_for_registry(oai_identify, self.fixture.registry) # Assert identify_in_database = oai_identify_api.get_by_registry_id(self.fixture.registry.id) self.assertEquals(identify_in_database, oai_identify)
def test_harvest_params_with_resumption_token(self, mock_get): # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records() resumption_token = "h34fh" expected_params = {'verb': 'ListRecords', 'resumptionToken': "h34fh"} # Act oai_verbs_api.list_records(url=self.url, metadata_prefix=self.metadata_prefix, set_h=self.set, from_date=self.from_, until_date=self.until, resumption_token=resumption_token) # Asset mock_get.assert_called_with(self.url, expected_params, verify=SSL_CERTIFICATES_DIR)
def test_update_registry(self, mock_identify, mock_metadata_formats, mock_sets, mock_get): # Arrange mock_user = create_mock_user("1", is_superuser=True) mock_request = create_mock_request(user=mock_user) identify = OaiPmhMock.mock_oai_identify(version=2) mock_identify.return_value = identify, status.HTTP_200_OK first_metadata_format = OaiPmhMock.mock_oai_metadata_format(version=2) mock_metadata_formats.return_value = first_metadata_format, status.HTTP_200_OK first_set = OaiPmhMock.mock_oai_set(version=2) mock_sets.return_value = first_set, status.HTTP_200_OK text = "<test>Hello</test>" mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = text # Act result = oai_registry_api.update_registry_info(self.fixture.registry, request=mock_request) # Assert _assert_identify(self, identify, result.id) _assert_metadata_format(self, first_metadata_format, result.id) _assert_set(self, first_set, result.id)
def test_harvest_params_returns_serialized_data_and_resumption_token(self, mock_get): # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records() resumption_token = "h34fh" # Act result, resumption_token = oai_verbs_api.list_records(url=self.url, metadata_prefix=self.metadata_prefix, set_h=self.set, from_date=self.from_, until_date=self.until, resumption_token=resumption_token) # Asset self.assertEqual(result.status_code, status.HTTP_200_OK) self.assertNotEqual(resumption_token, None) self.assertTrue(len(result.data), 1)
def test_upsert_creates_if_does_not_exist(self): """ Test upsert create """ # Arrange oai_harvester_set = OaiPmhMock.mock_oai_first_set() self.fixture.insert_registry(insert_related_collections=False) # Act oai_registry_api._upsert_identify_for_registry(oai_harvester_set, self.fixture.registry) # Assert set_in_database = oai_harvester_set_api.\ get_by_set_spec_and_registry_id(oai_harvester_set.set_spec, self.fixture.registry.id) self.assertEquals(oai_harvester_set, set_in_database)
def test_harvest_params(self, mock_get): # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records() expected_params = {'verb': 'ListRecords', 'metadataPrefix': self.metadata_prefix, 'set': self.set, 'from': self.from_, 'until': self.until } # Act oai_verbs_api.list_records(url=self.url, metadata_prefix=self.metadata_prefix, set_h=self.set, from_date=self.from_, until_date=self.until) # Assert mock_get.assert_called_with(self.url, expected_params, verify=SSL_CERTIFICATES_DIR)
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_by_metadata_formats_and_sets_updates_dates( self, mock_convert_file, mock_get): """Test harvest by metadata formats and sets update Args: mock_get: Returns: """ # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records( with_resumption_token=False) metadata_format = self.fixture.oai_metadata_formats[0] set_ = self.fixture.oai_sets[0] mock_convert_file.return_value = None # Assert # Metadata Format date self.assertEquals(metadata_format.last_update, None) # Metadata Format + Set date with self.assertRaises(exceptions.DoesNotExist): oai_harvester_metadata_format_set_api.get_by_metadata_format_and_set( metadata_format, set_) # Act result = oai_registry_api._harvest_by_metadata_formats_and_sets( self.fixture.registry, [metadata_format], [set_], self.fixture.oai_sets) # Assert self.assertEquals(result, []) # Metadata Format date metadata_format_in_database = oai_harvester_metadata_format_api.get_by_id( metadata_format.id) self.assertNotEquals(metadata_format_in_database.last_update, None) # Metadata Format + Set date oai_h_mf_set = (oai_harvester_metadata_format_set_api. get_by_metadata_format_and_set(metadata_format, set_)) self.assertNotEquals(oai_h_mf_set.last_update, None)
def test_harvest_registry_saves_record(self, mock_convert_file, mock_get): """Test harvest save Args: mock_get: mock_convert_file: Returns: """ # Arrange mock_get.return_value.status_code = status.HTTP_200_OK mock_get.return_value.text = OaiPmhMock.mock_oai_response_list_records( with_resumption_token=False) mock_convert_file.return_value = None # Act result = oai_registry_api.harvest_registry(self.fixture.registry) # Assert record_in_database = oai_record_api.get_all_by_registry_id( self.fixture.registry.id) self.assertEquals(result, []) self.assertTrue(len(record_in_database) > 0)