Hi,

I've just started dipping my toes into the turbogears water, and I'm
currently looking at the testing aspect.

While the http request testing is covered in the getting started
guide, it isn't immediately obvious to me what the preferred way to
test the sqlobject components of a turbogears site. I suspect I've
missed some docs somewhere.

So far I've come up with this (which seems to work for me):

import cherrypy

from turbogears.tests import util

# Set the db to an in-memory sqlite one.
import turbogears.database
turbogears.database.set_db_uri("sqlite:/:memory:")

# Import my app and it's model
from myapp.controllers import Root
from myapp.model import Person

# Setup the in-memory stuff
Feed.createTable()
FeedItem.createTable()
Person.createTable()
Tag.createTable()
cherrypy.root = Root()

def test_empty_index():
    util.createRequest("/")
    assert "<HTML>" in cherrypy.response.body[0]

    d = util.call(cherrypy.root.index)
    assert d.has_key("items")
    assert len(list(d["items"])) == 0

def test_add_person():
    assert len(list(Person.select())) == 0
    p = Person(name="Michael Twomey", username="mick")
    assert len(list(Person.select())) == 1


Does this look correct?

BTW, I also ran into a problem with util.call vs util.createRequest.
It seems that if you do a util.call before you do a util.createRequest
you get a nasty attribute error (I'm guessing that createRequest sets
things up in cherrypy):

Traceback (most recent call last):
  File 
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/TestGears-0.2-py2.4.egg/testgears/collector.py",
line 26, in runTest
    self.func(*self.args)
  File "/Users/mtt/Source/Galaxy/galaxy/galaxy/tests/test_basic.py",
line 18, in test_empty_index
    d = util.call(cherrypy.root.index)
  File 
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/TurboGears-0.8a3-py2.4.egg/turbogears/tests/util.py",
line 25, in call
    return method(*args, **kw)
  File 
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/TurboGears-0.8a3-py2.4.egg/turbogears/controllers.py",
line 98, in newfunc
    cherrypy.response.headerMap['Content-Type']= content_type
  File 
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/CherryPy-2.1.0_rc2-py2.4.egg/cherrypy/__init__.py",
line 69, in __getattr__
    childobject = getattr(serving, self.__attrname__)
AttributeError: 'thread._local' object has no attribute 'response'

thanks,
  Michael

Reply via email to