-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simplest tests for admin rendering
- Loading branch information
Showing
5 changed files
with
81 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 5.0.1 on 2024-03-11 22:29 | ||
|
||
import django.core.serializers.json | ||
import django_pydantic_field.fields | ||
import tests.test_app.models | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("test_app", "0002_examplemodel"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="SampleModelWithRoot", | ||
fields=[ | ||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
( | ||
"root_field", | ||
django_pydantic_field.fields.PydanticSchemaField( | ||
config=None, | ||
default=list, | ||
encoder=django.core.serializers.json.DjangoJSONEncoder, | ||
schema=tests.test_app.models.RootSchema, | ||
), | ||
), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,45 @@ | ||
from django.middleware.csrf import CsrfViewMiddleware | ||
import pytest | ||
|
||
from django.contrib.auth.models import AnonymousUser | ||
from django.contrib.admin import site | ||
|
||
def test_model_admin_not_failing(): | ||
try: | ||
from .test_app import admin | ||
except: | ||
pytest.fail("Django admin handlers should not fail") | ||
from .test_app import admin, models | ||
|
||
pytestmark = [ | ||
pytest.mark.django_db(), | ||
] | ||
|
||
all_admins = { | ||
# This model cannot be instantiated without reasonable defaults | ||
# models.SampleModel: admin.SampleModelAdmin, | ||
# | ||
models.SampleForwardRefModel: admin.SampleForwardRefModelAdmin, | ||
models.SampleModelWithRoot: admin.SampleModelWithRootAdmin, | ||
models.ExampleModel: admin.ExampleModelAdmin | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def user(): | ||
return AnonymousUser() | ||
|
||
|
||
def patch_model_admin(admin_view, monkeypatch): | ||
monkeypatch.setattr(CsrfViewMiddleware, "process_view", lambda self, req, *a, **kw: self._accept(req)) | ||
monkeypatch.setattr(admin_view, "has_view_permission", lambda self, *args: True) | ||
monkeypatch.setattr(admin_view, "has_view_or_change_permission", lambda self, *args: True) | ||
monkeypatch.setattr(admin_view, "has_add_permission", lambda self, *args: True) | ||
monkeypatch.setattr(admin_view, "has_change_permission", lambda self, *args: True) | ||
monkeypatch.setattr(admin_view, "has_delete_permission", lambda self, *args: True) | ||
|
||
|
||
@pytest.mark.parametrize("model, admin_view", all_admins.items()) | ||
def test_model_admin_view_not_failing(model, admin_view, rf, user, monkeypatch): | ||
patch_model_admin(admin_view, monkeypatch) | ||
|
||
request = rf.get("/") | ||
request.user = user | ||
|
||
response = admin_view(model, site).changeform_view(request) | ||
assert response.status_code == 200 |