Pipelinen parannuksia building blockeilla
This commit is contained in:
11
zipit/template_runs/tmpl_v2/Dockerfile
Normal file
11
zipit/template_runs/tmpl_v2/Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
||||
FROM python:3.12-slim
|
||||
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
|
||||
ENV UV_CACHE_DIR=/tmp/uv-cache
|
||||
WORKDIR /app
|
||||
COPY pyproject.toml .
|
||||
RUN uv sync
|
||||
COPY *.py .
|
||||
RUN useradd -m appuser && chown -R appuser:appuser /app /tmp/uv-cache
|
||||
USER appuser
|
||||
EXPOSE 8000
|
||||
CMD ["uv", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
5
zipit/template_runs/tmpl_v2/docker-compose.yml
Normal file
5
zipit/template_runs/tmpl_v2/docker-compose.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
ports:
|
||||
- "18765:8000"
|
||||
51
zipit/template_runs/tmpl_v2/main.py
Normal file
51
zipit/template_runs/tmpl_v2/main.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from fastapi import FastAPI, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from models import Base, engine, SessionLocal, Todo
|
||||
from schemas import TodoCreate, TodoResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@app.post("/todos/", response_model=TodoResponse, status_code=201)
|
||||
def create_todo(item: TodoCreate, db: Session = Depends(get_db)):
|
||||
db_item = Todo(**item.model_dump())
|
||||
db.add(db_item)
|
||||
db.commit()
|
||||
db.refresh(db_item)
|
||||
return db_item
|
||||
|
||||
@app.get("/todos/", response_model=list[TodoResponse])
|
||||
def list_todos(db: Session = Depends(get_db)):
|
||||
return db.query(Todo).all()
|
||||
|
||||
@app.get("/todos/{item_id}", response_model=TodoResponse)
|
||||
def get_todo(item_id: int, db: Session = Depends(get_db)):
|
||||
item = db.query(Todo).filter(Todo.id == item_id).first()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Todo not found")
|
||||
return item
|
||||
|
||||
@app.put("/todos/{item_id}", response_model=TodoResponse)
|
||||
def update_todo(item_id: int, item: TodoCreate, db: Session = Depends(get_db)):
|
||||
db_item = db.query(Todo).filter(Todo.id == item_id).first()
|
||||
if not db_item:
|
||||
raise HTTPException(status_code=404, detail="Todo not found")
|
||||
for key, value in item.model_dump().items():
|
||||
setattr(db_item, key, value)
|
||||
db.commit()
|
||||
db.refresh(db_item)
|
||||
return db_item
|
||||
|
||||
@app.delete("/todos/{item_id}", status_code=204)
|
||||
def delete_todo(item_id: int, db: Session = Depends(get_db)):
|
||||
db_item = db.query(Todo).filter(Todo.id == item_id).first()
|
||||
if not db_item:
|
||||
raise HTTPException(status_code=404, detail="Todo not found")
|
||||
db.delete(db_item)
|
||||
db.commit()
|
||||
18
zipit/template_runs/tmpl_v2/models.py
Normal file
18
zipit/template_runs/tmpl_v2/models.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from sqlalchemy import create_engine, Column, Integer, Date, Integer, String, Text
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
DATABASE_URL = "sqlite:///./app.db"
|
||||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
|
||||
class Todo(Base):
|
||||
__tablename__ = "todos"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
title = Column(String(255), nullable=False)
|
||||
description = Column(Text)
|
||||
due_date = Column(Date)
|
||||
status = Column(String(20), nullable=False, default="pending")
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
11
zipit/template_runs/tmpl_v2/pyproject.toml
Normal file
11
zipit/template_runs/tmpl_v2/pyproject.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[project]
|
||||
name = "todo-app"
|
||||
version = "0.1.0"
|
||||
requires-python = ">=3.11"
|
||||
dependencies = [
|
||||
"fastapi",
|
||||
"uvicorn[standard]",
|
||||
"sqlalchemy",
|
||||
"pytest",
|
||||
"httpx",
|
||||
]
|
||||
63
zipit/template_runs/tmpl_v2/report.json
Normal file
63
zipit/template_runs/tmpl_v2/report.json
Normal file
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"run_id": "tmpl_v2",
|
||||
"model": "qwen2.5-coder:7b-instruct-q4_K_M",
|
||||
"description": "Todo-sovellus FastAPI + SQLite, CRUD-endpointit ja testit",
|
||||
"spec": {
|
||||
"project_name": "todo-app",
|
||||
"description": "A simple Todo application with CRUD endpoints using FastAPI and SQLite.",
|
||||
"entities": [
|
||||
{
|
||||
"name": "Todo",
|
||||
"table_name": "todos",
|
||||
"fields": [
|
||||
{
|
||||
"name": "title",
|
||||
"sa_type": "String(255)",
|
||||
"py_type": "str",
|
||||
"nullable": false,
|
||||
"default": null
|
||||
},
|
||||
{
|
||||
"name": "description",
|
||||
"sa_type": "Text",
|
||||
"py_type": "str | None",
|
||||
"nullable": true,
|
||||
"default": null
|
||||
},
|
||||
{
|
||||
"name": "due_date",
|
||||
"sa_type": "Date",
|
||||
"py_type": "date | None",
|
||||
"nullable": true,
|
||||
"default": null
|
||||
},
|
||||
{
|
||||
"name": "status",
|
||||
"sa_type": "String(20)",
|
||||
"py_type": "str",
|
||||
"nullable": false,
|
||||
"default": "pending"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"extra_imports": [
|
||||
"from datetime import date"
|
||||
]
|
||||
},
|
||||
"files": {
|
||||
"models.py": 712,
|
||||
"schemas.py": 293,
|
||||
"main.py": 1711,
|
||||
"test_main.py": 2394,
|
||||
"pyproject.toml": 177,
|
||||
"Dockerfile": 339
|
||||
},
|
||||
"valid": true,
|
||||
"docker": {
|
||||
"build": true,
|
||||
"pytest": true,
|
||||
"api": true,
|
||||
"errors": []
|
||||
}
|
||||
}
|
||||
14
zipit/template_runs/tmpl_v2/schemas.py
Normal file
14
zipit/template_runs/tmpl_v2/schemas.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from pydantic import BaseModel
|
||||
from datetime import date
|
||||
|
||||
class TodoCreate(BaseModel):
|
||||
title: str
|
||||
description: str | None = None
|
||||
due_date: date | None = None
|
||||
status: str = "pending"
|
||||
|
||||
class TodoResponse(TodoCreate):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
43
zipit/template_runs/tmpl_v2/spec.json
Normal file
43
zipit/template_runs/tmpl_v2/spec.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"project_name": "todo-app",
|
||||
"description": "A simple Todo application with CRUD endpoints using FastAPI and SQLite.",
|
||||
"entities": [
|
||||
{
|
||||
"name": "Todo",
|
||||
"table_name": "todos",
|
||||
"fields": [
|
||||
{
|
||||
"name": "title",
|
||||
"sa_type": "String(255)",
|
||||
"py_type": "str",
|
||||
"nullable": false,
|
||||
"default": null
|
||||
},
|
||||
{
|
||||
"name": "description",
|
||||
"sa_type": "Text",
|
||||
"py_type": "str | None",
|
||||
"nullable": true,
|
||||
"default": null
|
||||
},
|
||||
{
|
||||
"name": "due_date",
|
||||
"sa_type": "Date",
|
||||
"py_type": "date | None",
|
||||
"nullable": true,
|
||||
"default": null
|
||||
},
|
||||
{
|
||||
"name": "status",
|
||||
"sa_type": "String(20)",
|
||||
"py_type": "str",
|
||||
"nullable": false,
|
||||
"default": "pending"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"extra_imports": [
|
||||
"from datetime import date"
|
||||
]
|
||||
}
|
||||
58
zipit/template_runs/tmpl_v2/test_main.py
Normal file
58
zipit/template_runs/tmpl_v2/test_main.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
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
|
||||
|
||||
TEST_DB = "sqlite:///./test.db"
|
||||
test_engine = create_engine(TEST_DB, connect_args={"check_same_thread": False})
|
||||
TestSession = sessionmaker(autocommit=False, autoflush=False, bind=test_engine)
|
||||
Base.metadata.create_all(bind=test_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 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
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
|
||||
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()
|
||||
item_id = created['id']
|
||||
response = client.get(f'/todos/{item_id}')
|
||||
assert response.status_code == 200
|
||||
assert response.json()['id'] == item_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()
|
||||
item_id = created['id']
|
||||
response = client.put(f'/todos/{item_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()
|
||||
item_id = created['id']
|
||||
response = client.delete(f'/todos/{item_id}')
|
||||
assert response.status_code == 204
|
||||
response = client.get(f'/todos/{item_id}')
|
||||
assert response.status_code == 404
|
||||
Reference in New Issue
Block a user