Skip to content

Commit

Permalink
Init tests io
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathanlauga committed Nov 14, 2021
1 parent 945c2ab commit 84ebb38
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ascii_art/io/scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ def create_header(url: str) -> dict:
-------
dict
header dictionnary
Raises
------
TypeError
url must be a string
ValueError
url must start with 'http'
"""
if not isinstance(url, str):
raise TypeError("url must be a string")
if not (url.startswith("http://") or url.startswith("https://")):
raise ValueError("url must start with 'http'")

header = DEFAULT_HEADER

url_split = url.split("//")
Expand Down Expand Up @@ -68,7 +80,7 @@ def get_data_from_url(url: str) -> requests.Response:

if not isinstance(url, str):
raise TypeError("url must be a string")
if not url.startswith("http"):
if not (url.startswith("http://") or url.startswith("https://")):
raise ValueError("url must start with 'http'")

headers = create_header(url)
Expand Down
5 changes: 5 additions & 0 deletions ascii_art/io/tests/test_img.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Test scrap image from keywords
"""

if __name__ == "__main__":
pass
43 changes: 43 additions & 0 deletions ascii_art/io/tests/test_scrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Tests IO scrap functions
"""
import pytest

from ascii_art.io.scrap import create_header
from ascii_art.io.scrap import get_data_from_url
from ascii_art.io.scrap import get_img_url_list_from_keyword
from ascii_art.io.scrap import scrap_img_on_bing


@pytest.mark.parametrize(
"url, host",
[
("http://test.com", "test.com"),
("http://i.am.an.url.com", "i.am.an.url.com"),
],
)
def test_create_header(url, host):
"""Test the function create header with correct input"""
header = create_header(url)
assert header["Host"] == host


@pytest.mark.parametrize(
"url",
[10, 1.2, ["http://error.com"]],
)
def test_create_header_typeerror(url):
"""Test the function create header with incorrect input"""

with pytest.raises(TypeError):
create_header(url)


@pytest.mark.parametrize(
"url",
["not_an_url", "http:/test.com", "https//42.fr"],
)
def test_create_header_url_http_error(url):
"""Test the function create header with string but not url"""

with pytest.raises(ValueError):
create_header(url)

0 comments on commit 84ebb38

Please sign in to comment.