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 }
@attr.s class Test: foo: int = attr.ib() bar: int = attr.ib({field_meta('Bar')}) baz: float = attr.ib() """) }, "complex": { "model": ("Test", { "foo": int, "baz": DOptional(DList(DList(str))), "bar": DOptional(IntString), "qwerty": FloatString, "asdfg": DOptional(int), "dict": DDict(int), "ID": int, "not": bool, "1day": int, "день_недели": str, }), "fields_data": { "foo": { "name": "foo", "type": "int", "body": "attr.ib()" }, "baz": { "name": "baz", "type": "Optional[List[List[str]]]", "body": "attr.ib(factory=list)"
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} test_dict_nested = {"b": {"d": test_dict}} test_dict_nested_meta = {"b": {"d": test_dict_meta}} test_data += [ pytest.param(test_dict, test_dict_meta, id="flat_dict"), pytest.param(test_dict_nested, test_dict_nested_meta, id="dict"), ]
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)) ] 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} test_dict_nested = {"b": {"d": test_dict}} test_dict_nested_meta = {"b": {"d": test_dict_meta}} test_data += [ pytest.param(test_dict, test_dict_meta, id="flat_dict"), pytest.param(test_dict_nested, test_dict_nested_meta, id="dict"), ]
] }, "generated": trim(""" class Test: foo: int bar: int baz: float """) }, "complex": { "model": ("Test", { "foo": int, "baz": DOptional(DList(DList(str))), "bar": IntString, "d": DDict(Unknown) }), "fields_data": { "foo": { "name": "foo", "type": "int" }, "baz": { "name": "baz", "type": "Optional[List[List[str]]]" }, "bar": { "name": "bar", "type": "IntString" }, "d": {
DList(DUnion(str, int, FloatString, IntString)), DList(DUnion(str, int)), id="union_of_str_int_FloatString" ), pytest.param( DOptional(DUnion(DOptional(str), str)), DOptional(str), id="optional_union_nested" ), pytest.param( DUnion(Null, str, Null), DOptional(str), id="optional_str" ), pytest.param( DUnion(DDict(str), DDict(str), DDict(str)), DDict(str), id="dict_union" ), pytest.param( DUnion(DDict(str), DDict(int), DDict(str)), DDict(DUnion(str, int)), id="dict_union_2" ), ] @pytest.mark.parametrize("value,expected", test_data) def test_optimize_type(models_generator: MetadataGenerator, value, expected): result = models_generator.optimize_type(value) assert result == expected