python - 测试数据库在 Django Channels Pytest 测试期间不保留数据
问题描述
我正在按照文档为 Django Channels 应用程序编写测试。我可以成功测试与 WebSocket 消费者的连接和通信。但是,在其中一项测试中,我需要创建一个用户。
这是我的测试方法:
@pytest.mark.django_db
@pytest.mark.asyncio
async def test_1():
my_user = get_user_model().objects.create_user(username='my_user', email='user@user.com', password='123456')
my_user.save()
print(User.objects.all())
communicator = WebsocketCommunicator(MyConsumer, '/websocket/')
communicator.instance.scope['user'] = my_user
connected, subprotocol = await communicator.connect()
assert connected
await communicator.send_json_to({
'command': 'test',
})
response = await communicator.receive_json_from(timeout=5)
await communicator.disconnect()
错误发生在处理'command' == 'test'
案例的方法内部。当它尝试保存另一个具有 User 作为外键的模型实例时,测试失败。
client = Client(user=scope['user'])
client.save()
如果我print(User.objects.all())
在消费者内部的方法中添加一个(在前两行之间),它会返回一个空的 QuerySet,而print
测试方法本身中的第一个返回创建的用户。当然,由于没有用户,因此测试失败并显示:
django.db.utils.IntegrityError: insert or update on table "chat_client" violates foreign key constraint "chat_client_user_id_d10928fc_fk_auth_user_id"
DETAIL: Key (user_id)=(1) is not present in table "auth_user".
我尝试将用户创建转换为另一种异步方法并await
为其设置,但没有任何改变。为什么User
在测试执行期间表被清空?如何保留在测试开始时创建的数据?
解决方案
正如@hoefling 在评论中指出的那样,唯一缺少的就是使测试具有事务性,如本问题所示。
就我而言,我只需要将测试装饰器更改为:
@pytest.mark.django_db(transaction=True)
推荐阅读
- python - Python selenium 从打开的模型中获取元素值给出未打开的模型值
- gremlin - 有没有办法重用聚合步骤?
- python - Pytesseract 将重复的问号识别为不同的字符
- c# - 未终止的字符串。将 byte[] 转换为 Json 的预期分隔符
- javascript - 如何将 vue 库构建为依赖于其他组件的 umd 模块?
- git - 如何将 tfvc 迁移到 Git,包括历史
- google-sheets - 使用 api 在谷歌电子表格中特定列的末尾添加一个新单元格(行)
- html - 如何让页脚位于页面或窗口的底部?
- delphi - 条纹集成
- java - AWS Cloudwatch 中一段时间内没有日志时如何获取警报?