Skip to content

Commit 6f3924c

Browse files
committed
add fdiff.remote tests
1 parent b13a0d8 commit 6f3924c

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

tests/test_remote.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import os
2+
import tempfile
3+
4+
import pytest
5+
6+
import aiohttp
7+
8+
from fdiff.remote import _get_filepath_from_url, async_fetch, async_fetch_and_write, create_async_get_request_session_and_run
9+
10+
REMOTE_FONT_1 = "https://github.com/source-foundry/fdiff/raw/master/tests/testfiles/Roboto-Regular.subset1.ttf"
11+
REMOTE_FONT_2 = "https://github.com/source-foundry/fdiff/raw/master/tests/testfiles/Roboto-Regular.subset2.ttf"
12+
13+
URL_200 = "https://httpbin.org/status/200"
14+
URL_404 = "https://httpbin.org/status/404"
15+
16+
17+
def test_get_temp_filepath_from_url():
18+
res = _get_filepath_from_url(REMOTE_FONT_1, os.path.join("test", "path"))
19+
assert res == os.path.join("test", "path", "Roboto-Regular.subset1.ttf")
20+
21+
22+
@pytest.mark.asyncio
23+
async def test_async_fetch_200():
24+
async with aiohttp.ClientSession() as session:
25+
url, status, binary = await async_fetch(session, URL_200)
26+
assert url == URL_200
27+
assert status == 200
28+
assert binary is not None
29+
30+
31+
@pytest.mark.asyncio
32+
async def test_async_fetch_404():
33+
async with aiohttp.ClientSession() as session:
34+
url, status, binary = await async_fetch(session, URL_404)
35+
assert url == URL_404
36+
assert status == 404
37+
assert binary is None
38+
39+
40+
@pytest.mark.asyncio
41+
async def test_async_fetch_and_write_200():
42+
with tempfile.TemporaryDirectory() as tmpdirname:
43+
async with aiohttp.ClientSession() as session:
44+
fwres = await async_fetch_and_write(session, REMOTE_FONT_1, tmpdirname)
45+
assert fwres.url == REMOTE_FONT_1
46+
assert fwres.filepath == _get_filepath_from_url(REMOTE_FONT_1, tmpdirname)
47+
assert fwres.http_status == 200
48+
assert fwres.write_success is True
49+
50+
51+
@pytest.mark.asyncio
52+
async def test_async_fetch_and_write_404():
53+
with tempfile.TemporaryDirectory() as tmpdirname:
54+
async with aiohttp.ClientSession() as session:
55+
fwres = await async_fetch_and_write(session, URL_404, tmpdirname)
56+
assert fwres.url == URL_404
57+
assert fwres.filepath is None
58+
assert fwres.http_status == 404
59+
assert fwres.write_success is False
60+
61+
62+
@pytest.mark.asyncio
63+
async def test_create_async_get_request_session_and_run_200():
64+
urls = [REMOTE_FONT_1, REMOTE_FONT_2]
65+
with tempfile.TemporaryDirectory() as tmpdirname:
66+
tasks = await create_async_get_request_session_and_run(urls, tmpdirname)
67+
for x, task in enumerate(tasks):
68+
assert task.exception() is None
69+
assert task.result().url == urls[x]
70+
assert task.result().http_status == 200
71+
assert os.path.exists(task.result().filepath)
72+
assert task.result().write_success is True
73+
74+
75+
@pytest.mark.asyncio
76+
async def test_create_async_get_request_session_and_run_404_single():
77+
urls = [REMOTE_FONT_1, URL_404]
78+
with tempfile.TemporaryDirectory() as tmpdirname:
79+
tasks = await create_async_get_request_session_and_run(urls, tmpdirname)
80+
for x, task in enumerate(tasks):
81+
if x == 0:
82+
assert task.exception() is None
83+
assert task.result().url == urls[x]
84+
assert task.result().http_status == 200
85+
assert os.path.exists(task.result().filepath)
86+
assert task.result().write_success is True
87+
else:
88+
assert task.exception() is None
89+
assert task.result().url == urls[x]
90+
assert task.result().http_status == 404
91+
assert task.result().filepath is None
92+
assert task.result().write_success is False
93+
94+
95+
@pytest.mark.asyncio
96+
async def test_create_async_get_request_session_and_run_404_both():
97+
urls = [URL_404, URL_404]
98+
with tempfile.TemporaryDirectory() as tmpdirname:
99+
tasks = await create_async_get_request_session_and_run(urls, tmpdirname)
100+
for x, task in enumerate(tasks):
101+
assert task.exception() is None
102+
assert task.result().url == urls[x]
103+
assert task.result().http_status == 404
104+
assert task.result().filepath is None
105+
assert task.result().write_success is False

0 commit comments

Comments
 (0)