Search code examples
python-2.7flaskpeewee

test_database() does not use the database given


I'm trying to test my models using peewee's test_database which is supposed to use the database passed when executing the SQL in the context block. However running the test, I noticed that the production database is always used, which should not be the case.

Here's the exception I get:

======================================================================
ERROR: test_Admin (__main__.DatabaseTestSuite)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "db/db_test.py", line 14, in test_Admin
    with test_database(test_db, (Admin), create_tables=True):
  File "/usr/local/lib/python2.7/dist-packages/playhouse/test_utils.py", line 21, in __enter__
    for m in self.models:
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 4723, in __iter__
    return iter(self.select())
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3149, in __iter__
    return iter(self.execute())
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3142, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2826, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3683, in execute_sql
    self.commit()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3507, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3676, in execute_sql
    cursor.execute(sql, params or ())
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 166, in execute
    result = self._query(query)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 322, in _query
    conn.query(q)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 835, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1019, in _read_query_result
    result.read()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1302, in read
    first_packet = self.connection._read_packet()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 981, in _read_packet
    packet.check_error()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
ProgrammingError: (1146, u"Table 'db.admin' doesn't exist")

----------------------------------------------------------------------

Here's the test code:

from db import *
import unittest
from playhouse.test_utils import test_database


test_db = MySQLDatabase('testdb', 
    user='testuser', 
    password='testpass')


class DatabaseTestSuite(unittest.TestCase):

    def test_Admin(self):
        with test_database(test_db, (Admin), create_tables=True):

            Admin.create(username="testuser", 
                email="testuser@email.com")

            result = Admin.select().where(Admin.user == "testuser")

            unittest.assertIsNotNone(result)


if __name__ == '__main__':
    unittest.main()

I've opened an issue on the github page, but received no help so far. You can find the issue here. It should provide extra details if needed. Any help would be appreciated, thanks.


Solution

  • The model tuple has a single item and thus requires a trailing comma. Changing this

    with test_database(test_db, (Admin), create_tables=True):
    

    to this

    with test_database(test_db, (Admin,), create_tables=True):
    

    resolved it.