2012-06-20 20:41:04 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
|
|
|
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
|
2013-02-10 17:25:21 +00:00
|
|
|
# Copyright (C) 2012-2013 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-11-26 17:44:31 +00:00
|
|
|
import os
|
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-11-26 17:44:31 +00:00
|
|
|
# create a temporary directory for images
|
|
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.tmpdir)
|
|
|
|
|
|
|
|
def get_saved_images(self):
|
|
|
|
"""Get saved images."""
|
|
|
|
dirs = tuple(self.name.split('/'))
|
|
|
|
return os.listdir(os.path.join(self.tmpdir, *dirs))
|
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-12-04 06:02:40 +00:00
|
|
|
num = 0
|
2012-11-26 17:44:31 +00:00
|
|
|
max_strips = 5
|
|
|
|
for strip in islice(scraperobj.getAllStrips(), 0, max_strips):
|
2012-12-07 23:45:18 +00:00
|
|
|
images = []
|
2012-10-11 13:17:01 +00:00
|
|
|
for image in strip.getImages():
|
2012-12-07 23:45:18 +00:00
|
|
|
images.append(image.url)
|
2012-10-11 12:40:54 +00:00
|
|
|
self.save(image)
|
2012-12-07 23:45:18 +00:00
|
|
|
self.check(images, 'failed to find images at %s' % strip.stripUrl)
|
2012-12-04 06:02:40 +00:00
|
|
|
if not self.scraperclass.multipleImagesPerStrip:
|
2012-12-07 23:45:18 +00:00
|
|
|
self.check(len(images) == 1, 'found more than 1 image at %s: %s' % (strip.stripUrl, images))
|
2012-12-05 20:52:52 +00:00
|
|
|
if num > 0 and self.scraperclass.prevUrlMatchesStripUrl:
|
2012-11-21 20:57:26 +00:00
|
|
|
self.check_stripurl(strip)
|
2012-06-20 19:58:13 +00:00
|
|
|
num += 1
|
2012-11-21 20:57:26 +00:00
|
|
|
if self.scraperclass.prevSearch:
|
2012-12-04 06:02:40 +00:00
|
|
|
self.check(num >= 4, 'traversal failed after %d strips, check the prevSearch pattern at %s.' % (num, strip.stripUrl))
|
|
|
|
# Check that exactly or for multiple pages at least 5 images are saved.
|
|
|
|
# This is different than the image number check above since it checks saved files,
|
|
|
|
# ie. it detects duplicate filenames.
|
2012-11-26 17:44:31 +00:00
|
|
|
saved_images = self.get_saved_images()
|
|
|
|
num_images = len(saved_images)
|
2012-12-04 06:02:40 +00:00
|
|
|
attrs = (num_images, saved_images, max_strips, self.tmpdir)
|
2012-11-26 17:44:31 +00:00
|
|
|
if self.scraperclass.multipleImagesPerStrip:
|
2012-12-04 06:02:40 +00:00
|
|
|
self.check(num_images >= max_strips, 'saved %d %s instead of at least %d images in %s' % attrs)
|
2012-11-26 17:44:31 +00:00
|
|
|
else:
|
2012-12-04 06:02:40 +00:00
|
|
|
self.check(num_images == max_strips, 'saved %d %s instead of %d images in %s' % attrs)
|
2012-06-20 19:58:13 +00:00
|
|
|
|
2012-11-21 20:57:26 +00:00
|
|
|
def check_stripurl(self, strip):
|
|
|
|
if not self.scraperclass.stripUrl:
|
|
|
|
# no indexing support
|
|
|
|
return
|
|
|
|
# test that the stripUrl regex matches the retrieved strip URL
|
|
|
|
urlmatch = re.escape(self.scraperclass.stripUrl)
|
|
|
|
urlmatch = urlmatch.replace(r"\%s", r".+")
|
|
|
|
urlmatch = "^%s$" % urlmatch
|
|
|
|
ro = re.compile(urlmatch)
|
|
|
|
mo = ro.search(strip.stripUrl)
|
|
|
|
self.check(mo is not None, 'strip URL %r does not match stripUrl pattern %s' % (strip.stripUrl, urlmatch))
|
|
|
|
|
2012-10-11 12:40:54 +00:00
|
|
|
def save(self, image):
|
2012-06-20 19:58:13 +00:00
|
|
|
try:
|
2012-11-26 17:44:31 +00:00
|
|
|
image.save(self.tmpdir)
|
2012-11-20 17:53:53 +00:00
|
|
|
except Exception as msg:
|
2012-11-26 17:44:31 +00:00
|
|
|
self.check(False, 'could not save %s to %s: %s' % (image.url, self.tmpdir, msg))
|
2012-06-20 19:58:13 +00:00
|
|
|
|
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
|
|
|
|
2012-11-26 17:44:31 +00:00
|
|
|
def make_comic_tester(name, **kwargs):
|
|
|
|
"""Create and return a _ComicTester class with given name and attributes."""
|
|
|
|
return type(name, (_ComicTester,), kwargs)
|
|
|
|
|
|
|
|
|
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."""
|
2012-11-26 17:44:31 +00:00
|
|
|
g = globals()
|
2012-12-12 16:41:29 +00:00
|
|
|
# optional: limit number of scraper tests
|
|
|
|
max_scrapers = 0
|
|
|
|
if max_scrapers:
|
|
|
|
scrapers = islice(scraper.get_scrapers(), 0, max_scrapers)
|
|
|
|
else:
|
|
|
|
scrapers = scraper.get_scrapers()
|
|
|
|
for scraperclass in scrapers:
|
2012-10-11 12:40:54 +00:00
|
|
|
name = 'Test'+scraperclass.__name__
|
2012-11-26 17:44:31 +00:00
|
|
|
g[name] = make_comic_tester(name, scraperclass=scraperclass)
|
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
generate_comic_testers()
|