Skip to content

Commit

Permalink
tests: redis integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gil-air-may committed Jun 12, 2024
1 parent 452b4b3 commit 595e20d
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 5 deletions.
5 changes: 4 additions & 1 deletion config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
connections = {"MYSQL": "mysql+mysqldb://gervasgu:gervasgu@0.0.0.0/FoodOps"}
connections = {
"MYSQL": "mysql+mysqldb://gervasgu:gervasgu@0.0.0.0/FoodOps",
"REDIS": {"host": "localhost", "port": 6379},
}
6 changes: 6 additions & 0 deletions core/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from infrastructure.cache import tab_cache
import json


def get_cached(id):
return json.loads(tab_cache.get_value(id))
Empty file.
13 changes: 13 additions & 0 deletions infrastructure/cache/tab_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import redis
import ipdb
from config import connections


redis_host = connections["REDIS"]["host"]
redis_port = connections["REDIS"]["port"]

r = redis.Redis(host=redis_host, port=redis_port, decode_responses=True)


def get_value(key):
return r.get(key)
9 changes: 7 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fastapi import FastAPI
from core import repo
from core import repo, cache
from dotenv import load_dotenv

load_dotenv()
Expand All @@ -13,5 +13,10 @@ def read_root():


@app.get("/tabs")
def tabs():
def get_tabs():
return repo.get_all_tabs()


@app.get("/cache")
def get_cache(tab: int):
return cache.get_cached(tab)
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
annotated-types==0.6.0
anyio==4.3.0
asttokens==2.4.1
async-timeout==4.0.3
certifi==2024.2.2
cffi==1.16.0
charset-normalizer==3.3.2
Expand All @@ -18,11 +19,13 @@ fastapi==0.111.0
fastapi-cli==0.0.3
greenlet==3.0.3
h11==0.14.0
hiredis==2.3.2
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
idna==3.7
iniconfig==2.0.0
install==1.3.5
ipdb==0.13.13
ipython==8.24.0
jedi==0.19.1
Expand Down Expand Up @@ -51,6 +54,7 @@ pytest-cov==5.0.0
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
redis==5.0.5
requests==2.31.0
rich==13.7.1
ruff==0.4.4
Expand Down
27 changes: 26 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from testcontainers.mysql import MySqlContainer
from testcontainers.redis import RedisContainer
import sqlalchemy
from config import connections
from scripts.database import seed_utils
from utils import execute_non_query
import json
import ipdb

mysql = MySqlContainer("mysql:5.7.17", port=3306)
mysql.start()
Expand All @@ -11,6 +14,28 @@
engine = sqlalchemy.create_engine(mysql.get_connection_url())
seed_commands = seed_utils.get_seed_commands()


for statement in seed_commands.split(";"):
execute_non_query(engine, statement)

redis_container = RedisContainer().__enter__()
redis_client = redis_container.get_client()
redis_conn = redis_client.get_connection_kwargs()

# ipdb.set_trace()

redis_client.set(
1,
json.dumps(
{
"tab_id": 1,
"table_number": 1,
"is_paid": 0,
"items": '[{"name": "chicken_salad", "amount": 1}]',
"from_day": "2024-05-18",
"created_at": "2024-05-18T20:36:42",
},
),
)

connections["REDIS"]["host"] = redis_conn["host"]
connections["REDIS"]["port"] = redis_conn["port"]
14 changes: 13 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from starlette.testclient import TestClient
import ipdb


from main import app
Expand All @@ -26,3 +25,16 @@ def test_get_all_tabs():
"created_at": "2024-05-18T20:36:42",
}
]


def test_get_cache():
response = client.get("/cache?tab=1")
assert response.status_code == 200
assert response.json() == {
"tab_id": 1,
"table_number": 1,
"is_paid": 0,
"items": '[{"name": "chicken_salad", "amount": 1}]',
"from_day": "2024-05-18",
"created_at": "2024-05-18T20:36:42",
}

0 comments on commit 595e20d

Please sign in to comment.