June 2007
Monthly Archive
Monthly Archive
Posted by Andrey Khavryuchenko on 30 Jun 2007 | Tagged as: Blog, django
I hate hitting database in my unit tests.
Even if it’s tiny, nearly empty and easy to fill. Things only become worse when you have to test lots different cases that contradict each other and yet are small enough to justify a separate database fixture.
Thus, tonight I’m fighting my laziness to decipher django.db.models enough to be able to create slim and easy db mocks inside a nose unit test.
So far is far too late for my brain to spin at full speed and no code was produced. Yet I’ve come down to two distinct ways to implement this:
Definitely the second way is faster to either implement or fail. So will it be.
Watch for updates with working code..
Posted by Andrey Khavryuchenko on 06 Jun 2007 | Tagged as: Blog, django
Last two days I’ve spent fixing Djiggit wrt international, esp cyrillics feeds.
Here are 3 points that I’ve learned:
Long story.
It took me several hours to trace why cyrillics is stored as ‘????’ in the database. Starting from top - my code - to the database itself.
Distilled down for mysql:
manage.py dumpdata > <dumpfile>.json
mysql> show variables like 'ch%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
manage.py loaddata <dumpfile>
Happy hacking!
Posted by Andrey Khavryuchenko on 05 Jun 2007 | Tagged as: Blog, django
Djiggit is build on the Django unicode branch and yet it require non-obvious solutions to everyday tasks.
Imagine you’re trying to present a (cyrillic) tag in url-ready form. Usually this is solved by
{{ tagname|urlencode }}
But when the ‘tagname’ contains non-ascii symbols, urlencode barfs:
KeyError at /
u'\u0420'
Request Method: GET
Request URL: http://localhost:8088/
Exception Type: KeyError
Exception Value: u'\u0420'
Exception Location: /usr/lib/python2.4/urllib.py in quote, line 1117
Template error
My current (temporary) solution is to encode this manually, in python:
urllib.quote(tagname.encode('utf8')
But this is not DRY and makes me think there’s a better way.
Perhaps it’s time to add a custom filter and push it into django codeline.
Posted by Andrey Khavryuchenko on 04 Jun 2007 | Tagged as: Blog, django
Just a little writeup..
Now have a pleasure of not starting django development webserver, nor using apache to do http-level tests in my project.
Thanks to twill and its wsgi integration.
Works smoothly now, but caused me little cursing when twill refused to see rss feed.
After digging the twill code I’ve changed a single line - still don’t know how correct is this:
--- /usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg/twill/wsgi_intercept.py.orig 2007-06-04 21:10:37 +0300
+++ /usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg/twill/wsgi_intercept.py 2007-06-04 21:10:45 +0300
@@ -265,7 +265,7 @@
for data in self.write_results:
self.output.write(data)
- if generator_data:
+ if generator_data is not None:
self.output.write(generator_data)
for data in self.result:
self.output.write(data)
Nevertheless it didn’t break anything and I have a luxury to test rss feeds too:
# -*- coding: utf-8 -*-
# $Id: tests.py 326 2007-06-04 15:58:48Z akhavr $
from nose.tools import *
from twill.commands import *
from twill import add_wsgi_intercept
def setup():
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "web.settings"
from django.core.servers.basehttp import AdminMediaHandler
from django.core.handlers.wsgi import WSGIHandler
app = AdminMediaHandler(WSGIHandler())
add_wsgi_intercept("127.0.0.1", 9876, lambda: app)
@with_setup(setup)
def test_rss():
'test rss feed'
go('http://127.0.0.1:9876/kds-djangofeed/feed/rss/')
code('200')
find('<rss version="2.0"><channel><title>Django Feed Planet.</title>')
return