Make it easier to write tests for comic modules

This also adds a simple test for the "turnoff" module to demonstrate
these features.
This commit is contained in:
Tobias Gruetzmacher 2019-12-01 22:36:49 +01:00
parent 6fd3282047
commit 4d2fac1a9c
6 changed files with 68 additions and 38 deletions

View file

@ -12,14 +12,11 @@ try:
except ImportError:
from backports.functools_lru_cache import lru_cache
from responses import add, GET, POST
_basedir = os.path.dirname(__file__)
from responses import add, GET
def _file(name):
return os.path.join(_basedir, 'responses', name)
return os.path.join(os.path.dirname(__file__), 'responses', name)
@lru_cache()
@ -34,27 +31,20 @@ def _img():
return f.read()
def page(url, pagename):
add(GET, url, _content(pagename))
def png(url):
add(GET, url, _img(), content_type='image/jpeg')
def jpeg(url):
add(GET, url, _img(), content_type='image/jpeg')
def xkcd():
add(GET, 'https://xkcd.com/', _content('xkcd-1899'))
for page in (302, 303, 1898, 1899):
add(GET, 'https://xkcd.com/%i/' % page, _content('xkcd-%i' % page))
add(GET, re.compile(r'https://imgs\.xkcd\.com/.*\.png'), _img(), content_type='image/png')
def bloomingfaeries():
add(GET, 'http://www.bloomingfaeries.com/', _content('bf-home'))
add(GET, 'http://www.bloomingfaeries.com/comic/public/bloomin-faeries-405/', _content('bf-405'))
add(GET, re.compile(r'http://www\.bloomingfaeries\.com/.*\.jpg'), _img(), content_type='image/jpeg')
def zenpencils():
add(GET, 'https://zenpencils.com/', _content('zp-home'))
add(GET, 'https://zenpencils.com/comic/missing/', _content('zp-223'))
add(GET, 'https://zenpencils.com/comic/lifejacket/', _content('zp-222'))
add(GET, re.compile(r'https://cdn-zenpencils\.netdna-ssl\.com/wp-content/uploads/.*\.jpg'), _img(),
content_type='image/jpeg')
def vote():
add(POST, 'https://buildbox.23.gs/count/', '')
page('https://xkcd.com/', 'xkcd-1899')
for num in (302, 303, 1898, 1899):
page('https://xkcd.com/%i/' % num, 'xkcd-%i' % num)
png(re.compile(r'https://imgs\.xkcd\.com/.*\.png'))

Binary file not shown.

Binary file not shown.

View file

@ -5,12 +5,14 @@
from __future__ import absolute_import, division, print_function
import json
import re
import pytest
import responses
import dosagelib.cmd
import httpmocks
import json
def cmd(*options):
@ -29,6 +31,9 @@ def cmd_err(*options):
class TestDosage(object):
"""Test the dosage commandline client."""
# This shouldn't hit the network at all, so add responses without mocks to
# make sure it doesn't do that
@responses.activate
def test_list_comics(self):
for option in ("-l", "--list", "--singlelist"):
cmd_ok(option)
@ -62,10 +67,13 @@ class TestDosage(object):
@responses.activate
def test_fetch_html_and_rss_2(self, tmpdir):
httpmocks.bloomingfaeries()
httpmocks.page('http://www.bloomingfaeries.com/', 'bf-home')
httpmocks.page(re.compile('http://www.*faeries-405/'), 'bf-405')
httpmocks.png(re.compile(r'http://www\.bloomingfaeries\.com/.*\.jpg'))
cmd_ok("--numstrips", "2", "--baseurl", "bla", "--basepath",
str(tmpdir), "--output", "rss", "--output", "html", "--adult",
"BloomingFaeries")
str(tmpdir), "--output", "rss", "--output", "html", "--adult",
"BloomingFaeries")
@responses.activate
def test_fetch_indexed(self, tmpdir):
@ -74,8 +82,11 @@ class TestDosage(object):
@responses.activate
def test_json_page_key_bounce_and_multi_image(self, tmpdir):
httpmocks.zenpencils()
print(tmpdir)
httpmocks.page('https://zenpencils.com/', 'zp-home')
httpmocks.page('https://zenpencils.com/comic/missing/', 'zp-223')
httpmocks.page('https://zenpencils.com/comic/lifejacket/', 'zp-222')
httpmocks.jpeg(re.compile(r'https://cdn-.*\.jpg'))
cmd_ok("-v", "-b", str(tmpdir), "-o", "json", "ZenPencils")
directory = tmpdir.join('ZenPencils')

30
tests/test_modules.py Normal file
View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019 Tobias Gruetzmacher
from __future__ import absolute_import, division, print_function
import re
import responses
import dosagelib.cmd
import httpmocks
def cmd(*options):
"""'Fake' run dosage with given options."""
assert dosagelib.cmd.main(("--allow-multiple",) + options) == 0
class TestModules(object):
"""Test that specific comic modules work correctly."""
@responses.activate
def test_turnoff(self, tmpdir):
httpmocks.page('https://turnoff.us/', 'turnoff-home')
httpmocks.page('https://turnoff.us/geek/the-bad-design-punisher',
'turnoff-229')
httpmocks.png(re.compile(r'https://turnoff\.us/image/en/.*\.png'))
cmd('--numstrips', '2', '--basepath', str(tmpdir), 'turnoff')

View file

@ -1,14 +1,13 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2017 Tobias Gruetzmacher
# Copyright (C) 2015-2019 Tobias Gruetzmacher
from __future__ import absolute_import, division, print_function
import responses
from dosagelib import scraper
import httpmocks
class ATestScraper(scraper._BasicScraper):
@ -16,8 +15,8 @@ class ATestScraper(scraper._BasicScraper):
class TestVote(object):
@responses.activate
def test_vote(self):
httpmocks.vote()
responses.add(responses.POST, 'https://buildbox.23.gs/count/', '')
ATestScraper('Test_Test').vote()