diff --git a/tests/test_database.py b/tests/test_database.py index 44360f0..ad3e8f3 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -37,3 +37,24 @@ def test_get_all_todos_works_with_local_connections(monkeypatch, tmp_path) -> No insert_todo(Todo(task="alpha", category="study")) assert len(get_all_todos()) == 1 + + +def test_get_all_todos_returns_rows_ordered_by_position(monkeypatch, tmp_path) -> None: + db_path = tmp_path / "todos.db" + monkeypatch.setattr("trushell.database.DB_PATH", db_path) + + _create_table() + with get_db_connection() as conn: + conn.execute( + "INSERT INTO todos VALUES (?, ?, ?, ?, ?, ?)", + ("second", "work", "", None, 0, 1), + ) + conn.execute( + "INSERT INTO todos VALUES (?, ?, ?, ?, ?, ?)", + ("first", "work", "", None, 0, 0), + ) + + tasks = get_all_todos() + + assert [task.task for task in tasks] == ["first", "second"] + assert [task.position for task in tasks] == [0, 1] diff --git a/trushell/database.py b/trushell/database.py index 594b1fa..a5647b1 100644 --- a/trushell/database.py +++ b/trushell/database.py @@ -54,7 +54,7 @@ def insert_todo(todo: Todo) -> None: def get_all_todos() -> List[Todo]: with get_db_connection() as conn: - results = conn.execute("SELECT * FROM todos").fetchall() + results = conn.execute("SELECT * FROM todos ORDER BY position").fetchall() return [Todo(*result) for result in results]