dylanglenister commited on
Commit
3a9698b
·
1 Parent(s): 969f308

Fixing connection closing logic

Browse files
tests/base_test.py CHANGED
@@ -13,7 +13,8 @@ from typing import Any
13
 
14
  from bson import ObjectId
15
 
16
- from src.data.connection import Collections, get_collection, get_database
 
17
 
18
 
19
  class BaseMongoTest(unittest.TestCase):
@@ -21,22 +22,28 @@ class BaseMongoTest(unittest.TestCase):
21
 
22
  @classmethod
23
  def setUpClass(cls):
24
- """Initialize test database connection"""
25
  cls.db = get_database()
26
  # Map production collections to test collections
27
- # The key is the collection's actual name (the attribute's value)
28
  cls._collections = {
29
  value: f"test_{value.lower()}" for name, value in vars(Collections).items()
30
  if not name.startswith('_') and isinstance(value, str)
31
  }
32
 
 
 
 
 
 
33
  def setUp(self):
34
- """Create clean test collections before each test"""
35
  for test_name in self._collections.values():
36
  self.db.drop_collection(test_name)
37
 
38
  def tearDown(self):
39
- """Clean up test collections after each test"""
 
 
40
  for test_name in self._collections.values():
41
  self.db.drop_collection(test_name)
42
 
@@ -45,7 +52,6 @@ class BaseMongoTest(unittest.TestCase):
45
  Helper to get a document by ID.
46
  `collection` should be the production collection name from `Collections`.
47
  """
48
- # Look up the test collection name using the production name as the key
49
  test_coll_name = self._collections.get(collection)
50
  if not test_coll_name:
51
  raise KeyError(f"No test collection mapping found for '{collection}'")
 
13
 
14
  from bson import ObjectId
15
 
16
+ from src.data.connection import (Collections, close_connection, get_collection,
17
+ get_database)
18
 
19
 
20
  class BaseMongoTest(unittest.TestCase):
 
22
 
23
  @classmethod
24
  def setUpClass(cls):
25
+ """Initialize test database connection once for the entire test class."""
26
  cls.db = get_database()
27
  # Map production collections to test collections
 
28
  cls._collections = {
29
  value: f"test_{value.lower()}" for name, value in vars(Collections).items()
30
  if not name.startswith('_') and isinstance(value, str)
31
  }
32
 
33
+ @classmethod
34
+ def tearDownClass(cls):
35
+ """Close the database connection once after all tests in the class are done."""
36
+ close_connection()
37
+
38
  def setUp(self):
39
+ """Create clean test collections before each test."""
40
  for test_name in self._collections.values():
41
  self.db.drop_collection(test_name)
42
 
43
  def tearDown(self):
44
+ """Clean up test collections after each test."""
45
+ # This tearDown is for individual test cleanup, not connection closing.
46
+ # We can leave it empty or keep the collection drop for extra safety.
47
  for test_name in self._collections.values():
48
  self.db.drop_collection(test_name)
49
 
 
52
  Helper to get a document by ID.
53
  `collection` should be the production collection name from `Collections`.
54
  """
 
55
  test_coll_name = self._collections.get(collection)
56
  if not test_coll_name:
57
  raise KeyError(f"No test collection mapping found for '{collection}'")
tests/test_account.py CHANGED
@@ -188,9 +188,6 @@ class TestAccountRepositoryExceptions(BaseMongoTest):
188
  account_repo.get_all_accounts(collection_name=self.test_collection)
189
 
190
  if __name__ == "__main__":
191
- try:
192
- logger().info("Starting MongoDB repository integration tests...")
193
- unittest.main(verbosity=2)
194
- finally:
195
- logger().info("Tests completed and database connection closed.")
196
- close_connection()
 
188
  account_repo.get_all_accounts(collection_name=self.test_collection)
189
 
190
  if __name__ == "__main__":
191
+ logger().info("Starting MongoDB repository integration tests...")
192
+ unittest.main(verbosity=2)
193
+ logger().info("Tests completed and database connection closed.")
 
 
 
tests/test_patient.py CHANGED
@@ -142,9 +142,6 @@ class TestPatientRepositoryExceptions(BaseMongoTest):
142
  patient_repo.search_patients("test", collection_name=self.test_collection)
143
 
144
  if __name__ == "__main__":
145
- try:
146
- logger().info("Starting MongoDB repository integration tests...")
147
- unittest.main(verbosity=2)
148
- finally:
149
- logger().info("Tests completed and database connection closed.")
150
- close_connection()
 
142
  patient_repo.search_patients("test", collection_name=self.test_collection)
143
 
144
  if __name__ == "__main__":
145
+ logger().info("Starting MongoDB repository integration tests...")
146
+ unittest.main(verbosity=2)
147
+ logger().info("Tests completed and database connection closed.")
 
 
 
tests/test_session.py CHANGED
@@ -174,9 +174,6 @@ class TestSessionRepositoryExceptions(BaseMongoTest):
174
  session_repo.add_message(str(ObjectId()), "t", True, collection_name=self.test_collection)
175
 
176
  if __name__ == "__main__":
177
- try:
178
- logger().info("Starting MongoDB repository integration tests...")
179
- unittest.main(verbosity=2)
180
- finally:
181
- logger().info("Tests completed and database connection closed.")
182
- close_connection()
 
174
  session_repo.add_message(str(ObjectId()), "t", True, collection_name=self.test_collection)
175
 
176
  if __name__ == "__main__":
177
+ logger().info("Starting MongoDB repository integration tests...")
178
+ unittest.main(verbosity=2)
179
+ logger().info("Tests completed and database connection closed.")