-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_task7.py
More file actions
63 lines (48 loc) · 1.85 KB
/
test_task7.py
File metadata and controls
63 lines (48 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
""" Файл для тестирования """
import asyncio
import sys
import aiohttp
import pytest
from fetcher import read_urls_from_file, parse_args, run
from fetcher import fetch_url
@pytest.mark.asyncio
async def test_fetch_url_success():
"""Сценарий успешного фетчинга"""
sem = asyncio.Semaphore(1)
with open('urls.txt', encoding='utf-8') as f:
url = f.read().split('\n')[-1]
async with aiohttp.ClientSession() as session:
text = await fetch_url(url, session, sem)
assert text is not None
assert 'decoreo.ru' in text
@pytest.mark.asyncio
async def test_fetch_url_fail():
"""Сценарий не успешного фетчинга"""
sem = asyncio.Semaphore(1)
async with aiohttp.ClientSession() as session:
url = "https://some-weird-link/"
text = await fetch_url(url, session, sem)
assert text is None
def test_read_urls_from_file():
"""Тестирует функцию чтения урлов из файла"""
with open('urls.txt', encoding='utf-8') as f:
old_urls = f.read().split('\n')
with open('test_urls.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(old_urls))
urls = read_urls_from_file(f.name)
assert urls == old_urls
def test_parse_args(monkeypatch):
"""Тестирует парсинг аргументов командной строки"""
test_args = ["fetcher.py", "5", "urls.txt"]
monkeypatch.setattr(sys, "argv", test_args)
args = parse_args()
assert args.concurrency == 5
assert args.urlfile == "urls.txt"
@pytest.mark.asyncio
async def test_run(tmp_path):
"""Тестирует функцию run."""
file = tmp_path / "urls.txt"
lurl = "https://geotargetly.com/"
file.write_text(lurl)
urls = read_urls_from_file(str(file))
await run(urls, 1)