Replace online tests with mocks.

We want to test our code, not the comic modules.
This commit is contained in:
Tobias Gruetzmacher 2017-10-13 00:34:37 +02:00
parent 69e6144843
commit d88f6aeee3
11 changed files with 74 additions and 6 deletions

View file

@ -11,7 +11,7 @@ from ..helpers import bounceStarter
class Xkcd(_ParserScraper):
name = 'xkcd'
url = 'http://xkcd.com/'
url = 'https://xkcd.com/'
starter = bounceStarter
stripUrl = url + '%s/'
firstStripUrl = stripUrl % '1'

52
tests/httpmocks.py Normal file
View file

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Tobias Gruetzmacher
from __future__ import absolute_import, division, print_function
import gzip
import os.path
import re
try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache
from responses import add, GET, POST
_basedir = os.path.dirname(__file__)
def _file(name):
return os.path.join(_basedir, 'responses', name)
@lru_cache()
def _content(name):
with gzip.open(_file(name + '.html.gz'), 'r') as f:
return f.read()
@lru_cache()
def _img():
with open(_file('empty.png'), 'rb') as f:
return f.read()
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 vote():
add(POST, 'http://gaecounter.appspot.com/count/', 'no')

Binary file not shown.

Binary file not shown.

BIN
tests/responses/empty.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -6,20 +6,25 @@
from __future__ import absolute_import, division, print_function
import pytest
import sys
import responses
import dosagelib.cmd
import httpmocks
def cmd(*options):
"""'Fake' run dosage with given options."""
return dosagelib.cmd.main(('--allow-multiple',) + options)
def cmd_ok(*options):
assert cmd(*options) == 0
def cmd_err(*options):
assert cmd(*options) == 1
class TestDosage(object):
"""Test the dosage commandline client."""
@ -48,14 +53,20 @@ class TestDosage(object):
def test_multiple_comics_match(self):
cmd_err('Garfield')
@responses.activate
def test_fetch_html_and_rss_json(self, tmpdir):
httpmocks.xkcd()
cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "-o", "html", "-o", "rss",
"-o", "json", "xkcd")
"-o", "json", "xkcd")
@responses.activate
def test_fetch_html_and_rss_2(self, tmpdir):
httpmocks.bloomingfaeries()
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):
httpmocks.xkcd()
cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "xkcd:303")

View file

@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2016 Tobias Gruetzmacher
# Copyright (C) 2015-2017 Tobias Gruetzmacher
from __future__ import absolute_import, division, print_function
import responses
from dosagelib import scraper
import httpmocks
class ATestScraper(scraper._BasicScraper):
@ -14,6 +17,8 @@ class ATestScraper(scraper._BasicScraper):
class TestVote(object):
@responses.activate
def test_vote(self):
httpmocks.vote()
answer = ATestScraper('Test_Test').vote()
assert answer in ('counted', 'no'), 'invalid answer %r' % answer