Example #1
0
    def test_model_with_listtype_of_modeltype(self):  # pylint: disable=no-self-use,invalid-name
        """
        Test that a model with a list type of models can correctly be
        """
        class Item(XMLModel):
            number = IntType()

        class TestModel(XMLModel):
            items = ListType(ModelType(Item))

        bad_data = dict(
            items=dict(number=1)
        )
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(
            items=[
                dict(number=1)
            ]
        )
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, TestModel)
        # Assert good data stays good
        assert actual == expected
Example #2
0
    def test_model_with_dicttype_of_listtype_of_modeltype(self):  # pylint: disable=no-self-use,invalid-name
        class Item(XMLModel):
            number = StringType()

        class TestModel(XMLModel):
            items = DictType(ListType(ModelType(Item)))

        bad_data = dict(
            items=dict(
                bars=dict(number=1)
            )
        )
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(
            items=dict(
                bars=[
                    dict(number=1)
                ]
            )
        )
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, Model)
        # Assert good data stays good
        assert actual == expected
Example #3
0
    def test_model_with_listtype_of_mixed_value(self):  # pylint: disable=no-self-use,invalid-name
        """
        Ensure a model with a list type that has a value ``[1, None]`` is handled correctly.
        """
        class TestModel(XMLModel):
            numbers = ListType(IntType())

        bad_data = dict(numbers=[1, None])
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(numbers=[1, None])
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, TestModel)
        # Assert good data stays good
        assert actual == expected
Example #4
0
    def test_model_with_listtype_of_none(self):  # pylint: disable=no-self-use,invalid-name
        """
        Ensure a model with a list type that has a value of None isn't turned into ``[None]``.
        """
        class TestModel(XMLModel):
            numbers = ListType(IntType())

        bad_data = dict(numbers=None)
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(numbers=None)
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, TestModel)
        # Assert good data stays good
        assert actual == expected
Example #5
0
    def test_model_with_listtype_of_inttype(self):  # pylint: disable=no-self-use,invalid-name
        """
        Ensure a model with a list type can be handled.
        """
        class TestModel(XMLModel):
            numbers = ListType(IntType())

        bad_data = dict(numbers=1)
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(numbers=[1])
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, TestModel)
        # Assert good data stays good
        assert actual == expected
Example #6
0
    def test_model_with_listtype_of_modeltype_with_listtype_of_modeltype(self):  # pylint: disable=no-self-use,invalid-name
        """
        Test that a model with a list type of models are correctly converted.

        This test also tests the serialized_name functionality of schematics.
        """
        class Item(XMLModel):
            number = IntType()

        class Package(XMLModel):
            items = ListType(ModelType(Item), serialized_name='contents')

        class TestModel(XMLModel):
            packages = ListType(ModelType(Package), serialized_name='pkg')

        bad_data = dict(
            pkg=dict(
                contents=dict(number=1)
            )
        )
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(
            pkg=[
                dict(
                    contents=[
                        dict(number=1)
                    ]
                )
            ]
        )
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, TestModel)
        # Assert good data stays good
        assert actual == expected

        # Assert data is traversed even if the parent is OK.
        bad_data = dict(
            pkg=[
                dict(
                    contents=dict(number=1)
                )
            ]
        )
        actual = ensure_lists_in_model(bad_data, TestModel)
        assert actual == expected
Example #7
0
    def test_poly_model_type_raises(self):  # pylint: disable=no-self-use,invalid-name
        """
        PolyModelType is not implemented yet.
        """
        class Package(XMLModel):
            title = StringType()

        class Item(XMLModel):
            number = IntType()

        class TestModel(XMLModel):
            items = ListType(PolyModelType([Item, Package]))

        bad_data = dict(
            items=dict(number=1)
        )
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(
            items=[
                dict(number=1)
            ]
        )
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, TestModel)
        # Assert good data stays good
        assert actual == expected

        bad_data = dict(
            items=dict(title='Great products')
        )
        actual = ensure_lists_in_model(bad_data, TestModel)
        expected = dict(
            items=[
                dict(title='Great products')
            ]
        )
        # Assert bad data can be turned good
        assert actual == expected

        actual = ensure_lists_in_model(expected, TestModel)
        # Assert good data stays good
        assert actual == expected