added test coverage for image getter

pull/3/head
Kevin F 1 year ago
parent 1bfa912241
commit 98ee396ea7
  1. 1
      .gitignore
  2. 9
      imgin/get.py
  3. 5
      run_tests.sh
  4. 7
      tests/test.py
  5. 26
      tests/test_album.py
  6. 22
      tests/test_single_image.py

1
.gitignore vendored

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

@ -1,10 +1,11 @@
import sys
from os import remove
from os import remove, write
from threading import Thread
#from gevent import sleep
from time import sleep
import requests
import bs4
from gevent import sleep
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):
try:
found_url = "https:" + el['content']
if '?1' in found_url:
continue
except KeyError:
error("Could not obtain url for detected image")
continue
@ -66,7 +69,7 @@ def get(url: str, write_dir: str, delete=True):
if delete:
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
found_list_file = IMAGE_CACHE + orig_url.replace('/', '_')
found_list_file = write_dir + orig_url.replace('/', '_')
with open(found_list_file, 'w') as f:
f.write(','.join(found_urls))
Thread(target=delete_file, args=[found_list_file]).start()

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

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

@ -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()

@ -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()
Loading…
Cancel
Save