Python单元测试(使用SQLAlchemy)不写入/更新数据库?

发布于 2024-10-09 12:41:07 字数 1371 浏览 4 评论 0原文

我很困惑为什么我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉鱼一梦 2024-10-16 12:41:07

不了解 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文