Q: Correct way to excersize your Turbogears SQLObject model by script?

2 views
Skip to first unread message

Mike Kent

unread,
Dec 5, 2005, 10:15:24 PM12/5/05
to TurboGears
I put together a simple learning project using 'tg-admin quickstart',
and set up a simple model. Now I want to write a quick-n-dirty python
script to use the model with TestGears. After much skiming of docs and
experimentation, I came up with this:

from sqlobject import *
from turbogears.database import *

connStr = "mysql://mike:michelle@localhost/books"
conn = connectionForURI(connStr) #Why do I have to do both this...
sqlhub.processConnection = conn
hub = AutoConnectHub(connStr) #...and this, to get it to work.

import books.model

def test_newAuthor():
auth = books.model.Author(last_name="Smith", first_name="John")
assert auth is not None
assert auth.last_name == "Smith"
assert auth.first_name == "John"

This can't really be the right way to do this. Why do I have to use
both 'connnectionForURI()' and 'AutoConnectHub()' to avoid an exception
with a message that ends:

File
"/usr/local/lib/python2.4/site-packages/TurboGears-0.8a5-py2.4.egg/turbogears/database.py",
line 116, in set_hub
raise KeyError, "No database configuration found!"
KeyError: 'No database configuration found!'

Kevin Dangoor

unread,
Dec 6, 2005, 10:29:39 AM12/6/05
to turbo...@googlegroups.com
Hi Mike,

This is a little easier (and more obvious) in TurboGears 0.9. There's
now a turbogears.database.set_db_uri function you can call to set the
database URI.

You can do the equivalent like this:
cherrypy.config.update({'global':
{"sqlobject.dburi" : dburi}
})

and then you can get rid of all of the hub and connection stuff that
you're doing there.

There's also a DBTest helper class in 0.9.

Kevin
--
Kevin Dangoor
Author of the Zesty News RSS newsreader

email: k...@blazingthings.com
company: http://www.BlazingThings.com
blog: http://www.BlueSkyOnMars.com

myha...@gmail.com

unread,
Dec 6, 2005, 7:14:06 PM12/6/05
to TurboGears
Kevin:

but I think it is not very good to do so, model.py depend on the
cherrypy runtime.
the program is hard to test and run without cherrypy.
so the best way is that model.py is separated from cherrypy's
config.Do you think so?

Kevin Dangoor

unread,
Dec 7, 2005, 8:03:56 AM12/7/05
to turbo...@googlegroups.com

Technically, you don't *have* to use CherryPy's config module in this
instance. You can instantiate your own AutoConnectHub with a URI for
the database connection you want.

The config mechanism for all of TurboGears is CherryPy's. I don't see
a need to grow our own independent config system or to have one config
for the model and other config files for other things. Is there some
other separation you'd envision?

I also don't think that using CP's config system in any way breaks the
MVC separation.

Kevin

Reply all
Reply to author
Forward
0 new messages