diff --git a/bindings/python/tests/database.db b/bindings/python/tests/database.db deleted file mode 100644 index 89397b76e..000000000 Binary files a/bindings/python/tests/database.db and /dev/null differ diff --git a/bindings/python/tests/test_database.py b/bindings/python/tests/test_database.py index 4a8301395..a8276b21c 100644 --- a/bindings/python/tests/test_database.py +++ b/bindings/python/tests/test_database.py @@ -1,3 +1,4 @@ +import os import sqlite3 import pytest @@ -5,13 +6,44 @@ import pytest import limbo +@pytest.fixture(autouse=True) +def setup_database(): + db_path = "tests/database.db" + db_wal_path = "tests/database.db-wal" + + # Ensure the database file is created fresh for each test + if os.path.exists(db_path): + os.remove(db_path) + if os.path.exists(db_wal_path): + os.remove(db_wal_path) + + # Create a new database file + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + cursor.execute("CREATE TABLE users (id INT PRIMARY KEY, username TEXT)") + cursor.execute("INSERT INTO users VALUES (1, 'alice')") + cursor.execute("INSERT INTO users VALUES (2, 'bob')") + conn.commit() + conn.close() + + yield db_path + + # Cleanup after the test + if os.path.exists(db_path): + os.remove(db_path) + if os.path.exists(db_wal_path): + os.remove(db_wal_path) + + @pytest.mark.parametrize("provider", ["sqlite3", "limbo"]) -def test_fetchall_select_all_users(provider): - conn = connect(provider, "tests/database.db") +def test_fetchall_select_all_users(provider, setup_database): + conn = connect(provider, setup_database) cursor = conn.cursor() cursor.execute("SELECT * FROM users") users = cursor.fetchall() + + conn.close() assert users assert users == [(1, "alice"), (2, "bob")] @@ -23,6 +55,8 @@ def test_fetchall_select_user_ids(provider): cursor.execute("SELECT id FROM users") user_ids = cursor.fetchall() + + conn.close() assert user_ids assert user_ids == [(1,), (2,)] @@ -37,6 +71,8 @@ def test_in_memory_fetchone_select_all_users(provider): cursor.execute("SELECT * FROM users") alice = cursor.fetchone() + + conn.close() assert alice assert alice == (1, "alice") @@ -52,6 +88,8 @@ def test_fetchone_select_all_users(provider): assert alice == (1, "alice") bob = cursor.fetchone() + + conn.close() assert bob assert bob == (2, "bob") @@ -63,6 +101,8 @@ def test_fetchone_select_max_user_id(provider): cursor.execute("SELECT MAX(id) FROM users") max_id = cursor.fetchone() + + conn.close() assert max_id assert max_id == (2,) @@ -70,20 +110,20 @@ def test_fetchone_select_max_user_id(provider): # Test case for: https://github.com/tursodatabase/limbo/issues/494 @pytest.mark.parametrize("provider", ["sqlite3", "limbo"]) def test_commit(provider): - con = connect(provider, "tests/database.db") - cur = con.cursor() + conn = connect(provider, "tests/database.db") + cur = conn.cursor() cur.execute(""" CREATE TABLE IF NOT EXISTS users_b ( id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT UNIQUE NOT NULL, + username TEXT NOT NULL, email TEXT NOT NULL, role TEXT NOT NULL, created_at DATETIME NOT NULL DEFAULT (datetime('now')) ) """) - con.commit() + conn.commit() sample_users = [ ("alice", "alice@example.com", "admin"), @@ -95,11 +135,13 @@ def test_commit(provider): for username, email, role in sample_users: cur.execute("INSERT INTO users_b (username, email, role) VALUES (?, ?, ?)", (username, email, role)) - con.commit() + conn.commit() # Now query the table res = cur.execute("SELECT * FROM users_b") record = res.fetchone() + + conn.close() assert record