From 01649ce9db229b5bf8106ae45af817400690491f Mon Sep 17 00:00:00 2001 From: Eugenio Lacuesta Date: Thu, 24 Aug 2023 15:36:42 -0300 Subject: [PATCH] Rename util method, add tests --- scrapy_playwright/_utils.py | 7 ++--- scrapy_playwright/handler.py | 6 ++-- tests/tests_asyncio/test_utils.py | 46 ++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/scrapy_playwright/_utils.py b/scrapy_playwright/_utils.py index 66a7396..e668c74 100644 --- a/scrapy_playwright/_utils.py +++ b/scrapy_playwright/_utils.py @@ -82,12 +82,11 @@ async def _get_page_content( raise -def _read_float_setting(settings: Settings, key: str) -> Optional[float]: +def _get_float_setting(settings: Settings, key: str) -> Optional[float]: try: return float(settings[key]) - except (KeyError, TypeError, ValueError): - pass - return None + except: + return None async def _get_header_value( diff --git a/scrapy_playwright/handler.py b/scrapy_playwright/handler.py index 9f99149..9561f96 100644 --- a/scrapy_playwright/handler.py +++ b/scrapy_playwright/handler.py @@ -36,7 +36,7 @@ _get_page_content, _is_safe_close_error, _maybe_await, - _read_float_setting, + _get_float_setting, ) @@ -87,11 +87,11 @@ def __init__(self, crawler: Crawler) -> None: self.context_semaphore = asyncio.Semaphore( value=settings.getint("PLAYWRIGHT_MAX_CONTEXTS") ) - self.close_context_interval: Optional[float] = _read_float_setting( + self.close_context_interval: Optional[float] = _get_float_setting( settings, "PLAYWRIGHT_CLOSE_CONTEXT_INTERVAL" ) - self.default_navigation_timeout: Optional[float] = _read_float_setting( + self.default_navigation_timeout: Optional[float] = _get_float_setting( settings, "PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT" ) diff --git a/tests/tests_asyncio/test_utils.py b/tests/tests_asyncio/test_utils.py index d353517..3e11ab8 100644 --- a/tests/tests_asyncio/test_utils.py +++ b/tests/tests_asyncio/test_utils.py @@ -1,4 +1,5 @@ import logging +from decimal import Decimal from unittest import IsolatedAsyncioTestCase from unittest.mock import AsyncMock @@ -6,9 +7,11 @@ from playwright.async_api import Error as PlaywrightError from scrapy import Spider from scrapy.http.headers import Headers +from scrapy.settings import Settings from scrapy_playwright._utils import ( _NAVIGATION_ERROR_MSG, _encode_body, + _get_float_setting, _get_header_value, _get_page_content, _maybe_await, @@ -129,20 +132,19 @@ async def test_encode_mismatch(self): class TestHeaderValue(IsolatedAsyncioTestCase): @pytest.mark.asyncio - async def test_get_header_ok(self): + async def test_get_header_value(self): async def _identity(x): return x - resource = AsyncMock() - resource.header_value = _identity - assert "asdf" == await _get_header_value(resource, "asdf") - assert "qwerty" == await _get_header_value(resource, "qwerty") + res1 = AsyncMock() + res1.header_value = _identity + assert "asdf" == await _get_header_value(res1, "asdf") + assert "qwerty" == await _get_header_value(res1, "qwerty") - async def test_get_header_exception(self): - resource = AsyncMock() - resource.header_value.side_effect = Exception("nope") - assert await _get_header_value(resource, "asdf") is None - assert await _get_header_value(resource, "qwerty") is None + res2 = AsyncMock() + res2.header_value.side_effect = Exception("nope") + assert await _get_header_value(res2, "asdf") is None + assert await _get_header_value(res2, "qwerty") is None class TestMaybeAwait(IsolatedAsyncioTestCase): @@ -157,3 +159,27 @@ async def _awaitable_identity(x): assert await _maybe_await("foo") == "foo" assert await _maybe_await("bar") == "bar" assert await _maybe_await(1234) == 1234 + + +class TestGetFloatSetting(IsolatedAsyncioTestCase): + @pytest.mark.asyncio + async def test_get_float_setting(self): + settings = Settings( + { + "FLOAT": 1.5, + "DECIMAL": Decimal("2.5"), + "INT": 3, + "NUMERIC_STRING": "123", + "NON_NUMERIC_STRING": "asdf", + "NONE": None, + "LIST": [1, 2, 3], + } + ) + assert _get_float_setting(settings, "FLOAT") == 1.5 + assert _get_float_setting(settings, "DECIMAL") == 2.5 + assert _get_float_setting(settings, "INT") == 3.0 + assert _get_float_setting(settings, "NUMERIC_STRING") == 123 + assert _get_float_setting(settings, "NON_NUMERIC_STRING") is None + assert _get_float_setting(settings, "NONE") is None + assert _get_float_setting(settings, "LIST") is None + assert _get_float_setting(settings, "MISSING_KEY") is None