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

Better import naming

Browse files
Files changed (1) hide show
  1. tests/test_account.py +34 -34
tests/test_account.py CHANGED
@@ -7,7 +7,7 @@ from pymongo import ASCENDING
7
  from pymongo.errors import DuplicateKeyError
8
 
9
  from src.data.connection import Collections, get_collection
10
- from src.data.repositories import account as Repo
11
  from src.utils.logger import logger
12
  from tests.base_test import BaseMongoTest
13
 
@@ -20,7 +20,7 @@ class TestAccountRepository(BaseMongoTest):
20
  self.test_collection = self._collections[Collections.ACCOUNT]
21
 
22
  # Call the updated init function directly to set up the test collection
23
- Repo.init(collection_name=self.test_collection, drop=True)
24
 
25
  # Add a unique index on 'name' to test for duplicate key errors
26
  get_collection(self.test_collection).create_index(
@@ -33,11 +33,11 @@ class TestAccountRepository(BaseMongoTest):
33
  self.assertIn(self.test_collection, self.db.list_collection_names())
34
 
35
  # 2. Test the drop functionality
36
- Repo.create_account("ToDelete", "Doctor", collection_name=self.test_collection)
37
  self.assertEqual(get_collection(self.test_collection).count_documents({}), 1)
38
 
39
  # Re-initialize with drop=True
40
- Repo.init(collection_name=self.test_collection, drop=True)
41
 
42
  # Assert the collection is now empty but still exists
43
  self.assertEqual(get_collection(self.test_collection).count_documents({}), 0)
@@ -48,7 +48,7 @@ class TestAccountRepository(BaseMongoTest):
48
  # Test basic creation
49
  name = "Test Doctor"
50
  role = "Doctor"
51
- account_id = Repo.create_account(
52
  name=name,
53
  role=role,
54
  collection_name=self.test_collection
@@ -63,7 +63,7 @@ class TestAccountRepository(BaseMongoTest):
63
  self.assertEqual(doc["created_at"], doc["updated_at"]) # type: ignore
64
 
65
  # Test creation with specialty
66
- account_id_spec = Repo.create_account(
67
  name="Specialist",
68
  role="Doctor",
69
  specialty="Cardiology",
@@ -75,18 +75,18 @@ class TestAccountRepository(BaseMongoTest):
75
 
76
  # Test creating a user with a duplicate name raises an error
77
  with self.assertRaises(DuplicateKeyError):
78
- Repo.create_account(name=name, role="Nurse", collection_name=self.test_collection)
79
 
80
  def test_get_account(self):
81
  """Test retrieving a single account by its ID."""
82
- account_id = Repo.create_account(
83
  "GetMe", "Doctor", collection_name=self.test_collection
84
  )
85
  original_doc = self.get_doc_by_id(Collections.ACCOUNT, account_id)
86
  self.assertNotIn("last_seen", original_doc) # type: ignore
87
 
88
  # Get the account and verify 'last_seen' is updated
89
- account = Repo.get_account(account_id, collection_name=self.test_collection)
90
  self.assertIsNotNone(account)
91
  self.assertEqual(account["_id"], account_id) # type: ignore
92
  self.assertEqual(account["name"], "GetMe") # type: ignore
@@ -96,26 +96,26 @@ class TestAccountRepository(BaseMongoTest):
96
 
97
  # Test retrieval of a non-existent account returns None
98
  non_existent_id = str(ObjectId())
99
- account = Repo.get_account(non_existent_id, collection_name=self.test_collection)
100
  self.assertIsNone(account)
101
 
102
  def test_get_account_by_name(self):
103
  """Test retrieving an account by name."""
104
  name = "FindByName"
105
- Repo.create_account(name, "Nurse", collection_name=self.test_collection)
106
 
107
- account = Repo.get_account_by_name(name, collection_name=self.test_collection)
108
  self.assertIsNotNone(account)
109
  self.assertEqual(account["name"], name) # type: ignore
110
  self.assertIsInstance(account["_id"], str) # type: ignore
111
 
112
  # Test retrieval of a non-existent name returns None
113
- account = Repo.get_account_by_name("NonExistent", collection_name=self.test_collection)
114
  self.assertIsNone(account)
115
 
116
  def test_update_account(self):
117
  """Test updating an existing account's data."""
118
- account_id = Repo.create_account(
119
  name="Update Test",
120
  role="Doctor",
121
  collection_name=self.test_collection
@@ -124,7 +124,7 @@ class TestAccountRepository(BaseMongoTest):
124
  self.assertIsNotNone(original_doc)
125
 
126
  updates = {"name": "Updated Name", "specialty": "Pediatrics"}
127
- success = Repo.update_account(account_id, updates, collection_name=self.test_collection)
128
  self.assertTrue(success)
129
 
130
  updated_doc = self.get_doc_by_id(Collections.ACCOUNT, account_id)
@@ -135,11 +135,11 @@ class TestAccountRepository(BaseMongoTest):
135
  self.assertLess(original_doc["updated_at"], updated_doc["updated_at"]) # type: ignore
136
 
137
  # Test that updating a non-existent account returns False
138
- success = Repo.update_account(str(ObjectId()), {"name": "No One"}, collection_name=self.test_collection)
139
  self.assertFalse(success)
140
 
141
  # Test that 'created_at' is ignored in updates and does not change
142
- success = Repo.update_account(
143
  account_id,
144
  {"created_at": datetime(2000, 1, 1)},
145
  collection_name=self.test_collection
@@ -150,48 +150,48 @@ class TestAccountRepository(BaseMongoTest):
150
 
151
  def test_search_accounts(self):
152
  """Test account search functionality for various cases."""
153
- Repo.create_account("Alpha Doctor", "Doctor", collection_name=self.test_collection)
154
- Repo.create_account("Beta Doctor", "Doctor", collection_name=self.test_collection)
155
- Repo.create_account("Charlie Medical", "Medical Student", collection_name=self.test_collection)
156
 
157
  # Test case-insensitive partial match
158
- results = Repo.search_accounts("beta", collection_name=self.test_collection)
159
  self.assertEqual(len(results), 1)
160
  self.assertEqual(results[0]["name"], "Beta Doctor")
161
 
162
  # Test case-insensitive full match
163
- results = Repo.search_accounts("ALPHA DOCTOR", collection_name=self.test_collection)
164
  self.assertEqual(len(results), 1)
165
 
166
  # Test query that matches multiple entries with a limit
167
- results = Repo.search_accounts("Doctor", limit=2, collection_name=self.test_collection)
168
  self.assertEqual(len(results), 2)
169
  self.assertEqual(results[0]['name'], 'Alpha Doctor') # Assumes ascending sort by name
170
  self.assertEqual(results[1]['name'], 'Beta Doctor')
171
 
172
  # Test query with no matches
173
- results = Repo.search_accounts("NonExistent", collection_name=self.test_collection)
174
  self.assertEqual(len(results), 0)
175
 
176
  # Test empty query string returns an empty list
177
- results = Repo.search_accounts("", collection_name=self.test_collection)
178
  self.assertEqual(len(results), 0)
179
 
180
  def test_get_all_accounts(self):
181
  """Test retrieving all accounts with limit and sorting."""
182
- Repo.create_account("Charlie", "Doctor", collection_name=self.test_collection)
183
- Repo.create_account("Alpha", "Nurse", collection_name=self.test_collection)
184
- Repo.create_account("Beta", "Caregiver", collection_name=self.test_collection)
185
 
186
  # Test getting all accounts, verifying ascending sort order by name
187
- all_accounts = Repo.get_all_accounts(collection_name=self.test_collection)
188
  self.assertEqual(len(all_accounts), 3)
189
  self.assertEqual(all_accounts[0]["name"], "Alpha")
190
  self.assertEqual(all_accounts[1]["name"], "Beta")
191
  self.assertEqual(all_accounts[2]["name"], "Charlie")
192
 
193
  # Test with a limit
194
- limited_accounts = Repo.get_all_accounts(limit=2, collection_name=self.test_collection)
195
  self.assertEqual(len(limited_accounts), 2)
196
  self.assertEqual(limited_accounts[0]["name"], "Alpha")
197
  self.assertEqual(limited_accounts[1]["name"], "Beta")
@@ -199,15 +199,15 @@ class TestAccountRepository(BaseMongoTest):
199
  def test_get_account_frame(self):
200
  """Test retrieving accounts as a pandas DataFrame."""
201
  # Test with an empty collection
202
- df_empty = Repo.get_account_frame(collection_name=self.test_collection)
203
  self.assertIsInstance(df_empty, DataFrame)
204
  self.assertTrue(df_empty.empty)
205
 
206
  # Add data and test again
207
- id1 = Repo.create_account("Frame Alpha", "Doctor", collection_name=self.test_collection)
208
- Repo.create_account("Frame Beta", "Nurse", specialty="ICU", collection_name=self.test_collection)
209
 
210
- df = Repo.get_account_frame(collection_name=self.test_collection)
211
  self.assertIsInstance(df, DataFrame)
212
  self.assertEqual(len(df), 2)
213
 
 
7
  from pymongo.errors import DuplicateKeyError
8
 
9
  from src.data.connection import Collections, get_collection
10
+ from src.data.repositories import account as account_repo
11
  from src.utils.logger import logger
12
  from tests.base_test import BaseMongoTest
13
 
 
20
  self.test_collection = self._collections[Collections.ACCOUNT]
21
 
22
  # Call the updated init function directly to set up the test collection
23
+ account_repo.init(collection_name=self.test_collection, drop=True)
24
 
25
  # Add a unique index on 'name' to test for duplicate key errors
26
  get_collection(self.test_collection).create_index(
 
33
  self.assertIn(self.test_collection, self.db.list_collection_names())
34
 
35
  # 2. Test the drop functionality
36
+ account_repo.create_account("ToDelete", "Doctor", collection_name=self.test_collection)
37
  self.assertEqual(get_collection(self.test_collection).count_documents({}), 1)
38
 
39
  # Re-initialize with drop=True
40
+ account_repo.init(collection_name=self.test_collection, drop=True)
41
 
42
  # Assert the collection is now empty but still exists
43
  self.assertEqual(get_collection(self.test_collection).count_documents({}), 0)
 
48
  # Test basic creation
49
  name = "Test Doctor"
50
  role = "Doctor"
51
+ account_id = account_repo.create_account(
52
  name=name,
53
  role=role,
54
  collection_name=self.test_collection
 
63
  self.assertEqual(doc["created_at"], doc["updated_at"]) # type: ignore
64
 
65
  # Test creation with specialty
66
+ account_id_spec = account_repo.create_account(
67
  name="Specialist",
68
  role="Doctor",
69
  specialty="Cardiology",
 
75
 
76
  # Test creating a user with a duplicate name raises an error
77
  with self.assertRaises(DuplicateKeyError):
78
+ account_repo.create_account(name=name, role="Nurse", collection_name=self.test_collection)
79
 
80
  def test_get_account(self):
81
  """Test retrieving a single account by its ID."""
82
+ account_id = account_repo.create_account(
83
  "GetMe", "Doctor", collection_name=self.test_collection
84
  )
85
  original_doc = self.get_doc_by_id(Collections.ACCOUNT, account_id)
86
  self.assertNotIn("last_seen", original_doc) # type: ignore
87
 
88
  # Get the account and verify 'last_seen' is updated
89
+ account = account_repo.get_account(account_id, collection_name=self.test_collection)
90
  self.assertIsNotNone(account)
91
  self.assertEqual(account["_id"], account_id) # type: ignore
92
  self.assertEqual(account["name"], "GetMe") # type: ignore
 
96
 
97
  # Test retrieval of a non-existent account returns None
98
  non_existent_id = str(ObjectId())
99
+ account = account_repo.get_account(non_existent_id, collection_name=self.test_collection)
100
  self.assertIsNone(account)
101
 
102
  def test_get_account_by_name(self):
103
  """Test retrieving an account by name."""
104
  name = "FindByName"
105
+ account_repo.create_account(name, "Nurse", collection_name=self.test_collection)
106
 
107
+ account = account_repo.get_account_by_name(name, collection_name=self.test_collection)
108
  self.assertIsNotNone(account)
109
  self.assertEqual(account["name"], name) # type: ignore
110
  self.assertIsInstance(account["_id"], str) # type: ignore
111
 
112
  # Test retrieval of a non-existent name returns None
113
+ account = account_repo.get_account_by_name("NonExistent", collection_name=self.test_collection)
114
  self.assertIsNone(account)
115
 
116
  def test_update_account(self):
117
  """Test updating an existing account's data."""
118
+ account_id = account_repo.create_account(
119
  name="Update Test",
120
  role="Doctor",
121
  collection_name=self.test_collection
 
124
  self.assertIsNotNone(original_doc)
125
 
126
  updates = {"name": "Updated Name", "specialty": "Pediatrics"}
127
+ success = account_repo.update_account(account_id, updates, collection_name=self.test_collection)
128
  self.assertTrue(success)
129
 
130
  updated_doc = self.get_doc_by_id(Collections.ACCOUNT, account_id)
 
135
  self.assertLess(original_doc["updated_at"], updated_doc["updated_at"]) # type: ignore
136
 
137
  # Test that updating a non-existent account returns False
138
+ success = account_repo.update_account(str(ObjectId()), {"name": "No One"}, collection_name=self.test_collection)
139
  self.assertFalse(success)
140
 
141
  # Test that 'created_at' is ignored in updates and does not change
142
+ success = account_repo.update_account(
143
  account_id,
144
  {"created_at": datetime(2000, 1, 1)},
145
  collection_name=self.test_collection
 
150
 
151
  def test_search_accounts(self):
152
  """Test account search functionality for various cases."""
153
+ account_repo.create_account("Alpha Doctor", "Doctor", collection_name=self.test_collection)
154
+ account_repo.create_account("Beta Doctor", "Doctor", collection_name=self.test_collection)
155
+ account_repo.create_account("Charlie Medical", "Medical Student", collection_name=self.test_collection)
156
 
157
  # Test case-insensitive partial match
158
+ results = account_repo.search_accounts("beta", collection_name=self.test_collection)
159
  self.assertEqual(len(results), 1)
160
  self.assertEqual(results[0]["name"], "Beta Doctor")
161
 
162
  # Test case-insensitive full match
163
+ results = account_repo.search_accounts("ALPHA DOCTOR", collection_name=self.test_collection)
164
  self.assertEqual(len(results), 1)
165
 
166
  # Test query that matches multiple entries with a limit
167
+ results = account_repo.search_accounts("Doctor", limit=2, collection_name=self.test_collection)
168
  self.assertEqual(len(results), 2)
169
  self.assertEqual(results[0]['name'], 'Alpha Doctor') # Assumes ascending sort by name
170
  self.assertEqual(results[1]['name'], 'Beta Doctor')
171
 
172
  # Test query with no matches
173
+ results = account_repo.search_accounts("NonExistent", collection_name=self.test_collection)
174
  self.assertEqual(len(results), 0)
175
 
176
  # Test empty query string returns an empty list
177
+ results = account_repo.search_accounts("", collection_name=self.test_collection)
178
  self.assertEqual(len(results), 0)
179
 
180
  def test_get_all_accounts(self):
181
  """Test retrieving all accounts with limit and sorting."""
182
+ account_repo.create_account("Charlie", "Doctor", collection_name=self.test_collection)
183
+ account_repo.create_account("Alpha", "Nurse", collection_name=self.test_collection)
184
+ account_repo.create_account("Beta", "Caregiver", collection_name=self.test_collection)
185
 
186
  # Test getting all accounts, verifying ascending sort order by name
187
+ all_accounts = account_repo.get_all_accounts(collection_name=self.test_collection)
188
  self.assertEqual(len(all_accounts), 3)
189
  self.assertEqual(all_accounts[0]["name"], "Alpha")
190
  self.assertEqual(all_accounts[1]["name"], "Beta")
191
  self.assertEqual(all_accounts[2]["name"], "Charlie")
192
 
193
  # Test with a limit
194
+ limited_accounts = account_repo.get_all_accounts(limit=2, collection_name=self.test_collection)
195
  self.assertEqual(len(limited_accounts), 2)
196
  self.assertEqual(limited_accounts[0]["name"], "Alpha")
197
  self.assertEqual(limited_accounts[1]["name"], "Beta")
 
199
  def test_get_account_frame(self):
200
  """Test retrieving accounts as a pandas DataFrame."""
201
  # Test with an empty collection
202
+ df_empty = account_repo.get_account_frame(collection_name=self.test_collection)
203
  self.assertIsInstance(df_empty, DataFrame)
204
  self.assertTrue(df_empty.empty)
205
 
206
  # Add data and test again
207
+ id1 = account_repo.create_account("Frame Alpha", "Doctor", collection_name=self.test_collection)
208
+ account_repo.create_account("Frame Beta", "Nurse", specialty="ICU", collection_name=self.test_collection)
209
 
210
+ df = account_repo.get_account_frame(collection_name=self.test_collection)
211
  self.assertIsInstance(df, DataFrame)
212
  self.assertEqual(len(df), 2)
213