def test_untyped():
    dct: UnannotatedBook = {
        'author': 'Chairman Mao',
        'book_id': 42,
        'title': 'Little Red Book',
        'publication_date': datetime(1973, 1, 1, 21, 52, 13),
        'keywords': ['Revolution', 'Communism'],
        'phrases': [
            'Revolutionary wars are inevitable in class society',
            'War is the continuation of politics'
        ],
        'age': 24,
        'pages': None
    }
    annotation = Annotated[UnannotatedBook, XMLEntity('Book')]
    config = SerializerConfig(pascalcase, snakecase)

    text = serialize(dct, annotation, config)
    roundtrip = deserialize(text, annotation, config)
    assert dct == roundtrip
예제 #2
0
def test_unannotated():
    """Test for deserializing a typed dict without JSON annotations"""

    obj: UnannotatedBook = {
        'author':
        'Chairman Mao',
        'book_id':
        42,
        'title':
        'Little Red Book',
        'publication_date':
        datetime(1973, 1, 1, 21, 52, 13),
        'keywords': ['Revolution', 'Communism'],
        'phrases': [
            'Revolutionary wars are inevitable in class society',
            'War is the continuation of politics'
        ],
        'age':
        24,
    }
    text = serialize(
        obj, UnannotatedBook,
        SerializerConfig(camelcase, snakecase, pretty_print=False))
    assert text == '{"bookId": 42, "title": "Little Red Book", "author": "Chairman Mao", "publicationDate": "1973-01-01T21:52:13.00Z", "keywords": ["Revolution", "Communism"], "phrases": ["Revolutionary wars are inevitable in class society", "War is the continuation of politics"], "age": 24}'
"""Tests for the untyped serializer"""

from datetime import timedelta, datetime

from stringcase import snakecase, camelcase

from jetblack_serialization.config import SerializerConfig
from jetblack_serialization.json.untyped_serializer import serialize

CONFIG = SerializerConfig(camelcase, snakecase)


def test_serialize():
    """Tests for serialize"""
    dct = {
        'str_arg': 'text',
        'int_arg': 42,
        'float_arg': 3.14,
        'date_arg': datetime(2019, 12, 31, 23, 59, 59),
        'duration_arg': timedelta(hours=1, minutes=7)
    }
    text = serialize(dct, CONFIG)
    assert text == '{"strArg": "text", "intArg": 42, "floatArg": 3.14, "dateArg": "2019-12-31T23:59:59.00Z", "durationArg": "PT1H7M"}'
try:
    from typing import TypedDict  # type:ignore
except:  # pylint: disable=bare-except
    from typing_extensions import TypedDict

from typing_extensions import Annotated  # type: ignore

from jetblack_serialization.config import SerializerConfig
from jetblack_serialization.xml.serializer import serialize
from jetblack_serialization.xml.annotations import (
    XMLEntity,
    XMLAttribute
)

CONFIG = SerializerConfig(pascalcase, snakecase)


class Book(TypedDict, total=False):
    book_id: Annotated[
        int,
        XMLAttribute("bookId")
    ]
    title: Annotated[
        str,
        XMLEntity("Title")
    ]
    author: Annotated[
        str,
        XMLEntity("Author")
    ]