Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,8 @@ | |
tests_require=[ | ||
'pytest', | ||
'coveralls', | ||
'pytest-cov' | ||
'pytest-cov', | ||
'pytest-asyncio' | ||
], | ||
test_suite='tests', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
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)) | ||
|
||
# Remove the test file | ||
temp_file.unlink() | ||
icon.path.unlink() | ||
@pytest.mark.asyncio | ||
async def test_base64_size(): | ||
favicons = await favicon.from_file(html_file) | ||
icon = favicons[0] | ||
|
||
asyncio.run(run_test()) | ||
assert icon.size == (16, 16) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
@pytest.mark.asyncio | ||
async def test_base64_extension(): | ||
favicons = await favicon.from_file(html_file) | ||
icon = favicons[0] | ||
|
||
assert icon.extension == 'ico' | ||
|
||
|
||
@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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())) | ||
@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() | ||
|
||
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() | ||
icon.path.unlink() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
|
||
async def run_test(): | ||
for html_file in files: | ||
favicons = await self.favicon.from_file(html_file, | ||
'https://github.com') | ||
icon = favicons[0] | ||
assert icon.type is FaviconType.URL | ||
assert str(icon.link) == 'https://github.githubassets.com/favicon.ico' | ||
|
||
self.assertEqual(icon.type, FaviconType.URL) | ||
self.assertEqual(str(icon.link), | ||
'https://github.githubassets.com/favicon.ico') | ||
asyncio.run(run_test()) | ||
|
||
def test_meta_link(self): | ||
html_file = Path('./tests/html/meta_favicon.html') | ||
@pytest.mark.asyncio | ||
async def test_meta_tag(): | ||
html_file = Path('./tests/html/meta_favicon.html') | ||
|
||
async def run_test(): | ||
icons = await self.favicon.from_file(html_file, | ||
'https://gitlab.com') | ||
icon = icons[0] | ||
icons = await favicon.from_file(html_file, 'https://gitlab.com') | ||
assert len(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()) | ||
icon = icons[0] | ||
|
||
def test_largest_icon(self): | ||
html_file = Path('./tests/html/largest_gitlab.html') | ||
assert icon.type is FaviconType.URL | ||
assert str(icon.link) == 'https://assets.gitlab-static.net/assets/msapplication-tile-1196ec67452f618d39cdd85e2e3a542f76574c071051ae7effbfde01710eb17d.png' | ||
|
||
async def run_tests(): | ||
icons = await self.favicon.from_file(html_file) | ||
|
||
largest_icon = icons.get_largest() | ||
self.assertTupleEqual(largest_icon.size, (188, 188)) | ||
@pytest.mark.asyncio | ||
async def test_largest_icon(): | ||
html_file = Path('./tests/html/largest_gitlab.html') | ||
|
||
asyncio.run(run_tests()) | ||
icons = await favicon.from_file(html_file) | ||
assert len(icons) != 0 | ||
|
||
largest_icon = icons.get_largest() | ||
assert largest_icon | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
assert largest_icon.size == (188, 188) |