Python unittest (using SQLAlchemy) does not write/update database?

≡放荡痞女 提交于 2019-12-11 03:17:04

问题


I am puzzled at why my Python unittest runs perfectly fine without actually updating the database.

I can even see the SQL statements from SQLAlchemy and step through the newly created user object's email --

...INFO sqlalchemy.engine.base.Engine.0x...954c INSERT INTO users (user_id, user_name, email, ...) VALUES (%(user_id)s, %(user_name)s, %(email)s, ...)
...INFO sqlalchemy.engine.base.Engine.0x...954c {'user_id': u'4cfdafe3f46544e1b4ad0c7fccdbe24a', 'email': u'test@example.com', ...}
> .../tests/unit_tests/test_signup.py(127)test_signup_success()
-> user = user_q.filter_by(user_name='test').first()
(Pdb) n
...INFO sqlalchemy.engine.base.Engine.0x...954c SELECT users.user_id AS users_user_id, ...
FROM users 
WHERE users.user_name = %(user_name_1)s 
 LIMIT 1 OFFSET 0
...INFO sqlalchemy.engine.base.Engine.0x...954c {'user_name_1': 'test'}
> .../tests/unit_tests/test_signup.py(128)test_signup_success()
-> self.assertTrue(isinstance(user, model.User))
(Pdb) user
<pweb.models.User object at 0x9c95b0c>
(Pdb) user.email
u'test@example.com'

Yet at the same time when I login to the test database, I do not see the new record there. Is it some feature from Python/unittest/SQLAlchemy/Pyramid/PostgreSQL that I'm totally unaware of?

Thanks.

Jerry


回答1:


Not knowing SQLAlchemy, but that sounds like the test is being run in a transaction and that this transaction is never committed. Either explicitly rolled back, or automatically rolled back when the connection closes.



来源:https://stackoverflow.com/questions/4533245/python-unittest-using-sqlalchemy-does-not-write-update-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!