2012-06-20 20:41:04 +00:00
|
|
|
# -*- 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
|
|
|
|
from itertools import izip
|
|
|
|
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-06-20 19:58:13 +00:00
|
|
|
def test_comic(self):
|
|
|
|
# Test a scraper. It must be able to traverse backward for
|
|
|
|
# at least 5 pages from the start, and find strip images
|
|
|
|
# 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:40:54 +00:00
|
|
|
for n, strip in izip(xrange(5), scraperobj.getAllStrips()):
|
|
|
|
images = strips.getImages()
|
|
|
|
if len(images) == 0:
|
2012-06-20 19:58:13 +00:00
|
|
|
empty += 1
|
2012-10-11 12:40:54 +00:00
|
|
|
for image in images:
|
|
|
|
self.save(image)
|
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):
|
|
|
|
self.assertTrue(condition, "%s: %s" % (self.name, msg))
|
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
def generate_comic_testers():
|
|
|
|
"""For each comic scraper, create a test class.
|
|
|
|
This currently generates over 4000 test classes (one for each comic),
|
|
|
|
so this takes a while."""
|
2012-10-11 12:40:54 +00:00
|
|
|
for scraperclass in scraper.get_scrapers():
|
|
|
|
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()
|