From 2681d6c45b35ece713c6124bdcaa10e97c099585 Mon Sep 17 00:00:00 2001 From: Abhijit Mamarde Date: Mon, 5 Jan 2026 01:32:23 +0530 Subject: [PATCH] on tests completions, delete all the temporary generated collections --- test_mongokv.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test_mongokv.py b/test_mongokv.py index 56a4cbf..5c2a915 100644 --- a/test_mongokv.py +++ b/test_mongokv.py @@ -3,6 +3,7 @@ import pytest import pytest_asyncio +import pymongo from mongokv import Mkv @@ -225,3 +226,29 @@ def test_sync_close_does_not_raise(mkv_sync: Mkv): mkv_sync.set("k", "v") mkv_sync.close() # Should not raise +# ---------- Module-level cleanup ---------- + +def teardown_module(module): + """ + Clean up all test collections from the database after all tests have run. + """ + client = None + try: + # Use a direct pymongo connection for cleanup + client = pymongo.MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000) + db = client["test_mkvdb"] + + # Check if server is available + client.admin.command('ping') + + # Drop all collections created during tests + for collection_name in db.list_collection_names(): + if collection_name.startswith("test_kv_"): + db.drop_collection(collection_name) + except pymongo.errors.ConnectionFailure: + # If we can't connect, there's nothing to clean up. + # This allows tests to pass even if Mongo is not running. + pass + finally: + if client: + client.close()