167 lines
5.1 KiB
Python
167 lines
5.1 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
|
|
# =====================================================
|
|
|
|
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
|
|
|
|
|
|
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")
|
|
|
|
|
|
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
|
|
|
|
|
|
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"
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
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"
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|