From 4f42625f15ada2e163c782944aafb41c07bfaf6d Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 6 Jun 2019 05:11:47 +0200 Subject: [PATCH] Tests: use pytest-asyncio to run unit tests --- pyfavicon/__init__.py | 7 +++- setup.py | 3 +- tests/test_base64_favicon.py | 66 ++++++++++++++++------------- tests/test_download_favicon.py | 32 +++++--------- tests/test_favicon_size.py | 26 +++++------- tests/test_favicon_url.py | 24 ++++------- tests/test_from_html.py | 77 ++++++++++++++++------------------ 7 files changed, 108 insertions(+), 127 deletions(-) diff --git a/pyfavicon/__init__.py b/pyfavicon/__init__.py index 88b9f0d..edeb3eb 100644 --- a/pyfavicon/__init__.py +++ b/pyfavicon/__init__.py @@ -123,7 +123,7 @@ def size(self) -> (int, int): return self._size @property - def path(self) -> str: + def path(self) -> pathlib.Path: return self._path @property @@ -206,7 +206,7 @@ def _generate_icon_name(self, website_url: yarl.URL) -> str: if Favicon.DOWNLOAD_DIR: self._path = Favicon.DOWNLOAD_DIR.joinpath(image_name) else: - self._path = os.path.join(gettempdir(), image_name) + self._path = pathlib.Path(gettempdir()).joinpath(image_name) class Icons: @@ -241,6 +241,9 @@ def append(self, icon: Icon): def __iter__(self): return self + + def __len__(self): + return len(self._data) def __next__(self): if self._current >= len(self._data): diff --git a/setup.py b/setup.py index efac19a..f120610 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,8 @@ tests_require=[ 'pytest', 'coveralls', - 'pytest-cov' + 'pytest-cov', + 'pytest-asyncio' ], test_suite='tests', ) diff --git a/tests/test_base64_favicon.py b/tests/test_base64_favicon.py index 4056d50..1566831 100644 --- a/tests/test_base64_favicon.py +++ b/tests/test_base64_favicon.py @@ -1,43 +1,49 @@ -import unittest -import asyncio from pyfavicon import Favicon, FaviconType from pathlib import Path import filecmp import tempfile +import pytest -class HTMLTest(unittest.TestCase): +favicon = Favicon(download_dir=Path(tempfile.gettempdir())) +html_file = Path('./tests/html/base64_favicon_link.html') - def setUp(self): - self.favicon = Favicon(download_dir=Path(tempfile.gettempdir())) - def test_url_icon_link_type(self): - files = [ - Path('./tests/html/base64_favicon_link.html'), - ] +@pytest.mark.asyncio +async def test_base64_type(): + favicons = await favicon.from_file(html_file) + icon = favicons[0] + assert icon.type is FaviconType.DATA - async def run_test(): - for html_file in files: - favicons = await self.favicon.from_file(html_file) - icon = favicons[0] - self.assertEqual(icon.type, FaviconType.DATA) - # Ensure that save works correctly - self.assertTupleEqual(icon.size, (16, 16)) - self.assertEqual(icon.extension, 'ico') - await icon.save() - self.assertTrue(icon.path.exists()) - # Compare file content - temp_file = Path(tempfile.NamedTemporaryFile().name) - with temp_file.open('wb') as fd: - fd.write(icon.data) - self.assertTrue(filecmp.cmp(temp_file, icon.path)) +@pytest.mark.asyncio +async def test_base64_size(): + favicons = await favicon.from_file(html_file) + icon = favicons[0] + + assert icon.size == (16, 16) - # Remove the test file - temp_file.unlink() - icon.path.unlink() +@pytest.mark.asyncio +async def test_base64_extension(): + favicons = await favicon.from_file(html_file) + icon = favicons[0] + + assert icon.extension == 'ico' - asyncio.run(run_test()) +@pytest.mark.asyncio +async def test_base64_save(): + favicons = await favicon.from_file(html_file) + icon = favicons[0] + await icon.save() + assert icon.path.exists() + + # Compare file content + temp_file = Path(tempfile.NamedTemporaryFile().name) + with temp_file.open('wb') as fd: + fd.write(icon.data) + assert filecmp.cmp(temp_file, icon.path) + + # Remove the test file + temp_file.unlink() + icon.path.unlink() -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_download_favicon.py b/tests/test_download_favicon.py index 417a5ed..21e54d4 100644 --- a/tests/test_download_favicon.py +++ b/tests/test_download_favicon.py @@ -1,28 +1,16 @@ -import unittest -import asyncio from pyfavicon import Favicon from pathlib import Path import tempfile -import yarl +import pytest +favicon = Favicon(download_dir=Path(tempfile.gettempdir())) -class HTMLTest(unittest.TestCase): - def setUp(self): - self.favicon = Favicon(download_dir=Path(tempfile.gettempdir())) - - def test_url_icon_link_type(self): - - async def run_test(): - icons = await self.favicon.from_url(yarl.URL('https://gitlab.com')) - icon = icons[0] - # Ensure that save works correctly - await icon.save() - self.assertTrue(icon.path.exists()) - # Remove the test file - icon.path.unlink() - asyncio.run(run_test()) - - -if __name__ == '__main__': - unittest.main() +@pytest.mark.asyncio +async def test_icon_download(): + icons = await favicon.from_url('https://gitlab.com') + for icon in icons: + await icon.save() + assert icon.path.exists() + + icon.path.unlink() diff --git a/tests/test_favicon_size.py b/tests/test_favicon_size.py index 6a0d136..b414e8c 100644 --- a/tests/test_favicon_size.py +++ b/tests/test_favicon_size.py @@ -1,7 +1,5 @@ -import unittest -import asyncio from pyfavicon import Favicon - +import pytest GITLAB_FAVICONS = { 'https://about.gitlab.com/ico/favicon.ico': (-1, -1), @@ -22,19 +20,17 @@ 'https://about.gitlab.com/ico/mstile-144x144.png': (144, 144) } +favicon = Favicon() -class HTMLTest(unittest.TestCase): - def setUp(self): - self.favicon = Favicon() +@pytest.mark.asyncio +async def test_icon_size(): + icons = await favicon.from_url('https://gitlab.com') + assert len(icons) != 0 - def test_icon_sizes(self): - async def run_test(): - icons = await self.favicon.from_url('https://gitlab.com') - for icon in icons: - self.assertEqual(GITLAB_FAVICONS[str(icon.link)], icon.size) + for icon in icons: + assert GITLAB_FAVICONS[str(icon.link)] == icon.size - largest = icons.get_largest(extension='png') - self.assertEqual(largest.size, (190, 175)) - self.assertEqual(largest.extension, 'png') - asyncio.run(run_test()) + largest = icons.get_largest(extension='png') + assert largest.size == (190, 175) + assert largest.extension == 'png' diff --git a/tests/test_favicon_url.py b/tests/test_favicon_url.py index a1c9c4b..3d08f96 100644 --- a/tests/test_favicon_url.py +++ b/tests/test_favicon_url.py @@ -1,6 +1,5 @@ -import unittest -import asyncio from pyfavicon import Favicon +import pytest CASES = [ ('', 'https://gitlab.com/favicon.ico'), @@ -14,20 +13,15 @@ ('', 'https://gitlab.com/favicon.ico') ] +favicon = Favicon() -class TestFaviconUrl(unittest.TestCase): - def setUp(self): - self.favicon = Favicon() - def test_favicon_url(self): - async def run_tests(): - favicon = Favicon() - for html_content, expected_result in CASES: - icons = await favicon.from_html(html_content, - "https://gitlab.com") - self.assertEqual(str(icons[0].link), expected_result) - asyncio.run(run_tests()) +@pytest.mark.asyncio +async def test_favicon_url(): + for html_content, expected_result in CASES: + icons = await favicon.from_html(html_content, + "https://gitlab.com") + assert len(icons) != 0 + assert str(icons[0].link) == expected_result -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_from_html.py b/tests/test_from_html.py index bda8f2e..0c68d4b 100644 --- a/tests/test_from_html.py +++ b/tests/test_from_html.py @@ -1,58 +1,51 @@ -import unittest -import asyncio from pyfavicon import Favicon, FaviconType from pathlib import Path +import pytest +favicon = Favicon() -class HTMLTest(unittest.TestCase): - def setUp(self): - self.favicon = Favicon() +@pytest.mark.asyncio +async def test_link_tag(): + files = [ + Path('./tests/html/url_icon_link.html'), + Path('./tests/html/url_shortcut_icon_link.html'), + Path('./tests/html/url_apple_touch_icon_precomposed_link.html'), + Path('./tests/html/url_apple_touch_icon_link.html'), + Path('./tests/html/url_fluid_icon_link.html'), + ] - def test_url_icon_link_type(self): - files = [ - Path('./tests/html/url_icon_link.html'), - Path('./tests/html/url_shortcut_icon_link.html'), - Path('./tests/html/url_apple_touch_icon_precomposed_link.html'), - Path('./tests/html/url_apple_touch_icon_link.html'), - Path('./tests/html/url_fluid_icon_link.html'), - ] + for html_file in files: + icons = await favicon.from_file(html_file, + 'https://github.com') + assert len(icons) != 0 + icon = icons[0] + + assert icon.type is FaviconType.URL + assert str(icon.link) == 'https://github.githubassets.com/favicon.ico' - async def run_test(): - for html_file in files: - favicons = await self.favicon.from_file(html_file, - 'https://github.com') - icon = favicons[0] - self.assertEqual(icon.type, FaviconType.URL) - self.assertEqual(str(icon.link), - 'https://github.githubassets.com/favicon.ico') - asyncio.run(run_test()) +@pytest.mark.asyncio +async def test_meta_tag(): + html_file = Path('./tests/html/meta_favicon.html') - def test_meta_link(self): - html_file = Path('./tests/html/meta_favicon.html') + icons = await favicon.from_file(html_file, 'https://gitlab.com') + assert len(icons) != 0 - async def run_test(): - icons = await self.favicon.from_file(html_file, - 'https://gitlab.com') - icon = icons[0] + icon = icons[0] - self.assertEqual(icon.type, FaviconType.URL) - self.assertEqual(str(icon.link), - 'https://assets.gitlab-static.net/assets/msapplication-tile-1196ec67452f618d39cdd85e2e3a542f76574c071051ae7effbfde01710eb17d.png') - asyncio.run(run_test()) + assert icon.type is FaviconType.URL + assert str(icon.link) == 'https://assets.gitlab-static.net/assets/msapplication-tile-1196ec67452f618d39cdd85e2e3a542f76574c071051ae7effbfde01710eb17d.png' - def test_largest_icon(self): - html_file = Path('./tests/html/largest_gitlab.html') - async def run_tests(): - icons = await self.favicon.from_file(html_file) +@pytest.mark.asyncio +async def test_largest_icon(): + html_file = Path('./tests/html/largest_gitlab.html') - largest_icon = icons.get_largest() - self.assertTupleEqual(largest_icon.size, (188, 188)) + icons = await favicon.from_file(html_file) + assert len(icons) != 0 - asyncio.run(run_tests()) + largest_icon = icons.get_largest() + assert largest_icon - -if __name__ == '__main__': - unittest.main() + assert largest_icon.size == (188, 188)