diff --git a/tests/test_api.py b/tests/test_api.py index d7bc416..c29f98e 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -38,6 +38,9 @@ class APITestCase(AioHTTPTestCase): return app async def tearDownAsync(self): + # Close every thread's connection first: Windows refuses to delete + # an open database file, and executor threads hold connections too. + self.api.db.close_all() for path in ( self._temp_db.name, self._temp_db.name + "-wal", diff --git a/tests/test_api_core.py b/tests/test_api_core.py index 86d4ca7..2d534e7 100644 --- a/tests/test_api_core.py +++ b/tests/test_api_core.py @@ -372,6 +372,7 @@ class CoreRoutesTestCase(AioHTTPTestCase): return app async def tearDownAsync(self): + self.api.db.close_all() shutil.rmtree(self.tmpdir, ignore_errors=True) def _fake_root(self): @@ -557,6 +558,7 @@ class TestInstanceHelpers(FolderPathsFixture): super().setUp() self.api = PromptManagerAPI() self.api.db = PromptDatabase(os.path.join(self.tmpdir, "prompts.db")) + self.addCleanup(self.api.db.close_all) # runs before the rmtree cleanup orig_dirs = list(GalleryConfig.MONITORING_DIRECTORIES) self.addCleanup(setattr, GalleryConfig, "MONITORING_DIRECTORIES", orig_dirs) diff --git a/tests/test_diagnostics_config.py b/tests/test_diagnostics_config.py index c05b95b..9ecf077 100644 --- a/tests/test_diagnostics_config.py +++ b/tests/test_diagnostics_config.py @@ -185,7 +185,7 @@ class TestAdminEndpointsUseConfigPath(unittest.TestCase): self.assertGreater(len(result.body), 0) self.assertTrue(result.body.startswith(b"SQLite format 3\x00")) finally: - real_db.close() + real_db.close_all() for suffix in ("", "-wal", "-shm"): if os.path.exists(db_file + suffix): os.unlink(db_file + suffix) diff --git a/tests/test_image_linking.py b/tests/test_image_linking.py index 4f6714f..f75d18c 100644 --- a/tests/test_image_linking.py +++ b/tests/test_image_linking.py @@ -51,6 +51,7 @@ class LinkingTestCase(unittest.TestCase): ) def tearDown(self): + self.db.close_all() # Windows cannot unlink an open database file for path in ( self.image, self.db_path, diff --git a/tests/test_node_execution_contract.py b/tests/test_node_execution_contract.py index 3889f2e..75de204 100644 --- a/tests/test_node_execution_contract.py +++ b/tests/test_node_execution_contract.py @@ -182,6 +182,7 @@ class NodeTestCase(unittest.TestCase): self.node.comfyui_integration = mock.Mock() def tearDown(self): + self.db.close_all() # Windows cannot unlink an open database file for suffix in ("", "-wal", "-shm"): if os.path.exists(self.db_path + suffix): os.unlink(self.db_path + suffix) diff --git a/tests/test_utils_diagnostics.py b/tests/test_utils_diagnostics.py index 64eef6f..33353d8 100644 --- a/tests/test_utils_diagnostics.py +++ b/tests/test_utils_diagnostics.py @@ -25,20 +25,23 @@ def same_path(a, b): class DiagnosticsTestCase(unittest.TestCase): def setUp(self): - self.tmp = tempfile.TemporaryDirectory() + self.tmp = tempfile.TemporaryDirectory(ignore_cleanup_errors=True) self.addCleanup(self.tmp.cleanup) self.db_path = os.path.join(self.tmp.name, "prompts.db") self.diag = GalleryDiagnostics(self.db_path) def _populate(self): db = PromptDatabase(self.db_path) - prompt_id = db.save_prompt( - text="a cat", prompt_hash=generate_prompt_hash("a cat") - ) - image = os.path.join(self.tmp.name, "cat.png") - with open(image, "wb") as f: - f.write(b"x") - db.link_image_to_prompt(prompt_id=prompt_id, image_path=image, metadata={}) + try: + prompt_id = db.save_prompt( + text="a cat", prompt_hash=generate_prompt_hash("a cat") + ) + image = os.path.join(self.tmp.name, "cat.png") + with open(image, "wb") as f: + f.write(b"x") + db.link_image_to_prompt(prompt_id=prompt_id, image_path=image, metadata={}) + finally: + db.close_all() # Windows cannot delete the temp dir while it is open return prompt_id def _chdir(self, path):