mardi 3 mai 2016

Using a test peewee database instance in a Flask app

I want to run my website (currently running using a Flask server) with test data. I'm using a peewee database Proxy which I initialize from my test. My model is:

database_proxy = Proxy()


class MySQLModel(Model):
    """ A base model that will use our MySQL database """

    class Meta:
        database = database_proxy


class Person(MySQLModel):
    id = PrimaryKeyField(primary_key=True)
    name = CharField()

during the test, I create a new memory database:

test_db = SqliteDatabase(':memory:')

and initialize the model's database:

database_proxy.initialize(test_db)
if database_proxy.is_closed():
    database_proxy.connect()
Person.create_table()

I then create the data, and run my Flask server:

app = Flask(__name__)
app.run(debug=True)

One of my routes selects all the People in my database. But this throws an exception, saying the table Person doesn't exist. For curious people, the function is:

@app.route("/api/people")
def list_people():
    ppl = models.Person.select(models.Person.id).dicts().execute()
    [ ... formatting and returning a json ... ]

If i run the same query outside the server, it works of course.

I made sure my Flask app is using the same database instance, by debugging and seeing the model Person uses the same SqliteDatabase object both in my test and in the flask function (i checked the memory address is the same).

What's going on here? Where is my data when I query it inside flask?

Example debug run:

[before running app.run()]

>>> models.Person.get()
<database.models.Person object at 0x7f8dde8e09d0>
>>> models.Person._meta.database.obj
<peewee.SqliteDatabase object at 0x7f8dde8e0190>

[after app.run()]

* Debugger is active!
 * Debugger pin code: 327-505-347
127.0.0.1 - - [03/May/2016 16:36:12] "GET / HTTP/1.1" 200 -
... Loads more pages ...
>>> models.Person._meta.database.obj
<peewee.SqliteDatabase object at 0x7f8dde8e0190>
>>> models.Person.get()
Traceback (most recent call last):
  File "/home/q/Software/pycharm-2016.1.2/helpers/pydev/_pydevd_bundle/pydevd_exec.py", line 3, in Exec
    exec exp in global_vars, local_vars
  File "<input>", line 1, in <module>
  File "/home/q/dev/dev/venv/local/lib/python2.7/site-packages/peewee.py", line 4505, in get
    return sq.get()
  File "/home/q/dev/dev/venv/local/lib/python2.7/site-packages/peewee.py", line 2885, in get
    return next(clone.execute())
  File "/home/q/dev/dev/venv/local/lib/python2.7/site-packages/peewee.py", line 2932, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/home/q/dev/dev/venv/local/lib/python2.7/site-packages/peewee.py", line 2628, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/home/q/dev/dev/venv/local/lib/python2.7/site-packages/peewee.py", line 3461, in execute_sql
    self.commit()
  File "/home/q/dev/dev/venv/local/lib/python2.7/site-packages/peewee.py", line 3285, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/home/q/dev/dev/venv/local/lib/python2.7/site-packages/peewee.py", line 3454, in execute_sql
    cursor.execute(sql, params or ())
OperationalError: no such table: person

Aucun commentaire:

Enregistrer un commentaire