Unittest Web2py with Test Runner Error database is locked

72 views
Skip to first unread message

Maelle Taurand

unread,
Feb 10, 2016, 2:53:27 PM2/10/16
to web2py-users

Hy,

I want to make  unit test with web2py.

I follow this page :  https://pythonhosted.org/web2py_utils/test_runner.html.

Is Web2py Test Runner  always a good way for unittesting ?



After  running execute_tests.py, i have got thie result and an error (logged in report file) 


Result :

self.assertEquals(self.nbP+1, len(self.db().select(self.db.person.id)))
AssertionError: 2 != 1



It seems the insert was not performed ?


ERROR: test_ajoutPerson (web2py_utils.test_runner.TestAjoutPerson)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "xxxxxx/web2py/applications/app1/tests/manage.py", line 28, in test_ajoutPerson
    res = self.env['ajoutPerson']("titi")
  File "xxxxxx/web2py/applications/app1/controllers/manage.py", line 24, in ajoutPerson
    db.person.insert(name=name)
  File "xxxxxx/web2py/gluon/packages/dal/pydal/objects.py", line 712, in insert
    ret = self._db._adapter.insert(self, self._listify(fields))
  File "xxxxxxweb2py/gluon/packages/dal/pydal/adapters/base.py", line 739, in insert
    raise e
OperationalError: database is locked




Thanks for help,


Here is the controller's function i want to test.
******************************************************************************************
controller : manage.py 

def formAjoutPerson():
    form
=SQLFORM(db.person)  # person is a table that contains one field named 'name'

   
if form.validate():

        name
=form.vars.name
       
        mess
=ajoutPerson(name)
        response
.flash = mess

   
elif form.errors :
        response
.flash='error in the form'

   
return locals()

def ajoutPerson(name):

   
# the libelle must be unique
    rows
= db(db.person.name == name).select(db.person.id)
   
   
if len(rows) == 0:
        db
.person.insert(name=name)
        mess
= 'new person added'
   
else:
        mess
= 'Error : name already exists
    return mess



******************************************************************************************
Here is the test

class TestAjoutPerson(unittest.TestCase):
   
   
def setUp(self):
     
self.env = new_env(app='app1', controller='manage')
     
self.db = copy_db(self.env, db_name='db', db_link='sqlite:memory')    
         
   
     
self.db.person.insert(name="toto")
     
self.db.commit()

      rows
=self.db().select(self.db.person.id)
     
self.nbP=len(rows)
     
   
# Test method
   
def test_ajoutPerson(self):

     res
= self.env['ajoutPerson']("titi")
     
self.db.commit()
     
self.assertEquals("new person added", res)
         
     
#TEST
     
self.assertEquals(self.nbP+1, len(self.db().select(self.db.person.id)))

     

suite
= unittest.TestSuite() # define a group of unit tests
suite
.addTest(unittest.makeSuite(TestAjoutTest)) #add to the suite test class

unittest
.TextTestRunner(verbosity=2).run(suite)
**************************************************************************************

Dave S

unread,
Feb 10, 2016, 3:46:12 PM2/10/16
to web2py-users


On Wednesday, February 10, 2016 at 11:53:27 AM UTC-8, Maelle Taurand wrote:

Hy,

I want to make  unit test with web2py.

I follow this page :  https://pythonhosted.org/web2py_utils/test_runner.html.

Is Web2py Test Runner  always a good way for unittesting ?


After  running execute_tests.py, i have got thie result and an error (logged in report file) 


Is your database using sqlite?  (That's the "out of the box" web2py configuration, but locking issues vs concurrency make it less desirable as a production database)..  You should also tell us what version of web2py, what python version, and what OS.  If you're running on one of the Windows OS platforms, then your python version may be the one bundled with web2py.

/dps

Maelle Taurand

unread,
Feb 11, 2016, 8:40:02 AM2/11/16
to web...@googlegroups.com
 
I'm testing with mysql database.

This resolvs the database locked error.

But the result of my test is still not OK.


 
self.assertEquals(self.nbP+1, len(self.db().select(self.db.person.id)))
AssertionError: 2 != 1

And the insert is not performed.


Maybe is the call to the controller's function not ok ?
Reply all
Reply to author
Forward
0 new messages