From 4d2fac1a9c604423368e80b85190a8c47cacc6c5 Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Sun, 1 Dec 2019 22:36:49 +0100 Subject: [PATCH] Make it easier to write tests for comic modules This also adds a simple test for the "turnoff" module to demonstrate these features. --- tests/httpmocks.py | 46 +++++++++++---------------- tests/responses/turnoff-229.html.gz | Bin 0 -> 241 bytes tests/responses/turnoff-home.html.gz | Bin 0 -> 6339 bytes tests/test_dosage.py | 23 ++++++++++---- tests/test_modules.py | 30 +++++++++++++++++ tests/test_vote.py | 7 ++-- 6 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 tests/responses/turnoff-229.html.gz create mode 100644 tests/responses/turnoff-home.html.gz create mode 100644 tests/test_modules.py diff --git a/tests/httpmocks.py b/tests/httpmocks.py index a0a12160a..da9d2b108 100644 --- a/tests/httpmocks.py +++ b/tests/httpmocks.py @@ -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')) diff --git a/tests/responses/turnoff-229.html.gz b/tests/responses/turnoff-229.html.gz new file mode 100644 index 0000000000000000000000000000000000000000..d7896543ea96ecd8a9a342cbf8ba8d807be1d580 GIT binary patch literal 241 zcmVgw)N zrB5$EO-xLBH*?N6Co@Azb!&`*24`M@cFq zfi9~g1fHIV_aELre81#XNy3Q3-OKLmZcKz7fvKv@%F4>CuH^2g+GBM%>++Q98hYj9 zUHJ8pqV7}(`0Qz`a$-u4Sh{K%HgGjjhY3ou zOc$ECSBo!0S%I;BCNN+*vC^P#TaFt;^)=a5%Q9SP5~PDllG04g)qqYT3Fxq#NrW86 z=qQ$@=#WO53+WYfw7Di>9xO9PcQCalbxTv%eAk#XCl8?H&(Zj)@KCs!!GWSxSNg-0 zU*tPVK0z@%Ms3Hkq2q4K2}}9S-PrMI`A{T7Cf>j%u&;!{SQpoGOlv+*dFZWPngJAe zI8xBa*rxLRUV*$~-EPzA?{`q4rl@i8-@M&JhDdTW`#`R{2~irN0Hr{<#85+%C^G*P zRV8H%5iQT9x{jqdz%VpZp&o##c$f*4q$W^IQdQmZ&z*)m;6wIw<;(_3ujVdxJBfwxG!blTK~b7Fcb-LFIen-Z3H0fsOxH#7}t8 zHFHThP{&p%k+D?=&dZ5tlDef>H|b!C6tUSOQ{DAOD)w{AX_+{r>09h$lRKKM?n1`|*oq^05|*Q=$T$dHuMJEsVEG4L@aB+n2<22j z28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