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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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