Skip to content

Commit

Permalink
pass optional httpx AsyncClient in init (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger authored Aug 13, 2023
1 parent 8eaf7fa commit 9be5ea7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/pywaze/route_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ class WazeRouteCalculator:
def __init__(
self,
region="EU",
client: httpx.AsyncClient | None = None,
):
self.region = region
self.client = httpx.AsyncClient()
self.client = client
if self.client is None:
self.client = httpx.AsyncClient()

def already_coords(self, address: str) -> bool:
"""Already coordinates or address."""
Expand Down
12 changes: 10 additions & 2 deletions tests/test_route_calculator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for route_calculator module."""
import httpx
import pytest
from pywaze import route_calculator
from tests.const import (
Expand Down Expand Up @@ -51,6 +52,7 @@ async def test_calc_route_info(
"start",
"end",
"get_route_response",
"httpx_client",
"expected_route_time",
"expected_route_distance",
),
Expand All @@ -59,13 +61,15 @@ async def test_calc_route_info(
"50.00332659227126,8.262322651915843",
"50.08414976707619,8.247836017342934",
GET_ROUTE_RESPONSE_COORDS,
None,
18.4,
12.715,
),
(
"Kaiserstraße 30 55116 Mainz, Germany",
"Luisenstraße 30 65185 Wiesbaden, Germany",
GET_ROUTE_RESPONSE_ADDRESSES,
httpx.AsyncClient(),
18.183333333333334,
12.644,
),
Expand All @@ -75,11 +79,15 @@ async def test_calc_route_info(
"get_route_mock", "wiesbaden_to_coords_mock", "mainz_to_coords_mock"
)
async def test_calc_all_routes_info(
start: str, end: str, expected_route_time: float, expected_route_distance: float
start: str,
end: str,
httpx_client: httpx.AsyncClient | None,
expected_route_time: float,
expected_route_distance: float,
):
"""Test calc_all_routes_info."""

async with route_calculator.WazeRouteCalculator() as client:
async with route_calculator.WazeRouteCalculator(client=httpx_client) 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
Expand Down

0 comments on commit 9be5ea7

Please sign in to comment.