Small style fixes (mostly in tests)

This commit is contained in:
Tobias Gruetzmacher 2020-04-18 13:03:02 +02:00
parent c2e3264be0
commit e70bf8c7ad
8 changed files with 38 additions and 39 deletions

View file

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019 Tobias Gruetzmacher
from collections import defaultdict
from random import uniform
from time import time, sleep
# Copyright (C) 2019-2020 Tobias Gruetzmacher
import collections
import random
import time
import requests
from requests.adapters import HTTPAdapter
@ -38,7 +38,7 @@ class Session(requests.Session):
self.mount('https://', HTTPAdapter(max_retries=retry))
self.headers.update({'User-Agent': UserAgent})
self.throttles = defaultdict(lambda: RandomThrottle(0.0, 0.3))
self.throttles = collections.defaultdict(lambda: RandomThrottle())
def send(self, request, **kwargs):
if 'timeout' not in kwargs:
@ -56,16 +56,16 @@ class Session(requests.Session):
class RandomThrottle(object):
def __init__(self, th_min, th_max):
def __init__(self, th_min=0.0, th_max=0.3):
self.th_min = th_min
self.th_max = th_max
self.next = time()
self.next = time.time()
def delay(self):
d = self.next - time()
d = self.next - time.time()
if d > 0:
sleep(d)
self.next = time() + uniform(self.th_min, self.th_max)
time.sleep(d)
self.next = time.time() + random.uniform(self.th_min, self.th_max)
# A default session for cookie and connection sharing

View file

@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019 Tobias Gruetzmacher
# Copyright (C) 2019-2020 Tobias Gruetzmacher
import time
import pytest
@pytest.fixture
def nosleep(monkeypatch):
@pytest.fixture()
def _nosleep(monkeypatch):
def sleep(seconds):
pass

View file

@ -15,13 +15,11 @@ def get_test_scrapers():
"""Return scrapers that should be tested."""
if "TESTALL" in os.environ:
# test all comics (this will take some time)
scrapers = scraper.get_scrapers()
else:
return scraper.get_scrapers()
if 'TESTCOMICS' in os.environ:
scraper_pattern = re.compile(os.environ['TESTCOMICS'])
else:
# Get limited number of scraper tests on Travis builds to make it
# faster
# Get limited number of scraper tests as default
testscrapernames = [
# "classic" _BasicScraper
'AbstruseGoose',
@ -30,14 +28,12 @@ def get_test_scrapers():
# _WordPressScraper
'GrrlPower'
]
scraper_pattern = re.compile('^(' + '|'.join(testscrapernames) +
')$')
scraper_pattern = re.compile('^(' + '|'.join(testscrapernames) + ')$')
scrapers = [
return [
scraperobj for scraperobj in scraper.get_scrapers()
if scraper_pattern.match(scraperobj.name)
]
return scrapers
def pytest_generate_tests(metafunc):

View file

@ -26,7 +26,7 @@ def cmd_err(*options):
assert cmd(*options) == 1
@pytest.mark.usefixtures("nosleep")
@pytest.mark.usefixtures("_nosleep")
class TestDosage(object):
"""Test the dosage commandline client."""

View file

@ -14,7 +14,7 @@ def cmd(*options):
assert dosagelib.cmd.main(("--allow-multiple",) + options) == 0
@pytest.mark.usefixtures("nosleep")
@pytest.mark.usefixtures("_nosleep")
class TestModules(object):
"""Test that specific comic modules work correctly."""

View file

@ -22,5 +22,5 @@ class TestScraper(object):
assert len(result) > 1
def test_find_scrapers_error(self):
with pytest.raises(ValueError):
scraper.find_scrapers("")
with pytest.raises(ValueError, match='empty comic name'):
scraper.find_scrapers('')

View file

@ -22,7 +22,7 @@ class TestRegex(object):
ValuePrefix = '/bla/'
@pytest.mark.parametrize("tag,value,domatch", [
@pytest.mark.parametrize(('tag', 'value', 'domatch'), [
('<img src="%s">', ValuePrefix + 'foo', True),
('< img src = "%s" >', ValuePrefix, True),
('<img class="prev" src="%s">', ValuePrefix + '...', True),

View file

@ -28,8 +28,11 @@ commands =
--tee --output-file={toxworkdir}/flake8.log
deps =
flake8
flake8-2020
flake8-coding
flake8-future-import
flake8-pytest
flake8-pytest-style
[flake8]
# we aim for 79, but this suppresses warnings for now...