Beispiel #1
0
def test_convert(models_generator: MetadataGenerator):
    data = {
        "dict_field": {},
        "another_dict_field": {
            "test_dict_field_a": 1,
            "test_dict_field_b": "a"
        },
        "another_dict_field_2": {
            "test_dict_field_a": 1
        },
        "another_dict_field_3": {
            "test_dict_field_a": 1,
            "test_dict_field_b": 2
        },
        "int_field": 1,
        "not": False
    }
    meta = models_generator._convert(data)
    assert meta == {
        "dict_field": DDict(Unknown),
        "another_dict_field": DDict(DUnion(int, StringLiteral({"a"}))),
        "another_dict_field_2": DDict(int),
        "another_dict_field_3": DDict(int),
        "int_field": int,
        "not": bool
    }
def test_hash_string():
    a = {'a': int}
    b = {'b': int}
    c = {'a': float}
    assert len(set(map(get_hash_string, (a, b, c)))) == 3

    union = DUnion(str, float)
    h1 = union.to_hash_string()
    union.replace(complex, index=0)
    h2 = union.to_hash_string()
    assert h1 != h2, f"{h1}, {h2}"
            not_: bool = attr.ib({field_meta('not')})
            one_day: int = attr.ib({field_meta('1day')})
            den_nedeli: str = attr.ib({field_meta('день_недели')})
            baz: Optional[List[List[str]]] = attr.ib(factory=list)
            bar: Optional[IntString] = attr.ib(default=None)
            asdfg: Optional[int] = attr.ib(default=None)
        """)
    },
    "converters": {
        "model": ("Test", {
            "a": int,
            "b": IntString,
            "c": DOptional(FloatString),
            "d": DList(DList(DList(IntString))),
            "e": DDict(IntString),
            "u": DUnion(DDict(IntString), DList(DList(IntString))),
        }),
        "generated": trim("""
        import attr
        from json_to_models.dynamic_typing import FloatString, IntString
        from json_to_models.models import ClassType
        from json_to_models.models.string_converters import convert_strings
        from typing import Dict, List, Optional, Union


        @attr.s
        @convert_strings(['b', 'c#O.S', 'd#L.L.L.S', 'e#D.S'], class_type=ClassType.Attrs)
        class Test:
            a: int = attr.ib()
            b: IntString = attr.ib()
            d: List[List[List[IntString]]] = attr.ib()
Beispiel #4
0
                                           FloatString, IntString, Null,
                                           StringLiteral, Unknown)
from json_to_models.generator import MetadataGenerator

# JSON data | MetaData
test_data = [
    pytest.param(1.0, float, id="float"),
    pytest.param(1, int, id="int"),
    pytest.param(True, bool, id="bool"),
    pytest.param("abc", StringLiteral({"abc"}), id="str"),
    pytest.param(None, Null, id="null"),
    pytest.param([], DList(Unknown), id="list_empty"),
    pytest.param([1], DList(int), id="list_single"),
    pytest.param([*range(100)], DList(int), id="list_single_type"),
    pytest.param([1, "a", 2, "c"],
                 DList(DUnion(int, StringLiteral({'a', 'c'}))),
                 id="list_multi"),
    pytest.param("1", IntString, id="int_str"),
    pytest.param("1.0", FloatString, id="float_str"),
    pytest.param("true", BooleanString, id="bool_str"),
    pytest.param({
        "test_dict_field_a": 1,
        "test_dict_field_b": "a"
    },
                 DDict(DUnion(int, StringLiteral({"a"}))),
                 id="simple_dict"),
    pytest.param({}, DDict(Unknown), id="empty_dict")
]

test_dict = {param.id: param.values[0] for param in test_data}
test_dict_meta = {param.id: param.values[1] for param in test_data}
Beispiel #5
0
import pytest

from json_to_models.dynamic_typing import DOptional, DUnion, FloatString, IntString
from json_to_models.generator import MetadataGenerator

# List of fields sets | result field set
test_data = [
    pytest.param(
        [{'a': int}, {'a': int, 'b': int}],
        {'a': int, 'b': DOptional(int)},
        id="optional_fields"
    ),
    pytest.param(
        [{'a': int}, {'a': float}],
        {'a': DUnion(int, float)},
        id="union"
    ),
    pytest.param(
        [{'a': DUnion(str, bool)}, {'a': float}],
        {'a': DUnion(str, bool, float)},
        id="merge_unions"
    ),
    pytest.param(
        [{'a': DUnion(str, bool)}, {'a': DUnion(int, float)}],
        {'a': DUnion(str, bool, int, float)},
        id="merge_unions2"
    ),
    pytest.param(
        [{'a': int}, {}, {'a': int}, {}],
        {'a': DOptional(int)},
def test_dunion_creation(value, expected):
    result = DUnion(*value)
    assert result == expected
from builtins import complex

import pytest

from json_to_models.dynamic_typing import DUnion, StringLiteral, get_hash_string

# *args | MetaData
test_dunion = [
    pytest.param([int, int], DUnion(int), id="unique_types"),
    pytest.param([int, DUnion(int)], DUnion(int), id="nested_union_&_merge"),
    pytest.param([str, DUnion(int, DUnion(float, complex))],
                 DUnion(int, float, complex, str),
                 id="complex_merge"),
    pytest.param([str, StringLiteral({'a'})],
                 DUnion(str),
                 id="str_literal_to_string"),
    pytest.param(
        [StringLiteral({'b'}), StringLiteral({'a'})],
        DUnion(StringLiteral({'a', 'b'})),
        id="str_literal_merge"),
    pytest.param([StringLiteral({str(i)}) for i in range(100)],
                 DUnion(str),
                 id="str_literal_too_much"),
]


@pytest.mark.parametrize("value,expected", test_dunion)
def test_dunion_creation(value, expected):
    result = DUnion(*value)
    assert result == expected
Beispiel #8
0
import pytest

from json_to_models.dynamic_typing import (BooleanString, DDict, DList, DOptional, DTuple, DUnion, FloatString,
                                           IntString, Null, Unknown)
from json_to_models.generator import MetadataGenerator

# MetaData | Optimized MetaData
test_data = [
    pytest.param(DUnion(int), int, id="single_DUnion"),
    pytest.param(
        {'a': DUnion({'b': int}, {'b': float})},
        {'a': {'b': float}},
        id="merge_nested_dicts"
    ),
    pytest.param(
        {'1': DUnion(
            {'a': DUnion({'b': int}, {'b': float})},
            {'a': DUnion({'b': float}, {'b': int})},
            {'a': Null},
        )},
        {'1': {'a': DOptional({'b': float})}},
        id="merge_nested_dicts"
    ),
    pytest.param(
        DUnion(FloatString, IntString),
        FloatString,
        id="str_types_merge"
    ),
    pytest.param(
        DUnion(FloatString, BooleanString),
        str,