File: test_files.py

package info (click to toggle)
python-djantic 0.7.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: python: 2,599; sh: 19; makefile: 14
file content (47 lines) | stat: -rw-r--r-- 1,333 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from tempfile import NamedTemporaryFile

import pytest
from testapp.models import Attachment

from djantic import ModelSchema


@pytest.mark.django_db
def test_image_field_schema():
    class AttachmentSchema(ModelSchema):
        class Config:
            model = Attachment

    image_file = NamedTemporaryFile(suffix=".jpg")
    attachment = Attachment.objects.create(
        description="My image upload",
        image=image_file.name,
    )

    assert AttachmentSchema.schema() == {
        "title": "AttachmentSchema",
        "description": "Attachment(id, description, image)",
        "type": "object",
        "properties": {
            "id": {"title": "Id", "description": "id", "type": "integer"},
            "description": {
                "title": "Description",
                "description": "description",
                "maxLength": 255,
                "type": "string",
            },
            "image": {
                "title": "Image",
                "description": "image",
                "maxLength": 100,
                "type": "string",
            },
        },
        "required": ["description"],
    }

    assert AttachmentSchema.from_django(attachment).dict() == {
        "id": attachment.id,
        "description": attachment.description,
        "image": attachment.image.name,
    }