Python单元测试(使用SQLAlchemy)不写入/更新数据库?
我很困惑为什么我的 Python 单元测试在没有实际更新数据库的情况下运行得很好。
我什至可以看到来自 SQLAlchemy 的 SQL 语句并逐步浏览新创建的用户对象的电子邮件 -
...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'[email protected]', ...}
> .../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'[email protected]'
然而,在我登录到测试数据库时同时,我不在那里查看新记录。这是我完全不知道的 Python/unittest/SQLAlchemy/Pyramid/PostgreSQL 的某些功能吗?
谢谢。
杰瑞
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'[email protected]', ...}
> .../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'[email protected]'
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不了解 SQLAlchemy,但这听起来像是测试正在事务中运行,并且该事务从未提交。要么显式回滚,要么在连接关闭时自动回滚。
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.