mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
chore: update API tests workflow and database connection logic for testing environment
This commit is contained in:
parent
7b3229ff79
commit
7b220e8883
3 changed files with 53 additions and 20 deletions
28
.github/workflows/api-tests.yaml
vendored
28
.github/workflows/api-tests.yaml
vendored
|
|
@ -13,14 +13,19 @@ on:
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Setup uv
|
steps:
|
||||||
uses: astral-sh/setup-uv@v1
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
version: "latest"
|
python-version: "3.12"
|
||||||
|
|
||||||
|
- name: Install uv
|
||||||
|
uses: astral-sh/setup-uv@v2
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
|
|
@ -30,4 +35,13 @@ jobs:
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
cd apps/api
|
cd apps/api
|
||||||
uv run pytest -v
|
uv run pytest src/tests/ -v --tb=short
|
||||||
|
env:
|
||||||
|
TESTING: "true"
|
||||||
|
|
||||||
|
- name: Run security tests with coverage
|
||||||
|
run: |
|
||||||
|
cd apps/api
|
||||||
|
uv run pytest src/tests/security/ --cov=src.security --cov-report=xml --cov-report=term-missing
|
||||||
|
env:
|
||||||
|
TESTING: "true"
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,20 @@ def import_all_models():
|
||||||
import_all_models()
|
import_all_models()
|
||||||
|
|
||||||
learnhouse_config = get_learnhouse_config()
|
learnhouse_config = get_learnhouse_config()
|
||||||
engine = create_engine(
|
|
||||||
|
# Check if we're in test mode
|
||||||
|
is_testing = os.getenv("TESTING", "false").lower() == "true"
|
||||||
|
|
||||||
|
if is_testing:
|
||||||
|
# Use SQLite for tests
|
||||||
|
engine = create_engine(
|
||||||
|
"sqlite:///:memory:",
|
||||||
|
echo=False,
|
||||||
|
connect_args={"check_same_thread": False}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Use configured database for production/development
|
||||||
|
engine = create_engine(
|
||||||
learnhouse_config.database_config.sql_connection_string, # type: ignore
|
learnhouse_config.database_config.sql_connection_string, # type: ignore
|
||||||
echo=False,
|
echo=False,
|
||||||
pool_pre_ping=True, # type: ignore
|
pool_pre_ping=True, # type: ignore
|
||||||
|
|
@ -40,15 +53,18 @@ engine = create_engine(
|
||||||
max_overflow=0,
|
max_overflow=0,
|
||||||
pool_recycle=300, # Recycle connections after 5 minutes
|
pool_recycle=300, # Recycle connections after 5 minutes
|
||||||
pool_timeout=30
|
pool_timeout=30
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create all tables after importing all models
|
# Only create tables if not in test mode (tests will handle this themselves)
|
||||||
SQLModel.metadata.create_all(engine)
|
if not is_testing:
|
||||||
logfire.instrument_sqlalchemy(engine=engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
logfire.instrument_sqlalchemy(engine=engine)
|
||||||
|
|
||||||
async def connect_to_db(app: FastAPI):
|
async def connect_to_db(app: FastAPI):
|
||||||
app.db_engine = engine # type: ignore
|
app.db_engine = engine # type: ignore
|
||||||
logging.info("LearnHouse database has been started.")
|
logging.info("LearnHouse database has been started.")
|
||||||
|
# Only create tables if not in test mode
|
||||||
|
if not is_testing:
|
||||||
SQLModel.metadata.create_all(engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
def get_db_session():
|
def get_db_session():
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,8 @@ import os
|
||||||
# Ensure src/ is on the Python path for all tests
|
# Ensure src/ is on the Python path for all tests
|
||||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
||||||
|
|
||||||
|
# Set testing environment variable to use SQLite
|
||||||
|
os.environ["TESTING"] = "true"
|
||||||
|
|
||||||
# Suppress logfire warnings in tests
|
# Suppress logfire warnings in tests
|
||||||
os.environ["LOGFIRE_IGNORE_NO_CONFIG"] = "1"
|
os.environ["LOGFIRE_IGNORE_NO_CONFIG"] = "1"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue