Skip to content

Fix LiveServer for in-memory SQLite database on Django >= 3. #947

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Rafal Stozek
Donald Stufft <[email protected]>
Nicolas Delaby <[email protected]>
Daniel Hahler <https://twitter.com/blueyed>
Michael Howitz
Hasan Ramezani <[email protected]>
10 changes: 10 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

unreleased
----------

Bugfixes
^^^^^^^^

* Fix fixture ``.live_server_helper.LiveServer`` when using an in-memory SQLite
database on Django >= 3.


v4.4.0 (2021-06-06)
-------------------

Expand Down
5 changes: 5 additions & 0 deletions pytest_django/django_compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Note that all functions here assume django is available. So ensure
# this is the case before you call them.

import pkg_resources


IS_DJANGO_2 = pkg_resources.get_distribution("Django").version < '3'


def is_django_unittest(request_or_item) -> bool:
"""Returns True if the request_or_item is a Django test case, otherwise False"""
Expand Down
38 changes: 37 additions & 1 deletion pytest_django/live_server_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any, Dict

from .django_compat import IS_DJANGO_2


class LiveServer:
"""The liveserver fixture
Expand All @@ -24,7 +26,10 @@ def __init__(self, addr: str) -> None:
and conn.settings_dict["NAME"] == ":memory:"
):
# Explicitly enable thread-shareability for this connection
conn.allow_thread_sharing = True
if IS_DJANGO_2:
conn.allow_thread_sharing = True
else:
conn.inc_thread_sharing()
connections_override[conn.alias] = conn

liveserver_kwargs["connections_override"] = connections_override
Expand Down Expand Up @@ -60,6 +65,37 @@ def __init__(self, addr: str) -> None:

def stop(self) -> None:
"""Stop the server"""
from django.db import connections

liveserver_kwargs = {} # type: Dict[str, Any]

connections_override = {}
for conn in connections.all():
# If using in-memory sqlite databases, pass the connections to
# the server thread.
if (
conn.settings_dict["ENGINE"] == "django.db.backends.sqlite3"
and conn.settings_dict["NAME"] == ":memory:"
):
# Explicitly enable thread-shareability for this connection
if IS_DJANGO_2:
conn.allow_thread_sharing = False
else:
conn.dec_thread_sharing()
connections_override[conn.alias] = conn

liveserver_kwargs["connections_override"] = connections_override
from django.conf import settings

if "django.contrib.staticfiles" in settings.INSTALLED_APPS:
from django.contrib.staticfiles.handlers import StaticFilesHandler

liveserver_kwargs["static_handler"] = StaticFilesHandler
else:
from django.test.testcases import _StaticFilesHandler

liveserver_kwargs["static_handler"] = _StaticFilesHandler

self.thread.terminate()
self.thread.join()

Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ ignore_missing_imports = True
ignore_missing_imports = True
[mypy-psycopg2cffi.*]
ignore_missing_imports = True
[mypy-pkg_resources.*]
ignore_missing_imports = True