mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-29 14:04:22 +01:00
Merge 'bindings/python: Fix flaky tests' from Diego Reis
Just following best practices and making each test idempotent and independent of each other. Closes #1152
This commit is contained in:
Binary file not shown.
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user