Skip to content

Commit

Permalink
add redis to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
bhnvx committed Feb 26, 2024
1 parent 502e6d0 commit 4a49d84
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ jobs:

test:
services:
redis:
image: redis:7.0.12
ports:
- 127.0.0.1:6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
mongodb:
image: bitnami/mongodb:5.0.16
ports:
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions app/entities/redis_repositories/category_point_redis_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Sequence

from app.entities.category.category_codes import CategoryCode
from app.utils.redis_ import redis


class CategoryPointRedisRepository:
@classmethod
async def get(cls, key: str) -> tuple[CategoryCode, ...] | None:
cached = await redis.get(key)
if cached is None:
return None
if cached == "":
return ()
return tuple(CategoryCode(code) for code in cached.split(","))

@classmethod
async def set(cls, key: str, codes: Sequence[CategoryCode]) -> None:
await redis.set(key, ",".join(codes))

@classmethod
async def delete(cls, *key: str) -> None:
await redis.delete(*key)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from app.entities.category.category_codes import CategoryCode
from app.entities.redis_repositories.category_point_redis_repository import (
CategoryPointRedisRepository,
)


async def test_category_point_set_and_get() -> None:
# Given
key = "1_2"
codes = (CategoryCode.CHICKEN, CategoryCode.PIZZA)

# When
await CategoryPointRedisRepository.set(key, codes)

# Then
assert await CategoryPointRedisRepository.get(key) == codes


async def test_category_point_delete() -> None:
# Given
key = "1_2"
codes = (CategoryCode.CHICKEN, CategoryCode.PIZZA)
await CategoryPointRedisRepository.set(key, codes)

# When
await CategoryPointRedisRepository.delete(key)

# Then
assert await CategoryPointRedisRepository.get(key) is None


async def test_category_point_set_and_get_empty() -> None:
# Given
key = "1_2"
codes = ()

# When
await CategoryPointRedisRepository.set(key, codes)

# Then
assert await CategoryPointRedisRepository.get(key) == codes
6 changes: 6 additions & 0 deletions app/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest_asyncio

from app.utils.mongo import db
from app.utils.redis_ import redis


@pytest_asyncio.fixture(scope="session")
Expand All @@ -18,3 +19,8 @@ def event_loop() -> Generator[AbstractEventLoop, None, None]:
async def setup_db() -> None:
for collection_name in await db.list_collection_names():
await db[collection_name].drop()


@pytest_asyncio.fixture(scope="function", autouse=True)
async def setup_redis() -> None:
await redis.flushall()
12 changes: 12 additions & 0 deletions app/utils/redis_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

from redis.asyncio import ConnectionError, Redis

# https://github.com/python/typeshed/issues/7597#issuecomment-1117572695
redis: "Redis[str]" = Redis.from_url(
os.environ.get("REDIS_URL", "redis://localhost:6379"),
decode_responses=True, # redis.get() 이 byte 대신 str 을 리턴합니다.
socket_timeout=5,
retry_on_timeout=True, # 타임아웃 발생시 재시도 합니다.
retry_on_error=[ConnectionError], # 커넥션 에러가 발생 했을 때 재시도 합니다.
)
189 changes: 188 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ orjson = "^3.8.14"
python = "^3.11"
requests = "^2.31.0"
uvicorn = "^0.22.0"
redis = "4.6.0"

[tool.poetry.group.dev.dependencies]
black = {extras = ["d"], version = "^23.3.0"}
Expand All @@ -61,3 +62,4 @@ mypy = "^1.3.0"
pytest = "^7.3.1"
pytest-asyncio = "^0.21.0"
toml-sort = "^0.23.0"
types-redis = "4.6.0.3"

0 comments on commit 4a49d84

Please sign in to comment.