Skip to content

Commit 1502065

Browse files
committed
fix: unit tests split up and all pass
1 parent 8e17f27 commit 1502065

File tree

6 files changed

+58
-39
lines changed

6 files changed

+58
-39
lines changed

src/officers/crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import utils
1212
from data import semesters
1313
from officers.constants import OfficerPosition
14-
from officers.models import OfficerInfoResponse
14+
from officers.models import OfficerInfoResponse, OfficerTermCreate
1515
from officers.tables import OfficerInfo, OfficerTerm
1616

1717
# NOTE: this module should not do any data validation; that should be done in the urls.py or higher layer

src/officers/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import date
22

3-
from pydantic import BaseModel, Field
3+
from pydantic import BaseModel, ConfigDict, Field
44

55
from officers.constants import OFFICER_LEGAL_NAME_MAX, OfficerPositionEnum
66

src/officers/tables.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ def serializable_dict(self) -> dict:
7171
"photo_url": self.photo_url,
7272
}
7373

74-
def update_from_params(self, params: OfficerTermUpdate, self_update: bool = True):
75-
if self_update:
76-
update_data = params.model_dump(exclude_unset=True, exclude={"position", "start_date", "end_date", "photo_url"})
77-
else:
74+
def update_from_params(self, params: OfficerTermUpdate, admin_update: bool = True):
75+
if admin_update:
7876
update_data = params.model_dump(exclude_unset=True)
77+
else:
78+
update_data = params.model_dump(exclude_unset=True, exclude={"position", "start_date", "end_date", "photo_url"})
7979
for k, v in update_data.items():
8080
setattr(self, k, v)
8181

src/officers/urls.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async def get_officer_info(
168168
},
169169
operation_id="create_officer_term"
170170
)
171-
async def new_officer_term(
171+
async def create_officer_term(
172172
request: Request,
173173
db_session: database.DBSession,
174174
officer_info_list: list[OfficerTermCreate],
@@ -214,7 +214,7 @@ async def new_officer_term(
214214
},
215215
operation_id="update_officer_info"
216216
)
217-
async def update_info(
217+
async def update_officer_info(
218218
request: Request,
219219
db_session: database.DBSession,
220220
computing_id: str,
@@ -246,7 +246,7 @@ async def update_info(
246246
},
247247
operation_id="update_officer_term_by_id"
248248
)
249-
async def update_term(
249+
async def update_officer_term(
250250
request: Request,
251251
db_session: database.DBSession,
252252
term_id: int,
@@ -265,7 +265,7 @@ async def update_term(
265265
if utils.is_past_term(old_officer_term):
266266
raise HTTPException(status_code=403, detail="you may not update past terms")
267267

268-
old_officer_term.update_from_params(body)
268+
old_officer_term.update_from_params(body, is_site_admin)
269269

270270
# TODO (#27): log all important changes to a .log file
271271
await officers.crud.update_officer_term(db_session, old_officer_term)
@@ -286,7 +286,7 @@ async def update_term(
286286
},
287287
operation_id="delete_officer_term_by_id"
288288
)
289-
async def remove_officer_term(
289+
async def delete_officer_term(
290290
request: Request,
291291
db_session: database.DBSession,
292292
term_id: int,

tests/integration/test_elections.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ async def test_read_elections(database_setup):
6161
election_false = await get_election(db_session, "this-not-a-election")
6262
assert election_false is None
6363

64-
6564
# Test getting specific election
6665
election = await get_election(db_session, "test-election-1")
6766
assert election is not None

tests/integration/test_officers.py

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async def test__post_officer_terms(client: AsyncClient):
139139
}])
140140
assert response.status_code == 422
141141

142-
async def test__patch_officer_terms(client: AsyncClient):
142+
async def test__patch_officer_term(client: AsyncClient):
143143
# Only admins can update new terms
144144
response = await client.patch("officers/info/abc11", json={
145145
"legal_name": "fancy name",
@@ -183,7 +183,7 @@ async def test__get_all_officers_admin(admin_client):
183183
assert len(response.json()) == 9
184184
assert response.json()[1]["phone_number"] == "1234567890"
185185

186-
async def test__get_officer_term(admin_client):
186+
async def test__get_officer_term_admin(admin_client):
187187
response = await admin_client.get(f"/officers/terms/{load_test_db.SYSADMIN_COMPUTING_ID}?include_future_terms=false")
188188
assert response.status_code == 200
189189
assert response.json() != []
@@ -194,6 +194,11 @@ async def test__get_officer_term(admin_client):
194194
assert response.json() != []
195195
assert len(response.json()) == 3
196196

197+
response = await admin_client.get("/officers/terms/ehbc12?include_future_terms=true")
198+
assert response.status_code == 200
199+
assert response.json() == []
200+
201+
async def test__get_officer_info_admin(admin_client):
197202
response = await admin_client.get("/officers/info/abc11")
198203
assert response.status_code == 200
199204
assert response.json() != {}
@@ -204,23 +209,21 @@ async def test__get_officer_term(admin_client):
204209
response = await admin_client.get("/officers/info/balargho")
205210
assert response.status_code == 404
206211

207-
response = await admin_client.get("/officers/terms/ehbc12?include_future_terms=true")
208-
assert response.status_code == 200
209-
assert response.json() == []
210-
211-
response = await admin_client.post("officers/term", content=json.dumps([{
212+
async def test__post_officer_term_admin(admin_client):
213+
response = await admin_client.post("officers/term", json=[{
212214
"computing_id": "ehbc12",
213215
"position": OfficerPositionEnum.DIRECTOR_OF_MULTIMEDIA,
214216
"start_date": "2025-12-29",
215217
"legal_name": "Eh Bc"
216-
}]))
218+
}])
217219
assert response.status_code == 200
218220

219221
response = await admin_client.get("/officers/terms/ehbc12?include_future_terms=true")
220222
assert response.status_code == 200
221223
assert response.json() != []
222224
assert len(response.json()) == 1
223225

226+
async def test__patch_officer_info_admin(admin_client):
224227
response = await admin_client.patch("officers/info/abc11", content=json.dumps({
225228
"legal_name": "Person A2",
226229
"phone_number": "12345asdab67890",
@@ -245,7 +248,9 @@ async def test__get_officer_term(admin_client):
245248
}))
246249
assert response.status_code == 404
247250

248-
response = await admin_client.patch("officers/term/1", content=json.dumps({
251+
async def test__patch_officer_term_admin(admin_client):
252+
target_id = 1
253+
response = await admin_client.patch(f"officers/term/{target_id}", json={
249254
"position": OfficerPositionEnum.TREASURER,
250255
"start_date": (date.today() - timedelta(days=365)).isoformat(),
251256
"end_date": (date.today() - timedelta(days=1)).isoformat(),
@@ -255,25 +260,41 @@ async def test__get_officer_term(admin_client):
255260
"favourite_pl_0": "4",
256261
"favourite_pl_1": "5",
257262
"biography": "hello o77"
258-
}))
263+
})
259264
assert response.status_code == 200
260265

261266
response = await admin_client.get("/officers/terms/abc11?include_future_terms=true")
262267
assert response.status_code == 200
263-
resJson = response.json()
264-
assert resJson[1]["position"] == OfficerPositionEnum.TREASURER
265-
assert resJson[1]["start_date"] == (date.today() - timedelta(days=365)).isoformat()
266-
assert resJson[1]["end_date"] == (date.today() - timedelta(days=1)).isoformat()
267-
assert resJson[1]["nickname"] != "1"
268-
assert resJson[1]["favourite_course_0"] != "2"
269-
assert resJson[1]["favourite_course_1"] != "3"
270-
assert resJson[1]["favourite_pl_0"] != "4"
271-
assert resJson[1]["favourite_pl_1"] != "5"
272-
assert resJson[1]["biography"] == "hello o77"
273-
274-
async with database_setup.session() as db_session:
275-
all_terms = await all_officers(db_session, include_future_terms=True)
276-
assert len(all_terms) == 10
268+
modifiedTerm = next((item for item in response.json() if item["id"] == target_id), None)
269+
print(modifiedTerm)
270+
assert modifiedTerm is not None
271+
assert modifiedTerm["position"] == OfficerPositionEnum.TREASURER
272+
assert modifiedTerm["start_date"] == (date.today() - timedelta(days=365)).isoformat()
273+
assert modifiedTerm["end_date"] == (date.today() - timedelta(days=1)).isoformat()
274+
assert modifiedTerm["nickname"] == "1"
275+
assert modifiedTerm["favourite_course_0"] == "2"
276+
assert modifiedTerm["favourite_course_1"] == "3"
277+
assert modifiedTerm["favourite_pl_0"] == "4"
278+
assert modifiedTerm["favourite_pl_1"] == "5"
279+
assert modifiedTerm["biography"] == "hello o77"
280+
281+
# other one shouldn't be modified
282+
assert response.status_code == 200
283+
modifiedTerm = next((item for item in response.json() if item["id"] == target_id + 1), None)
284+
print(modifiedTerm)
285+
assert modifiedTerm is not None
286+
assert modifiedTerm["position"] == OfficerPositionEnum.EXECUTIVE_AT_LARGE
287+
assert modifiedTerm["start_date"] != (date.today() - timedelta(days=365)).isoformat()
288+
assert modifiedTerm["end_date"] != (date.today() - timedelta(days=1)).isoformat()
289+
assert modifiedTerm["nickname"] != "1"
290+
assert modifiedTerm["favourite_course_0"] != "2"
291+
assert modifiedTerm["favourite_course_1"] != "3"
292+
assert modifiedTerm["favourite_pl_0"] != "4"
293+
assert modifiedTerm["favourite_pl_1"] != "5"
294+
assert modifiedTerm["biography"] != "hello o77"
295+
296+
response = await admin_client.get("officers/all?include_future_terms=True")
297+
assert len(response.json()) == 10
277298

278299
response = await admin_client.delete("officers/term/1")
279300
assert response.status_code == 200
@@ -284,6 +305,5 @@ async def test__get_officer_term(admin_client):
284305
response = await admin_client.delete("officers/term/4")
285306
assert response.status_code == 200
286307

287-
async with database_setup.session() as db_session:
288-
all_terms = await all_officers(db_session, include_private_data=True, include_future_terms=True)
289-
assert len(all_terms) == (8 - 4)
308+
response = await admin_client.get("officers/all?include_future_terms=True")
309+
assert len(response.json()) == 6

0 commit comments

Comments
 (0)