From 9be5ea78229b8010cacb334f79e207241c27c9d5 Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Sun, 13 Aug 2023 16:55:03 +0200 Subject: [PATCH] pass optional httpx AsyncClient in init (#9) --- src/pywaze/route_calculator.py | 5 ++++- tests/test_route_calculator.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/pywaze/route_calculator.py b/src/pywaze/route_calculator.py index 87ef3a3..f66a41d 100644 --- a/src/pywaze/route_calculator.py +++ b/src/pywaze/route_calculator.py @@ -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.""" diff --git a/tests/test_route_calculator.py b/tests/test_route_calculator.py index f2f2751..d3cc18a 100644 --- a/tests/test_route_calculator.py +++ b/tests/test_route_calculator.py @@ -1,4 +1,5 @@ """Tests for route_calculator module.""" +import httpx import pytest from pywaze import route_calculator from tests.const import ( @@ -51,6 +52,7 @@ async def test_calc_route_info( "start", "end", "get_route_response", + "httpx_client", "expected_route_time", "expected_route_distance", ), @@ -59,6 +61,7 @@ async def test_calc_route_info( "50.00332659227126,8.262322651915843", "50.08414976707619,8.247836017342934", GET_ROUTE_RESPONSE_COORDS, + None, 18.4, 12.715, ), @@ -66,6 +69,7 @@ async def test_calc_route_info( "Kaiserstraße 30 55116 Mainz, Germany", "Luisenstraße 30 65185 Wiesbaden, Germany", GET_ROUTE_RESPONSE_ADDRESSES, + httpx.AsyncClient(), 18.183333333333334, 12.644, ), @@ -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