Fix test setup for backend
This commit is contained in:
parent
5528ae8519
commit
78138e83c7
@ -1,3 +1,7 @@
|
||||
flake8 src/*
|
||||
black src/*
|
||||
isort src/*
|
||||
|
||||
flake8 tests/*
|
||||
black tests/*
|
||||
isort tests/*
|
||||
|
||||
@ -11,4 +11,5 @@ interrogate
|
||||
python-jose[cryptography]
|
||||
passlib
|
||||
jwt
|
||||
PyJWT
|
||||
PyJWT
|
||||
pytest-asyncio
|
||||
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
@ -5,9 +5,9 @@ from fastapi import Depends, HTTPException
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from crud.users import get_user_by_username, pwd_context
|
||||
from models import User
|
||||
from schemas.users import UserCreate
|
||||
from src.crud.users import get_user_by_username, pwd_context
|
||||
from src.models import User
|
||||
from src.schemas.users import UserCreate
|
||||
|
||||
DEFAULT_NR_HIGH_SCORES = 10
|
||||
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from enums import CourseEnum, course_enum_list
|
||||
from models import CourseProgress, User
|
||||
from schemas.courseprogress import CourseProgressBase
|
||||
from src.enums import CourseEnum, course_enum_list
|
||||
from src.models import CourseProgress, User
|
||||
from src.schemas.courseprogress import CourseProgressBase
|
||||
|
||||
|
||||
def get_course_progress(db: Session, user: User, course: CourseEnum):
|
||||
|
||||
@ -2,10 +2,10 @@ from fastapi import HTTPException
|
||||
from sqlalchemy import desc
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from enums import MinigameEnum
|
||||
from models import HighScore, User
|
||||
from schemas.highscores import HighScoreBase
|
||||
from schemas.users import UserHighScore
|
||||
from src.enums import MinigameEnum
|
||||
from src.models import HighScore, User
|
||||
from src.schemas.highscores import HighScoreBase
|
||||
from src.schemas.users import UserHighScore
|
||||
|
||||
|
||||
def get_high_scores(db: Session, minigame: MinigameEnum, nr_highest: int):
|
||||
|
||||
@ -2,8 +2,8 @@ from fastapi import HTTPException
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models import User
|
||||
from schemas.users import UserCreate
|
||||
from src.models import User
|
||||
from src.schemas.users import UserCreate
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
21
src/main.py
21
src/main.py
@ -1,18 +1,21 @@
|
||||
import sys
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from crud import authentication as crud_authentication
|
||||
from crud import courseprogress as crud_courseprogress
|
||||
from crud import highscores as crud_highscores
|
||||
from crud import users as crud_users
|
||||
from database import SessionLocal, engine, get_db
|
||||
from enums import CourseEnum, MinigameEnum
|
||||
from models import Base
|
||||
from schemas import courseprogress, highscores, users
|
||||
sys.path.append("..")
|
||||
|
||||
from src.crud import authentication as crud_authentication
|
||||
from src.crud import courseprogress as crud_courseprogress
|
||||
from src.crud import highscores as crud_highscores
|
||||
from src.crud import users as crud_users
|
||||
from src.database import Base, SessionLocal, engine, get_db
|
||||
from src.enums import CourseEnum, MinigameEnum
|
||||
from src.schemas import courseprogress, highscores, users
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
@ -79,7 +82,7 @@ async def get_course_progress(
|
||||
return crud_courseprogress.get_course_progress(db, current_user, course)
|
||||
|
||||
|
||||
@app.patch("/courseprogress")
|
||||
@app.patch("/courseprogress/{course_name}")
|
||||
async def get_course_progress(
|
||||
course_progress: courseprogress.CourseProgressBase,
|
||||
current_user_name: str = Depends(crud_authentication.get_current_user_name),
|
||||
|
||||
@ -2,8 +2,8 @@ from sqlalchemy import Column, Float, ForeignKey, Integer, String
|
||||
from sqlalchemy.dialects.postgresql import ARRAY
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base
|
||||
from enums import CourseEnum, MinigameEnum, StrEnumType
|
||||
from src.database import Base
|
||||
from src.enums import CourseEnum, MinigameEnum, StrEnumType
|
||||
|
||||
|
||||
class User(Base):
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
from enums import CourseEnum
|
||||
from src.enums import CourseEnum
|
||||
|
||||
|
||||
class CourseProgressBase(BaseModel):
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
from enums import MinigameEnum
|
||||
from src.enums import MinigameEnum
|
||||
|
||||
|
||||
class HighScoreBase(BaseModel):
|
||||
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
20
tests/config/database.py
Normal file
20
tests/config/database.py
Normal file
@ -0,0 +1,20 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from src.database import Base
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "postgresql://admin:WeSign123!@localhost/wesigntest"
|
||||
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||||
|
||||
TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def override_get_db():
|
||||
try:
|
||||
db = TestSessionLocal()
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
27
tests/config/setup.py
Normal file
27
tests/config/setup.py
Normal file
@ -0,0 +1,27 @@
|
||||
import sys
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from src.database import Base as ProductionBase
|
||||
from tests.config.database import (SQLALCHEMY_DATABASE_URL, TestBase,
|
||||
TestSessionLocal)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session():
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||||
print(SQLALCHEMY_DATABASE_URL)
|
||||
ProductionBase.metadata.create_all(bind=engine)
|
||||
TestBase.metadata.create_all(bind=engine)
|
||||
session = TestSessionLocal(bind=engine)
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.rollback()
|
||||
TestBase.metadata.drop_all(bind=engine)
|
||||
ProductionBase.metadata.drop_all(bind=engine)
|
||||
session.close()
|
||||
25
tests/test_users.py
Normal file
25
tests/test_users.py
Normal file
@ -0,0 +1,25 @@
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
from src.main import app, get_db
|
||||
from tests.config.database import override_get_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_user():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": "user27", "password": "mettn", "avatar": "lion"},
|
||||
)
|
||||
|
||||
print(response)
|
||||
assert response.status_code == 200
|
||||
15
tests/usertests.py
Normal file
15
tests/usertests.py
Normal file
@ -0,0 +1,15 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from main import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_add_user():
|
||||
response = client.post(
|
||||
"/users",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": "Lukas", "password": "mettn"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
Loading…
x
Reference in New Issue
Block a user