Showing posts with label twill. Show all posts
Showing posts with label twill. Show all posts

Thursday, October 20, 2011

tddspry, twill and UnicodeDecodeError

Problem was caused by this naive code, which tries to find string "http://www.fedex.com/Tracking?cntry_code=us&tracknumber_list=123465789&language=english" in plain HTML
class ShipmentsListTest(DatabaseTestCase):
    def test_tracking_link_presence(self):
        self.go200('members:shipments_list')
        self.find(self.rentorder.outgoing_tracking_link, flat=True, escape=True)
Which causes this:
  File "/Users/zomg/.virtualenvs/dev/src/tddspry/tddspry/django/cases.py", line 497, in find
    real_count = html.count(what)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 277: ordinal not in range(128)
Follownig tddspry code we can found that on escape flag it applies django escape method, which returns an unicode string.
from django.utils.html import escape as real_escape
.. skipped ..

def find(self, what, flags='', flat=False, count=None, escape=False):
    if escape:
        what = real_escape(what)

Unfortunately, in twill won't work with unicode strings. So here is quick workaround, make string escaping manually and force it to string.
from django.utils.html import escape as real_escape

class ShipmentsListTest(DatabaseTestCase):
    def test_tracking_link_presence(self):
        self.go200('members:shipments_list')
        link = str(real_escape(self.rentorder.outgoing_tracking_link))
        self.find(link, flat=True)

Voila!