Python unittest(使用SQLAlchemy)是否不编写/更新数据库? [英] Python unittest (using SQLAlchemy) does not write/update database?

查看:222
本文介绍了Python unittest(使用SQLAlchemy)是否不编写/更新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我的Python单元测试可以在没有实际更新数据库的情况下完美运行的问题。

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

我什至可以看到来自SQLAlchemy的SQL语句并逐步完成新创建的操作用户对象的电子邮件-

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'

但是在同一时间,当我登录到测试数据库时,我没有看到那里的新记录。我完全不了解Python / unittest / SQLAlchemy / Pyramid / PostgreSQL的某些功能吗?

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?

谢谢。

Jerry

推荐答案

不知道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.

这篇关于Python unittest(使用SQLAlchemy)是否不编写/更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆