Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdandm committed Nov 18, 2018
1 parent 4cf6697 commit 26970fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@pytest.fixture
def models_generator():
return MetadataGenerator()
return MetadataGenerator(dict_keys_regex=[r"^test_dict_field_\w+$"], dict_keys_fields=["dict_field"])


@pytest.fixture
Expand Down
13 changes: 10 additions & 3 deletions test/test_code_generation/test_attrs_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from json_to_models.dynamic_typing import (DList, DOptional, FloatString, IntString, ModelMeta, compile_imports)
from json_to_models.dynamic_typing import (DDict, DList, DOptional, FloatString, IntString, ModelMeta, compile_imports)
from json_to_models.models import sort_fields
from json_to_models.models.attr import AttrsModelCodeGenerator, METADATA_FIELD_NAME, sort_kwargs
from json_to_models.models.base import generate_code
Expand Down Expand Up @@ -87,7 +87,8 @@ class Test:
"baz": DOptional(DList(DList(str))),
"bar": DOptional(IntString),
"qwerty": FloatString,
"asdfg": DOptional(int)
"asdfg": DOptional(int),
"dict": DDict(int)
}),
"fields_data": {
"foo": {
Expand All @@ -114,19 +115,25 @@ class Test:
"name": "asdfg",
"type": "Optional[int]",
"body": f"attr.ib(default=None, {field_meta('asdfg')})"
},
"dict": {
"name": "dict",
"type": "Dict[str, int]",
"body": f"attr.ib({field_meta('dict')})"
}
},
"generated": trim(f"""
import attr
from attr.converter import optional
from json_to_models.dynamic_typing import FloatString, IntString
from typing import List, Optional
from typing import Dict, List, Optional
@attr.s
class Test:
foo: int = attr.ib({field_meta('foo')})
qwerty: FloatString = attr.ib(converter=FloatString, {field_meta('qwerty')})
dict: Dict[str, int] = attr.ib({field_meta('dict')})
baz: Optional[List[List[str]]] = attr.ib(factory=list, {field_meta('baz')})
bar: Optional[IntString] = attr.ib(default=None, converter=optional(IntString), {field_meta('bar')})
asdfg: Optional[int] = attr.ib(default=None, {field_meta('asdfg')})
Expand Down
23 changes: 22 additions & 1 deletion test/test_generator/test_detect_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from json_to_models.dynamic_typing import BooleanString, DList, DUnion, FloatString, IntString, NoneType, Unknown
from json_to_models.dynamic_typing import BooleanString, DDict, DList, DUnion, FloatString, IntString, NoneType, Unknown
from json_to_models.generator import MetadataGenerator

# JSON data | MetaData
Expand All @@ -17,6 +17,7 @@
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, str)), id="dict")
]

test_dict = {param.id: param.values[0] for param in test_data}
Expand All @@ -34,3 +35,23 @@
@pytest.mark.parametrize("value,expected", test_data)
def test_detect_type(models_generator: MetadataGenerator, value, expected):
assert models_generator._detect_type(value) == expected


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, str)),
"another_dict_field_2": DDict(int),
"another_dict_field_3": DDict(int),
"int_field": int,
"not_": bool
}

0 comments on commit 26970fc

Please sign in to comment.