Skip to content

Commit

Permalink
Add simplest tests for admin rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
surenkov committed Mar 11, 2024
1 parent 0c89277 commit 22f9c85
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
6 changes: 4 additions & 2 deletions django_pydantic_field/v2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ def get_transform(self, lookup_name: str):
transform = SchemaKeyTransformAdapter(transform)
return transform

def get_default(self) -> types.ST:
def get_default(self) -> ty.Any:
default_value = super().get_default()
return self.adapter.validate_python(default_value)
if self.has_default():
return self.adapter.validate_python(default_value)
return default_value

def formfield(self, **kwargs):
field_kwargs = dict(
Expand Down
1 change: 1 addition & 0 deletions tests/settings/django_test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
SECRET_KEY = "1"
SITE_ID = 1
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = "/static/"

INSTALLED_APPS = [
"django.contrib.contenttypes",
Expand Down
31 changes: 31 additions & 0 deletions tests/test_app/migrations/0003_samplemodelwithroot.py
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,
),
),
],
),
]
7 changes: 3 additions & 4 deletions tests/test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ class ExampleModel(models.Model):


if PYDANTIC_V2:
class RootModel(pydantic.RootModel):
class RootSchema(pydantic.RootModel):
root: t.List[int]

else:
class RootModel(pydantic.BaseModel):
class RootSchema(pydantic.BaseModel):
__root__: t.List[int]


class SampleModelWithRoot(models.Model):
class Meta:
managed = False
root_field = SchemaField(schema=RootSchema, default=list)
47 changes: 42 additions & 5 deletions tests/test_model_admin.py
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

0 comments on commit 22f9c85

Please sign in to comment.