Skip to content

Commit

Permalink
add to inheritance usage test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ksauder committed Sep 21, 2024
1 parent a0d6e2d commit 74a918c
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions tests/test_orm_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import pytest
from django.db import models
from pydantic import ValidationError
from pydantic import BaseModel, ValidationError

from ninja import ModelSchema
from ninja import ModelSchema, Schema
from ninja.errors import ConfigError


Expand Down Expand Up @@ -245,19 +245,28 @@ class Meta:

def test_desired_inheritance():
class Item(models.Model):
id = models.PositiveIntegerField
id = models.PositiveIntegerField(primary_key=True)
slug = models.CharField()

class Meta:
app_label = "tests"

class ProjectModelSchema(ModelSchema):
class ProjectBaseSchema(Schema):
# add any project wide Schema/pydantic configs
_omissible_serialize = (
"serializer_func" # model_serializer(mode="wrap")(_omissible_serialize)
)

class ProjectBaseModelSchema(ModelSchema, ProjectBaseSchema):
_pydantic_config = "config"

class ResourceModelSchema(ProjectModelSchema):
class Meta:
primary_key_optional = False

class ResourceModelSchema(ProjectBaseModelSchema):
field1: str

class Meta:
class Meta(ProjectBaseModelSchema.Meta):
model = Item
fields = ["id"]

Expand All @@ -268,25 +277,23 @@ class Meta(ResourceModelSchema.Meta):
model = Item
fields = ["id", "slug"]

assert issubclass(ItemModelSchema, BaseModel)
assert ItemModelSchema.Meta.primary_key_optional is False

i = ItemModelSchema(id=1, slug="slug", field1="1", field2="2")

assert i._pydantic_config == "config"
assert i._omissible_serialize == "serializer_func"
assert i.model_dump_json() == '{"field1":"1","id":1,"field2":"2","slug":"slug"}'
assert i.model_json_schema() == {
"properties": {
"field1": {
"title": "Field1",
"type": "string",
},
"id": {
"anyOf": [
{
"type": "integer",
},
{
"type": "null",
},
],
"default": None,
"title": "ID",
"type": "integer",
"title": "Id",
},
"field2": {
"title": "Field2",
Expand All @@ -299,6 +306,7 @@ class Meta(ResourceModelSchema.Meta):
},
"required": [
"field1",
"id",
"field2",
"slug",
],
Expand Down

0 comments on commit 74a918c

Please sign in to comment.