added test coverage for image getter

This commit is contained in:
Kevin F 2021-10-11 01:44:23 +00:00
parent 1bfa912241
commit 98ee396ea7
6 changed files with 58 additions and 12 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ venv/*
imgin/__pycache__/* imgin/__pycache__/*
testdata/* testdata/*
.vscode/* .vscode/*
*.egg-info

View File

@ -1,10 +1,11 @@
import sys import sys
from os import remove from os import remove, write
from threading import Thread from threading import Thread
#from gevent import sleep
from time import sleep
import requests import requests
import bs4 import bs4
from gevent import sleep
from .config import IMAGE_CACHE, SINGLE_IMAGE_DELETE_AFTER_SECS from .config import IMAGE_CACHE, SINGLE_IMAGE_DELETE_AFTER_SECS
@ -51,6 +52,8 @@ def get(url: str, write_dir: str, delete=True):
for count, el in enumerate(soup.select('.post-image meta[itemprop="contentUrl"]'), start=1): for count, el in enumerate(soup.select('.post-image meta[itemprop="contentUrl"]'), start=1):
try: try:
found_url = "https:" + el['content'] found_url = "https:" + el['content']
if '?1' in found_url:
continue
except KeyError: except KeyError:
error("Could not obtain url for detected image") error("Could not obtain url for detected image")
continue continue
@ -66,7 +69,7 @@ def get(url: str, write_dir: str, delete=True):
if delete: if delete:
Thread(target=delete_file, args=[f"{write_dir}{found_url[-11:]}"]).start() Thread(target=delete_file, args=[f"{write_dir}{found_url[-11:]}"]).start()
# Write the found urls to a file with the name of the album so the viewer endpoint can get them # Write the found urls to a file with the name of the album so the viewer endpoint can get them
found_list_file = IMAGE_CACHE + orig_url.replace('/', '_') found_list_file = write_dir + orig_url.replace('/', '_')
with open(found_list_file, 'w') as f: with open(found_list_file, 'w') as f:
f.write(','.join(found_urls)) f.write(','.join(found_urls))
Thread(target=delete_file, args=[found_list_file]).start() Thread(target=delete_file, args=[found_list_file]).start()

View File

@ -1,13 +1,14 @@
#!/bin/bash #!/bin/bash
ran=0 ran=0
SECONDS=0 ; SECONDS=0 ;
close () { close () {
exit 10; exit 10;
} }
for f in tests/*.py; do for f in tests/*.py; do
python3 "$f" || close # if needed python3 "$f" || close & # if needed
let "ran++" let "ran++"
done done
echo "ran $ran test files successfully in $SECONDS seconds" echo "ran $ran test files successfully in $SECONDS seconds"
rm -f *.dat rm -f *.dat
rm -rf /tmp/imgin*

View File

@ -1,7 +0,0 @@
import unittest
class TestBasic(unittest.TestCase):
def test_basic(self):
self.assertTrue(True)
unittest.main()

26
tests/test_album.py Normal file
View File

@ -0,0 +1,26 @@
import unittest
import os
from glob import glob
from imgin import get
CACHE_DIR = '/tmp/imgin-imgur-images-album/'
class TestAlbum(unittest.TestCase):
def test_album_a(self):
code = 'ethCwFv'
get(f"https://imgur.com/a/{code}", CACHE_DIR)
files = glob(CACHE_DIR + '*')
for i in files:
if i.endswith('m_a_ethCwFv'):
continue
print(f'got tests/test_images/album/{i[-11:]} checking if it is an image we should have')
self.assertTrue(os.path.exists(f'tests/test_images/album/{i[-11:]}'))
try:
os.mkdir(CACHE_DIR)
except FileExistsError:
pass
unittest.main()

View File

@ -0,0 +1,22 @@
import unittest
import os
from imgin import get
CACHE_DIR = '/tmp/imgin-imgur-images-single/'
class TestSingleImage(unittest.TestCase):
def test_single_image(self):
img = "7TiLluI.jpg"
get(f"https://imgur.com/{img}", CACHE_DIR)
with open(f"tests/test_images/{img}", "rb") as expected:
with open(CACHE_DIR + img, "rb") as actual:
self.assertEqual(actual.read(), expected.read())
try:
os.mkdir(CACHE_DIR)
except FileExistsError:
pass
unittest.main()