Skip to content

Commit

Permalink
fix: support asynchronous context manager protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Aug 7, 2023
1 parent 2e8b387 commit 2875431
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/pywaze/route_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ async def close(self):
"""Close the client."""
await self.client.aclose()

async def __aenter__(self):
"""Support asynchronous context manager protocol."""
return self

async def __aexit__(self, exc_type, exc, tb):
"""Close the client."""
await self.close()
18 changes: 9 additions & 9 deletions tests/test_route_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ async def test_calc_route_info(
):
"""Test calc_route_info."""

rc = route_calculator.WazeRouteCalculator()
route_time, route_distance = await rc.calc_route_info(start, end)
assert route_time == expected_route_time
assert route_distance == expected_route_distance
async with route_calculator.WazeRouteCalculator() as client:
route_time, route_distance = await client.calc_route_info(start, end)
assert route_time == expected_route_time
assert route_distance == expected_route_distance


@pytest.mark.parametrize(
Expand Down Expand Up @@ -79,8 +79,8 @@ async def test_calc_all_routes_info(
):
"""Test calc_all_routes_info."""

rc = route_calculator.WazeRouteCalculator()
results = await rc.calc_all_routes_info(start, end)
route_time, route_distance = list(results.values())[0]
assert route_time == expected_route_time
assert route_distance == expected_route_distance
async with route_calculator.WazeRouteCalculator() as client:
results = await client.calc_all_routes_info(start, end)
route_time, route_distance = list(results.values())[0]
assert route_time == expected_route_time
assert route_distance == expected_route_distance

0 comments on commit 2875431

Please sign in to comment.