dylanglenister commited on
Commit
f15fec7
·
1 Parent(s): 5733813

Updated patient tests

Browse files
Files changed (1) hide show
  1. tests/test_patient.py +80 -83
tests/test_patient.py CHANGED
@@ -1,148 +1,145 @@
1
  import unittest
 
2
 
3
  from bson import ObjectId
 
4
 
5
- from src.data.connection import Collections, close_connection, get_collection
 
6
  from src.data.repositories import patient as patient_repo
7
  from src.utils.logger import logger
8
  from tests.base_test import BaseMongoTest
9
 
10
 
11
  class TestPatientRepository(BaseMongoTest):
 
12
 
13
  def setUp(self):
14
  """Set up a clean test environment before each test."""
15
  super().setUp()
16
  self.test_collection = self._collections[Collections.PATIENT]
17
- # Initialize the collection with schema and indexes for each test
18
  patient_repo.init(collection_name=self.test_collection, drop=True)
19
 
20
  def test_init_functionality(self):
21
  """Test that the init function correctly sets up the collection and its indexes."""
22
- # Verify collection exists
23
  self.assertIn(self.test_collection, self.db.list_collection_names())
24
-
25
- # Verify that the index on assigned_doctor_id was created
26
  index_info = get_collection(self.test_collection).index_information()
27
  self.assertIn("assigned_doctor_id_1", index_info)
28
 
29
  def test_create_patient(self):
30
- """Test patient creation with required and optional fields."""
31
  # Test minimal creation
32
  patient_id = patient_repo.create_patient(
33
- name="John Doe",
34
- age=45,
35
- sex="Male",
36
- ethnicity="Caucasian",
37
- collection_name=self.test_collection
38
  )
39
  self.assertIsInstance(patient_id, str)
40
  doc = self.get_doc_by_id(Collections.PATIENT, patient_id)
41
  self.assertIsNotNone(doc)
42
  self.assertEqual(doc["name"], "John Doe") # type: ignore
43
- self.assertIn("created_at", doc) # type: ignore
44
- self.assertEqual(doc["created_at"], doc["updated_at"]) # type: ignore
45
 
46
- # Test full creation
47
- doctor_id = ObjectId()
48
- patient_id_full = patient_repo.create_patient(
49
  name="Jane Doe",
50
  age=30,
51
  sex="Female",
52
  ethnicity="Asian",
53
- address="123 Test St",
54
- phone="555-0123",
55
- email="jane@test.com",
56
- medications=["Med A", "Med B"],
57
- past_assessment_summary="Previous checkup normal",
58
- assigned_doctor_id=str(doctor_id),
59
  collection_name=self.test_collection
60
  )
61
- doc_full = self.get_doc_by_id(Collections.PATIENT, patient_id_full)
62
- self.assertIsNotNone(doc_full)
63
- self.assertEqual(doc_full["email"], "jane@test.com") # type: ignore
64
- self.assertEqual(len(doc_full["medications"]), 2) # type: ignore
65
- self.assertIsInstance(doc_full["assigned_doctor_id"], ObjectId) # type: ignore
66
- self.assertEqual(doc_full["assigned_doctor_id"], doctor_id) # type: ignore
 
 
 
 
 
 
67
 
68
  def test_get_patient_by_id(self):
69
- """Test retrieving a single patient by their ID."""
70
  patient_id = patient_repo.create_patient("GetMe", 33, "Female", "Other", collection_name=self.test_collection)
71
-
72
  patient = patient_repo.get_patient_by_id(patient_id, collection_name=self.test_collection)
73
  self.assertIsNotNone(patient)
74
  self.assertEqual(patient["_id"], patient_id) # type: ignore
75
- self.assertEqual(patient["name"], "GetMe") # type: ignore
76
- self.assertIsInstance(patient["_id"], str) # type: ignore
77
-
78
  # Test retrieval of a non-existent patient returns None
79
- non_existent_id = str(ObjectId())
80
- patient = patient_repo.get_patient_by_id(non_existent_id, collection_name=self.test_collection)
81
- self.assertIsNone(patient)
82
 
83
  def test_update_patient_profile(self):
84
- """Test patient profile updates for existing and non-existing patients."""
85
- patient_id = patient_repo.create_patient(
86
- name="Update Test",
87
- age=25,
88
- sex="Male",
89
- ethnicity="Hispanic",
90
- collection_name=self.test_collection
91
- )
92
- original_doc = self.get_doc_by_id(Collections.PATIENT, patient_id)
93
- self.assertIsNotNone(original_doc)
94
-
95
- # Test partial update
96
  updates = {"age": 26, "phone": "555-9999"}
97
- modified_count = patient_repo.update_patient_profile(
98
- patient_id, updates, collection_name=self.test_collection
99
- )
100
  self.assertEqual(modified_count, 1)
101
-
102
- updated_doc = self.get_doc_by_id(Collections.PATIENT, patient_id)
103
- self.assertIsNotNone(updated_doc)
104
- self.assertEqual(updated_doc["age"], 26) # type: ignore
105
- self.assertEqual(updated_doc["phone"], "555-9999") # type: ignore
106
- self.assertLess(original_doc["updated_at"], updated_doc["updated_at"]) # type: ignore
107
-
108
  # Test updating a non-existent patient returns a modified count of 0
109
- non_existent_id = str(ObjectId())
110
- modified_count = patient_repo.update_patient_profile(
111
- non_existent_id, {"name": "Ghost"}, collection_name=self.test_collection
112
- )
113
- self.assertEqual(modified_count, 0)
114
 
115
  def test_search_patients(self):
116
  """Test patient search functionality with various queries and limits."""
117
- # Create test patients
118
  patient_repo.create_patient("Alice Smith", 30, "Female", "Asian", collection_name=self.test_collection)
119
  patient_repo.create_patient("Bob Smith", 45, "Male", "Caucasian", collection_name=self.test_collection)
120
- patient_repo.create_patient("Charlie Brown", 60, "Male", "African", collection_name=self.test_collection)
121
-
122
- # Test search by partial name, should be case-insensitive
123
  results = patient_repo.search_patients("smith", collection_name=self.test_collection)
124
  self.assertEqual(len(results), 2)
125
-
126
- # Test search is sorted ascending by name
127
  self.assertEqual(results[0]["name"], "Alice Smith")
128
- self.assertEqual(results[1]["name"], "Bob Smith")
 
129
 
130
- # Test case-insensitive exact match
131
- results = patient_repo.search_patients("charlie brown", collection_name=self.test_collection)
132
- self.assertEqual(len(results), 1)
133
- self.assertEqual(results[0]["name"], "Charlie Brown")
134
 
135
- # Test limit parameter
136
- results = patient_repo.search_patients("S", limit=1, collection_name=self.test_collection)
137
- self.assertEqual(len(results), 1)
138
 
139
- # Test query with no results
140
- results = patient_repo.search_patients("Zebra", collection_name=self.test_collection)
141
- self.assertEqual(len(results), 0)
 
 
142
 
143
- # Test empty query string returns an empty list
144
- results = patient_repo.search_patients("", collection_name=self.test_collection)
145
- self.assertEqual(len(results), 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
  if __name__ == "__main__":
148
  try:
 
1
  import unittest
2
+ from unittest.mock import patch
3
 
4
  from bson import ObjectId
5
+ from pymongo.errors import ConnectionFailure
6
 
7
+ from src.data.connection import (ActionFailed, Collections, close_connection,
8
+ get_collection)
9
  from src.data.repositories import patient as patient_repo
10
  from src.utils.logger import logger
11
  from tests.base_test import BaseMongoTest
12
 
13
 
14
  class TestPatientRepository(BaseMongoTest):
15
+ """Test class for the 'happy path' and edge cases of patient repository functions."""
16
 
17
  def setUp(self):
18
  """Set up a clean test environment before each test."""
19
  super().setUp()
20
  self.test_collection = self._collections[Collections.PATIENT]
 
21
  patient_repo.init(collection_name=self.test_collection, drop=True)
22
 
23
  def test_init_functionality(self):
24
  """Test that the init function correctly sets up the collection and its indexes."""
 
25
  self.assertIn(self.test_collection, self.db.list_collection_names())
 
 
26
  index_info = get_collection(self.test_collection).index_information()
27
  self.assertIn("assigned_doctor_id_1", index_info)
28
 
29
  def test_create_patient(self):
30
+ """Test patient creation with minimal and full data."""
31
  # Test minimal creation
32
  patient_id = patient_repo.create_patient(
33
+ "John Doe", 45, "Male", "Caucasian", collection_name=self.test_collection
 
 
 
 
34
  )
35
  self.assertIsInstance(patient_id, str)
36
  doc = self.get_doc_by_id(Collections.PATIENT, patient_id)
37
  self.assertIsNotNone(doc)
38
  self.assertEqual(doc["name"], "John Doe") # type: ignore
 
 
39
 
40
+ # Test full creation with all optional parameters
41
+ doctor_id = str(ObjectId())
42
+ full_id = patient_repo.create_patient(
43
  name="Jane Doe",
44
  age=30,
45
  sex="Female",
46
  ethnicity="Asian",
47
+ address="123 Wellness Way",
48
+ phone="555-123-4567",
49
+ email="jane.doe@example.com",
50
+ medications=["Lisinopril", "Metformin"],
51
+ past_assessment_summary="Routine check-up, generally healthy.",
52
+ assigned_doctor_id=doctor_id,
53
  collection_name=self.test_collection
54
  )
55
+ self.assertIsInstance(full_id, str)
56
+ full_doc = self.get_doc_by_id(Collections.PATIENT, full_id)
57
+
58
+ # Verify all fields were saved correctly
59
+ self.assertIsNotNone(full_doc)
60
+ self.assertEqual(full_doc["address"], "123 Wellness Way") # type: ignore
61
+ self.assertEqual(full_doc["phone"], "555-123-4567") # type: ignore
62
+ self.assertEqual(full_doc["email"], "jane.doe@example.com") # type: ignore
63
+ self.assertEqual(len(full_doc["medications"]), 2) # type: ignore
64
+ self.assertIn("Lisinopril", full_doc["medications"]) # type: ignore
65
+ self.assertEqual(full_doc["past_assessment_summary"], "Routine check-up, generally healthy.") # type: ignore
66
+ self.assertEqual(str(full_doc["assigned_doctor_id"]), doctor_id) # type: ignore
67
 
68
  def test_get_patient_by_id(self):
69
+ """Test retrieving an existing patient by ID."""
70
  patient_id = patient_repo.create_patient("GetMe", 33, "Female", "Other", collection_name=self.test_collection)
 
71
  patient = patient_repo.get_patient_by_id(patient_id, collection_name=self.test_collection)
72
  self.assertIsNotNone(patient)
73
  self.assertEqual(patient["_id"], patient_id) # type: ignore
 
 
 
74
  # Test retrieval of a non-existent patient returns None
75
+ self.assertIsNone(patient_repo.get_patient_by_id(str(ObjectId()), collection_name=self.test_collection))
 
 
76
 
77
  def test_update_patient_profile(self):
78
+ """Test successfully updating an existing patient's profile."""
79
+ patient_id = patient_repo.create_patient("Update Test", 25, "Male", "Hispanic", collection_name=self.test_collection)
 
 
 
 
 
 
 
 
 
 
80
  updates = {"age": 26, "phone": "555-9999"}
81
+ modified_count = patient_repo.update_patient_profile(patient_id, updates, collection_name=self.test_collection)
 
 
82
  self.assertEqual(modified_count, 1)
83
+ doc = self.get_doc_by_id(Collections.PATIENT, patient_id)
84
+ self.assertEqual(doc["age"], 26) # type: ignore
 
 
 
 
 
85
  # Test updating a non-existent patient returns a modified count of 0
86
+ self.assertEqual(patient_repo.update_patient_profile(str(ObjectId()), {"name": "Ghost"}, collection_name=self.test_collection), 0)
 
 
 
 
87
 
88
  def test_search_patients(self):
89
  """Test patient search functionality with various queries and limits."""
 
90
  patient_repo.create_patient("Alice Smith", 30, "Female", "Asian", collection_name=self.test_collection)
91
  patient_repo.create_patient("Bob Smith", 45, "Male", "Caucasian", collection_name=self.test_collection)
 
 
 
92
  results = patient_repo.search_patients("smith", collection_name=self.test_collection)
93
  self.assertEqual(len(results), 2)
 
 
94
  self.assertEqual(results[0]["name"], "Alice Smith")
95
+ # Test limit parameter
96
+ self.assertEqual(len(patient_repo.search_patients("s", limit=1, collection_name=self.test_collection)), 1)
97
 
 
 
 
 
98
 
99
+ class TestPatientRepositoryExceptions(BaseMongoTest):
100
+ """Test class for the exception handling of all patient repository functions."""
 
101
 
102
+ def setUp(self):
103
+ """Set up the test environment before each test."""
104
+ super().setUp()
105
+ self.test_collection = self._collections[Collections.PATIENT]
106
+ patient_repo.init(collection_name=self.test_collection, drop=True)
107
 
108
+ def test_write_error_raises_action_failed(self):
109
+ """Test that creating or updating with data violating schema raises ActionFailed."""
110
+ # Test creating a patient with a 'sex' value not in the schema's enum
111
+ with self.assertRaises(ActionFailed):
112
+ patient_repo.create_patient("Schema Test", 25, "InvalidValue", "Other", collection_name=self.test_collection)
113
+
114
+ # Test updating a patient with an invalid value
115
+ patient_id = patient_repo.create_patient("UpdateSchema", 30, "Male", "Other", collection_name=self.test_collection)
116
+ with self.assertRaises(ActionFailed):
117
+ patient_repo.update_patient_profile(patient_id, {"ethnicity": 123}, collection_name=self.test_collection)
118
+
119
+ def test_invalid_id_raises_action_failed(self):
120
+ """Test that functions raise ActionFailed when given a malformed ObjectId string."""
121
+ with self.assertRaises(ActionFailed):
122
+ patient_repo.create_patient("Test", 30, "Male", "Other", assigned_doctor_id="not-a-valid-id", collection_name=self.test_collection)
123
+ with self.assertRaises(ActionFailed):
124
+ patient_repo.get_patient_by_id("not-a-valid-id", collection_name=self.test_collection)
125
+ with self.assertRaises(ActionFailed):
126
+ patient_repo.update_patient_profile("not-a-valid-id", {"name": "test"}, collection_name=self.test_collection)
127
+
128
+ @patch('src.data.repositories.patient.get_collection')
129
+ def test_all_functions_raise_on_connection_error(self, mock_get_collection):
130
+ """Test that all repo functions catch generic PyMongoErrors and raise ActionFailed."""
131
+ mock_get_collection.side_effect = ConnectionFailure("Simulated connection error")
132
+
133
+ with self.assertRaises(ActionFailed):
134
+ patient_repo.init(collection_name=self.test_collection, drop=True)
135
+ with self.assertRaises(ActionFailed):
136
+ patient_repo.create_patient("Test", 30, "Male", "Other", collection_name=self.test_collection)
137
+ with self.assertRaises(ActionFailed):
138
+ patient_repo.get_patient_by_id(str(ObjectId()), collection_name=self.test_collection)
139
+ with self.assertRaises(ActionFailed):
140
+ patient_repo.update_patient_profile(str(ObjectId()), {"name": "test"}, collection_name=self.test_collection)
141
+ with self.assertRaises(ActionFailed):
142
+ patient_repo.search_patients("test", collection_name=self.test_collection)
143
 
144
  if __name__ == "__main__":
145
  try: