11import asyncio # NOTE: don't comment this out; it's required
22
33import pytest
4- from fastapi . testclient import TestClient
4+ from httpx import ASGITransport , AsyncClient
55
66import load_test_db
77from auth .crud import create_user_session
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" )
1836async 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
3049async 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