fix: use tempfile as db path in constraint.py

This commit is contained in:
Jussi Saurio
2025-05-29 21:03:10 +03:00
parent d18ab34193
commit d52a9a635c

View File

@@ -2,6 +2,7 @@
# Eventually extract these tests to be in the fuzzing integration tests # Eventually extract these tests to be in the fuzzing integration tests
import os import os
import tempfile
from faker import Faker from faker import Faker
from faker.providers.lorem.en_US import Provider as P from faker.providers.lorem.en_US import Provider as P
from cli_tests.test_limbo_cli import TestLimboShell from cli_tests.test_limbo_cli import TestLimboShell
@@ -244,7 +245,6 @@ class Table(BaseModel):
class ConstraintTest(BaseModel): class ConstraintTest(BaseModel):
table: Table table: Table
db_path: str = "testing/constraint.db"
insert_stmts: list[str] insert_stmts: list[str]
insert_errors: list[str] insert_errors: list[str]
update_errors: list[str] update_errors: list[str]
@@ -370,46 +370,31 @@ def all_tests() -> list[ConstraintTest]:
return tests return tests
def cleanup(db_fullpath: str):
wal_path = f"{db_fullpath}-wal"
shm_path = f"{db_fullpath}-shm"
paths = [db_fullpath, wal_path, shm_path]
for path in paths:
if os.path.exists(path):
os.remove(path)
def main(): def main():
tests = all_tests() tests = all_tests()
for test in tests: for test in tests:
console.info(test.table) console.info(test.table)
db_path = test.db_path with tempfile.NamedTemporaryFile(suffix='.db') as tmp:
try: try:
# Use with syntax to automatically close shell on error # Use with syntax to automatically close shell on error
with TestLimboShell("") as limbo: with TestLimboShell("") as limbo:
limbo.execute_dot(f".open {db_path}") limbo.execute_dot(f".open {tmp.name}")
test.run(limbo) test.run(limbo)
except Exception as e:
console.error(f"Test FAILED: {e}")
console.debug(test.table.create_table(), test.insert_stmts)
exit(1)
except Exception as e:
console.error(f"Test FAILED: {e}")
console.debug(test.table.create_table(), test.insert_stmts)
cleanup(db_path)
exit(1)
# delete db after every compat test so we we have fresh db for next test
cleanup(db_path)
db_path = "testing/constraint.db"
tests = [custom_test_2, regression_test_update_single_key] tests = [custom_test_2, regression_test_update_single_key]
for test in tests: for test in tests:
try: with tempfile.NamedTemporaryFile(suffix='.db') as tmp:
with TestLimboShell("") as limbo: try:
limbo.execute_dot(f".open {db_path}") with TestLimboShell("") as limbo:
test(limbo) limbo.execute_dot(f".open {tmp.name}")
except Exception as e: test(limbo)
console.error(f"Test FAILED: {e}") except Exception as e:
cleanup(db_path) console.error(f"Test FAILED: {e}")
exit(1) exit(1)
cleanup(db_path)
console.info("All tests passed successfully.") console.info("All tests passed successfully.")