def test_get_with_offline_cache_returns_None(self): """ Caso o cache esteja fora do ar não lançamos exepction apenas retornamos None (como se a chave não estivesse no cache) e fazemos um logger.error() avisando que não conseguimos conecta no cache """ self.assertIsNone(cache.get("some-key"))
def test_get_with_offline_cache_check_logger(self): self.assertIsNone(cache.get("my-key")) self.assertEqual(self.logger_mock.error.call_count, 1) self.logger_mock.error.assert_called_with({ "action": "cache-get", "state": "error", "key": "my-key", "cache-backend": "redis://127.0.0.1:6379/0", })
def download_by_id(download_id): file_data = cache.get(f"downloads/{download_id}") if not file_data: return Response(status=HTTPStatus.NOT_FOUND) file_url = file_data['file_url'] task_id = file_data['task_id'] file_path = file_data['file_path'] response = requests.get(file_url, stream=True) if response.status_code == HTTPStatus.NOT_FOUND: return Response(response=json.dumps({}), status=HTTPStatus.NOT_FOUND) filename = f"{task_id}_{file_path.strip('/')}.log" return Response(response=response.iter_content(chunk_size=4096), status=200, headers={ "Content-Disposition": f"attachment; filename={filename}", "Content-Type": "application/octet-stream" })
def test_get_calls_backend_get_method(self): with mock.patch.object(cache, "__cache_backend") as cache_backend_mock: cache_backend_mock.get.return_value = "My-Key-Value" self.assertEqual("My-Key-Value", cache.get("my-key")) cache_backend_mock.get.assert_called_with("my-key")