Skip to content

Commit cef45d0

Browse files
dee077carltongibson
authored andcommitted
Used fix for ChannelsLiveServerTestCase from #2164 and cleaned conftest.py
1 parent 9191c9b commit cef45d0

File tree

3 files changed

+39
-121
lines changed

3 files changed

+39
-121
lines changed

channels/testing/live.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from functools import partial
22

33
from daphne.testing import DaphneProcess
4-
from django import VERSION
54
from django.contrib.staticfiles.handlers import ASGIStaticFilesHandler
65
from django.core.exceptions import ImproperlyConfigured
76
from django.db import connections
@@ -40,47 +39,46 @@ def live_server_url(self):
4039
def live_server_ws_url(self):
4140
return "ws://%s:%s" % (self.host, self._port)
4241

43-
def _pre_setup(self):
42+
@classmethod
43+
def setUpClass(cls):
4444
for connection in connections.all():
45-
if self._is_in_memory_db(connection):
45+
if cls._is_in_memory_db(connection):
4646
raise ImproperlyConfigured(
4747
"ChannelLiveServerTestCase can not be used with in memory databases"
4848
)
4949

50-
super(ChannelsLiveServerTestCase, self)._pre_setup()
50+
super().setUpClass()
5151

52-
self._live_server_modified_settings = modify_settings(
53-
ALLOWED_HOSTS={"append": self.host}
52+
cls._live_server_modified_settings = modify_settings(
53+
ALLOWED_HOSTS={"append": cls.host}
5454
)
55-
self._live_server_modified_settings.enable()
55+
cls._live_server_modified_settings.enable()
5656

5757
get_application = partial(
5858
make_application,
59-
static_wrapper=self.static_wrapper if self.serve_static else None,
59+
static_wrapper=cls.static_wrapper if cls.serve_static else None,
6060
)
61-
self._server_process = self.ProtocolServerProcess(self.host, get_application)
62-
self._server_process.start()
63-
self._server_process.ready.wait()
64-
self._port = self._server_process.port.value
65-
66-
def _post_teardown(self):
67-
self._server_process.terminate()
68-
self._server_process.join()
69-
self._live_server_modified_settings.disable()
70-
super(ChannelsLiveServerTestCase, self)._post_teardown()
71-
72-
@staticmethod
73-
def _is_in_memory_db(connection):
61+
cls._server_process = cls.ProtocolServerProcess(cls.host, get_application)
62+
cls._server_process.start()
63+
while True:
64+
if not cls._server_process.ready.wait(timeout=1):
65+
if cls._server_process.is_alive():
66+
continue
67+
raise RuntimeError("Server stopped") from None
68+
break
69+
cls._port = cls._server_process.port.value
70+
71+
@classmethod
72+
def tearDownClass(cls):
73+
cls._server_process.terminate()
74+
cls._server_process.join()
75+
cls._live_server_modified_settings.disable()
76+
super().tearDownClass()
77+
78+
@classmethod
79+
def _is_in_memory_db(cls, connection):
7480
"""
7581
Check if DatabaseWrapper holds in memory database.
7682
"""
7783
if connection.vendor == "sqlite":
7884
return connection.is_in_memory_db()
79-
80-
81-
# Workaround for Django 5.2: _pre_setup became a classmethod.
82-
# TODO: Remove this workaround once support for Django <5.2 is dropped.
83-
if VERSION >= (5, 2):
84-
ChannelsLiveServerTestCase._pre_setup = classmethod(
85-
ChannelsLiveServerTestCase._pre_setup
86-
)

tests/conftest.py

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,12 @@
1+
import os
2+
13
import pytest
24
from django.conf import settings
35

46

57
def pytest_configure():
6-
settings.configure(
7-
DATABASES={
8-
"default": {
9-
"ENGINE": "django.db.backends.sqlite3",
10-
# Override Django’s default behaviour of using an in-memory database
11-
# in tests for SQLite, since that avoids connection.close() working.
12-
"TEST": {"NAME": "test_db.sqlite3"},
13-
}
14-
},
15-
INSTALLED_APPS=[
16-
"daphne",
17-
"django.contrib.auth",
18-
"django.contrib.contenttypes",
19-
"django.contrib.sessions",
20-
"django.contrib.admin",
21-
"django.contrib.staticfiles",
22-
"tests.sample_project.sampleapp",
23-
"channels",
24-
],
25-
STATIC_URL="static/",
26-
ASGI_APPLICATION="tests.sample_project.config.asgi.application",
27-
MIDDLEWARE=[
28-
"django.contrib.sessions.middleware.SessionMiddleware",
29-
"django.contrib.auth.middleware.AuthenticationMiddleware",
30-
],
31-
ROOT_URLCONF="tests.sample_project.config.urls",
32-
CHANNEL_LAYERS={
33-
"default": {
34-
"BACKEND": "channels.layers.InMemoryChannelLayer",
35-
},
36-
},
37-
TEMPLATES=[
38-
{
39-
"BACKEND": "django.template.backends.django.DjangoTemplates",
40-
"APP_DIRS": True,
41-
"OPTIONS": {
42-
"context_processors": [
43-
"django.template.context_processors.request",
44-
"django.contrib.auth.context_processors.auth",
45-
],
46-
},
47-
},
48-
],
49-
SECRET_KEY="Not_a_secret_key",
50-
)
8+
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.sample_project.config.settings"
9+
settings._setup()
5110

5211

5312
def pytest_generate_tests(metafunc):
Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
1-
"""
2-
Django settings for sample_project project.
3-
4-
Generated by 'django-admin startproject' using Django 5.2.
5-
6-
For more information on this file, see
7-
https://docs.djangoproject.com/en/5.2/topics/settings/
8-
9-
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/5.2/ref/settings/
11-
"""
12-
131
from pathlib import Path
142

15-
# Build paths inside the project like this: BASE_DIR / 'subdir'.
163
BASE_DIR = Path(__file__).resolve().parent.parent
174

5+
SECRET_KEY = "Not_a_secret_key"
186

19-
# Quick-start development settings - unsuitable for production
20-
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
21-
22-
# SECURITY WARNING: keep the secret key used in production secret!
23-
SECRET_KEY = "django-insecure-w%kkd-b39(9uvz68x!-9+xt=&9q&^j@#uc_&=g%-s@bcsz*qbu"
24-
25-
# SECURITY WARNING: don't run with debug turned on in production!
267
DEBUG = True
278

289
ALLOWED_HOSTS = []
2910

30-
31-
# Application definition
32-
3311
INSTALLED_APPS = [
3412
"daphne",
3513
"django.contrib.admin",
@@ -38,7 +16,7 @@
3816
"django.contrib.sessions",
3917
"django.contrib.messages",
4018
"django.contrib.staticfiles",
41-
"sampleapp",
19+
"tests.sample_project.sampleapp",
4220
"channels",
4321
]
4422

@@ -52,7 +30,7 @@
5230
"django.middleware.clickjacking.XFrameOptionsMiddleware",
5331
]
5432

55-
ROOT_URLCONF = "config.urls"
33+
ROOT_URLCONF = "tests.sample_project.config.urls"
5634

5735
TEMPLATES = [
5836
{
@@ -70,32 +48,26 @@
7048
},
7149
]
7250

73-
WSGI_APPLICATION = "config.wsgi.application"
74-
ASGI_APPLICATION = "config.asgi.application"
51+
WSGI_APPLICATION = "tests.sample_project.config.wsgi.application"
52+
ASGI_APPLICATION = "tests.sample_project.config.asgi.application"
7553

7654
CHANNEL_LAYERS = {
7755
"default": {
7856
"BACKEND": "channels.layers.InMemoryChannelLayer",
7957
},
8058
}
8159

82-
# Database
83-
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
84-
8560
DATABASES = {
8661
"default": {
8762
"ENGINE": "django.db.backends.sqlite3",
8863
"NAME": BASE_DIR / "sampleapp/sampleapp.sqlite3",
89-
"TEST": {
90-
"NAME": BASE_DIR / "sampleapp/test_sampleapp.sqlite3",
91-
},
64+
# Override Django’s default behaviour of using an in-memory database
65+
# in tests for SQLite, since that avoids connection.close() working.
66+
"TEST": {"NAME": "test_db.sqlite3"},
9267
}
9368
}
9469

9570

96-
# Password validation
97-
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
98-
9971
AUTH_PASSWORD_VALIDATORS = [
10072
{
10173
"NAME": (
@@ -114,10 +86,6 @@
11486
},
11587
]
11688

117-
118-
# Internationalization
119-
# https://docs.djangoproject.com/en/5.2/topics/i18n/
120-
12189
LANGUAGE_CODE = "en-us"
12290

12391
TIME_ZONE = "UTC"
@@ -126,13 +94,6 @@
12694

12795
USE_TZ = True
12896

129-
130-
# Static files (CSS, JavaScript, Images)
131-
# https://docs.djangoproject.com/en/5.2/howto/static-files/
132-
13397
STATIC_URL = "static/"
13498

135-
# Default primary key field type
136-
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
137-
13899
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

0 commit comments

Comments
 (0)