Skip to content

Commit 7034ae9

Browse files
committed
Enable Pytest lint
1 parent 2495c4e commit 7034ae9

File tree

7 files changed

+45
-44
lines changed

7 files changed

+45
-44
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ select = [
1212
# isort
1313
"I",
1414
# pytest style
15-
# "PT",
15+
"PT",
1616
# eradicate commented code
1717
# "ERA",
1818
# ruff lint

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import dockerflow.checks.registry
77

88

9-
@pytest.fixture
9+
@pytest.fixture()
1010
def version_content():
1111
"""
1212
as documented on https://github.com/mozilla-services/Dockerflow/blob/main/docs/version_object.md
@@ -20,6 +20,6 @@ def version_content():
2020

2121

2222
@pytest.fixture(autouse=True)
23-
def clear_checks():
23+
def _clear_checks():
2424
yield
2525
dockerflow.checks.registry.clear_checks()

tests/core/test_logging.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@pytest.fixture()
18-
def reset_logging():
18+
def _reset_logging():
1919
logging.shutdown()
2020
reload(logging)
2121

@@ -31,7 +31,8 @@ def assert_records(records):
3131
return details
3232

3333

34-
def test_initialization_from_ini(reset_logging, caplog, tmpdir):
34+
@pytest.mark.usefixtures("_reset_logging")
35+
def test_initialization_from_ini(caplog, tmpdir):
3536
ini_content = textwrap.dedent(
3637
"""
3738
[loggers]
@@ -229,7 +230,5 @@ def test_ignore_json_message(caplog):
229230
}
230231
}
231232
}
232-
""".replace(
233-
"\\", "\\\\"
234-
)
233+
""".replace("\\", "\\\\")
235234
) # HACK: Fix escaping for easy copy/paste

tests/django/test_django.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020

2121

2222
@pytest.fixture(autouse=True)
23-
def reset_checks():
23+
def _reset_checks():
2424
yield
2525
registry.registered_checks = set()
2626
registry.deployment_checks = set()
2727

2828

2929
@pytest.fixture(autouse=True)
30-
def setup_request_summary_logger(dockerflow_middleware):
30+
def _setup_request_summary_logger(dockerflow_middleware):
3131
dockerflow_middleware.summary_logger.addHandler(logging.NullHandler())
3232
dockerflow_middleware.summary_logger.setLevel(logging.INFO)
3333

3434

35-
@pytest.fixture
35+
@pytest.fixture()
3636
def dockerflow_middleware():
3737
return DockerflowMiddleware(get_response=HttpResponse())
3838

@@ -55,7 +55,7 @@ def test_version_missing(dockerflow_middleware, mocker, rf):
5555
assert response.status_code == 404
5656

5757

58-
@pytest.mark.django_db
58+
@pytest.mark.django_db()
5959
def test_heartbeat(client, settings):
6060
response = client.get("/__heartbeat__")
6161
assert response.status_code == 200
@@ -73,7 +73,7 @@ def test_heartbeat(client, settings):
7373
assert content.get("details") is None
7474

7575

76-
@pytest.mark.django_db
76+
@pytest.mark.django_db()
7777
def test_heartbeat_debug(client, settings):
7878
settings.DOCKERFLOW_CHECKS = [
7979
"tests.django.django_checks.warning",
@@ -89,7 +89,7 @@ def test_heartbeat_debug(client, settings):
8989
assert content["details"]
9090

9191

92-
@pytest.mark.django_db
92+
@pytest.mark.django_db()
9393
def test_heartbeat_silenced(client, settings):
9494
settings.DOCKERFLOW_CHECKS = [
9595
"tests.django.django_checks.warning",
@@ -107,8 +107,9 @@ def test_heartbeat_silenced(client, settings):
107107
assert "error" not in content["details"]
108108

109109

110-
@pytest.mark.django_db
111-
def test_heartbeat_logging(dockerflow_middleware, reset_checks, rf, settings, caplog):
110+
@pytest.mark.django_db()
111+
@pytest.mark.usefixtures("_reset_checks")
112+
def test_heartbeat_logging(dockerflow_middleware, rf, settings, caplog):
112113
request = rf.get("/__heartbeat__")
113114
settings.DOCKERFLOW_CHECKS = [
114115
"tests.django.django_checks.warning",
@@ -123,7 +124,7 @@ def test_heartbeat_logging(dockerflow_middleware, reset_checks, rf, settings, ca
123124
assert ("WARNING", "tests.checks.W001: some warning") in logged
124125

125126

126-
@pytest.mark.django_db
127+
@pytest.mark.django_db()
127128
def test_lbheartbeat_makes_no_db_queries(dockerflow_middleware, rf):
128129
queries = CaptureQueriesContext(connection)
129130
request = rf.get("/__lbheartbeat__")
@@ -133,7 +134,7 @@ def test_lbheartbeat_makes_no_db_queries(dockerflow_middleware, rf):
133134
assert len(queries) == 0
134135

135136

136-
@pytest.mark.django_db
137+
@pytest.mark.django_db()
137138
def test_redis_check(client, settings):
138139
settings.DOCKERFLOW_CHECKS = ["dockerflow.django.checks.check_redis_connected"]
139140
checks.register()
@@ -152,7 +153,7 @@ def assert_log_record(request, record, errno=0, level=logging.INFO):
152153
assert isinstance(record.t, int)
153154

154155

155-
@pytest.fixture
156+
@pytest.fixture()
156157
def dockerflow_request(rf):
157158
return rf.get("/", HTTP_USER_AGENT="dockerflow/tests", HTTP_ACCEPT_LANGUAGE="tlh")
158159

@@ -233,15 +234,15 @@ def test_check_database_connected_misconfigured(mocker):
233234
assert errors[0].id == health.ERROR_MISCONFIGURED_DATABASE
234235

235236

236-
@pytest.mark.django_db
237+
@pytest.mark.django_db()
237238
def test_check_database_connected_unsuable(mocker):
238239
mocker.patch("django.db.connection.is_usable", return_value=False)
239240
errors = checks.check_database_connected([])
240241
assert len(errors) == 1
241242
assert errors[0].id == health.ERROR_UNUSABLE_DATABASE
242243

243244

244-
@pytest.mark.django_db
245+
@pytest.mark.django_db()
245246
def test_check_database_connected_success(mocker):
246247
errors = checks.check_database_connected([])
247248
assert errors == []
@@ -257,7 +258,7 @@ def test_check_migrations_applied_cannot_check_migrations(exception, mocker):
257258
assert errors[0].id == health.INFO_CANT_CHECK_MIGRATIONS
258259

259260

260-
@pytest.mark.django_db
261+
@pytest.mark.django_db()
261262
def test_check_migrations_applied_unapplied_migrations(mocker):
262263
mock_loader = mocker.patch("django.db.migrations.loader.MigrationLoader")
263264
mock_loader.return_value.applied_migrations = ["spam", "eggs"]
@@ -291,7 +292,7 @@ def test_check_migrations_applied_unapplied_migrations(mocker):
291292

292293

293294
@pytest.mark.parametrize(
294-
"exception,error",
295+
("exception", "error"),
295296
[
296297
(redis.ConnectionError, health.ERROR_CANNOT_CONNECT_REDIS),
297298
(NotImplementedError, health.ERROR_MISSING_REDIS_CLIENT),

tests/fastapi/test_fastapi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def create_app():
2020
return app
2121

2222

23-
@pytest.fixture
23+
@pytest.fixture()
2424
def app():
2525
return create_app()
2626

2727

28-
@pytest.fixture
28+
@pytest.fixture()
2929
def client(app):
3030
return TestClient(app)
3131

@@ -66,7 +66,7 @@ def test_mozlog_failure(client, mocker, caplog):
6666
"dockerflow.fastapi.views.get_version", side_effect=ValueError("crash")
6767
)
6868

69-
with pytest.raises(ValueError):
69+
with pytest.raises(expected_exception=ValueError):
7070
client.get("/__version__")
7171

7272
record = caplog.records[0]

tests/flask/test_flask.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def load_user(user_id):
3434
return MockUser(user_id)
3535

3636

37-
@pytest.fixture
37+
@pytest.fixture()
3838
def app():
3939
app = Flask("dockerflow")
4040
app.secret_key = "super sekrit"
@@ -48,31 +48,31 @@ def client(app):
4848
return app.test_client()
4949

5050

51-
@pytest.fixture
51+
@pytest.fixture()
5252
def dockerflow(app):
5353
return Dockerflow(app)
5454

5555

5656
@pytest.fixture()
57-
def setup_request_summary_logger(dockerflow):
57+
def _setup_request_summary_logger(dockerflow):
5858
dockerflow.summary_logger.addHandler(logging.NullHandler())
5959
dockerflow.summary_logger.setLevel(logging.INFO)
6060

6161

62-
@pytest.fixture
62+
@pytest.fixture()
6363
def db(app):
6464
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
6565
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
6666
return SQLAlchemy(app)
6767

6868

69-
@pytest.fixture
69+
@pytest.fixture()
7070
def migrate(app, db):
7171
test_migrations = os.path.join(os.path.dirname(__file__), "migrations")
7272
return Migrate(app, db, directory=test_migrations)
7373

7474

75-
@pytest.fixture
75+
@pytest.fixture()
7676
def redis_store(app):
7777
return FlaskRedis.from_custom_provider(FakeStrictRedis, app)
7878

@@ -283,7 +283,8 @@ def assert_log_record(record, errno=0, level=logging.INFO):
283283
headers = {"User-Agent": "dockerflow/tests", "Accept-Language": "tlh"}
284284

285285

286-
def test_request_summary(caplog, app, client, setup_request_summary_logger):
286+
@pytest.mark.usefixtures("_setup_request_summary_logger")
287+
def test_request_summary(caplog, app, client):
287288
caplog.set_level(logging.INFO)
288289
with app.test_request_context("/"):
289290
client.get("/", headers=headers)
@@ -487,7 +488,7 @@ def test_check_migrations_applied_unapplied_migrations(mocker, app, db, migrate)
487488

488489

489490
@pytest.mark.parametrize(
490-
"exception,error",
491+
("exception", "error"),
491492
[
492493
(redis.ConnectionError, health.ERROR_CANNOT_CONNECT_REDIS),
493494
(redis.RedisError, health.ERROR_REDIS_EXCEPTION),

tests/sanic/test_sanic.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def fake_redis(*args, **kw):
5151
return FakeRedis(*args, **kw)
5252

5353

54-
@pytest.fixture(scope="function")
54+
@pytest.fixture()
5555
def app():
5656
app = Sanic(f"dockerflow-{uuid.uuid4().hex}")
5757

@@ -64,24 +64,24 @@ async def root(request):
6464
return app
6565

6666

67-
@pytest.fixture
67+
@pytest.fixture()
6868
def dockerflow(app):
6969
return Dockerflow(app)
7070

7171

7272
@pytest.fixture()
73-
def setup_request_summary_logger(dockerflow):
73+
def _setup_request_summary_logger(dockerflow):
7474
dockerflow.summary_logger.addHandler(logging.NullHandler())
7575
dockerflow.summary_logger.setLevel(logging.INFO)
7676

7777

78-
@pytest.fixture
78+
@pytest.fixture()
7979
def dockerflow_redis(app):
8080
app.config["REDIS"] = {"address": "redis://:password@localhost:6379/0"}
8181
return Dockerflow(app, redis=SanicRedis(app))
8282

8383

84-
@pytest.fixture
84+
@pytest.fixture()
8585
def test_client(app):
8686
return SanicTestClient(app)
8787

@@ -210,7 +210,7 @@ def test_redis_check(dockerflow_redis, mocker, test_client):
210210

211211

212212
@pytest.mark.parametrize(
213-
"error,messages",
213+
("error", "messages"),
214214
[
215215
(
216216
"connection",
@@ -251,7 +251,8 @@ def assert_log_record(caplog, errno=0, level=logging.INFO, rid=None, t=int, path
251251
headers = {"User-Agent": "dockerflow/tests", "Accept-Language": "tlh"}
252252

253253

254-
def test_request_summary(caplog, setup_request_summary_logger, test_client):
254+
@pytest.mark.usefixtures("_setup_request_summary_logger")
255+
def test_request_summary(caplog, test_client):
255256
request, _ = test_client.get(headers=headers)
256257
assert isinstance(request.ctx.start_timestamp, float)
257258
assert request.ctx.id is not None
@@ -270,9 +271,8 @@ def exception_raiser(request):
270271
assert record.getMessage() == "exception message"
271272

272273

273-
def test_request_summary_failed_request(
274-
app, caplog, setup_request_summary_logger, test_client
275-
):
274+
@pytest.mark.usefixtures("_setup_request_summary_logger")
275+
def test_request_summary_failed_request(app, caplog, test_client):
276276
@app.middleware
277277
def hostile_callback(request):
278278
# simulating resetting request changes

0 commit comments

Comments
 (0)