このプロジェクトは、herokuにデプロイされているWebアプリです。
これがdb_test_many_create.pyの私の単純な団結です:
import unittest
from petalapp.database.models import User, Hospital, Data, ROLE_USER
from petalapp import db
class BuildDestroyTables(unittest.TestCase):
user_test_1 = User("test_user_nickname","user_email",ROLE_USER)
data_test_1 = Data(1)
hospital_test_1 = Hospital("test_hospital_1")
def setUP(self):
db.drop_all()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_user_setup(self):
db.session.add(self.user_test_1)
db.session.commit()
if __name__ == "__main__":
unittest.main()
コマンドを実行すると:
heroku run python petalapp/database/db_test_many_create.py --app warm-atoll-3630
データベースに対してテストするには、次のトレースバックを取得します。
Running `python petalapp/database/db_test_many_create.py` attached to terminal... up, run.6505
E
======================================================================
ERROR: test_user_setup (__main__.BuildDestroyTables)
----------------------------------------------------------------------
Traceback (most recent call last):
File "petalapp/database/db_test_many_create.py", line 30, in test_user_setup
db.session.commit()
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 114, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 703, in commit
self.transaction.commit()
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 361, in commit
self._prepare_impl()
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 340, in _prepare_impl
self.session.flush()
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1718, in flush
self._flush(objects)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1789, in _flush
flush_context.execute()
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 331, in execute
rec.execute(self)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 475, in execute
uow
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 64, in save_obj
table, insert)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 558, in _emit_insert_statements
execute(statement, params)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1449, in execute
params)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1584, in _execute_clauseelement
compiled_sql, distilled_params
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1698, in _execute_context
context)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1691, in _execute_context
context)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 331, in do_execute
cursor.execute(statement, parameters)
ProgrammingError: (ProgrammingError) relation "user" does not exist
LINE 1: INSERT INTO "user" (nickname, email, role) VALUES (E'test_us...
^
'INSERT INTO "user" (nickname, email, role) VALUES (%(nickname)s, %(email)s, %(role)s) RETURNING "user".id' {'nickname': 'test_user_nickname', 'role': 0, 'email': 'user_email'}
----------------------------------------------------------------------
Ran 1 test in 0.349s
FAILED (errors=1)
ただし、インタラクティブセッションで変更を加えた場合(それが行っていると思った場合):
In [2]: from petalapp.database.models import User, Hospital, Data, ROLE_USER
In [3]: from petalapp import db
In [4]: db.drop_all()
In [5]: db.create_all()
In [6]: user_test_1 = User("test_user_nickname","user_email",ROLE_USER)
In [7]: db.session.add(user_test_1)
In [8]: db.session.commit()
In [9]: u = User.query.get(1)
In [10]: print(u)
<Name : u'test_user_nickname'>
すべてが期待どおりになり、setUpとtearDownを正しく使用していないと思います。unittestsを使用してデータベースを効果的にテストするにはどうすればよいですか?