[pytest-django] Database is not properly rolled back after async tests
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 38/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- databases, testing-qa
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện lỗi từ ví dụ test_app.py của issue với pytest, pytest-django và pytest-asyncio. Xác minh xem thao tác ghi vào cơ sở dữ liệu của bài kiểm thử bất đồng bộ có vẫn hiển thị với test_check_if_model_present hay không; được coi là hoàn tất khi bài kiểm thử thứ hai đạt với thao tác ghi đã được rollback sau bài kiểm thử đầu tiên.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
When using pytest-django's pytest.mark.django_db marker in conjunction with pytest.mark.asyncio, any writes to the database are not rolled back when the test completes and affect subsequent tests.
To test, I created a fresh django app, installed pytest-django and pytest-asyncio, created a simple model, and wrote two tests:
models.py:
from django.db import models
class MyModel(models.Model):
my_field = models.TextField(null=True, blank=True)
test_app.py:
import pytest
from asgiref.sync import sync_to_async
from app.models import MyModel
@pytest.mark.django_db
@pytest.mark.asyncio
async def test_save_model_to_db():
await sync_to_async(MyModel.objects.create)()
@pytest.mark.django_db
def test_check_if_model_present():
assert MyModel.objects.count() == 0
Running pytest resulted in the first test succeeding and the second failing:
@pytest.mark.django_db
def test_check_if_model_present():
> assert MyModel.objects.count() == 0
E assert 1 == 0
At first I thought it might be due to how Django runs synchronous code from an asynchronous context. Maybe it was creating the instance in another transaction that pytest wasn't rolling back.
So, I updated the test code to set DJANGO_ALLOW_ASYNC_UNSAFE to "true" so I could remove the sync_to_async() wrapper around the create() call, ensuring the instance would be included created in the same thread and within pytest's transaction.
Unfortunately, the test failed for the same reason. This could be an issue with pytest-asyncio or compatibility with pytest-django.
- Ngôn ngữ chính
- Python
- Star
- 1.7k
- Fork
- 207
- Merge trung bình
- 5 giờ 2 phút
- Pull request đã merge (30 ngày)
- 11
Chuẩn bị môi trường
Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của pytest-dev/pytest-asyncio
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
pytest-dev/pytest-asyncio#1475 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 56/100
pytest-dev/pytest-asyncio#1514 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
pytest-dev/pytest-asyncio#1501 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
pytest-dev/pytest-asyncio#1463 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Don't warn about `asyncio_default_fixture_loop_scope` unless the test suite uses `pytest-asyncio`Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
pytest-dev/pytest-asyncio#1344 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của pytest-dev/pytest-asyncio
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 1 ngày
-
https://search.utilibre.orgĐang mởinstance instance add
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
searxng/searx-instances#941 · 1 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
FluidNumerics/fluid-walk-blocker#89 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày