From e70bf8c7ad35dcae4b3e04491952a87110c3a7f3 Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Sat, 18 Apr 2020 13:03:02 +0200 Subject: [PATCH] Small style fixes (mostly in tests) --- dosagelib/http.py | 20 ++++++++++---------- tests/conftest.py | 6 +++--- tests/modules/conftest.py | 38 +++++++++++++++++--------------------- tests/test_dosage.py | 2 +- tests/test_modules.py | 2 +- tests/test_scraper.py | 4 ++-- tests/test_util.py | 2 +- tox.ini | 3 +++ 8 files changed, 38 insertions(+), 39 deletions(-) diff --git a/dosagelib/http.py b/dosagelib/http.py index a324863e0..e8a6b56b6 100644 --- a/dosagelib/http.py +++ b/dosagelib/http.py @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 1384cc5f5..156c81613 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/modules/conftest.py b/tests/modules/conftest.py index 53caef723..b32584359 100644 --- a/tests/modules/conftest.py +++ b/tests/modules/conftest.py @@ -15,29 +15,25 @@ 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() + return scraper.get_scrapers() + if 'TESTCOMICS' in os.environ: + scraper_pattern = re.compile(os.environ['TESTCOMICS']) else: - 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 - testscrapernames = [ - # "classic" _BasicScraper - 'AbstruseGoose', - # complex _ParserScraper - 'GoComics/CalvinAndHobbes', - # _WordPressScraper - 'GrrlPower' - ] - scraper_pattern = re.compile('^(' + '|'.join(testscrapernames) + - ')$') - - scrapers = [ - scraperobj for scraperobj in scraper.get_scrapers() - if scraper_pattern.match(scraperobj.name) + # Get limited number of scraper tests as default + testscrapernames = [ + # "classic" _BasicScraper + 'AbstruseGoose', + # complex _ParserScraper + 'GoComics/CalvinAndHobbes', + # _WordPressScraper + 'GrrlPower' ] - return scrapers + scraper_pattern = re.compile('^(' + '|'.join(testscrapernames) + ')$') + + return [ + scraperobj for scraperobj in scraper.get_scrapers() + if scraper_pattern.match(scraperobj.name) + ] def pytest_generate_tests(metafunc): diff --git a/tests/test_dosage.py b/tests/test_dosage.py index 0504ef026..41e1f02a8 100644 --- a/tests/test_dosage.py +++ b/tests/test_dosage.py @@ -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.""" diff --git a/tests/test_modules.py b/tests/test_modules.py index 9faa15674..4b0d6c3e0 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -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.""" diff --git a/tests/test_scraper.py b/tests/test_scraper.py index fb7b5a90a..8591ab7b0 100644 --- a/tests/test_scraper.py +++ b/tests/test_scraper.py @@ -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('') diff --git a/tests/test_util.py b/tests/test_util.py index a533dde93..b69aa33de 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -22,7 +22,7 @@ class TestRegex(object): ValuePrefix = '/bla/' - @pytest.mark.parametrize("tag,value,domatch", [ + @pytest.mark.parametrize(('tag', 'value', 'domatch'), [ ('', ValuePrefix + 'foo', True), ('< img src = "%s" >', ValuePrefix, True), ('', ValuePrefix + '...', True), diff --git a/tox.ini b/tox.ini index b41cb4a01..a191c993a 100644 --- a/tox.ini +++ b/tox.ini @@ -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...