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
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
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
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
....
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
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