Skip to content

Commit

Permalink
live_server_helper: improve sqlite in-memory check
Browse files Browse the repository at this point in the history
This will make it so it's covered by our tests.
  • Loading branch information
bluetech committed Dec 1, 2021
1 parent f64b252 commit 904a995
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
7 changes: 2 additions & 5 deletions pytest_django/live_server_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ def __init__(self, addr: str) -> None:
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 conn.vendor == 'sqlite' and conn.is_in_memory_db():
# Explicitly enable thread-shareability for this connection.
conn.inc_thread_sharing()
connections_override[conn.alias] = conn

Expand Down
6 changes: 4 additions & 2 deletions pytest_django_test/db_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

if _settings["ENGINE"] == "django.db.backends.sqlite3" and TEST_DB_NAME is None:
TEST_DB_NAME = ":memory:"
SECOND_DB_NAME = ":memory:"
SECOND_TEST_DB_NAME = ":memory:"
else:
DB_NAME += "_inner"

Expand All @@ -25,8 +27,8 @@
# An explicit test db name was given, is that as the base name
TEST_DB_NAME = "{}_inner".format(TEST_DB_NAME)

SECOND_DB_NAME = DB_NAME + '_second' if DB_NAME is not None else None
SECOND_TEST_DB_NAME = TEST_DB_NAME + '_second' if DB_NAME is not None else None
SECOND_DB_NAME = DB_NAME + '_second' if DB_NAME is not None else None
SECOND_TEST_DB_NAME = TEST_DB_NAME + '_second' if DB_NAME is not None else None


def get_db_engine():
Expand Down
6 changes: 3 additions & 3 deletions pytest_django_test/settings_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "/should_not_be_accessed",
"NAME": ":memory:",
},
"replica": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "/should_not_be_accessed",
"NAME": ":memory:",
"TEST": {
"MIRROR": "default",
},
},
"second": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "/should_not_be_accessed",
"NAME": ":memory:",
},
}
8 changes: 6 additions & 2 deletions pytest_django_test/settings_sqlite_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "/pytest_django_tests_default",
"TEST": {"NAME": _filename_default},
"TEST": {
"NAME": _filename_default,
},
},
"replica": {
"ENGINE": "django.db.backends.sqlite3",
Expand All @@ -28,6 +30,8 @@
"second": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "/pytest_django_tests_second",
"TEST": {"NAME": _filename_second},
"TEST": {
"NAME": _filename_second,
},
},
}

0 comments on commit 904a995

Please sign in to comment.