Skip to content

Add support for db fixture (test inside transaction) for asyncio tests #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

lode-braced
Copy link

@lode-braced lode-braced commented Aug 12, 2025

In using and debugging pytest-django with async tests & fixtures, I ended up hacking together a way to get the db fixture working by enabling the transaction in the sync to async executor thread in which async orm queries run.

This PR aims to integrate that hack as actual functionality into pytest-django.

The async work is done, I also worked to refine the sync side database access: not allowing db access to threads other than the main thread which is running the test (& transaction). That second part is causing some test failures I've yet been unable to fix on 3.9 & django 4.2, will try to fix or drop it from the PR.

edit: I managed to fix the issue with the test failures: my monkeypatches were dropping the self attribute. Ready for review.

Suggestions & feedback welcome.

@lode-braced lode-braced marked this pull request as draft August 12, 2025 08:48
Copy link
Member

@kingbuzzman kingbuzzman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a code review

From what I can briefly see; this looks great, i've personally had this issue come up, so happy you've taken this on!

@lode-braced lode-braced marked this pull request as ready for review August 12, 2025 10:26
@lode-braced
Copy link
Author

@kingbuzzman CI and myself are now happy with where the branch is at, feel free to have a deeper look now.

@kingbuzzman
Copy link
Member

@lode-braced can you please sync with main, there are new CI rules

@lode-braced
Copy link
Author

@lode-braced can you please sync with main, there are new CI rules

Done!

tox.ini Outdated
@@ -10,6 +10,7 @@ envlist =
[testenv]
dependency_groups =
testing
!dj42: async
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a bug in my CI change.... 🧐 -- this should have failed it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, @lode-braced can you please sync with main once more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
... and fixed the resulting linting issue too ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants