Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file field test case #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 46 additions & 1 deletion tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from tempfile import NamedTemporaryFile
from typing import List

import pytest
from pydantic import ConfigDict
from testapp.models import Attachment

from pydantic import ConfigDict
from djantic import ModelSchema


Expand Down Expand Up @@ -50,3 +51,47 @@ class AttachmentSchema(ModelSchema):
"description": attachment.description,
"image": attachment.image.name,
}


@pytest.mark.django_db
def test_model_validate():
class AttachmentSchema(ModelSchema):
model_config = ConfigDict(model=Attachment)

image_file = NamedTemporaryFile(suffix=".jpg")
attachment = Attachment.objects.create(
description="My image upload",
image=image_file.name,
)
raw_init_item = AttachmentSchema.model_validate(
attachment,
from_attributes=True,
)

wrapper_item = AttachmentSchema.from_django(attachment)
assert wrapper_item == raw_init_item


@pytest.mark.django_db
def test_nest_validate():
class AttachmentSchema(ModelSchema):
model_config = ConfigDict(model=Attachment)

class AttachmentListSchema(ModelSchema):
items: List[AttachmentSchema]

image_file = NamedTemporaryFile(suffix=".jpg")
attachment = Attachment.objects.create(
description="My image upload",
image=image_file.name,
)
raw_init_item = AttachmentSchema.model_validate(
attachment,
from_attributes=True,
)
AttachmentListSchema.model_validate(
{
"items": [raw_init_item],
},
from_attributes=True,
)
Loading