Skip to content

Commit

Permalink
Rename util method, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elacuesta committed Aug 24, 2023
1 parent d97c6f3 commit 01649ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
7 changes: 3 additions & 4 deletions scrapy_playwright/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions scrapy_playwright/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
_get_page_content,
_is_safe_close_error,
_maybe_await,
_read_float_setting,
_get_float_setting,
)


Expand Down Expand Up @@ -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"
)

Expand Down
46 changes: 36 additions & 10 deletions tests/tests_asyncio/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import logging
from decimal import Decimal
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock

import pytest
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,
Expand Down Expand Up @@ -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):
Expand All @@ -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

0 comments on commit 01649ce

Please sign in to comment.