Skip to content

Commit 6939b23

Browse files
committed
Format code with Ruff
1 parent 8a499b2 commit 6939b23

22 files changed

+214
-215
lines changed

pytest_django/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
__version__ = "unknown"
66

77

8-
__all__ = (
8+
__all__ = [
99
"__version__",
10-
)
10+
]

pytest_django/asserts.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ def assertRaisesMessage(
113113
expected_exception: type[Exception],
114114
expected_message: str,
115115
*args,
116-
**kwargs
116+
**kwargs,
117117
):
118118
...
119119

120120
def assertWarnsMessage(
121121
expected_warning: Warning,
122122
expected_message: str,
123123
*args,
124-
**kwargs
124+
**kwargs,
125125
):
126126
...
127127

@@ -209,7 +209,7 @@ def assertNumQueries(
209209
func=...,
210210
*args,
211211
using: str = ...,
212-
**kwargs
212+
**kwargs,
213213
):
214214
...
215215

pytest_django/fixtures.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import django
2929

3030

31-
_DjangoDbDatabases = Optional[Union[Literal['__all__'], Iterable[str]]]
31+
_DjangoDbDatabases = Optional[Union[Literal["__all__"], Iterable[str]]]
3232
_DjangoDbAvailableApps = Optional[List[str]]
3333
# transaction, reset_sequences, databases, serialized_rollback, available_apps
3434
_DjangoDb = Tuple[bool, bool, _DjangoDbDatabases, bool, _DjangoDbAvailableApps]
@@ -134,7 +134,7 @@ def django_db_setup(
134134
db_cfg = setup_databases(
135135
verbosity=request.config.option.verbose,
136136
interactive=False,
137-
**setup_databases_args
137+
**setup_databases_args,
138138
)
139139

140140
yield
@@ -145,9 +145,7 @@ def django_db_setup(
145145
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
146146
except Exception as exc: # noqa: BLE001
147147
request.node.warn(
148-
pytest.PytestWarning(
149-
f"Error when trying to teardown test databases: {exc!r}"
150-
)
148+
pytest.PytestWarning(f"Error when trying to teardown test databases: {exc!r}")
151149
)
152150

153151

@@ -181,13 +179,12 @@ def _django_db_helper(
181179
available_apps,
182180
) = False, False, None, False, None
183181

184-
transactional = transactional or reset_sequences or (
185-
"transactional_db" in request.fixturenames
186-
or "live_server" in request.fixturenames
187-
)
188-
reset_sequences = reset_sequences or (
189-
"django_db_reset_sequences" in request.fixturenames
182+
transactional = (
183+
transactional
184+
or reset_sequences
185+
or ("transactional_db" in request.fixturenames or "live_server" in request.fixturenames)
190186
)
187+
reset_sequences = reset_sequences or ("django_db_reset_sequences" in request.fixturenames)
191188
serialized_rollback = serialized_rollback or (
192189
"django_db_serialized_rollback" in request.fixturenames
193190
)
@@ -229,6 +226,7 @@ class PytestDjangoTestCase(test_case_class): # type: ignore[misc,valid-type]
229226
# functionality to these methods, in which case skipping them completely
230227
# would not be desirable. Let's cross that bridge when we get there...
231228
if not transactional:
229+
232230
@classmethod
233231
def setUpClass(cls) -> None:
234232
super(django.test.TestCase, cls).setUpClass()
@@ -558,9 +556,11 @@ def live_server(request: pytest.FixtureRequest):
558556
"""
559557
skip_if_no_django()
560558

561-
addr = request.config.getvalue("liveserver") or os.getenv(
562-
"DJANGO_LIVE_TEST_SERVER_ADDRESS"
563-
) or "localhost"
559+
addr = (
560+
request.config.getvalue("liveserver")
561+
or os.getenv("DJANGO_LIVE_TEST_SERVER_ADDRESS")
562+
or "localhost"
563+
)
564564

565565
server = live_server_helper.LiveServer(addr)
566566
yield server

pytest_django/live_server_helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, addr: str, *, start: bool = True) -> None:
4545
self.thread = LiveServerThread(host, **liveserver_kwargs)
4646

4747
self._live_server_modified_settings = modify_settings(
48-
ALLOWED_HOSTS={"append": host}
48+
ALLOWED_HOSTS={"append": host},
4949
)
5050
# `_live_server_modified_settings` is enabled and disabled by
5151
# `_live_server_helper`.

pytest_django/plugin.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,17 @@ def pytest_addoption(parser: pytest.Parser) -> None:
112112
help="Enable Django migrations on test setup",
113113
)
114114
parser.addini(
115-
CONFIGURATION_ENV, "django-configurations class to use by pytest-django."
115+
CONFIGURATION_ENV,
116+
"django-configurations class to use by pytest-django.",
116117
)
117118
group.addoption(
118119
"--liveserver",
119120
default=None,
120121
help="Address and port for the live_server fixture.",
121122
)
122123
parser.addini(
123-
SETTINGS_MODULE_ENV, "Django settings module to use by pytest-django."
124+
SETTINGS_MODULE_ENV,
125+
"Django settings module to use by pytest-django.",
124126
)
125127

126128
parser.addini(
@@ -131,8 +133,7 @@ def pytest_addoption(parser: pytest.Parser) -> None:
131133
)
132134
parser.addini(
133135
"django_debug_mode",
134-
"How to set the Django DEBUG setting (default `False`). "
135-
"Use `keep` to not override.",
136+
"How to set the Django DEBUG setting (default `False`). " "Use `keep` to not override.",
136137
default="False",
137138
)
138139
group.addoption(
@@ -308,9 +309,7 @@ def pytest_load_initial_conftests(
308309

309310
if (
310311
options.itv
311-
or _get_boolean_value(
312-
os.environ.get(INVALID_TEMPLATE_VARS_ENV), INVALID_TEMPLATE_VARS_ENV
313-
)
312+
or _get_boolean_value(os.environ.get(INVALID_TEMPLATE_VARS_ENV), INVALID_TEMPLATE_VARS_ENV)
314313
or early_config.getini(INVALID_TEMPLATE_VARS_ENV)
315314
):
316315
os.environ[INVALID_TEMPLATE_VARS_ENV] = "true"
@@ -370,6 +369,7 @@ def pytest_report_header(config: pytest.Config) -> list[str] | None:
370369

371370
if "django" in sys.modules:
372371
import django
372+
373373
report_header.insert(0, f"version: {django.get_version()}")
374374

375375
if report_header:
@@ -534,6 +534,7 @@ def _django_setup_unittest(
534534
# Before pytest 5.4: https://github.com/pytest-dev/pytest/issues/5991
535535
# After pytest 5.4: https://github.com/pytest-dev/pytest-django/issues/824
536536
from _pytest.unittest import TestCaseFunction
537+
537538
original_runtest = TestCaseFunction.runtest
538539

539540
def non_debugging_runtest(self) -> None:
@@ -707,7 +708,7 @@ def _template_string_if_invalid_marker(
707708
request: pytest.FixtureRequest,
708709
) -> None:
709710
"""Apply the @pytest.mark.ignore_template_errors marker,
710-
internal to pytest-django."""
711+
internal to pytest-django."""
711712
marker = request.keywords.get("ignore_template_errors", None)
712713
if os.environ.get(INVALID_TEMPLATE_VARS_ENV, "false") == "true":
713714
if marker and django_settings_is_configured():

pytest_django_test/app/migrations/0001_initial.py

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
class Migration(migrations.Migration):
9-
109
initial = True
1110

1211
dependencies: tuple[tuple[str, str], ...] = ()

pytest_django_test/db_helpers.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
# An explicit test db name was given, is that as the base name
3131
TEST_DB_NAME = f"{TEST_DB_NAME}_inner"
3232

33-
SECOND_DB_NAME = DB_NAME + '_second' if DB_NAME is not None else None
34-
SECOND_TEST_DB_NAME = TEST_DB_NAME + '_second' if DB_NAME is not None else None
33+
SECOND_DB_NAME = DB_NAME + "_second" if DB_NAME is not None else None
34+
SECOND_TEST_DB_NAME = TEST_DB_NAME + "_second" if DB_NAME is not None else None
3535

3636

3737
def get_db_engine() -> str:
@@ -87,10 +87,7 @@ def run_mysql(*args: str) -> CmdResult:
8787

8888

8989
def skip_if_sqlite_in_memory() -> None:
90-
if (
91-
_settings["ENGINE"] == "django.db.backends.sqlite3"
92-
and _settings["TEST"]["NAME"] is None
93-
):
90+
if _settings["ENGINE"] == "django.db.backends.sqlite3" and _settings["TEST"]["NAME"] is None:
9491
pytest.skip("Do not test db reuse since database does not support it")
9592

9693

@@ -107,9 +104,7 @@ def drop_database(db_suffix: str | None = None) -> None:
107104

108105
if db_engine == "postgresql":
109106
r = run_psql("postgres", "-c", f"DROP DATABASE {name}")
110-
assert "DROP DATABASE" in force_str(
111-
r.std_out
112-
) or "does not exist" in force_str(r.std_err)
107+
assert "DROP DATABASE" in force_str(r.std_out) or "does not exist" in force_str(r.std_err)
113108
return
114109

115110
if db_engine == "mysql":
@@ -136,8 +131,7 @@ def db_exists(db_suffix: str | None = None) -> bool:
136131
return r.status_code == 0
137132

138133
assert db_engine == "sqlite3", f"{db_engine} cannot be tested properly!"
139-
assert TEST_DB_NAME != ":memory:", (
140-
"sqlite in-memory database cannot be checked for existence!")
134+
assert TEST_DB_NAME != ":memory:", "sqlite in-memory database cannot be checked for existence!"
141135
return os.path.exists(name)
142136

143137

@@ -155,8 +149,7 @@ def mark_database() -> None:
155149
return
156150

157151
assert db_engine == "sqlite3", f"{db_engine} cannot be tested properly!"
158-
assert TEST_DB_NAME != ":memory:", (
159-
"sqlite in-memory database cannot be marked!")
152+
assert TEST_DB_NAME != ":memory:", "sqlite in-memory database cannot be marked!"
160153

161154
conn = sqlite3.connect(TEST_DB_NAME)
162155
try:
@@ -180,8 +173,7 @@ def mark_exists() -> bool:
180173
return r.status_code == 0
181174

182175
assert db_engine == "sqlite3", f"{db_engine} cannot be tested properly!"
183-
assert TEST_DB_NAME != ":memory:", (
184-
"sqlite in-memory database cannot be checked for mark!")
176+
assert TEST_DB_NAME != ":memory:", "sqlite in-memory database cannot be checked for mark!"
185177

186178
conn = sqlite3.connect(TEST_DB_NAME)
187179
try:

pytest_django_test/db_router.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class DbRouter:
22
def db_for_read(self, model, **hints):
3-
if model._meta.app_label == 'app' and model._meta.model_name == 'seconditem':
4-
return 'second'
3+
if model._meta.app_label == "app" and model._meta.model_name == "seconditem":
4+
return "second"
55
return None
66

77
def db_for_write(self, model, **hints):
8-
if model._meta.app_label == 'app' and model._meta.model_name == 'seconditem':
9-
return 'second'
8+
if model._meta.app_label == "app" and model._meta.model_name == "seconditem":
9+
return "second"
1010
return None
1111

1212
def allow_migrate(self, db, app_label, model_name=None, **hints):
13-
if app_label == 'app' and model_name == 'seconditem':
14-
return db == 'second'
13+
if app_label == "app" and model_name == "seconditem":
14+
return db == "second"

pytest_django_test/settings_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
}
2929
]
3030

31-
DATABASE_ROUTERS = ['pytest_django_test.db_router.DbRouter']
31+
DATABASE_ROUTERS = ["pytest_django_test.db_router.DbRouter"]
3232

3333
USE_TZ = True

pytest_django_test/urls_overridden.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44

55
urlpatterns = [
6-
path("overridden_url/", lambda r: HttpResponse("Overridden urlconf works!"))
6+
path("overridden_url/", lambda r: HttpResponse("Overridden urlconf works!")),
77
]

tests/conftest.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
def pytest_configure(config: pytest.Config) -> None:
2222
config.addinivalue_line(
23-
"markers", "django_project: options for the django_pytester fixture"
23+
"markers",
24+
"django_project: options for the django_pytester fixture",
2425
)
2526

2627

@@ -68,9 +69,8 @@ def django_pytester(
6869
db_settings["second"]["NAME"] = SECOND_DB_NAME
6970
db_settings["second"].setdefault("TEST", {})["NAME"] = SECOND_TEST_DB_NAME
7071

71-
test_settings = (
72-
dedent(
73-
"""
72+
test_settings = dedent(
73+
"""
7474
import django
7575
7676
# Pypy compatibility
@@ -109,13 +109,11 @@ def django_pytester(
109109
]
110110
111111
%(extra_settings)s
112-
"""
113-
)
114-
% {
115-
"db_settings": repr(db_settings),
116-
"extra_settings": dedent(options["extra_settings"]),
117-
}
118-
)
112+
"""
113+
) % {
114+
"db_settings": repr(db_settings),
115+
"extra_settings": dedent(options["extra_settings"]),
116+
}
119117

120118
if options["project_root"]:
121119
project_root = pytester.mkdir(options["project_root"])

tests/test_asserts.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ def _get_actual_assertions_names() -> list[str]:
1919

2020
from django.test import TestCase as DjangoTestCase
2121

22-
obj = DjangoTestCase('run')
22+
obj = DjangoTestCase("run")
2323

2424
def is_assert(func) -> bool:
25-
return func.startswith('assert') and '_' not in func
25+
return func.startswith("assert") and "_" not in func
2626

27-
base_methods = [name for name, member in
28-
inspect.getmembers(DefaultTestCase)
29-
if is_assert(name)]
27+
base_methods = [
28+
name for name, member in inspect.getmembers(DefaultTestCase) if is_assert(name)
29+
]
3030

31-
return [name for name, member in inspect.getmembers(obj)
32-
if is_assert(name) and name not in base_methods]
31+
return [
32+
name
33+
for name, member in inspect.getmembers(obj)
34+
if is_assert(name) and name not in base_methods
35+
]
3336

3437

3538
def test_django_asserts_available() -> None:
@@ -47,11 +50,11 @@ def test_sanity() -> None:
4750

4851
from pytest_django.asserts import assertContains, assertNumQueries
4952

50-
response = HttpResponse('My response')
53+
response = HttpResponse("My response")
5154

52-
assertContains(response, 'My response')
55+
assertContains(response, "My response")
5356
with pytest.raises(AssertionError):
54-
assertContains(response, 'Not my response')
57+
assertContains(response, "Not my response")
5558

5659
assertNumQueries(0, lambda: 1 + 1)
5760
with assertNumQueries(0):

0 commit comments

Comments
 (0)