TODO-app/tests/api/test_app.py

169 lines
5.6 KiB
Python

import sqlite3
from app import create_app
# =====================================================
# Helpers
# =====================================================
def fetch_one(db_path: str, sql: str, params=()):
con = sqlite3.connect(db_path)
try:
return con.execute(sql, params).fetchone()
finally:
con.close()
def fetch_all(db_path: str, sql: str, params=()):
con = sqlite3.connect(db_path)
try:
return con.execute(sql, params).fetchall()
finally:
con.close()
# =====================================================
# Tests
# =====================================================
# Test that the dashboard loads successfully
def test_dashboard_loads(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
resp = client.get("/dashboard")
assert resp.status_code == 200
# Test that creating a todo inserts a row in the database
def test_create_todo_inserts_row(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
resp = client.post(
"/todo/create",
data={"title": "Test", "description": "Desc", "status": "not-started"},
follow_redirects=True,
)
assert resp.status_code == 200
row = fetch_one(str(db_path), "SELECT title, description, status FROM todo")
assert row == ("Test", "Desc", "not-started")
# Test that creating a todo requires title and description
def test_create_requires_fields(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
resp = client.post(
"/todo/create",
data={"title": "", "description": "", "status": "not-started"},
follow_redirects=True,
)
assert resp.status_code == 200
row = fetch_one(str(db_path), "SELECT COUNT(*) FROM todo")
assert row[0] == 0
# Test that updating a todo's status works
def test_invalid_status_is_rejected_on_update(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
# skapa en todo
client.post("/todo/create", data={"title": "A", "description": "B", "status": "not-started"})
todo_id = fetch_one(str(db_path), "SELECT id FROM todo")[0]
resp = client.post(
f"/todo/{todo_id}/status",
data={"status": "hax-status"},
follow_redirects=True,
)
assert resp.status_code == 200
# status ska vara kvar som innan
status = fetch_one(str(db_path), "SELECT status FROM todo WHERE id=?", (todo_id,))[0]
assert status == "not-started"
# Test that updating a todo's status to 'done' works
def test_update_status_to_done(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
client.post("/todo/create", data={"title": "A", "description": "B", "status": "not-started"})
todo_id = fetch_one(str(db_path), "SELECT id FROM todo")[0]
resp = client.post(
f"/todo/{todo_id}/status",
data={"status": "done"},
follow_redirects=True,
)
assert resp.status_code == 200
status, edited = fetch_one(str(db_path), "SELECT status, edited FROM todo WHERE id=?", (todo_id,))
assert status == "done"
assert edited is not None
# Test that deleting a todo removes it from the database
def test_delete_todo(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
client.post("/todo/create", data={"title": "A", "description": "B", "status": "not-started"})
todo_id = fetch_one(str(db_path), "SELECT id FROM todo")[0]
resp = client.post(
f"/todo/{todo_id}/delete",
follow_redirects=True,
)
assert resp.status_code == 200
row = fetch_one(str(db_path), "SELECT COUNT(*) FROM todo")
assert row[0] == 0
# Test that the dashboard lists created todos
def test_dashboard_lists_created_todo(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
client.post("/todo/create", data={"title": "X", "description": "Y", "status": "not-started"})
resp = client.get("/dashboard")
assert resp.status_code == 200
assert b"X" in resp.data
assert b"Y" in resp.data
# Test that creating a todo with invalid status defaults to 'not-started'
def test_create_invalid_status_defaults_to_not_started(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
client.post("/todo/create", data={"title": "T", "description": "D", "status": "weird"})
row = fetch_one(str(db_path), "SELECT status FROM todo")
assert row[0] == "not-started"
# Test that deleting a nonexistent todo does not crash
def test_delete_nonexistent_does_not_crash(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
resp = client.post("/todo/999/delete", follow_redirects=True)
assert resp.status_code == 200
# Test that updating a nonexistent todo does not crash
def test_update_nonexistent_does_not_crash(tmp_path):
db_path = tmp_path / "test.db"
app = create_app({"TESTING": True, "DB_PATH": str(db_path)})
client = app.test_client()
resp = client.post("/todo/999/status", data={"status": "done"}, follow_redirects=True)
assert resp.status_code == 200