Problem/questions with testing Model

1 view
Skip to first unread message

Denny

unread,
Apr 16, 2007, 3:13:19 PM4/16/07
to TurboGears
I am a newbie to TurboGears and to Python. Been studying TurboGears
for a couple of weeks, and when trying to implement testing, I'm
having some problems/questions!

1. I've modified the sample test scripts (in the '/tests' directory),
then run nosetests. I kept getting the error:
ProgrammingError: (1146, "Table 'research_test.sandboxes'
doesn't exist")

I'm using MySQL on Ubuntu Linux. I have defined different databases
for development and testing (research and research_test respectively.

I finally manually created the sandboxes table. Re-running nosetests
reported other errors in my code which I was able to correct. I ran
nosetests again, and the sandboxes table was again missing!!

My model defines a single table with 8 fields. 'tg-admin sql create'
successfully creates this table in the development database, but I
can't find a way to create the table in the testing database!!?!

Is there a way to have the model table(s) created (either
programmaticaly or some tg-admin command) AND/OR prevent the testing
tables from being deleted when running nosetests??

Thanks in advance,
Denny

Joost Moesker

unread,
Apr 16, 2007, 6:13:04 PM4/16/07
to TurboGears
The setup and tearDown methods on testutils.DBTest automatically
create and drop the database schema per testcase. For this to work
you need to set the model attribute of the class to reference your
model module. It's common to use an SQLite 'in memory' database for
unittests to speed-up the peformance.

Marco Mariani

unread,
Apr 17, 2007, 8:35:44 AM4/17/07
to turbo...@googlegroups.com
Joost Moesker wrote:
> The setup and tearDown methods on testutils.DBTest automatically
> create and drop the database schema per testcase.

DBTest is SQLObject-specific

In my case (with sqlalchemy) I cannot drop and recreate the schema for
each test because it would take too much time -- the schema is
postgres-specific, autoloaded, and has triggers, bells and whistles.

I've come up with this:

> class DBTestCase(unittest.TestCase):
> transaction = None
>
> def setUp(self):
> # In case of errors, we cleare here the session and
> transaction, or other tests will fail
> if self.transaction:
> self.transaction.rollback()
> self.transaction = None
> session.clear()
>
> self.transaction = session.create_transaction()
>
> def tearDown(self):
> # Hit the db before rollback
> # This way, we catch some integrity errors, type checking from
> sqlalchemy, and the like
> session.flush()
> self.transaction.rollback()
> self.transaction = None
> session.clear()

Works for me.
The first session.clear() is suspicious, but I had no time to
investigate yet


Denny

unread,
Apr 17, 2007, 5:57:51 PM4/17/07
to TurboGears
I don't understand what you're saying here!!??!! Can you be more
specific about what needs to be done,
may examples of what I may need to add to my testing code!?!! I'm not
using SQLite, but MySQL.

My test.cfg contains:

sqlobject.dburi="notrans_mysql://researcher:researcher@localhost:
3306/research_test"

test-models.py seems to complete OK. but test-controllers.py is where
my problem seems to be.
If I manually create the required table in the above database,
nosetests completes successfully, but
the table has been deleted!!?!!

Below are the results of my latest nosetests run; and
the contents of my test-controllers.py (which seems to be what's
failing):

======================================================================
ERROR: test module research.tests.test_controllers in /home/dthury/
projects/Somerville/research
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/
suite.py", line 51, in run
self.setUp()
File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/
suite.py", line 200, in setUp
self.module = _import(self.moduleName, [self.path], self.conf)
File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/
importer.py", line 101, in _import
mod = load_module(fqname, fh, filename, desc)
File "/home/dthury/projects/Somerville/research/research/tests/
test_controllers.py", line 2, in ?
from research.controllers import *
File "/home/dthury/projects/Somerville/research/research/
controllers.py", line 26, in ?
class sandboxSelectionFields(widgets.WidgetsList):
File "/home/dthury/projects/Somerville/research/research/
controllers.py", line 27, in sandboxSelectionFields
sandboxID = widgets.RadioButtonList(options=get_sandbox_options(),
validator=validators.NotEmpty)
File "/home/dthury/projects/Somerville/research/research/
controllers.py", line 21, in get_sandbox_options
for sandbox in sandbox_list:
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/sresults.py", line 155, in __iter__
return iter(list(self.lazyIter()))
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/sresults.py", line 163, in lazyIter
return conn.iterSelect(self)
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/dbconnection.py", line 365, in iterSelect
select, keepConnection=False)
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/dbconnection.py", line 705, in __init__
self.dbconn._executeRetry(self.rawconn, self.cursor, self.query)
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry
return cursor.execute(myquery)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
137, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line
33, in defaulterrorhandler
raise errorclass, errorvalue


ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't
exist")

----------------------------------------------------------------------
Ran 1 test in 1.636s

FAILED (errors=1)

==================================================
===================test controllers.py====================

from turbogears import testutil
from research.controllers import *
from research.model import *
import cherrypy


cherrypy.root = Root()

def test_sandbox_controller():
result = testutil.call(cherrypy.root.play)
print 'play returned :', result
assert result == True

def test_method():
"the index method should return a string called now"
import types
result = testutil.call(cherrypy.root.index)
print "Index method returned:", result

def test_indextitle():
"The indexpage should have the right title"
testutil.createRequest("/")
print cherrypy.response.body[0]
assert "<title>SNP Research System</title>" in
cherrypy.response.body[0]


Thanks,
Denny

Ian Wilson

unread,
Apr 17, 2007, 10:35:56 PM4/17/07
to turbo...@googlegroups.com
The following setup will DROP and CREATE your database therefore
deleting all data in it before each test. You will need to populate
your data base with test data in the setUp method.

import turbogears.testutil
import turbogears.database
import sqlobject

turbogears.database.set_db_uri("notrans_mysql://researcher:researcher@localhost:
3306/research_test")

import research.model


from research.controllers import *
from research.model import *
import cherrypy

cherrypy.root = Root()

class TestModel(turbogears.testutil.DBTest):

model = research.model

def setUp(self):
turbogears.testutil.DBTest.setUp(self)
#SETUP TEST DATA HERE

def test_sandbox_controller():
result = testutil.call(cherrypy.root.play)
print 'play returned :', result
assert result == True

....

Denny

unread,
Apr 18, 2007, 12:28:46 PM4/18/07
to TurboGears
Still no-go!!! 'nosetests' seems to fail while importing my
'research.controllers' module. It seems to want the database tables
during that import. The below traceback is pointing to a function in
my controllers.py which makes queries for data to be presented in a
radio button list. Am I doing something fundamentally wrong??

Here's the recent traceback!

> nosetests
E.


======================================================================
ERROR: test module research.tests.test_controllers in /home/dthury/
projects/Somerville/research
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/
suite.py", line 51, in run
self.setUp()
File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/
suite.py", line 200, in setUp
self.module = _import(self.moduleName, [self.path], self.conf)
File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/
importer.py", line 101, in _import
mod = load_module(fqname, fh, filename, desc)

<--- below is from my code --->

File "/home/dthury/projects/Somerville/research/research/tests/
test_controllers.py", line 8, in ?


from research.controllers import *
File "/home/dthury/projects/Somerville/research/research/

controllers.py", line 27, in ?
class sandboxSelectionFields(widgets.WidgetsList):
File "/home/dthury/projects/Somerville/research/research/
controllers.py", line 28, in sandboxSelectionFields


sandboxID = widgets.RadioButtonList(options=get_sandbox_options(),
validator=validators.NotEmpty)
File "/home/dthury/projects/Somerville/research/research/

controllers.py", line 22, in get_sandbox_options
for sandbox in sandbox_list:

<--- The above line is referring to a line of code where I'm
iterating over the results of a db query
But this is done before the setUp function is run to create/
load my test database!! --->

File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/sresults.py", line 155, in __iter__
return iter(list(self.lazyIter()))
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/sresults.py", line 163, in lazyIter
return conn.iterSelect(self)
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/dbconnection.py", line 365, in iterSelect
select, keepConnection=False)
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/dbconnection.py", line 705, in __init__
self.dbconn._executeRetry(self.rawconn, self.cursor, self.query)
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/
sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry
return cursor.execute(myquery)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
137, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line
33, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't
exist")

----------------------------------------------------------------------
Ran 1 test in 1.516s

FAILED (errors=1)


#--------------------------------------------------------------------------------

Ian Wilson

unread,
Apr 18, 2007, 4:07:14 PM4/18/07
to turbo...@googlegroups.com
Ummm it doesn't look like you even use controllers. So why don't you
try it without that import.

-Ian

Denny

unread,
Apr 19, 2007, 7:12:30 PM4/19/07
to TurboGears
I tried what you suggested, and commented out the import of my
controllers!

The 1st think that failed was initializing cherrypy.root

I commented THAT out, and now the line:
result = turbogears.testutil.call(cherrypy.root.index)
fails, because cherrypy,root is not initialized!!!

It looks like, in order to test, my 'index' controller, I need to
import my controllers!

Now what do I do??

thanks,
Denny

Reply all
Reply to author
Forward
0 new messages