2
2
from unittest import mock
3
3
4
4
import aiohttp
5
+ from aioresponses import aioresponses
5
6
import asynctest
7
+ from faker import Faker
6
8
from loguru import logger
7
9
import pytest
8
10
from tenacity import RetryError
18
20
# Disable logging for the tests.
19
21
logger .remove ()
20
22
23
+ # Set faker object.
24
+ fake = Faker ()
25
+
21
26
22
27
def load_test_page (page ):
23
28
"""Load a test page."""
@@ -394,10 +399,17 @@ def test_parse_page_content_01(mocker):
394
399
page_fd = TEST_DATA_DIR / 'traffic-fatality-2-3'
395
400
page = page_fd .read_text ()
396
401
mocker .patch ('scrapd.core.apd.parse_deceased_field' , side_effect = ValueError )
397
- apd .parse_page_content (page )
402
+ result = apd .parse_page_content (page )
403
+ assert len (result ) == 6
404
+
398
405
406
+ def test_parse_page_content_02 (mocker ):
407
+ """Ensure a log entry is created if there is no deceased field."""
408
+ result = apd .parse_page_content ('Case: 01-2345678' )
409
+ assert result
399
410
400
- def test_parse_page_content_02 ():
411
+
412
+ def test_parse_page_content_03 ():
401
413
"""Ensure a missing case number raises an exception."""
402
414
with pytest .raises (ValueError ):
403
415
apd .parse_page_content ('The is no case number here.' )
@@ -425,15 +437,8 @@ def test_parse_page_00(filename, expected):
425
437
426
438
427
439
@asynctest .patch ("scrapd.core.apd.fetch_news_page" ,
428
- side_effect = [load_test_page (page ) for page in [
429
- '296' ,
430
- '296?page=1' ,
431
- '296?page=27' ,
432
- ]])
433
- @asynctest .patch (
434
- "scrapd.core.apd.fetch_detail_page" ,
435
- return_value = load_test_page ('traffic-fatality-2-3' ),
436
- )
440
+ side_effect = [load_test_page (page ) for page in ['296' , '296?page=1' , '296?page=27' ]])
441
+ @asynctest .patch ("scrapd.core.apd.fetch_detail_page" , return_value = load_test_page ('traffic-fatality-2-3' ))
437
442
@pytest .mark .asyncio
438
443
async def test_date_filtering_00 (fake_details , fake_news ):
439
444
"""Ensure the date filtering do not fetch unnecessary data."""
@@ -444,22 +449,29 @@ async def test_date_filtering_00(fake_details, fake_news):
444
449
445
450
446
451
@asynctest .patch ("scrapd.core.apd.fetch_news_page" ,
447
- side_effect = [load_test_page (page ) for page in [
448
- '296' ,
449
- '296?page=1' ,
450
- '296?page=27' ,
451
- ]])
452
- @asynctest .patch (
453
- "scrapd.core.apd.fetch_detail_page" ,
454
- return_value = load_test_page ('traffic-fatality-2-3' ),
455
- )
452
+ side_effect = [load_test_page (page ) for page in ['296' , '296?page=1' , '296?page=27' ]])
453
+ @asynctest .patch ("scrapd.core.apd.fetch_detail_page" , return_value = load_test_page ('traffic-fatality-2-3' ))
456
454
@pytest .mark .asyncio
457
455
async def test_date_filtering_01 (fake_details , fake_news ):
458
456
"""Ensure the date filtering do not fetch unnecessary data."""
459
457
data , _ = await apd .async_retrieve (pages = - 5 , from_ = "2019-01-02" , to = "2019-01-03" )
460
458
assert isinstance (data , list )
461
459
462
460
461
+ @asynctest .patch ("scrapd.core.apd.fetch_news_page" ,
462
+ side_effect = [load_test_page (page ) for page in ['296' , '296?page=1' , '296?page=27' ]])
463
+ @asynctest .patch (
464
+ "scrapd.core.apd.fetch_detail_page" ,
465
+ side_effect = [load_test_page (page ) for page in ['traffic-fatality-2-3' ] + ['traffic-fatality-71-2' ] * 14 ])
466
+ @pytest .mark .asyncio
467
+ async def test_date_filtering_02 (fake_details , fake_news ):
468
+ """Ensure the date filtering do not fetch unnecessary data."""
469
+ data , page_count = await apd .async_retrieve (from_ = "2019-01-16" , to = "2019-01-16" )
470
+ assert isinstance (data , list )
471
+ assert len (data ) == 1
472
+ assert page_count == 2
473
+
474
+
463
475
@pytest .mark .asyncio
464
476
async def test_fetch_text_00 ():
465
477
"""Ensure `fetch_text` retries several times."""
@@ -474,6 +486,17 @@ async def test_fetch_text_00():
474
486
assert apd .fetch_text .retry .statistics ['attempt_number' ] > 1
475
487
476
488
489
+ @pytest .mark .asyncio
490
+ async def test_fetch_text_01 ():
491
+ """Ensure fetch_text retrieves some text."""
492
+ url = fake .uri ()
493
+ with aioresponses () as m :
494
+ m .get (url , payload = dict (foo = 'bar' ))
495
+ async with aiohttp .ClientSession () as session :
496
+ text = await apd .fetch_text (session , url )
497
+ assert '{"foo": "bar"}' == text
498
+
499
+
477
500
@asynctest .patch ("scrapd.core.apd.fetch_news_page" , side_effect = ValueError )
478
501
@pytest .mark .asyncio
479
502
async def test_async_retrieve_00 (fake_news ):
@@ -524,3 +547,30 @@ async def test_fetch_and_parse_01(page, mocker):
524
547
with pytest .raises (RetryError ):
525
548
apd .fetch_and_parse .retry .stop = stop_after_attempt (1 )
526
549
await apd .fetch_and_parse (None , 'url' )
550
+
551
+
552
+ @asynctest .patch ("scrapd.core.apd.fetch_text" , return_value = '' )
553
+ @pytest .mark .asyncio
554
+ async def test_fetch_news_page_00 (fetch_text ):
555
+ """Ensure the fetch function is called with the right parameters."""
556
+ page = 2
557
+ params = {'page' : page - 1 }
558
+ async with aiohttp .ClientSession () as session :
559
+ try :
560
+ await apd .fetch_news_page (session , page )
561
+ except Exception :
562
+ pass
563
+ fetch_text .assert_called_once_with (session , apd .APD_URL , params )
564
+
565
+
566
+ @asynctest .patch ("scrapd.core.apd.fetch_text" , return_value = '' )
567
+ @pytest .mark .asyncio
568
+ async def test_fetch_detail_page_00 (fetch_text ):
569
+ """Ensure the fetch function is called with the right parameters."""
570
+ url = fake .uri ()
571
+ async with aiohttp .ClientSession () as session :
572
+ try :
573
+ await apd .fetch_detail_page (session , url )
574
+ except Exception :
575
+ pass
576
+ fetch_text .assert_called_once_with (session , url )
0 commit comments