def preinit(): """ Pytest decorator to run preprcessing proceduce before each test case ex: init database and cache """ clean_db() REDIS.flushdb() init_db() init_cache()
def post(payload: Payload): try: if REDIS.get(f"{payload.name}"): raise Exception(f"Fruit {payload.name} already exists!") REDIS.set(f"{payload.name}", f"{payload.count}") return PlainTextResponse("OK", 200) except Exception as error: logger.warning(error) return PlainTextResponse("Bad Request", 400)
def post(payload: Payload): try: if REDIS.get(f"{payload.name}"): raise Exception(f"Fruit {payload.name} already exists!") Fruit(**{"name": payload.name, "count": payload.count}).add() Fruit.commit() REDIS.set(f"{payload.name}", f"{payload.count}") return EnumResponse.OK.value.response except Exception as error: logger.warning(error) return EnumResponse.BAD_REQUEST.value.response
def get(name: str): try: if REDIS.get(name) is None: return EnumResponse.BAD_REQUEST.value.response return JSONResponse( { "name": name, "count": int(REDIS.get(name).decode()) }, 200) except Exception as error: logger.warning(error) return EnumResponse.INTERNAL_SERVER_ERROR.value.response
def get_url(name: str): try: return JSONResponse( { "name": name, "count": int(REDIS.get(name).decode()) }, 200) except Exception as error: logger.warning(error) return PlainTextResponse("Bad Request", 400)
def init_cache(): fruits: List[Fruit] = Fruit.query().all() for fruit in fruits: REDIS.set(fruit.name, f"{fruit.count}") # Intended to trigger server error REDIS.set("error", "error")