Skip to content

Commit

Permalink
add fixture for redis db index
Browse files Browse the repository at this point in the history
  • Loading branch information
azliu0 committed Apr 15, 2024
1 parent 12d7003 commit 71b8b6f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def _get_config_option(name: str, default_value: str | None = None) -> str:
"DATABASE_URL", "postgresql://postgres:password@database/pigeondb"
)
REDIS_URL = _get_config_option("REDIS_URL", "redis")
REDIS_DB_INDEX = _get_config_option("REDIS_DB_INDEX", "0")

FLASK_RUN_PORT = 2010
DEBUG = True
Expand Down
6 changes: 4 additions & 2 deletions server/nlp/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import Query

from server.config import REDIS_URL, RedisDocument
from server.config import REDIS_DB_INDEX, REDIS_URL, RedisDocument

cwd = os.path.dirname(__file__)

VECTOR_DIMENSION = 1536

# load redis client
client = redis.Redis(host=REDIS_URL, port=6379, decode_responses=True)
client = redis.Redis(
host=REDIS_URL, port=6379, decode_responses=True, db=int(REDIS_DB_INDEX)
)

# load corpus
# with open('corpus.json', 'r') as f:
Expand Down
18 changes: 17 additions & 1 deletion server_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import psycopg2
import pytest
import redis
from apiflask import APIFlask
from flask.testing import FlaskClient, FlaskCliRunner
from psycopg2 import sql
Expand Down Expand Up @@ -41,8 +42,23 @@ def db_url(db_name="pigeondb_test"):


@pytest.fixture(scope="session")
def app(db_url: str):
def redis_db_index():
"""Yields test db index for Redis.
Flushes test db if it already exists.
"""
test_db_index = 1
client = redis.Redis(host="localhost", port=6379, db=test_db_index)
client.flushdb()
client.close()

yield test_db_index


@pytest.fixture(scope="session")
def app(db_url: str, redis_db_index: int):
os.environ["DATABASE_URL"] = db_url
os.environ["REDIS_DB_INDEX"] = str(redis_db_index)

app = create_app()
app.config.update(
Expand Down

0 comments on commit 71b8b6f

Please sign in to comment.