From 822e6a0607a904e87e9f2f18726591cf7a024aca Mon Sep 17 00:00:00 2001 From: Vito Sansevero Date: Sat, 17 Oct 2026 07:51:18 -0700 Subject: [PATCH] test(db): reuse existing handle guards in all pair fixtures (#194) --- tests/test_api_prompts.py | 6 ++++++ tests/test_pair_dependency_concurrency.py | 6 ++++++ tests/test_pair_execution.py | 6 ++++++ tests/test_pair_image_linking.py | 6 ++++++ tests/test_pair_ingestion.py | 6 ++++++ tests/test_pair_restore_semantics.py | 6 ++++++ tests/test_pair_schema_restore.py | 6 ++++++ tests/test_pair_tag_maintenance.py | 6 ++++++ tests/test_pair_usage_tracking.py | 6 ++++++ tests/test_pair_vocabulary.py | 6 ++++++ tests/test_prompt_pair_node.py | 6 ++++++ tests/test_prompt_pairs.py | 6 ++++++ 12 files changed, 72 insertions(+) diff --git a/tests/test_api_prompts.py b/tests/test_api_prompts.py index 5773bf7..18b1c21 100644 --- a/tests/test_api_prompts.py +++ b/tests/test_api_prompts.py @@ -17,6 +17,11 @@ sys.modules.setdefault("server", MagicMock()) from aiohttp import web # noqa: E402 from aiohttp.test_utils import AioHTTPTestCase # noqa: E402 +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase # noqa: E402 from py.api import PromptManagerAPI # noqa: E402 import py.api.prompts as prompts_module # noqa: E402 @@ -47,6 +52,7 @@ class PromptAPITestCase(AioHTTPTestCase): await super().tearDownAsync() # closes the aiohttp test client # Windows refuses to unlink a database that still has open handles. self.api.db.close_all() + assert_closed(self, self._temp_db.name) for path in ( self._temp_db.name, self._temp_db.name + "-wal", diff --git a/tests/test_pair_dependency_concurrency.py b/tests/test_pair_dependency_concurrency.py index e715614..7a95c85 100644 --- a/tests/test_pair_dependency_concurrency.py +++ b/tests/test_pair_dependency_concurrency.py @@ -5,6 +5,11 @@ import sqlite3 import tempfile import unittest.mock +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from utils.hashing import generate_prompt_hash @@ -49,6 +54,7 @@ class TestPairDependencyConcurrency(unittest.TestCase): self.assertEqual(len(checks), len(actions)) finally: db.close_all() + assert_closed(self, directory) if __name__ == "__main__": diff --git a/tests/test_pair_execution.py b/tests/test_pair_execution.py index fd46d84..5f6bea2 100644 --- a/tests/test_pair_execution.py +++ b/tests/test_pair_execution.py @@ -10,6 +10,11 @@ from unittest.mock import Mock from PIL import Image from PIL.PngImagePlugin import PngInfo +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from prompt_manager_pair import PromptManagerPair from utils.metadata_extractor import ComfyUIMetadataExtractor @@ -35,6 +40,7 @@ class TestPairExecution(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) + self.addCleanup(assert_closed, self, self.tmp.name) self.db = PromptDatabase(str(Path(self.tmp.name) / "test.db")) self.addCleanup(self.db.close_all) self.node = PromptManagerPair.__new__(PromptManagerPair) diff --git a/tests/test_pair_image_linking.py b/tests/test_pair_image_linking.py index 350d055..6c87309 100644 --- a/tests/test_pair_image_linking.py +++ b/tests/test_pair_image_linking.py @@ -5,6 +5,11 @@ import tempfile import unittest from pathlib import Path +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from utils.hashing import generate_prompt_hash from utils.image_monitor import ImageGenerationHandler @@ -35,6 +40,7 @@ class TestPairImageLinking(unittest.TestCase): def tearDown(self): self.db.close_all() + assert_closed(self, self.temp.name) self.temp.cleanup() def test_exact_metadata_creates_pair_link_without_counting_a_run(self): diff --git a/tests/test_pair_ingestion.py b/tests/test_pair_ingestion.py index af94809..4cfdfa9 100644 --- a/tests/test_pair_ingestion.py +++ b/tests/test_pair_ingestion.py @@ -5,6 +5,11 @@ import unittest from pathlib import Path from unittest.mock import patch +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from database.pair_ingestion import ingest_resolved_pair from utils.hashing import generate_prompt_hash @@ -26,6 +31,7 @@ class TestPairIngestion(unittest.TestCase): def tearDown(self): self.db.close_all() + assert_closed(self, self.temp.name) self.temp.cleanup() def test_first_scan_repeat_and_negative_variants_reuse_components(self): diff --git a/tests/test_pair_restore_semantics.py b/tests/test_pair_restore_semantics.py index 80c7595..ad2272c 100644 --- a/tests/test_pair_restore_semantics.py +++ b/tests/test_pair_restore_semantics.py @@ -5,6 +5,11 @@ import os import tempfile import unittest.mock +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.models import PromptModel from database.operations import PromptDatabase @@ -13,6 +18,7 @@ class TestPairRestoreSemantics(unittest.TestCase): def setUp(self): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) + self.addCleanup(assert_closed, self, directory.name) self.path = os.path.join(directory.name, "restore.db") self.db = PromptDatabase(self.path) self.addCleanup(self.db.close_all) diff --git a/tests/test_pair_schema_restore.py b/tests/test_pair_schema_restore.py index 6caf8dc..16e919d 100644 --- a/tests/test_pair_schema_restore.py +++ b/tests/test_pair_schema_restore.py @@ -8,6 +8,11 @@ from contextlib import closing from pathlib import Path from unittest.mock import patch +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.models import PromptModel @@ -15,6 +20,7 @@ class PairSchemaTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) + self.addCleanup(assert_closed, self, self.temp.name) self.addCleanup(PromptModel.reset_schema_cache) self.models = [] self.addCleanup(self.close_models) diff --git a/tests/test_pair_tag_maintenance.py b/tests/test_pair_tag_maintenance.py index 9930d7a..c56e968 100644 --- a/tests/test_pair_tag_maintenance.py +++ b/tests/test_pair_tag_maintenance.py @@ -5,6 +5,11 @@ import sqlite3 import tempfile import unittest +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase @@ -12,6 +17,7 @@ class TestPairTagMaintenance(unittest.TestCase): def setUp(self): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) + self.addCleanup(assert_closed, self, directory.name) self.db = PromptDatabase(os.path.join(directory.name, "pair-tags.db")) self.addCleanup(self.db.close_all) diff --git a/tests/test_pair_usage_tracking.py b/tests/test_pair_usage_tracking.py index 1010e13..9039f29 100644 --- a/tests/test_pair_usage_tracking.py +++ b/tests/test_pair_usage_tracking.py @@ -6,6 +6,11 @@ import tempfile import unittest from unittest.mock import Mock +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from utils import usage_tracking from utils.prompt_graph import run_prompt_nodes @@ -26,6 +31,7 @@ class TestPairUsageTracking(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) + self.addCleanup(assert_closed, self, self.tmp.name) self.db = PromptDatabase(os.path.join(self.tmp.name, "pairs.db")) self.addCleanup(self.db.close_all) self.queued = QueuedRuns() diff --git a/tests/test_pair_vocabulary.py b/tests/test_pair_vocabulary.py index 89305ac..147274a 100644 --- a/tests/test_pair_vocabulary.py +++ b/tests/test_pair_vocabulary.py @@ -4,6 +4,11 @@ import os import tempfile import unittest +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from utils.hashing import generate_prompt_hash @@ -12,6 +17,7 @@ class TestPairVocabulary(unittest.TestCase): def setUp(self): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) + self.addCleanup(assert_closed, self, directory.name) self.db = PromptDatabase(os.path.join(directory.name, "vocabulary.db")) self.addCleanup(self.db.close_all) diff --git a/tests/test_prompt_pair_node.py b/tests/test_prompt_pair_node.py index eff150c..693a6c1 100644 --- a/tests/test_prompt_pair_node.py +++ b/tests/test_prompt_pair_node.py @@ -6,6 +6,11 @@ import tempfile import unittest from unittest.mock import Mock +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from prompt_manager_pair import PromptManagerPair from utils.usage_tracking import PendingFirstUse @@ -32,6 +37,7 @@ class TestPromptPairNode(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) + self.addCleanup(assert_closed, self, self.tmp.name) self.db = PromptDatabase(os.path.join(self.tmp.name, "pairs.db")) self.addCleanup(self.db.close_all) self.node = PromptManagerPair.__new__(PromptManagerPair) diff --git a/tests/test_prompt_pairs.py b/tests/test_prompt_pairs.py index fc8b6f3..c219d82 100644 --- a/tests/test_prompt_pairs.py +++ b/tests/test_prompt_pairs.py @@ -7,6 +7,11 @@ import unittest from concurrent.futures import ThreadPoolExecutor from unittest.mock import patch +try: + from tests.open_handles import assert_closed +except ImportError: # discovered with tests/ as the top-level directory + from open_handles import assert_closed + from database.operations import PromptDatabase from utils.hashing import generate_prompt_hash @@ -15,6 +20,7 @@ class TestPromptPairs(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) + self.addCleanup(assert_closed, self, self.tmp.name) self.db = PromptDatabase(os.path.join(self.tmp.name, "pairs.db")) self.addCleanup(self.db.close_all)