Skip to content

Commit e084d6d

Browse files
committed
fix ansync test harness & fix small crud bug
1 parent e79afef commit e084d6d

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/officers/crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async def current_officer_positions(db_session: database.DBSession, computing_id
139139
"""
140140
Returns the list of officer positions a user currently has. [] if not currently an officer.
141141
"""
142-
officer_term_list = get_active_officer_terms(db_session, computing_id)
142+
officer_term_list = await get_active_officer_terms(db_session, computing_id)
143143
return [term.position for term in officer_term_list]
144144

145145
async def get_officer_term_by_id(db_session: database.DBSession, term_id: int) -> OfficerTerm:

tests/integration/test_officers.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio # NOTE: don't comment this out; it's required
22

33
import pytest
4-
from fastapi.testclient import TestClient
4+
from httpx import ASGITransport, AsyncClient
55

66
import load_test_db
77
from auth.crud import create_user_session
@@ -13,6 +13,24 @@
1313
# TODO: setup a database on the CI machine & run this as a unit test then (since
1414
# this isn't really an integration test)
1515

16+
@pytest.fixture(scope="session")
17+
def anyio_backend():
18+
return "asyncio"
19+
20+
@pytest.fixture(scope="session")
21+
def event_loop():
22+
"""Create an instance of the default event loop for each test case"""
23+
loop = asyncio.get_event_loop_policy().new_event_loop()
24+
yield loop
25+
loop.close()
26+
27+
@pytest.fixture(scope="session")
28+
async def client():
29+
# base_url is just a random placeholder url
30+
# ASGITransport is just telling the async client to pass all requests to app
31+
async with AsyncClient(transport=ASGITransport(app), base_url="http://test") as client:
32+
yield client
33+
1634
# run this again for every function
1735
@pytest.fixture(scope="function")
1836
async def database_setup():
@@ -26,6 +44,7 @@ async def database_setup():
2644

2745
return sessionmanager
2846

47+
# TODO: switch to mark.anyio
2948
@pytest.mark.asyncio
3049
async def test__read_execs(database_setup):
3150
sessionmanager = await database_setup
@@ -67,46 +86,56 @@ async def test__read_execs(database_setup):
6786
# # TODO: the second time an update_officer_info call occurs, the user should be updated with info
6887
# pass
6988

70-
@pytest.mark.asyncio
71-
async def test__endpoints(database_setup):
89+
@pytest.mark.anyio
90+
async def test__endpoints(client, database_setup):
7291
# reset & load the test database
73-
_ = await database_setup
74-
client = TestClient(app)
7592

76-
response = client.get("/officers/current")
93+
response = await client.get("/officers/current")
7794
assert response.status_code == 200
7895
assert response.json() != {}
7996
assert len(response.json().values()) == 4
8097
assert not response.json()["executive at large"][0]["private_data"]
8198

99+
response = await client.get("/officers/all?include_future_terms=false")
100+
assert response.status_code == 200
101+
assert response.json() != []
102+
assert len(response.json()) == 6
103+
assert not response.json()[0]["private_data"]
104+
105+
response = await client.get("/officers/all?include_future_terms=true")
106+
assert response.status_code == 401
107+
82108
"""
83-
response = client.get("/officers/all")
109+
response = await client.get("/officers/all")
84110
assert response.status_code == 200
85111
assert response.json() != {}
86112
# TODO: ensure `include_future_terms` works
87113
88114
# TODO: ensure the test database is being used & has access to abc11
89-
response = client.get("/officers/terms/abc11")
115+
response = await client.get("/officers/terms/abc11")
90116
assert response.status_code == 200
91117
assert response.json() != {}
92118
"""
93119

94-
@pytest.mark.asyncio
95-
async def test__endpoints_admin(database_setup):
96-
# reset & load the test database
97-
sessionmanager = await database_setup
98-
client = TestClient(app)
99-
120+
@pytest.mark.anyio
121+
async def test__endpoints_admin(client, database_setup):
100122
# login as website admin
101123
session_id = "temp_id_" + load_test_db.SYSADMIN_COMPUTING_ID
102-
async with sessionmanager.session() as db_session:
124+
async with database_setup.session() as db_session:
103125
await create_user_session(db_session, session_id, load_test_db.SYSADMIN_COMPUTING_ID)
104126

105127
# test that more info is given if logged in & with access to it
106-
response = client.get("/officers/current", cookies={ "session_id": session_id })
128+
response = await client.get("/officers/current", cookies={ "session_id": session_id })
107129
assert response.status_code == 200
108130
assert response.json() != {}
109131
assert len(response.json().values()) == 4
110132
assert response.json()["executive at large"][0]["private_data"]
111133

134+
response = await client.get("/officers/all?include_future_terms=true", cookies={ "session_id": session_id })
135+
assert response.status_code == 200
136+
assert response.json() != []
137+
print(len(response.json()))
138+
assert len(response.json()) == 6 # TODO: we expect larger than 6
139+
assert response.json()[0]["private_data"]["phone_number"] == "1234567890"
140+
112141
# TODO: ensure that all endpoints are tested at least once

0 commit comments

Comments
 (0)