dosage/tests/test_comics.py

68 lines
2.3 KiB
Python
Raw Normal View History

# -*- coding: iso-8859-1 -*-
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012 Bastian Kleineidam
2012-06-20 19:58:13 +00:00
import tempfile
import shutil
2012-11-13 18:12:28 +00:00
import re
2012-10-11 12:55:54 +00:00
from itertools import islice
2012-06-20 19:58:13 +00:00
from unittest import TestCase
from dosagelib import scraper
class _ComicTester(TestCase):
"""Basic comic test class."""
scraperclass=None
2012-10-11 12:40:54 +00:00
def setUp(self):
self.name = self.scraperclass.get_name()
2012-10-11 18:19:10 +00:00
self.url = self.scraperclass.starter()
2012-10-11 12:40:54 +00:00
2012-06-20 19:58:13 +00:00
def test_comic(self):
# Test a scraper. It must be able to traverse backward for
2012-11-13 18:12:28 +00:00
# at least 5 strips from the start, and find strip images
2012-06-20 19:58:13 +00:00
# on at least 4 pages.
2012-10-11 12:40:54 +00:00
scraperobj = self.scraperclass()
2012-06-20 19:58:13 +00:00
num = empty = 0
2012-10-11 12:55:54 +00:00
for strip in islice(scraperobj.getAllStrips(), 0, 5):
2012-10-11 13:17:01 +00:00
images = 0
for image in strip.getImages():
images += 1
2012-10-11 12:40:54 +00:00
self.save(image)
2012-11-13 18:12:28 +00:00
if images:
# test that the stripUrl regex matches the retrieved strip URL
urlmatch = re.escape(self.scraperclass.stripUrl).replace("%s", r".+")
mo = re.compile(urlmatch).match(strip.stripUrl)
self.check(mo is not None, 'strip URL %r does not match %r' % (strip.stripUrl, self.scraperclass.stripUrl))
else:
2012-10-11 13:17:01 +00:00
empty += 1
2012-06-20 19:58:13 +00:00
num += 1
2012-10-11 12:40:54 +00:00
self.check(num >= 4, 'traversal failed after %d strips.' % num)
self.check(empty <= 1, 'failed to find images on %d pages.' % empty)
2012-06-20 19:58:13 +00:00
2012-10-11 12:40:54 +00:00
def save(self, image):
2012-06-20 19:58:13 +00:00
# create a temporary directory
tmpdir = tempfile.mkdtemp()
try:
2012-10-11 12:40:54 +00:00
image.save(tmpdir)
except Exception, msg:
self.check(False, 'could not save to %s: %s' % (tmpdir, msg))
2012-06-20 19:58:13 +00:00
finally:
shutil.rmtree(tmpdir)
2012-10-11 12:40:54 +00:00
def check(self, condition, msg):
2012-10-11 18:19:10 +00:00
self.assertTrue(condition, "%s %s %s" % (self.name, self.url, msg))
2012-10-11 12:40:54 +00:00
2012-06-20 19:58:13 +00:00
def generate_comic_testers():
2012-10-11 12:55:54 +00:00
"""For each comic scraper, create a test class."""
# Limit number of scraper tests for now
2012-10-11 16:18:32 +00:00
max_scrapers = 100
2012-10-11 12:55:54 +00:00
for scraperclass in islice(scraper.get_scrapers(), 0, max_scrapers):
2012-10-11 12:40:54 +00:00
name = 'Test'+scraperclass.__name__
2012-06-20 19:58:13 +00:00
globals()[name] = type(name,
(_ComicTester,),
2012-10-11 12:40:54 +00:00
dict(scraperclass=scraperclass)
2012-06-20 19:58:13 +00:00
)
generate_comic_testers()