Spaces:
Runtime error
Runtime error
Commit ·
afc114b
1
Parent(s): 6dfc429
Added Testing for User route
Browse files- app/main.py +1 -1
- requirements.txt +0 -0
- tests/__init__.py +0 -0
- tests/database.py +34 -0
- tests/test_users.py +13 -0
app/main.py
CHANGED
|
@@ -23,4 +23,4 @@ app.include_router(vote.router)
|
|
| 23 |
|
| 24 |
@app.get("/")
|
| 25 |
def home():
|
| 26 |
-
return "FastAPI Project"
|
|
|
|
| 23 |
|
| 24 |
@app.get("/")
|
| 25 |
def home():
|
| 26 |
+
return {"message": "FastAPI Project"}
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
tests/__init__.py
ADDED
|
File without changes
|
tests/database.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi.testclient import TestClient
|
| 2 |
+
import pytest
|
| 3 |
+
from app.database import get_db, Base
|
| 4 |
+
from app.main import app
|
| 5 |
+
from sqlalchemy import create_engine
|
| 6 |
+
from sqlalchemy.orm import sessionmaker
|
| 7 |
+
from app.config import setting
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
SQLALCHEMY_DATABASE_URL = f"postgresql://{setting.database_username}:{setting.database_password}@{setting.database_hostname}:{setting.database_port}/{setting.database_name}_Test"
|
| 11 |
+
|
| 12 |
+
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
| 13 |
+
TestingSessionMaker = sessionmaker(autoflush=False, autocommit=False, bind=engine)
|
| 14 |
+
|
| 15 |
+
@pytest.fixture
|
| 16 |
+
def session():
|
| 17 |
+
Base.metadata.drop_all(bind=engine)
|
| 18 |
+
Base.metadata.create_all(bind=engine)
|
| 19 |
+
db = TestingSessionMaker()
|
| 20 |
+
try:
|
| 21 |
+
yield db
|
| 22 |
+
finally:
|
| 23 |
+
db.close()
|
| 24 |
+
|
| 25 |
+
@pytest.fixture
|
| 26 |
+
def client(session):
|
| 27 |
+
def override_get_db():
|
| 28 |
+
try:
|
| 29 |
+
yield session
|
| 30 |
+
finally:
|
| 31 |
+
session.close()
|
| 32 |
+
|
| 33 |
+
app.dependency_overrides[get_db] = override_get_db
|
| 34 |
+
yield TestClient(app)
|
tests/test_users.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from app.schemas import user
|
| 2 |
+
from tests.database import client, session
|
| 3 |
+
|
| 4 |
+
def test_root(client):
|
| 5 |
+
response = client.get("/")
|
| 6 |
+
assert response.status_code == 200
|
| 7 |
+
assert response.json().get('message') == "FastAPI Project"
|
| 8 |
+
|
| 9 |
+
def test_create_user(client):
|
| 10 |
+
response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})
|
| 11 |
+
new_user = user.UserResponse(**response.json())
|
| 12 |
+
assert response.status_code == 201
|
| 13 |
+
assert new_user.email == "abdullah@gmail.com"
|