-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_issues.py
102 lines (79 loc) · 2.33 KB
/
test_issues.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Contains unit tests that reproduce previous issues. Whenever a new issue is
discovered, a failing test should be added to this file first before the issue
is fixed.
"""
from collections import namedtuple
import declxml as xml
from .helpers import strip_xml
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
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
def test_serialize_none_object_issue_7():
"""Attempts to serialize an object value that is None"""
class Athlete(object):
def __init__(self):
self.name = ''
self.age = 0
processor = xml.dictionary('race-result', [
xml.floating_point('time'),
xml.user_object('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