fix exception based tests
This commit is contained in:
@@ -32,6 +32,22 @@ class Job():
|
||||
"""Execute or run the job"""
|
||||
try:
|
||||
for msg in self.extractor:
|
||||
self.dispatch(msg)
|
||||
except exception.AuthenticationError:
|
||||
print("Authentication failed. Please provide a valid "
|
||||
"username/password pair.", file=sys.stderr)
|
||||
except exception.AuthorizationError:
|
||||
print("You do not have permission to access the resource ",
|
||||
"at '", self.url, "'", sep="", file=sys.stderr)
|
||||
except exception.NotFoundError as err:
|
||||
res = str(err) or "resource (gallery/image/user)"
|
||||
print("The ", res, " at '", self.url, "' does not exist",
|
||||
sep="", file=sys.stderr)
|
||||
except exception.StopExtraction:
|
||||
pass
|
||||
|
||||
def dispatch(self, msg):
|
||||
"""Call the appropriate message handler"""
|
||||
if msg[0] == Message.Url and self.pred_url:
|
||||
self.update_kwdict(msg[2])
|
||||
self.handle_url(msg[1], msg[2])
|
||||
@@ -55,18 +71,6 @@ class Job():
|
||||
self.extractor.category, msg[1]
|
||||
)
|
||||
# TODO: support for multiple message versions
|
||||
except exception.AuthenticationError:
|
||||
print("Authentication failed. Please provide a valid "
|
||||
"username/password pair.", file=sys.stderr)
|
||||
except exception.AuthorizationError:
|
||||
print("You do not have permission to access the resource ",
|
||||
"at '", self.url, "'", sep="", file=sys.stderr)
|
||||
except exception.NotFoundError as err:
|
||||
res = str(err) or "resource (gallery/image/user)"
|
||||
print("The ", res, " at '", self.url, "' does not exist",
|
||||
sep="", file=sys.stderr)
|
||||
except exception.StopExtraction:
|
||||
pass
|
||||
|
||||
def handle_url(self, url, kexwords):
|
||||
"""Handle Message.Url"""
|
||||
@@ -192,8 +196,8 @@ class UrlJob(Job):
|
||||
pass
|
||||
|
||||
|
||||
class HashJob(DownloadJob):
|
||||
"""Generate SHA1 hashes for extractor results"""
|
||||
class TestJob(DownloadJob):
|
||||
"""Generate test-results for extractor runs"""
|
||||
|
||||
class HashIO():
|
||||
"""Minimal file-like interface"""
|
||||
@@ -219,12 +223,20 @@ class HashJob(DownloadJob):
|
||||
def __init__(self, url, content=False):
|
||||
DownloadJob.__init__(self, url)
|
||||
self.content = content
|
||||
self.exception = None
|
||||
self.hash_url = hashlib.sha1()
|
||||
self.hash_keyword = hashlib.sha1()
|
||||
self.hash_content = hashlib.sha1()
|
||||
if content:
|
||||
self.fileobj = self.HashIO(self.hash_content)
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
for msg in self.extractor:
|
||||
self.dispatch(msg)
|
||||
except Exception as e:
|
||||
self.exception = e
|
||||
|
||||
def handle_url(self, url, keywords):
|
||||
self.update_url(url)
|
||||
self.update_keyword(keywords)
|
||||
|
||||
@@ -24,18 +24,17 @@ class TestExtractors(unittest.TestCase):
|
||||
config.set(("extractor", "seiga", "username"), email)
|
||||
|
||||
def _run_test(self, extr, url, result):
|
||||
hjob = job.HashJob(url, "content" in result)
|
||||
self.assertEqual(extr, hjob.extractor.__class__)
|
||||
if "exception" in result:
|
||||
self.assertRaises(result["exception"], hjob.run)
|
||||
return
|
||||
hjob.run()
|
||||
tjob = job.TestJob(url, "content" in result)
|
||||
self.assertEqual(extr, tjob.extractor.__class__)
|
||||
tjob.run()
|
||||
if "url" in result:
|
||||
self.assertEqual(hjob.hash_url.hexdigest(), result["url"])
|
||||
self.assertEqual(tjob.hash_url.hexdigest(), result["url"])
|
||||
if "keyword" in result:
|
||||
self.assertEqual(hjob.hash_keyword.hexdigest(), result["keyword"])
|
||||
self.assertEqual(tjob.hash_keyword.hexdigest(), result["keyword"])
|
||||
if "content" in result:
|
||||
self.assertEqual(hjob.hash_content.hexdigest(), result["content"])
|
||||
self.assertEqual(tjob.hash_content.hexdigest(), result["content"])
|
||||
if "exception" in result:
|
||||
self.assertEqual(tjob.exception.__class__, result["exception"])
|
||||
|
||||
|
||||
# dynamically genertate tests
|
||||
|
||||
Reference in New Issue
Block a user