示例#1
0
def test_named_tuple_serialize():
    """Serialize a namedtuple value"""
    value = Author(name='Robert A. Heinlein',
                   books=[
                       Book(title='Starship Troopers', year_published=1959),
                       Book(title='Stranger in a Strange Land',
                            year_published=1961)
                   ])

    processor = xml.named_tuple('author', Author, [
        xml.string('name'),
        xml.array(xml.named_tuple('book', Book, [
            xml.string('title'),
            xml.integer('year-published', alias='year_published')
        ]),
                  alias='books')
    ])

    expected = strip_xml("""
    <author>
        <name>Robert A. Heinlein</name>
        <book>
            <title>Starship Troopers</title>
            <year-published>1959</year-published>
        </book>
        <book>
            <title>Stranger in a Strange Land</title>
            <year-published>1961</year-published>
        </book>
    </author>
    """)

    actual = xml.serialize_to_string(processor, value)

    assert expected == actual
示例#2
0
def test_parse_missing_non_required_namedtuple_issue_24():
    """Parse XML with a non-required namedtuple value."""
    Author = namedtuple('Author', [
        'name',
        'genre',
    ])
    Genre = namedtuple('Genre', [
        'name',
    ])

    processor = xml.named_tuple('author', Author, [
        xml.string('name'),
        xml.named_tuple('genre', Genre, [
            xml.string('name')
        ], required=False)
    ])

    author_xml = """
    <author>
        <name>Robert A. Heinlein</name>
    </author>
    """

    expected_value = Author(name='Robert A. Heinlein', genre=None)

    actual_value = xml.parse_from_string(processor, author_xml)

    assert expected_value == actual_value
示例#3
0
    def test_named_tuple_non_root(self):
        """Custom error message for namedtuple values."""
        processor = xml.dictionary('data', [
            xml.named_tuple('user',
                            _UserTuple, [
                                xml.string('name'),
                                xml.integer('age'),
                            ],
                            hooks=self._hooks)
        ])

        xml_string = strip_xml("""
        <data>
            <user>
                <name>Bob</name>
                <age>24</age>
            </user>
        </data>
        """)

        value = {'user': _UserTuple(name='Bob', age=24)}

        location = 'data/user'

        self._assert_error_message(processor, value, xml_string, location)
def test_named_tuple_transform():
    """Transform a named tuple value"""
    Person = namedtuple('Person', ['name', 'age'])

    xml_string = strip_xml("""
    <person>
        <name>John</name>
        <age>24</age>
    </person>
    """)

    value = {
        'name': 'John',
        'age': 24,
    }

    def _after_parse(_, tuple_value):
        return {
            'name': tuple_value.name,
            'age': tuple_value.age,
        }

    def _before_serialize(_, dict_value):
        return Person(name=dict_value['name'], age=dict_value['age'])

    processor = xml.named_tuple('person',
                                Person, [
                                    xml.string('name'),
                                    xml.integer('age'),
                                ],
                                hooks=xml.Hooks(
                                    after_parse=_after_parse,
                                    before_serialize=_before_serialize))

    _transform_test_case_run(processor, value, xml_string)
示例#5
0
def test_serialize_none_namedtuple_issue_7():
    """Tests attempting to serialize a named tuple value that is None"""
    Athlete = namedtuple('Athlete', [
        'name',
        'age',
    ])

    processor = xml.dictionary('race-result', [
        xml.floating_point('time'),
        xml.named_tuple('athlete', Athlete, [
            xml.string('name'),
            xml.integer('age'),
        ], required=False)
    ])

    value = {
        'time': 87.5,
        'athlete': None,
    }

    expected_xml = strip_xml("""
    <race-result>
        <time>87.5</time>
    </race-result>
    """)

    actual_xml = xml.serialize_to_string(processor, value)

    assert expected_xml == actual_xml
示例#6
0
    def _processor(self):
        def validate(state, value):
            if value.name == 'Bob' and value.age == 24:
                state.raise_error(_ValidationError)
            return value

        hooks = xml.Hooks(
            after_parse=validate,
            before_serialize=validate,
        )

        processor = xml.named_tuple(
            'user',
            _UserTuple,
            [xml.string('name'), xml.integer('age')],
            hooks=hooks)

        return processor
示例#7
0
def test_named_tuple_parse_missing_field():
    """Tests attempting to a parse a named tuple with fields missing from the processor"""
    xml_string = """
    <author>
        <name>Robert A. Heinlein</name>
        <book>
            <title>Starship Troopers</title>
            <year-published>1959</year-published>
        </book>
        <book>
            <title>Stranger in a Strange Land</title>
            <year-published>1961</year-published>
        </book>
    </author>
    """

    # User forgot to specify processor for namedtuple's books field which
    # should lead to an error when attempting to create the named tuple value.
    processor = xml.named_tuple('author', Author, [xml.string('name')])

    with pytest.raises(TypeError):
        xml.parse_from_string(processor, xml_string)