async def test_should_get_one_from_fallback_after_open_circuit_breaker( fake_service, fake_entity, mocker ): fake_service.repository.memory_data_source.hgetall = asynctest.CoroutineMock( side_effect=RedisError ) fake_service.repository.fallback_data_source.db[ 'fake:fake' ] = dataclasses.asdict(fake_entity) entity = await fake_service.get_one('fake') assert entity == fake_entity assert fake_service.logger.warning.call_count == 1
async def test_should_get_many_with_one_item_already_not_found_from_fallback( repository, fake_entity): await repository.memory_data_source.delete('fake:fake') await repository.memory_data_source.delete('fake:fake2') await repository.memory_data_source.delete('fake:not-found:fake') await repository.memory_data_source.set('fake:not-found:fake2', '1') repository.fallback_data_source.db['fake:fake'] = dataclasses.asdict( fake_entity) entities = [ e async for e in repository.query( many=[fake_entity.id, 'fake2']).entities ] assert entities == [fake_entity] assert repository.memory_data_source.exists('fake:fake')
async def test_should_get_many_from_fallback_with_fields( repository, fake_entity): await repository.memory_data_source.delete('fake:fake') await repository.memory_data_source.delete('fake:not-found:fake') repository.fallback_data_source.db['fake:fake'] = dataclasses.asdict( fake_entity) fake_entity.number = None fake_entity.boolean = None entities = [ e async for e in repository.query(many=[fake_entity.id], fields=['id', 'integer', 'inner_entities']).entities ] assert entities == [fake_entity] assert repository.memory_data_source.exists('fake:fake')
async def test_should_get_one_from_fallback_when_not_found_on_memory( fake_service, serialized_fake_entity, fake_entity): await fake_service.repository.memory_data_source.delete( 'fake:other_fake:fake') await fake_service.repository.memory_data_source.delete( 'fake:not-found:other_fake:fake') await fake_service.repository.fallback_data_source.put( fake_service.repository.fallback_data_source.make_key( 'fake', 'other_fake:fake'), dataclasses.asdict(fake_entity), ) entity = await fake_service.get_one('fake', other_id='other_fake') assert entity == fake_entity assert fake_service.repository.memory_data_source.exists( 'fake:other_fake:fake')
async def test_should_delete_from_fallback_after_open_circuit_breaker( fake_service, fake_entity, mocker): await fake_service.repository.memory_data_source.delete('fake:fake') fake_service.repository.fallback_data_source.db[ 'fake:fake'] = dataclasses.asdict(fake_entity, dumps_value=True) assert await fake_service.get_one('fake') fake_service.repository.memory_data_source.set = asynctest.CoroutineMock( side_effect=RedisError) await fake_service.delete(fake_entity.id) fake_service.cache.clear() with pytest.raises(EntityNotFoundError): await fake_service.get_one('fake') assert fake_service.logger.warning.call_count == 2
async def test_should_get_one_from_fallback_when_not_found_on_memory_with_fields( fake_service, serialized_fake_entity, fake_entity): await fake_service.repository.memory_data_source.delete( 'fake:other_fake:fake') await fake_service.repository.fallback_data_source.put( fake_service.repository.fallback_data_source.make_key( 'fake', 'other_fake:fake'), dataclasses.asdict(fake_entity), ) fake_entity.number = None fake_entity.boolean = None entity = await fake_service.get_one( 'fake', other_id='other_fake', fields=['id', 'other_id', 'integer', 'inner_entities'], ) assert entity == fake_entity assert fake_service.repository.memory_data_source.exists( 'fake:other_fake:fake')
async def test_should_delete_from_fallback_after_open_circuit_breaker( fake_service, serialized_fake_entity, fake_entity, mocker): await fake_service.repository.memory_data_source.delete( 'fake:other_fake:fake') await fake_service.repository.memory_data_source.delete( 'fake:not-found:other_fake:fake') key = fake_service.repository.fallback_data_source.make_key( 'fake', 'other_fake', 'fake') await fake_service.repository.fallback_data_source.put( key, dataclasses.asdict(fake_entity, dumps_value=True)) assert await fake_service.get_one('fake', other_id='other_fake') fake_service.repository.memory_data_source.delete = asynctest.CoroutineMock( side_effect=RedisError) await fake_service.delete(fake_entity.id, other_id='other_fake') fake_service.cache.clear() with pytest.raises(EntityNotFoundError): await fake_service.get_one('fake', other_id='other_fake') assert fake_service.logger.warning.call_count == 2
async def add_fallback(self, entity: Any, *entities: Any, **kwargs: Any) -> None: data = jdataclasses.asdict(entity) await self.fallback_data_source.put(self.fallback_key(entity), data, **kwargs)
def make_memory_data_from_entity(self, entity: Any) -> HashData: return { k: int(v) if isinstance(v, bool) else v for k, v in jdataclasses.asdict(entity, dumps_value=True).items() if v is not None }