Skip to content

Commit f103152

Browse files
committed
Add pytest config; tweak naming schemes.
1 parent 57a99d8 commit f103152

10 files changed

Lines changed: 206 additions & 197 deletions

File tree

aiohttp_aiofiles_tutorial/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from config import EXPORT_FILE, HTML_HEADERS
1010
from logger import LOGGER
1111

12-
from .data import urls
12+
from .data import urls_to_fetch
1313
from .tasks import create_tasks
1414

1515

@@ -30,5 +30,5 @@ async def execute_fetcher_tasks(outfile: AsyncIOFile):
3030
:param AsyncIOFile outfile: Path of local file to write to.
3131
"""
3232
async with ClientSession(headers=HTML_HEADERS) as session:
33-
task_list = await create_tasks(session, urls, outfile)
33+
task_list = await create_tasks(session, urls_to_fetch, outfile)
3434
await asyncio.gather(*task_list)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Parse data from local files."""
22
from config import CSV_FILEPATH
33

4-
from .parser import parse_urls_from_csv
4+
from .urls import parse_urls_from_csv
55

6-
urls = parse_urls_from_csv(CSV_FILEPATH)
6+
urls_to_fetch = parse_urls_from_csv(CSV_FILEPATH)
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""Test reading tutorial sample data URLs."""
2-
from aiohttp_aiofiles_tutorial.data.parser import parse_urls_from_csv
32
from config import CSV_FILEPATH
43

4+
from aiohttp_aiofiles_tutorial.data.urls import parse_urls_from_csv
5+
56

67
def test_parse_urls():
78
"""Ensure URL sample data is parsed correctly."""
89

9-
urls = parse_urls_from_csv(CSV_FILEPATH)
10+
urls_to_fetch = parse_urls_from_csv(CSV_FILEPATH)
1011

11-
assert (type(urls)) == list
12-
assert "https://" in urls[0]
12+
assert (type(urls_to_fetch)) == list
13+
assert "https://" in urls_to_fetch[0]

aiohttp_aiofiles_tutorial/data/urls.csv

Lines changed: 174 additions & 173 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def parse_urls_from_csv(filepath: str) -> List[str]:
1313
"""
1414
urls = []
1515
with open(filepath, newline="") as f:
16-
reader = csv.reader(f)
16+
reader = csv.DictReader(f)
1717
for line in reader:
18-
urls.append(line[0])
18+
urls.append(line["url"])
1919

2020
return urls

aiohttp_aiofiles_tutorial/parser.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ async def parse_html_page_data(html: str, url: str) -> str:
2323
.replace("'", "`")
2424
)
2525
primary_tag = soup.head.select_one("meta[property='article:tag']").get("content")
26-
published_at = soup.head.select_one("meta[property='article:published_time']").get(
27-
"content"
28-
).split("T")[0]
26+
published_at = (
27+
soup.head.select_one("meta[property='article:published_time']")
28+
.get("content")
29+
.split("T")[0]
30+
)
2931
if primary_tag is None:
3032
primary_tag = ""
3133
return f"{title}, {description}, {primary_tag}, {url}, {published_at}"
3234
except ValueError as e:
33-
LOGGER.error(f"Parsing failed for {url}: {e}")
35+
LOGGER.error(f"ValueError occurred when parsing html for {url}: {e}")
3436
except Exception as e:
35-
LOGGER.error(f"Parsing failed for {url}: {e}")
37+
LOGGER.error(f"Parsing failed when parsing html for {url}: {e}")

aiohttp_aiofiles_tutorial/tests/test_parser.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Test metadata parser accuracy with local HTML file."""
22
import aiofiles
33
import pytest
4-
from aiohttp_aiofiles_tutorial.parser import parse_html_page_data
4+
import pytest_asyncio
55
from config import BASE_DIR
66

7+
from aiohttp_aiofiles_tutorial.parser import parse_html_page_data
8+
79

8-
@pytest.fixture
10+
@pytest_asyncio.fixture
911
async def sample_page_metadata():
1012
"""Expected metadata to be returned from parsing `intro_to_asyncio.html`"""
1113
title = "Intro to Asynchronous Python with Asyncio"
@@ -25,3 +27,4 @@ async def test_parse_html_page_data(sample_page_metadata):
2527
url = "https://hackersandslackers.com/intro-to-asyncio-concurrency/"
2628
metadata = await parse_html_page_data(html, url)
2729
assert metadata == sample_page_metadata
30+
await file.close()

poetry.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ profile = "black"
5252
src_paths = ["aiohttp_aiofiles_tutorial"]
5353

5454
[tool.black]
55-
line-length = 100
55+
line-length = 100

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
asyncio_mode = auto

0 commit comments

Comments
 (0)