def setup(self): factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_tasks_orcid_push_TestOrcidPush.json') self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) self.oauth_token = get_local_access_tokens(self.orcid) or 'mytoken'
class TestOrcidPushTask(object): # NOTE: Only a few test here (1 happy flow and a few error flows). Exhaustive # testing is done in the domain model tests. def setup(self): factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_tasks_orcid_push_TestOrcidPush.json') self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) self.oauth_token = get_local_access_tokens(self.orcid) or 'mytoken' def teardown(self): self.cache.redis.delete(self.cache._key) def test_push_new_work_happy_flow(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'): orcid_push(self.orcid, self.recid, self.oauth_token) assert not self.cache.has_work_content_changed(self.inspire_record) def test_push_new_work_invalid_data_orcid(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ pytest.raises(exceptions.InputDataInvalidException): orcid_push('0000-0002-0000-XXXX', self.recid, self.oauth_token) def test_push_new_work_already_existent(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): orcid_push(self.orcid, self.recid, self.oauth_token) assert not self.cache.has_work_content_changed(self.inspire_record)
def setup(self): self.recid = '1936475' self.putcode = 'myputcode' self.hash_value = 'myhash' self.orcid = '0000-0002-76YY-56XX' self.hash_value = 'sha1:acbc7dad4fd46e0deb60d6681c244a67e4be2543' factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_cache_record.json') self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid)
def setup(self): factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_api_test_push_record_with_orcid.json') self.recid = '8201' self.putcode = '920107' self.hash_value = 'sha1:c6d51f84927dd82d16ee0aaccb64d13313a023b2' self.orcid = '0000-0002-1825-0097' self.oauth_token = 'fake-token' self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid)
def test_push_updated_work_invalid_data_orcid(self): orcid = '0000-0002-0000-XXXX' self.cache = OrcidCache(orcid, self.recid) self.putcode = '46985330' self.cache.write_work_putcode(self.putcode) pusher = domain_models.OrcidPusher(orcid, self.recid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): pusher.push()
def setup(self): self.recid = '1936475' self.putcode = 'myputcode' self.hash_value = 'myhash' self.orcid = '0000-0002-76YY-56XX' self.hash_value = 'sha1:ede49b12e11f5284fdced7596a28791ddf32c8fc' factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_cache_record.json') self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid)
def setup(self): factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_models_TestOrcidPusherPostNewWork.json') self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) self.oauth_token = get_local_access_tokens(self.orcid) or 'mytoken' # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models' ).disabled = logging.CRITICAL
class TestOrcidPusherDuplicatedIdentifier(TestOrcidPusherBase): def setup(self): factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_domain_models_TestOrcidPusherDuplicatedIdentifier.json') self.clashing_record = TestRecordMetadata.create_from_kwargs( json={'control_number': '999', 'deleted': True}).record_metadata self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models').disabled = logging.CRITICAL self.putcode = '51551656' self.cache.write_work_putcode(self.putcode) self.cache2 = OrcidCache(self.orcid, '999') self.cache2.write_work_putcode('51548299') def teardown(self): self.cache.delete_work_putcode() self.cache2.delete_work_putcode() logging.getLogger('inspirehep.modules.orcid.domain_models').disabled = 0 cache_module.CACHE_PREFIX = None def test_happy_flow(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == 51551656 assert not self.cache.has_work_content_changed(self.inspire_record) def test_exc_in_apply_celery_task_with_retry(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ mock.patch('inspirehep.modules.orcid.utils.apply_celery_task_with_retry') as apply_celery_task_with_retry_mock, \ pytest.raises(TimeLimitExceeded): apply_celery_task_with_retry_mock.side_effect = TimeLimitExceeded pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push() def test_duplicated_external_identifier_pusher_exception(self): del self.clashing_record.json['deleted'] self.clashing_record.json['titles'] = [{'source': 'submitter', 'title': 'title1'}] with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(domain_exceptions.DuplicatedExternalIdentifierPusherException): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push()
class TestOrcidPusherCache(object): def setup(self): factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_models_TestOrcidPusher.json') self.inspire_record = factory.inspire_record self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.oauth_token = 'mytoken' self.cache = OrcidCache(self.orcid, self.recid) # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models' ).disabled = logging.CRITICAL def teardown(self): self.cache.redis.delete(self.cache._key) logging.getLogger( 'inspirehep.modules.orcid.domain_models').disabled = 0 def test_record_not_found(self): with pytest.raises(exceptions.RecordNotFoundException): domain_models.OrcidPusher(self.orcid, 'notfound25697xxx', self.oauth_token) def test_push_cache_hit_content_not_changed(self): putcode = '00000' self.cache.write_work_putcode(putcode, self.inspire_record) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == putcode def test_push_cache_hit_content_changed(self): putcode = '00000' cache_inspire_record = copy.deepcopy(self.inspire_record) cache_inspire_record['titles'][0]['title'] = 'foo' self.cache.write_work_putcode(putcode, cache_inspire_record) pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) with mock.patch.object(OrcidClient, 'put_updated_work') as mock_put_updated_work: mock_put_updated_work.return_value.__getitem__.return_value = '0000' pusher.push() mock_put_updated_work.assert_called_once_with(mock.ANY, putcode)
class TestOrcidPushTask(object): # NOTE: Only a few test here (1 happy flow and a few error flows). Exhaustive # testing is done in the domain model tests. def setup(self): factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_tasks_orcid_push_TestOrcidPush.json') self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) self.oauth_token = get_local_access_tokens(self.orcid) or 'mytoken' def setup_method(self, method): cache_module.CACHE_PREFIX = get_fqn(method) def teardown(self): self.cache.delete_work_putcode() cache_module.CACHE_PREFIX = None def test_push_new_work_happy_flow(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'): orcid_push(self.orcid, self.recid, self.oauth_token) assert not self.cache.has_work_content_changed(self.inspire_record) def test_push_new_work_invalid_data_orcid(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ pytest.raises(exceptions.InputDataInvalidException): orcid_push('0000-0002-0000-XXXX', self.recid, self.oauth_token) def test_push_new_work_already_existing(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): orcid_push(self.orcid, self.recid, self.oauth_token) assert not self.cache.has_work_content_changed(self.inspire_record) def test_stale_record_db_version(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*'), \ pytest.raises(domain_exceptions.StaleRecordDBVersionException): orcid_push(self.orcid, self.recid, self.oauth_token, kwargs_to_pusher=dict(record_db_version=100))
def setup(self): factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_domain_models_TestOrcidPusherDuplicatedIdentifier.json' ) self.clashing_record = TestRecordMetadata.create_from_kwargs( json={ 'control_number': '999', 'deleted': True }).record_metadata self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models' ).disabled = logging.CRITICAL self.putcode = '51551656' self.cache.write_work_putcode(self.putcode) self.cache2 = OrcidCache(self.orcid, '999') self.cache2.write_work_putcode('51548299')
def setup(self): factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_domain_models_TestOrcidPusherDuplicatedIdentifier.json') self.clashing_record = TestRecordMetadata.create_from_kwargs( json={'control_number': '999', 'deleted': True}).record_metadata self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models').disabled = logging.CRITICAL self.putcode = '51551656' self.cache.write_work_putcode(self.putcode) self.cache2 = OrcidCache(self.orcid, '999') self.cache2.write_work_putcode('51548299')
class TestOrcidPusherPutUpdatedWork(object): def setup(self): factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_models_TestOrcidPusher.json') self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) self.putcode = '46985330' self.cache.write_work_putcode(self.putcode) self.oauth_token = get_local_access_tokens(self.orcid) or 'mytoken' # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models' ).disabled = logging.CRITICAL def teardown(self): self.cache.redis.delete(self.cache._key) logging.getLogger( 'inspirehep.modules.orcid.domain_models').disabled = 0 def test_push_updated_work_happy_flow(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == int(self.putcode) assert not self.cache.has_work_content_changed(self.inspire_record) def test_push_updated_work_invalid_data_orcid(self): orcid = '0000-0002-0000-XXXX' self.cache = OrcidCache(orcid, self.recid) self.putcode = '46985330' self.cache.write_work_putcode(self.putcode) pusher = domain_models.OrcidPusher(orcid, self.recid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): pusher.push() def test_push_updated_work_invalid_data_token(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, 'tokeninvalid') with pytest.raises(exceptions.InputDataInvalidException): pusher.push() def test_push_updated_work_invalid_data_putcode(self): self.cache.write_work_putcode('00000') pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): pusher.push()
def test_recache_author_putcodes(isolated_app, vcr_cassette): orcid = '0000-0002-1825-0097' oauth_token = 'fake-token' recid1 = '4328' putcode1 = '912978' recid2 = '4327' putcode2 = '912977' # putcode3 = '912979' # Non inspire. with override_config(**CONFIG): recache_author_putcodes(orcid, oauth_token) # Ensure the putcodes have been cached. cache = OrcidCache(orcid, recid1) assert cache.read_work_putcode() == putcode1 cache = OrcidCache(orcid, recid2) assert cache.read_work_putcode() == putcode2 assert vcr_cassette.all_played
class TestOrcidPusherDuplicatedIdentifier(TestOrcidPusherBase): def setup(self): factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_domain_models_TestOrcidPusherDuplicatedIdentifier.json' ) self.clashing_record = TestRecordMetadata.create_from_kwargs( json={ 'control_number': '999', 'deleted': True }).record_metadata self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models' ).disabled = logging.CRITICAL self.putcode = '51551656' self.cache.write_work_putcode(self.putcode) self.cache2 = OrcidCache(self.orcid, '999') self.cache2.write_work_putcode('51548299') def teardown(self): self.cache.delete_work_putcode() self.cache2.delete_work_putcode() logging.getLogger( 'inspirehep.modules.orcid.domain_models').disabled = 0 cache_module.CACHE_PREFIX = None def test_happy_flow(self): with override_config( FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == 51551656 assert not self.cache.has_work_content_changed(self.inspire_record) def test_exc_in_apply_celery_task_with_retry(self): with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ mock.patch('inspirehep.modules.orcid.utils.apply_celery_task_with_retry') as apply_celery_task_with_retry_mock, \ pytest.raises(TimeLimitExceeded): apply_celery_task_with_retry_mock.side_effect = TimeLimitExceeded pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push() def test_duplicated_external_identifier_pusher_exception(self): del self.clashing_record.json['deleted'] self.clashing_record.json['titles'] = [{ 'source': 'submitter', 'title': 'title1' }] with override_config(FEATURE_FLAG_ENABLE_ORCID_PUSH=True, FEATURE_FLAG_ORCID_PUSH_WHITELIST_REGEX='.*', ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(domain_exceptions.DuplicatedExternalIdentifierPusherException): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) pusher.push()
def cache(self): return OrcidCache(self.orcid, self.recid)
class TestOrcidPusherPostNewWork(object): def setup(self): factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_models_TestOrcidPusherPostNewWork.json') self.orcid = '0000-0002-0942-3697' self.recid = factory.record_metadata.json['control_number'] self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) self.oauth_token = get_local_access_tokens(self.orcid) or 'mytoken' # Disable logging. logging.getLogger('inspirehep.modules.orcid.domain_models' ).disabled = logging.CRITICAL def teardown(self): self.cache.redis.delete(self.cache._key) logging.getLogger( 'inspirehep.modules.orcid.domain_models').disabled = 0 def test_push_new_work_happy_flow(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == 47160445 assert not self.cache.has_work_content_changed(self.inspire_record) def test_push_new_work_invalid_data_orcid(self): orcid = '0000-0002-0000-XXXX' pusher = domain_models.OrcidPusher(orcid, self.recid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): pusher.push() def test_push_new_work_invalid_data_token(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, 'tokeninvalid') with pytest.raises(exceptions.InputDataInvalidException): pusher.push() def test_push_new_work_invalid_data_xml(self): # Note: the recorded cassette returns (magically) a proper error. pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) with pytest.raises(exceptions.InputDataInvalidException): pusher.push() def test_push_new_work_already_existent(self): # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config( ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) result_putcode = pusher.push() assert result_putcode == '47160445' assert not self.cache.has_work_content_changed(self.inspire_record) def test_push_new_work_already_existent_putcode_exception(self): pusher = domain_models.OrcidPusher(self.orcid, self.recid, self.oauth_token) # ORCID_APP_CREDENTIALS is required because ORCID adds it as source_client_id_path. with override_config(ORCID_APP_CREDENTIALS={'consumer_key': '0000-0001-8607-8906'}), \ pytest.raises(exceptions.PutcodeNotFoundInOrcidException): pusher.push() assert self.cache.has_work_content_changed(self.inspire_record)
def test_delete_work_putcode_non_existing(self): recid = '0000' cache = OrcidCache(self.orcid, recid) cache.delete_work_putcode() assert not self.cache.read_work_putcode()
class TestOrcidCache(object): def setup(self): self.recid = '1936475' self.putcode = 'myputcode' self.hash_value = 'myhash' self.orcid = '0000-0002-76YY-56XX' self.hash_value = 'sha1:acbc7dad4fd46e0deb60d6681c244a67e4be2543' factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_cache_record.json') self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) def setup_method(self, method): cache_module.CACHE_PREFIX = get_fqn(method) def teardown(self): """ Cleanup the cache after each test (as atm there is no cache isolation). """ self.cache.delete_work_putcode() cache_module.CACHE_PREFIX = None def test_read_write_new_key(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) putcode = self.cache.read_work_putcode() assert putcode == self.putcode def test_read_write_existent_key(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) self.cache.write_work_putcode('0000', self.inspire_record) putcode = self.cache.read_work_putcode() assert putcode == '0000' def test_read_non_existent_key(self): putcode = self.cache.read_work_putcode() assert not putcode def test_has_work_content_changed_no(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) cache = OrcidCache(self.orcid, self.recid) assert not cache.has_work_content_changed(self.inspire_record) def test_has_work_content_changed_yes(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) self.inspire_record['titles'][0]['title'] = 'mytitle' cache = OrcidCache(self.orcid, self.recid) assert cache.has_work_content_changed(self.inspire_record) def test_write_work_putcode_do_recompute(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) self.cache.read_work_putcode() assert self.cache._cached_hash_value == self.hash_value def test_write_work_putcode_do_not_recompute(self): self.cache.write_work_putcode(self.putcode) self.cache.read_work_putcode() assert not self.cache._cached_hash_value def test_delete_work_putcode(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) putcode = self.cache.read_work_putcode() assert putcode == self.putcode self.cache.delete_work_putcode() assert not self.cache.read_work_putcode() def test_delete_work_putcode_non_existing(self): recid = '0000' cache = OrcidCache(self.orcid, recid) cache.delete_work_putcode() assert not self.cache.read_work_putcode()
class TestPushRecordWithOrcid(object): def setup(self): factory = TestRecordMetadata.create_from_file(__name__, 'test_orcid_api_test_push_record_with_orcid.json') self.recid = '8201' self.putcode = '920107' self.hash_value = 'sha1:c6d51f84927dd82d16ee0aaccb64d13313a023b2' self.orcid = '0000-0002-1825-0097' self.oauth_token = 'fake-token' self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) def teardown(self): """ Cleanup the cache after each test (as atm there is no cache isolation). """ key = self.cache._key self.cache.redis.delete(key) @pytest.mark.vcr() def test_new_record(self, vcr_cassette): with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.recache_author_putcodes', wraps=recache_author_putcodes) as mock_recache_author_putcodes: putcode = push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert putcode == self.putcode mock_recache_author_putcodes.assert_not_called() assert vcr_cassette.all_played @pytest.mark.vcr() def test_new_record_already_existent_error(self, vcr_cassette): with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.recache_author_putcodes', wraps=recache_author_putcodes) as mock_recache_author_putcodes: putcode = push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert putcode == self.putcode mock_recache_author_putcodes.assert_called_with(self.orcid, self.oauth_token) assert vcr_cassette.all_played @pytest.mark.vcr() def test_new_record_already_existent_error_and_putcode_not_found(self, vcr_cassette): with override_config(**CONFIG), \ mock.patch.object(OrcidCache, 'read_work_putcode', return_value=None): with pytest.raises(PutcodeNotFoundInCacheException): push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert vcr_cassette.all_played @pytest.mark.vcr() def test_updated_record_cache_hit(self, vcr_cassette): # Fill the cache with the right putcode but no hash. self.cache.write_work_putcode(self.putcode) with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.recache_author_putcodes', wraps=recache_author_putcodes) as mock_recache_author_putcodes: putcode = push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert putcode == self.putcode mock_recache_author_putcodes.assert_not_called() assert vcr_cassette.all_played def test_updated_record_cache_hit_same_hash(self): # Fill the cache with the right putcode and same hash. self.cache.write_work_putcode(self.putcode, self.inspire_record) with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.recache_author_putcodes', wraps=recache_author_putcodes) as mock_recache_author_putcodes: putcode = push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) assert putcode == self.putcode mock_recache_author_putcodes.assert_not_called() @pytest.mark.vcr() def test_distributed_lock_with_new_record(self, vcr_cassette): with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.distributed_lock', wraps=distributed_lock) as mock_distributed_lock: push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) mock_distributed_lock.assert_called_with('orcid:0000-0002-1825-0097', blocking=True) assert vcr_cassette.all_played @pytest.mark.vcr() def test_distributed_lock_with_updated_record(self, vcr_cassette): self.cache.write_work_putcode(self.putcode) with override_config(**CONFIG), \ mock.patch('inspirehep.modules.orcid.api.distributed_lock', wraps=distributed_lock) as mock_distributed_lock: push_record_with_orcid( recid=self.recid, orcid=self.orcid, oauth_token=self.oauth_token, ) mock_distributed_lock.assert_called_with('orcid:0000-0002-1825-0097', blocking=True) assert vcr_cassette.all_played
def test_has_work_content_changed_no(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) cache = OrcidCache(self.orcid, self.recid) assert not cache.has_work_content_changed(self.inspire_record)
def test_has_work_content_changed_yes(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) self.inspire_record['titles'][0]['title'] = 'mytitle' cache = OrcidCache(self.orcid, self.recid) assert cache.has_work_content_changed(self.inspire_record)
class TestOrcidCache(object): def setup(self): self.recid = '1936475' self.putcode = 'myputcode' self.hash_value = 'myhash' self.orcid = '0000-0002-76YY-56XX' self.hash_value = 'sha1:ede49b12e11f5284fdced7596a28791ddf32c8fc' factory = TestRecordMetadata.create_from_file( __name__, 'test_orcid_cache_record.json') self.inspire_record = factory.inspire_record self.cache = OrcidCache(self.orcid, self.recid) def teardown(self): """ Cleanup the cache after each test (as atm there is no cache isolation). """ key = self.cache._key self.cache.redis.delete(key) def test_read_write_new_key(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) putcode = self.cache.read_work_putcode() assert putcode == self.putcode def test_read_write_existent_key(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) self.cache.write_work_putcode('0000', self.inspire_record) putcode = self.cache.read_work_putcode() assert putcode == '0000' def test_read_non_existent_key(self): putcode = self.cache.read_work_putcode() assert not putcode def test_has_work_content_changed_no(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) cache = OrcidCache(self.orcid, self.recid) assert not cache.has_work_content_changed(self.inspire_record) def test_has_work_content_changed_yes(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) self.inspire_record['titles'][0]['title'] = 'mytitle' cache = OrcidCache(self.orcid, self.recid) assert cache.has_work_content_changed(self.inspire_record) def test_write_work_putcode_do_recompute(self): self.cache.write_work_putcode(self.putcode, self.inspire_record) self.cache.read_work_putcode() assert self.cache._cached_hash_value == self.hash_value def test_write_work_putcode_do_not_recompute(self): self.cache.write_work_putcode(self.putcode) self.cache.read_work_putcode() assert not self.cache._cached_hash_value