Pipelinen parannuksia building blockeilla
This commit is contained in:
59
zipit/template_runs/tmpl_v1/test_main.py
Normal file
59
zipit/template_runs/tmpl_v1/test_main.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from main import app, get_db
|
||||
from models import Base
|
||||
|
||||
engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
|
||||
TestSession = sessionmaker(bind=engine)
|
||||
|
||||
def override_get_db():
|
||||
db = TestSession()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
client = TestClient(app)
|
||||
|
||||
def setup_function():
|
||||
"""Luo taulut ennen jokaista testiä."""
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
def teardown_function():
|
||||
"""Tyhjennä taulut jokaisen testin jälkeen."""
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
def test_create_todo():
|
||||
response = client.post('/todos/', json={"title": "Test title", "description": "Test description", "due_date": "2024-01-15", "status": "pending"})
|
||||
assert response.status_code == 201
|
||||
assert response.json()["id"] is not None
|
||||
|
||||
def test_list_todos():
|
||||
client.post('/todos/', json={"title": "Test title", "description": "Test description", "due_date": "2024-01-15", "status": "pending"})
|
||||
response = client.get('/todos/')
|
||||
assert response.status_code == 200
|
||||
assert len(response.json()) >= 1
|
||||
|
||||
def test_get_todo_by_id():
|
||||
created = client.post('/todos/', json={"title": "Test title", "description": "Test description", "due_date": "2024-01-15", "status": "pending"}).json()
|
||||
response = client.get(f"/todos/{created["id"]}")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["id"] == created["id"]
|
||||
|
||||
def test_get_todo_not_found():
|
||||
response = client.get('/todos/99999')
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_update_todo():
|
||||
created = client.post('/todos/', json={"title": "Test title", "description": "Test description", "due_date": "2024-01-15", "status": "pending"}).json()
|
||||
response = client.put(f"/todos/{created["id"]}", json={"title": "Updated title", "description": "Test description", "due_date": "2024-01-15", "status": "pending"})
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_delete_todo():
|
||||
created = client.post('/todos/', json={"title": "Test title", "description": "Test description", "due_date": "2024-01-15", "status": "pending"}).json()
|
||||
response = client.delete(f"/todos/{created["id"]}")
|
||||
assert response.status_code == 204
|
||||
response = client.get(f"/todos/{created["id"]}")
|
||||
assert response.status_code == 404
|
||||
Reference in New Issue
Block a user