コード例 #1
0
def test_optional_type():
    class TestModel(middle.Model):
        maybe_name = middle.field(type=t.Optional[str])

    skel = translate(TestModel)

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == TestModel
    assert skel.name == "TestModel"

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == t.Union[str, middle.compat.NoneType]
    assert not skel.has_default_value
    assert skel.name == "maybe_name"
    assert skel.description is None
    assert skel.nullable
    assert skel.validator_data.type_check == (str, middle.compat.NoneType)
    assert skel.validator_data.rules is None
    assert skel.type_specific is None

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert skel.children is None
    assert skel.type == str
    assert not skel.has_default_value
    assert skel.validator_data is None
    assert skel.name is None
    assert skel.type_specific is None
    assert skel.description is None
    assert skel.nullable is False
コード例 #2
0
def test_simple_model():
    class TestModel(middle.Model):
        name = middle.field(type=str, description="The name", min_length=5)

    skel = translate(TestModel)

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == TestModel
    assert not skel.has_default_value
    assert skel.name == "TestModel"
    assert skel.description is None
    assert not skel.nullable
    assert skel.validator_data is None

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert skel.children is None
    assert skel.type == str
    assert not skel.has_default_value
    assert skel.name == "name"
    assert skel.description == "The name"
    assert not skel.nullable
    assert skel.validator_data.rules == {"min_length": 5}
    assert skel.validator_data.type_check == str
コード例 #3
0
def test_union_type_not_nullable():
    class TestModel(middle.Model):
        lots_of_values = middle.field(type=t.Union[str, int, float])

    skel = translate(TestModel)

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == TestModel
    assert skel.name == "TestModel"

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 3
    assert skel.type == t.Union[str, int, float]
    assert not skel.has_default_value
    assert skel.name == "lots_of_values"
    assert skel.description is None
    assert not skel.nullable
    assert skel.validator_data.type_check == (str, int, float)
    assert skel.validator_data.rules is None
    assert skel.type_specific == {"any_of": True}

    skel_str = skel.children[0]

    assert isinstance(skel_str, Skeleton)
    assert skel_str.children is None
    assert skel_str.type == str
    assert not skel_str.has_default_value
    assert skel_str.validator_data is None
    assert skel_str.name is None
    assert skel_str.type_specific is None
    assert skel_str.description is None
    assert skel_str.nullable is False

    skel_int = skel.children[1]

    assert isinstance(skel_int, Skeleton)
    assert skel_int.children is None
    assert skel_int.type == int
    assert not skel_int.has_default_value
    assert skel_int.validator_data is None
    assert skel_int.name is None
    assert skel_int.type_specific is None
    assert skel_int.description is None
    assert skel_int.nullable is False

    skel_float = skel.children[2]

    assert isinstance(skel_float, Skeleton)
    assert skel_float.children is None
    assert skel_float.type == float
    assert not skel_float.has_default_value
    assert skel_float.validator_data is None
    assert skel_float.name is None
    assert skel_float.type_specific is None
    assert skel_float.description is None
    assert skel_float.nullable is False
コード例 #4
0
def test_simple_model_with_typing():
    class TestModel(middle.Model):
        __description__ = "Test model for unit tests"
        name = middle.field(type=t.List[str],
                            description="List of names",
                            default=[])

    skel = translate(TestModel)

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == TestModel
    assert not skel.has_default_value
    assert skel.name == "TestModel"
    assert skel.description == "Test model for unit tests"
    assert not skel.nullable
    assert skel.validator_data is None

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == t.List[str]
    assert skel.has_default_value
    assert skel.default_value == []
    assert skel.name == "name"
    assert skel.description == "List of names"
    assert not skel.nullable
    assert skel.validator_data.rules is None
    assert skel.validator_data.type_check == list

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert skel.children is None
    assert skel.type == str
    assert not skel.has_default_value
    assert skel.validator_data is None
    assert skel.name is None
    assert skel.type_specific is None
    assert skel.description is None
    assert skel.nullable is False
コード例 #5
0
def test_enum_choices():
    @enum.unique
    class TestIntEnum(enum.IntEnum):
        TEST_1 = 1
        TEST_2 = 2
        TEST_3 = 3

    class TestModel(middle.Model):
        some_enum = middle.field(type=TestIntEnum)

    skel = translate(TestModel)

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == TestModel
    assert skel.name == "TestModel"

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == TestIntEnum
    assert skel.name == "some_enum"
    assert skel.description is None
    assert not skel.nullable
    assert skel.validator_data.rules is None
    assert skel.validator_data.type_check == TestIntEnum
    assert skel.type_specific is not None
    assert skel.type_specific == {"choices": [1, 2, 3]}

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert skel.children is None
    assert skel.type == int
    assert not skel.has_default_value
    assert skel.validator_data is None
    assert skel.name is None
    assert skel.type_specific is None
    assert skel.description is None
    assert skel.nullable is False
コード例 #6
0
def test_dict_type():
    class TestModel(middle.Model):
        options = middle.field(
            type=t.Dict[str, str],
            description="Options for TestModel",
            min_properties=1,
        )

    skel = translate(TestModel)

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == TestModel
    assert skel.name == "TestModel"

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 1
    assert skel.type == t.Dict[str, str]
    assert not skel.has_default_value
    assert skel.name == "options"
    assert skel.description == "Options for TestModel"
    assert not skel.nullable
    assert skel.validator_data.type_check == dict
    assert skel.validator_data.rules == {"min_properties": 1}
    assert skel.type_specific is None

    skel = skel.children[0]

    assert isinstance(skel, Skeleton)
    assert skel.children is None
    assert skel.type == str
    assert not skel.has_default_value
    assert skel.validator_data is None
    assert skel.name is None
    assert skel.type_specific is None
    assert skel.description is None
    assert skel.nullable is False
コード例 #7
0
def test_complex_model():

    skel = translate(Game)

    assert isinstance(skel, Skeleton)
    assert len(skel.children) == 10
    assert skel.type == Game
    assert not skel.has_default_value
    assert skel.name == "Game"
    assert skel.description == "An electronic game model"
    assert not skel.nullable
    assert skel.validator_data is None

    for c in skel.children:

        # ------------------------------------------------------------------- #
        # name field
        # ------------------------------------------------------------------- #
        if c.name == "name":
            assert isinstance(c, Skeleton)
            assert c.children is None
            assert c.type == str
            assert not c.has_default_value
            assert c.description == "The name of the game"
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.rules is None
            assert c.validator_data.type_check == str

        # ------------------------------------------------------------------- #
        # platform field
        # ------------------------------------------------------------------- #
        if c.name == "platform":
            assert isinstance(c, Skeleton)
            assert len(c.children) == 1
            assert c.type == PlatformEnum
            assert not c.has_default_value
            assert c.description == "Which platform it runs on"
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.rules is None
            assert c.validator_data.type_check == PlatformEnum
            assert c.type_specific is not None
            assert c.type_specific == {
                "choices": ["XBOX1", "PLAYSTATION4", "PC"]
            }
            # choices type
            assert isinstance(c.children[0], Skeleton)
            assert c.children[0].children is None
            assert c.children[0].type == str
            assert not c.children[0].has_default_value
            assert c.children[0].validator_data is None
            assert c.children[0].name is None
            assert c.children[0].type_specific is None
            assert c.children[0].description is None
            assert c.children[0].nullable is False

        # ------------------------------------------------------------------- #
        # score field
        # ------------------------------------------------------------------- #
        if c.name == "score":
            assert isinstance(c, Skeleton)
            assert c.children is None
            assert c.type == float
            assert not c.has_default_value
            assert c.description == "The average score of the game"
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.type_check == float
            assert c.validator_data.rules == {
                "minimum": 0,
                "maximum": 10,
                "multiple_of": 0.1,
            }

        # ------------------------------------------------------------------- #
        # resolution_tested field
        # ------------------------------------------------------------------- #
        if c.name == "resolution_tested":
            assert isinstance(c, Skeleton)
            assert c.children is None
            assert c.type == str
            assert not c.has_default_value
            assert c.description == "The resolution which the game was tested"
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.rules == {"pattern": "^\d+x\d+$"}
            assert c.validator_data.type_check == str

        # ------------------------------------------------------------------- #
        # genre field
        # ------------------------------------------------------------------- #
        if c.name == "genre":
            assert isinstance(c, Skeleton)
            assert len(c.children) == 1
            assert c.type == t.List[str]
            assert not c.has_default_value
            assert c.description == "One or more genres this game is part of"
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.type_check == list
            assert c.validator_data.rules == {
                "min_items": 1,
                "unique_items": True,
            }
            # inner list type
            assert isinstance(c.children[0], Skeleton)
            assert c.children[0].children is None
            assert c.children[0].type == str
            assert not c.children[0].has_default_value
            assert c.children[0].validator_data is None
            assert c.children[0].name is None
            assert c.children[0].type_specific is None
            assert c.children[0].description is None
            assert c.children[0].nullable is False

        # ------------------------------------------------------------------- #
        # rating field
        # ------------------------------------------------------------------- #
        if c.name == "rating":
            assert isinstance(c, Skeleton)
            assert len(c.children) == 1
            assert c.type == t.Dict[str, float]
            assert not c.has_default_value
            assert c.description == "Ratings given on specialized websites"
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.type_check == dict
            assert c.validator_data.rules == {"min_properties": 3}
            # inner dict type
            assert isinstance(c.children[0], Skeleton)
            assert c.children[0].children is None
            assert c.children[0].type == float
            assert not c.children[0].has_default_value
            assert c.children[0].validator_data is None
            assert c.children[0].name is None
            assert c.children[0].type_specific is None
            assert c.children[0].description is None
            assert c.children[0].nullable is False

        # ------------------------------------------------------------------- #
        # players field
        # ------------------------------------------------------------------- #
        if c.name == "players":
            assert isinstance(c, Skeleton)
            assert len(c.children) == 1
            assert c.type == t.Set[Player]
            assert not c.has_default_value
            assert (
                c.description == "Some of the notorious players of this game")
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.rules is None
            assert c.validator_data.type_check == set
            # inner set type
            ci = c.children[0]
            assert isinstance(ci, Skeleton)
            assert len(ci.children) == 2
            assert ci.type == Player
            assert not ci.has_default_value
            assert ci.validator_data is None
            assert ci.name == "Player"
            assert ci.type_specific is None
            assert ci.description is None
            assert ci.nullable is False
            # inner model (complex type)
            for cj in ci.children:
                if cj.name == "nickname":
                    assert isinstance(c, Skeleton)
                    assert cj.children is None
                    assert cj.type == str
                    assert not cj.has_default_value
                    assert (cj.description ==
                            "The nickname of the player over the internet")
                    assert not cj.nullable
                    assert cj.validator_data is not None
                    assert cj.validator_data.type_check == str
                    assert cj.validator_data.rules is None
                if cj.name == "youtube_channel":
                    assert isinstance(c, Skeleton)
                    assert cj.children is None
                    assert cj.type == str
                    assert cj.has_default_value
                    assert cj.default_value is None
                    assert (
                        cj.description == "The YouTube channel of the player")
                    assert not cj.nullable
                    assert cj.validator_data is not None
                    assert cj.validator_data.type_check == (
                        str,
                        middle.compat.NoneType,
                    )
                    assert cj.validator_data.rules is None

        # ------------------------------------------------------------------- #
        # language field
        # ------------------------------------------------------------------- #
        if c.name == "language":
            assert isinstance(c, Skeleton)
            assert len(c.children) == 1
            assert c.type == LanguageEnum
            assert not c.has_default_value
            assert c.description == "The main language of the game"
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.type_check == LanguageEnum
            assert c.validator_data.rules is None
            assert c.type_specific is not None
            assert c.type_specific == {"choices": [1, 2, 3, 4, 5]}
            # choices type
            assert isinstance(c.children[0], Skeleton)
            assert c.children[0].children is None
            assert c.children[0].type == int
            assert not c.children[0].has_default_value
            assert c.children[0].validator_data is None
            assert c.children[0].name is None
            assert c.children[0].type_specific is None
            assert c.children[0].description is None
            assert c.children[0].nullable is False

        # ------------------------------------------------------------------- #
        # awesome city field
        # ------------------------------------------------------------------- #
        if c.name == "awesome_city":
            assert isinstance(c, Skeleton)
            assert len(c.children) == 2
            assert c.type == City
            assert not c.has_default_value
            assert c.description == "One awesome city built"
            assert not c.nullable
            assert c.validator_data is None
            # inner model (complex type)
            for ci in c.children:
                if ci.name == "name":
                    assert isinstance(c, Skeleton)
                    assert ci.children is None
                    assert ci.type == str
                    assert not ci.has_default_value
                    assert ci.description == "The city name"
                    assert not ci.nullable
                    assert ci.validator_data is not None
                    assert ci.validator_data.type_check == str
                    assert ci.validator_data.rules is None
                if ci.name == "region":
                    assert isinstance(ci, Skeleton)
                    assert len(ci.children) == 1
                    assert ci.type == CityRegionEnum
                    assert ci.has_default_value
                    assert ci.default_value == CityRegionEnum.TEMPERATE
                    assert ci.description == "The region this city is located"
                    assert not ci.nullable
                    assert ci.validator_data is not None
                    assert ci.validator_data.type_check == CityRegionEnum
                    assert ci.validator_data.rules is None
                    assert ci.type_specific is not None
                    assert ci.type_specific == {
                        "choices": ["TROPICAL", "TEMPERATE", "BOREAL"]
                    }
                    # choices type
                    assert isinstance(ci.children[0], Skeleton)
                    assert ci.children[0].children is None
                    assert ci.children[0].type == str
                    assert not ci.children[0].has_default_value
                    assert ci.children[0].validator_data is None
                    assert ci.children[0].name is None
                    assert ci.children[0].type_specific is None
                    assert ci.children[0].description is None
                    assert ci.children[0].nullable is False

        # ------------------------------------------------------------------- #
        # remarkable_resources field
        # ------------------------------------------------------------------- #
        if c.name == "remarkable_resources":
            assert isinstance(c, Skeleton)
            assert len(c.children) == 2
            assert c.type == t.Union[Player, City]
            assert not c.has_default_value
            assert (c.description ==
                    "Some remarkable resources of this game over the internet")
            assert not c.nullable
            assert c.validator_data is not None
            assert c.validator_data.type_check == (Player, City)
            assert c.validator_data.rules is None
            # inner models (union of complex type)
            for ci in c.children:
                assert ci.type in (Player, City)
                assert isinstance(c, Skeleton)
                assert len(ci.children) > 0
                assert ci.name is not None
コード例 #8
0
def test_invalid_type_for_schema():
    class TestModel(middle.Model):
        name = middle.field(type=t.Tuple[str, int])

    with pytest.raises(InvalidType):
        translate(TestModel)
コード例 #9
0
def test_invalid_dict_type():
    class TestModel(middle.Model):
        options = middle.field(type=t.Dict[float, str])

    with pytest.raises(TypeError):
        translate(TestModel)