Skip to content

Commit

Permalink
add support for getting xml element + encoding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
czue committed Feb 3, 2014
1 parent 92f8ff5 commit 0b73d5e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion couchforms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime, hashlib, logging, time
from copy import copy
from lxml import etree
from xml.etree import ElementTree

from django.utils.datastructures import SortedDict
Expand Down Expand Up @@ -238,7 +239,13 @@ def get_xml(self):
return self[const.TAG_XML]
except AttributeError:
return None


def get_xml_element(self):
xml = self.get_xml()
if isinstance(xml, unicode):
xml = xml.encode('utf-8')
return etree.fromstring(xml)

@property
def attachments(self):
"""
Expand Down
1 change: 1 addition & 0 deletions couchforms/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .test_namespaces import *
from .test_auth import *
from .test_post import *
from .test_xml import *
except ImportError, e:
# for some reason the test harness squashes these so log them here for clarity
# otherwise debugging is a pain
Expand Down
7 changes: 7 additions & 0 deletions couchforms/tests/data/encoding.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='{encoding}' ?>
<form xmlns="http://commcarehq.org/couchforms-tests">
<meta>
<instanceID>{form_id}</instanceID>
</meta>
<test>{sample_value}</test>
</form>
31 changes: 31 additions & 0 deletions couchforms/tests/test_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 encoding=utf-8

import uuid
import os
from couchforms.util import post_xform_to_couch
from django.utils.unittest.case import TestCase


class XMLElementTest(TestCase):

def test_various_encodings(self):
tests = (
('utf-8', u'हिन्दी चट्टानों'),
('UTF-8', u'हिन्दी चट्टानों'),
('ASCII', 'hello'),
)
file_path = os.path.join(os.path.dirname(__file__), "data", "encoding.xml")
with open(file_path, "rb") as f:
xml_template = f.read()

for encoding, value in tests:
xml_data = xml_template.format(
encoding=encoding,
form_id=uuid.uuid4().hex,
sample_value=value.encode(encoding),
)
xform = post_xform_to_couch(xml_data)
self.assertEqual(value, xform.form['test'])
elem = xform.get_xml_element()
self.assertEqual(value, elem.find('{http://commcarehq.org/couchforms-tests}test').text)

0 comments on commit 0b73d5e

Please sign in to comment.