Skip to content

Commit debe5d8

Browse files
fix tests for Python 3.9
Signed-off-by: Achille Roussel <[email protected]>
1 parent cf363d0 commit debe5d8

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

tests/test_client.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,29 @@ def test_can_be_constructed_on_https():
3434
Client(api_url="https://example.com", api_key="foo")
3535

3636

37+
# On Python 3.8/3.9, pytest.mark.asyncio doesn't work with mock.patch.dict,
38+
# so we have to use the old-fashioned way of setting the environment variable
39+
# and then cleaning it up manually.
40+
#
41+
# @mock.patch.dict(os.environ, {"DISPATCH_API_KEY": "0000000000000000"})
3742
@pytest.mark.asyncio
38-
@mock.patch.dict(os.environ, {"DISPATCH_API_KEY": "0000000000000000"})
3943
async def test_api_key_from_env():
40-
async with server() as api:
41-
client = Client(api_url=api.url)
42-
43-
with pytest.raises(
44-
PermissionError,
45-
match=r"Dispatch received an invalid authentication token \(check DISPATCH_API_KEY is correct\)",
46-
) as mc:
47-
await client.dispatch([Call(function="my-function", input=42)])
48-
44+
prev_api_key = os.environ.get("DISPATCH_API_KEY")
45+
try:
46+
os.environ["DISPATCH_API_KEY"] = "0000000000000000"
47+
async with server() as api:
48+
client = Client(api_url=api.url)
49+
50+
with pytest.raises(
51+
PermissionError,
52+
match=r"Dispatch received an invalid authentication token \(check DISPATCH_API_KEY is correct\)",
53+
) as mc:
54+
await client.dispatch([Call(function="my-function", input=42)])
55+
finally:
56+
if prev_api_key is None:
57+
del os.environ["DISPATCH_API_KEY"]
58+
else:
59+
os.environ["DISPATCH_API_KEY"] = prev_api_key
4960

5061
@pytest.mark.asyncio
5162
async def test_api_key_from_arg():

tests/test_fastapi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def dispatch_test_init(self, reg: Registry) -> str:
5757
self.sockets = [sock]
5858
self.uvicorn = uvicorn.Server(config)
5959
self.runner = Runner()
60-
if sys.version_info >= (3, 9):
60+
if sys.version_info >= (3, 10):
6161
self.event = asyncio.Event()
6262
else:
6363
self.event = asyncio.Event(loop=self.runner.get_loop())

tests/test_http.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def dispatch_test_init(self, reg: Registry) -> str:
4646
self.aiohttp = Server(host, port, Dispatch(reg))
4747
self.aioloop.run(self.aiohttp.start())
4848

49-
if sys.version_info >= (3, 9):
49+
if sys.version_info >= (3, 10):
5050
self.aiowait = asyncio.Event()
5151
else:
5252
self.aiowait = asyncio.Event(loop=self.aioloop.get_loop())

0 commit comments

Comments
 (0)