Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
X
xml_marshaller
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
xml_marshaller
Commits
7e3e7540
Commit
7e3e7540
authored
Oct 28, 2010
by
Nicolas Delaby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create separate test module
parent
25fe6ba2
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
162 additions
and
1 deletion
+162
-1
src/xml_marshaller/__init__.py
src/xml_marshaller/__init__.py
+9
-1
src/xml_marshaller/tests/__init__.py
src/xml_marshaller/tests/__init__.py
+1
-0
src/xml_marshaller/tests/test_xml_marshaller.py
src/xml_marshaller/tests/test_xml_marshaller.py
+152
-0
No files found.
src/xml_marshaller/__init__.py
View file @
7e3e7540
#
# -*- coding: utf-8 -*-
# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
try
:
__import__
(
'pkg_resources'
).
declare_namespace
(
__name__
)
except
ImportError
:
from
pkgutil
import
extend_path
__path__
=
extend_path
(
__path__
,
__name__
)
src/xml_marshaller/tests/__init__.py
0 → 100644
View file @
7e3e7540
# -*- coding: utf-8 -*-
src/xml_marshaller/tests/test_xml_marshaller.py
0 → 100755
View file @
7e3e7540
# -*- coding: utf-8 -*-
import
unittest
import
xml_marshaller
from
xml_marshaller.xml_marshaller
import
load
,
loads
,
dump
,
dumps
,
dump_ns
,
\
dumps_ns
from
StringIO
import
StringIO
from
lxml
import
etree
import
pkg_resources
class
_A
:
def
__repr__
(
self
):
return
'<A instance>'
class
_B
(
object
):
def
__repr__
(
self
):
return
'<B instance>'
class
_C
(
object
):
def
__init__
(
self
,
attr1
,
attr2
=
None
):
self
.
attr1
=
attr1
self
.
attr2
=
attr2
def
__getinitargs__
(
self
):
return
(
self
.
attr1
,
)
def
__repr__
(
self
):
return
'<C instance>'
xsd_resource_file
=
pkg_resources
.
resource_stream
(
xml_marshaller
.
xml_marshaller
.
__name__
,
"doc/xml_marshaller.xsd"
)
xmlschema_doc
=
etree
.
parse
(
xsd_resource_file
)
xmlschema
=
etree
.
XMLSchema
(
xmlschema_doc
)
class
TestXMLMarhsaller
(
unittest
.
TestCase
):
"""
"""
use_namespace_uri
=
False
def
_checkXML
(
self
,
xml_data
):
"""Check generated XML against XSD
"""
if
self
.
use_namespace_uri
:
# Disabled for xml with namespaces.
# Because URI is not predictable
return
if
not
isinstance
(
xml_data
,
str
):
xml_string
=
xml_data
.
read
()
xml_data
.
seek
(
0
)
else
:
xml_string
=
xml_data
document_tree
=
etree
.
fromstring
(
xml_string
)
is_validated
=
xmlschema
.
validate
(
document_tree
)
log
=
xmlschema
.
error_log
error
=
log
.
last_error
self
.
assertTrue
(
is_validated
,
error
)
def
test_string_serialisation
(
self
):
"""
"""
data_list
=
[
None
,
1
,
pow
(
2
,
123L
),
19.72
,
1
+
5j
,
"here is a string & a <fake tag>"
,
(
1
,
2
,
3
),
[
'alpha'
,
'beta'
,
'gamma'
,
[
None
,
1
,
pow
(
2
,
123L
),
19.72
,
1
+
5j
,
"& a <fake tag>"
]],
{
'key'
:
'value'
,
1
:
2
},
'éàù^ç'
.
decode
(
'utf-8'
),
set
((
'a'
,
1
,)),
True
,
False
,
]
if
self
.
use_namespace_uri
:
dumper
=
dumps_ns
else
:
dumper
=
dumps
for
item
in
data_list
:
dumped
=
dumper
(
item
)
self
.
_checkXML
(
dumped
)
self
.
assertEquals
(
item
,
loads
(
dumped
))
def
test_file_serialisation
(
self
):
"""
"""
data_list
=
[
None
,
1
,
pow
(
2
,
123L
),
19.72
,
1
+
5j
,
"here is a string & a <fake tag>"
,
(
1
,
2
,
3
),
[
'alpha'
,
'beta'
,
'gamma'
,
[
None
,
1
,
pow
(
2
,
123L
),
19.72
,
1
+
5j
,
"& a <fake tag>"
]],
{
'key'
:
'value'
,
1
:
2
},
'éàù^ç'
.
decode
(
'utf-8'
),
set
((
'a'
,
1
,)),
True
,
False
,
]
if
self
.
use_namespace_uri
:
dumper
=
dump_ns
else
:
dumper
=
dump
for
item
in
data_list
:
file_like_object
=
StringIO
()
dumper
(
item
,
file_like_object
)
file_like_object
.
seek
(
0
)
self
.
_checkXML
(
file_like_object
)
self
.
assertEquals
(
item
,
load
(
file_like_object
))
def
test_class_serialisation
(
self
):
"""
"""
instance
=
_A
()
instance
.
subobject
=
_B
()
instance
.
subobject
.
list_attribute
=
[
None
,
1
,
pow
(
2
,
123L
),
19.72
,
1
+
5j
,
"here is a string & a <fake tag>"
]
instance
.
self
=
instance
if
self
.
use_namespace_uri
:
dumper
=
dumps_ns
else
:
dumper
=
dumps
dumped
=
dumper
(
instance
)
self
.
_checkXML
(
dumped
)
new_instance
=
loads
(
dumped
)
self
.
assertEquals
(
new_instance
.
__class__
,
_A
)
self
.
assertEquals
(
instance
.
subobject
.
list_attribute
,
new_instance
.
subobject
.
list_attribute
)
c_instance
=
_C
(
'value1'
,
attr2
=
'value2'
)
c_instance
.
attr3
=
'value3'
nested_instance
=
_C
(
'somevalue'
,
'someother'
)
nested_instance
.
attr3
=
"stillanother"
c_instance
.
nested_instance
=
nested_instance
c_marshalled
=
dumps
(
c_instance
)
self
.
_checkXML
(
c_marshalled
)
c_unmarshalled
=
loads
(
c_marshalled
)
self
.
assertEquals
(
c_unmarshalled
.
attr3
,
c_instance
.
attr3
)
self
.
assertEquals
(
c_unmarshalled
.
attr2
,
c_instance
.
attr2
)
self
.
assertEquals
(
c_unmarshalled
.
attr1
,
c_instance
.
attr1
)
self
.
assertEquals
(
c_unmarshalled
.
__class__
,
_C
)
self
.
assertEquals
(
c_unmarshalled
.
nested_instance
.
__class__
,
_C
)
self
.
assertEquals
(
c_unmarshalled
.
nested_instance
.
attr1
,
nested_instance
.
attr1
)
self
.
assertEquals
(
c_unmarshalled
.
nested_instance
.
attr2
,
nested_instance
.
attr2
)
self
.
assertEquals
(
c_unmarshalled
.
nested_instance
.
attr3
,
nested_instance
.
attr3
)
class
TestXMLMarhsallerWithNamespace
(
TestXMLMarhsaller
):
"""
"""
use_namespace_uri
=
True
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment