11import logging
2+ from decimal import Decimal
23from unittest import IsolatedAsyncioTestCase
34from unittest .mock import AsyncMock
45
56import pytest
67from playwright .async_api import Error as PlaywrightError
78from scrapy import Spider
89from scrapy .http .headers import Headers
10+ from scrapy .settings import Settings
911from scrapy_playwright ._utils import (
1012 _NAVIGATION_ERROR_MSG ,
1113 _encode_body ,
14+ _get_float_setting ,
1215 _get_header_value ,
1316 _get_page_content ,
1417 _maybe_await ,
@@ -129,20 +132,19 @@ async def test_encode_mismatch(self):
129132
130133class TestHeaderValue (IsolatedAsyncioTestCase ):
131134 @pytest .mark .asyncio
132- async def test_get_header_ok (self ):
135+ async def test_get_header_value (self ):
133136 async def _identity (x ):
134137 return x
135138
136- resource = AsyncMock ()
137- resource .header_value = _identity
138- assert "asdf" == await _get_header_value (resource , "asdf" )
139- assert "qwerty" == await _get_header_value (resource , "qwerty" )
139+ res1 = AsyncMock ()
140+ res1 .header_value = _identity
141+ assert "asdf" == await _get_header_value (res1 , "asdf" )
142+ assert "qwerty" == await _get_header_value (res1 , "qwerty" )
140143
141- async def test_get_header_exception (self ):
142- resource = AsyncMock ()
143- resource .header_value .side_effect = Exception ("nope" )
144- assert await _get_header_value (resource , "asdf" ) is None
145- assert await _get_header_value (resource , "qwerty" ) is None
144+ res2 = AsyncMock ()
145+ res2 .header_value .side_effect = Exception ("nope" )
146+ assert await _get_header_value (res2 , "asdf" ) is None
147+ assert await _get_header_value (res2 , "qwerty" ) is None
146148
147149
148150class TestMaybeAwait (IsolatedAsyncioTestCase ):
@@ -157,3 +159,27 @@ async def _awaitable_identity(x):
157159 assert await _maybe_await ("foo" ) == "foo"
158160 assert await _maybe_await ("bar" ) == "bar"
159161 assert await _maybe_await (1234 ) == 1234
162+
163+
164+ class TestGetFloatSetting (IsolatedAsyncioTestCase ):
165+ @pytest .mark .asyncio
166+ async def test_get_float_setting (self ):
167+ settings = Settings (
168+ {
169+ "FLOAT" : 1.5 ,
170+ "DECIMAL" : Decimal ("2.5" ),
171+ "INT" : 3 ,
172+ "NUMERIC_STRING" : "123" ,
173+ "NON_NUMERIC_STRING" : "asdf" ,
174+ "NONE" : None ,
175+ "LIST" : [1 , 2 , 3 ],
176+ }
177+ )
178+ assert _get_float_setting (settings , "FLOAT" ) == 1.5
179+ assert _get_float_setting (settings , "DECIMAL" ) == 2.5
180+ assert _get_float_setting (settings , "INT" ) == 3.0
181+ assert _get_float_setting (settings , "NUMERIC_STRING" ) == 123
182+ assert _get_float_setting (settings , "NON_NUMERIC_STRING" ) is None
183+ assert _get_float_setting (settings , "NONE" ) is None
184+ assert _get_float_setting (settings , "LIST" ) is None
185+ assert _get_float_setting (settings , "MISSING_KEY" ) is None
0 commit comments