dylanglenister commited on
Commit
c541ca0
·
1 Parent(s): e8fe75d

Added patient tests

Browse files
Files changed (1) hide show
  1. tests/test_patient.py +152 -0
tests/test_patient.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from bson import ObjectId
4
+
5
+ from src.data.connection import Collections, 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:
149
+ logger().info("Starting MongoDB repository integration tests...")
150
+ unittest.main(verbosity=2)
151
+ finally:
152
+ logger().info("Tests completed and database connection closed.")