Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ ignore = [
"S101", # Use of `assert` detected

# TODO - need to fix these
"ARG001", # Unused function argument
"ARG002", # Unused method argument
"C901", # .. is too complex
"COM812", # Trailing comma missing
"E501", # Line too long
Expand Down
18 changes: 9 additions & 9 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ def django_db_modify_db_settings_xdist_suffix(request: pytest.FixtureRequest) ->

@pytest.fixture(scope="session")
def django_db_modify_db_settings_parallel_suffix(
django_db_modify_db_settings_tox_suffix: None,
django_db_modify_db_settings_xdist_suffix: None,
django_db_modify_db_settings_tox_suffix: None, # noqa: ARG001
django_db_modify_db_settings_xdist_suffix: None, # noqa: ARG001
) -> None:
skip_if_no_django()


@pytest.fixture(scope="session")
def django_db_modify_db_settings(
django_db_modify_db_settings_parallel_suffix: None,
django_db_modify_db_settings_parallel_suffix: None, # noqa: ARG001
) -> None:
"""Modify db settings just before the databases are configured."""
skip_if_no_django()
Expand Down Expand Up @@ -162,12 +162,12 @@ def _get_databases_for_setup(
@pytest.fixture(scope="session")
def django_db_setup(
request: pytest.FixtureRequest,
django_test_environment: None,
django_test_environment: None, # noqa: ARG001
django_db_blocker: DjangoDbBlocker,
django_db_use_migrations: bool,
django_db_keepdb: bool,
django_db_createdb: bool,
django_db_modify_db_settings: None,
django_db_modify_db_settings: None, # noqa: ARG001
) -> Generator[None, None, None]:
"""Top level fixture to ensure test databases are available"""
from django.test.utils import setup_databases, teardown_databases
Expand Down Expand Up @@ -206,7 +206,7 @@ def django_db_setup(
@pytest.fixture
def _django_db_helper(
request: pytest.FixtureRequest,
django_db_setup: None,
django_db_setup: None, # noqa: ARG001
django_db_blocker: DjangoDbBlocker,
) -> Generator[None, None, None]:
if is_django_unittest(request):
Expand Down Expand Up @@ -458,7 +458,7 @@ def async_client() -> django.test.AsyncClient:


@pytest.fixture
def django_user_model(db: None) -> _UserModel:
def django_user_model(db: None) -> _UserModel: # noqa: ARG001
"""The class of Django's user model."""
from django.contrib.auth import get_user_model

Expand All @@ -474,7 +474,7 @@ def django_username_field(django_user_model: _UserModel) -> str:

@pytest.fixture
def admin_user(
db: None,
db: None, # noqa: ARG001
django_user_model: _User,
django_username_field: str,
) -> _User:
Expand Down Expand Up @@ -505,7 +505,7 @@ def admin_user(

@pytest.fixture
def admin_client(
db: None,
db: None, # noqa: ARG001
admin_user: _User,
) -> django.test.Client:
"""A Django test client logged in as an admin user."""
Expand Down
4 changes: 2 additions & 2 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def _dj_autoclear_mailbox() -> None:

@pytest.fixture
def mailoutbox(
django_mail_patch_dns: None,
django_mail_patch_dns: None, # noqa: ARG001
_dj_autoclear_mailbox: None,
) -> list[django.core.mail.EmailMessage] | None:
"""A clean email outbox to which Django-generated emails are sent."""
Expand Down Expand Up @@ -833,7 +833,7 @@ def _dj_db_wrapper(self) -> django.db.backends.base.base.BaseDatabaseWrapper:
def _save_active_wrapper(self) -> None:
self._history.append(self._dj_db_wrapper.ensure_connection)

def _blocking_wrapper(*args: Any, **kwargs: Any) -> NoReturn:
def _blocking_wrapper(*args: Any, **kwargs: Any) -> NoReturn: # noqa: ARG002
__tracebackhide__ = True
raise RuntimeError(
"Database access not allowed, "
Expand Down
8 changes: 6 additions & 2 deletions pytest_django/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(
verbosity: int = 1,
failfast: bool = False,
keepdb: bool = False,
**kwargs: Any,
**kwargs: Any, # noqa: ARG002
) -> None:
self.verbosity = verbosity
self.failfast = failfast
Expand All @@ -24,7 +24,11 @@ def add_arguments(cls, parser: ArgumentParser) -> None:
"--keepdb", action="store_true", help="Preserves the test DB between runs."
)

def run_tests(self, test_labels: Iterable[str], **kwargs: Any) -> int:
def run_tests(
self,
test_labels: Iterable[str],
**kwargs: Any, # noqa: ARG002
) -> int:
"""Run pytest and return the exitcode.

It translates some of Django's test command option to pytest's.
Expand Down
2 changes: 1 addition & 1 deletion pytest_django_test/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def admin_required_view(request: HttpRequest) -> HttpResponse:
return HttpResponse(Template("You are an admin").render(Context()))


def item_count(request: HttpRequest) -> HttpResponse:
def item_count(request: HttpRequest) -> HttpResponse: # noqa: ARG001
return HttpResponse(f"Item count: {Item.objects.count()}")
6 changes: 3 additions & 3 deletions pytest_django_test/db_router.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class DbRouter:
def db_for_read(self, model, **hints):
def db_for_read(self, model, **hints): # noqa: ARG002
if model._meta.app_label == "app" and model._meta.model_name == "seconditem":
return "second"
return None

def db_for_write(self, model, **hints):
def db_for_write(self, model, **hints): # noqa: ARG002
if model._meta.app_label == "app" and model._meta.model_name == "seconditem":
return "second"
return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
def allow_migrate(self, db, app_label, model_name=None, **hints): # noqa: ARG002
if app_label == "app" and model_name == "seconditem":
return db == "second"
56 changes: 30 additions & 26 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_noaccess_fixture(noaccess: None) -> None:


@pytest.fixture
def non_zero_sequences_counter(db: None) -> None:
def non_zero_sequences_counter(db: None) -> None: # noqa: ARG001
"""Ensure that the db's internal sequence counter is > 1.

This is used to test the `reset_sequences` feature.
Expand Down Expand Up @@ -73,28 +73,28 @@ def all_dbs(self, request: pytest.FixtureRequest) -> None:
else:
raise AssertionError() # pragma: no cover

def test_access(self, all_dbs: None) -> None:
def test_access(self, all_dbs: None) -> None: # noqa: ARG002
Item.objects.create(name="spam")

def test_clean_db(self, all_dbs: None) -> None:
def test_clean_db(self, all_dbs: None) -> None: # noqa: ARG002
# Relies on the order: test_access created an object
assert Item.objects.count() == 0

def test_transactions_disabled(self, db: None) -> None:
def test_transactions_disabled(self, db: None) -> None: # noqa: ARG002
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert connection.in_atomic_block

def test_transactions_enabled(self, transactional_db: None) -> None:
def test_transactions_enabled(self, transactional_db: None) -> None: # noqa: ARG002
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert not connection.in_atomic_block

def test_transactions_enabled_via_reset_seq(
self,
django_db_reset_sequences: None,
django_db_reset_sequences: None, # noqa: ARG002
) -> None:
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")
Expand All @@ -103,9 +103,9 @@ def test_transactions_enabled_via_reset_seq(

def test_django_db_reset_sequences_fixture(
self,
db: None,
db: None, # noqa: ARG002
django_pytester: DjangoPytester,
non_zero_sequences_counter: None,
non_zero_sequences_counter: None, # noqa: ARG002
) -> None:
if not db_supports_reset_sequences():
pytest.skip(
Expand All @@ -130,7 +130,11 @@ def test_django_db_reset_sequences_requested(
result = django_pytester.runpytest_subprocess("-v", "--reuse-db")
result.stdout.fnmatch_lines(["*test_django_db_reset_sequences_requested PASSED*"])

def test_serialized_rollback(self, db: None, django_pytester: DjangoPytester) -> None:
def test_serialized_rollback(
self,
db: None, # noqa: ARG002
django_pytester: DjangoPytester,
) -> None:
django_pytester.create_app_file(
"""
from django.db import migrations
Expand Down Expand Up @@ -176,25 +180,25 @@ def test_serialized_rollback_3():
assert result.ret == 0

@pytest.fixture
def mydb(self, all_dbs: None) -> None:
def mydb(self, all_dbs: None) -> None: # noqa: ARG002
# This fixture must be able to access the database
Item.objects.create(name="spam")

def test_mydb(self, mydb: None) -> None:
def test_mydb(self, mydb: None) -> None: # noqa: ARG002
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

# Check the fixture had access to the db
item = Item.objects.get(name="spam")
assert item

def test_fixture_clean(self, all_dbs: None) -> None:
def test_fixture_clean(self, all_dbs: None) -> None: # noqa: ARG002
# Relies on the order: test_mydb created an object
# See https://github.com/pytest-dev/pytest-django/issues/17
assert Item.objects.count() == 0

@pytest.fixture
def fin(self, request: pytest.FixtureRequest, all_dbs: None) -> Generator[None, None, None]:
def fin(self, all_dbs: None) -> Generator[None, None, None]: # noqa: ARG002
# This finalizer must be able to access the database
yield
Item.objects.create(name="spam")
Expand All @@ -203,27 +207,27 @@ def test_fin(self, fin: None) -> None:
# Check finalizer has db access (teardown will fail if not)
pass

def test_durable_transactions(self, all_dbs: None) -> None:
def test_durable_transactions(self, all_dbs: None) -> None: # noqa: ARG002
with transaction.atomic(durable=True):
item = Item.objects.create(name="foo")
assert Item.objects.get() == item


class TestDatabaseFixturesAllOrder:
@pytest.fixture
def fixture_with_db(self, db: None) -> None:
def fixture_with_db(self, db: None) -> None: # noqa: ARG002
Item.objects.create(name="spam")

@pytest.fixture
def fixture_with_transdb(self, transactional_db: None) -> None:
def fixture_with_transdb(self, transactional_db: None) -> None: # noqa: ARG002
Item.objects.create(name="spam")

@pytest.fixture
def fixture_with_reset_sequences(self, django_db_reset_sequences: None) -> None:
def fixture_with_reset_sequences(self, django_db_reset_sequences: None) -> None: # noqa: ARG002
Item.objects.create(name="spam")

@pytest.fixture
def fixture_with_serialized_rollback(self, django_db_serialized_rollback: None) -> None:
def fixture_with_serialized_rollback(self, django_db_serialized_rollback: None) -> None: # noqa: ARG002
Item.objects.create(name="ham")

def test_trans(self, fixture_with_transdb: None) -> None:
Expand Down Expand Up @@ -311,35 +315,35 @@ def test_databases(self, request: pytest.FixtureRequest) -> None:
assert marker.kwargs["databases"] == ["default", "replica", "second"]

@pytest.mark.django_db(databases=["second"])
def test_second_database(self, request: pytest.FixtureRequest) -> None:
def test_second_database(self) -> None:
SecondItem.objects.create(name="spam")

@pytest.mark.django_db(databases=["default"])
def test_not_allowed_database(self, request: pytest.FixtureRequest) -> None:
def test_not_allowed_database(self) -> None:
with pytest.raises(AssertionError, match="not allowed"):
SecondItem.objects.count()
with pytest.raises(AssertionError, match="not allowed"):
SecondItem.objects.create(name="spam")

@pytest.mark.django_db(databases=["replica"])
def test_replica_database(self, request: pytest.FixtureRequest) -> None:
def test_replica_database(self) -> None:
Item.objects.using("replica").count()

@pytest.mark.django_db(databases=["replica"])
def test_replica_database_not_allowed(self, request: pytest.FixtureRequest) -> None:
def test_replica_database_not_allowed(self) -> None:
with pytest.raises(AssertionError, match="not allowed"):
Item.objects.count()

@pytest.mark.django_db(transaction=True, databases=["default", "replica"])
def test_replica_mirrors_default_database(self, request: pytest.FixtureRequest) -> None:
def test_replica_mirrors_default_database(self) -> None:
Item.objects.create(name="spam")
Item.objects.using("replica").create(name="spam")

assert Item.objects.count() == 2
assert Item.objects.using("replica").count() == 2

@pytest.mark.django_db(databases="__all__")
def test_all_databases(self, request: pytest.FixtureRequest) -> None:
def test_all_databases(self) -> None:
Item.objects.count()
Item.objects.create(name="spam")
SecondItem.objects.count()
Expand Down Expand Up @@ -369,15 +373,15 @@ def test_available_apps_enabled(self, request: pytest.FixtureRequest) -> None:
assert marker.kwargs["available_apps"] == ["pytest_django_test.app"]

@pytest.mark.django_db
def test_available_apps_default(self, request: pytest.FixtureRequest) -> None:
def test_available_apps_default(self) -> None:
from django.apps import apps
from django.conf import settings

for app in settings.INSTALLED_APPS:
assert apps.is_installed(app)

@pytest.mark.django_db(available_apps=["pytest_django_test.app"])
def test_available_apps_limited(self, request: pytest.FixtureRequest) -> None:
def test_available_apps_limited(self) -> None:
from django.apps import apps
from django.conf import settings

Expand Down
Loading