Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f5976b2

Browse files
committedNov 27, 2024·
Greater separation of tests
1 parent a75309a commit f5976b2

File tree

4 files changed

+422
-397
lines changed

4 files changed

+422
-397
lines changed
 

‎tests/test_app/settings_multi_db.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110
LANGUAGE_CODE = "en-us"
111111
TIME_ZONE = "UTC"
112112
USE_I18N = True
113-
USE_L10N = True
114113
USE_TZ = True
115114

116115
# Default primary key field type

‎tests/test_app/settings_single_db.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
LANGUAGE_CODE = "en-us"
9999
TIME_ZONE = "UTC"
100100
USE_I18N = True
101-
USE_L10N = True
102101
USE_TZ = True
103102

104103
# Default primary key field type

‎tests/test_app/tests/test_components.py

Lines changed: 329 additions & 395 deletions
Large diffs are not rendered by default.

‎tests/test_app/tests/utils.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import asyncio
2+
import os
3+
import sys
4+
from functools import partial
5+
6+
from channels.testing import ChannelsLiveServerTestCase
7+
from channels.testing.live import make_application
8+
from django.core.exceptions import ImproperlyConfigured
9+
from django.core.management import call_command
10+
from django.db import connections
11+
from django.test.utils import modify_settings
12+
from playwright.sync_api import sync_playwright
13+
14+
from reactpy_django.utils import strtobool
15+
16+
GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS", "False")
17+
18+
19+
class PlaywrightTestCase(ChannelsLiveServerTestCase):
20+
21+
from reactpy_django import config
22+
23+
databases = {"default"}
24+
25+
@classmethod
26+
def setUpClass(cls):
27+
# Repurposed from ChannelsLiveServerTestCase._pre_setup
28+
for connection in connections.all():
29+
if cls._is_in_memory_db(cls, connection):
30+
raise ImproperlyConfigured(
31+
"ChannelLiveServerTestCase can not be used with in memory databases"
32+
)
33+
cls._live_server_modified_settings = modify_settings(
34+
ALLOWED_HOSTS={"append": cls.host}
35+
)
36+
cls._live_server_modified_settings.enable()
37+
cls.get_application = partial(
38+
make_application,
39+
static_wrapper=cls.static_wrapper if cls.serve_static else None,
40+
)
41+
cls.setUpServer()
42+
43+
# Open a Playwright browser window
44+
if sys.platform == "win32":
45+
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
46+
cls.playwright = sync_playwright().start()
47+
headless = strtobool(os.environ.get("PLAYWRIGHT_HEADLESS", GITHUB_ACTIONS))
48+
cls.browser = cls.playwright.chromium.launch(headless=bool(headless))
49+
cls.page = cls.browser.new_page()
50+
cls.page.set_default_timeout(5000)
51+
52+
@classmethod
53+
def setUpServer(cls):
54+
cls._server_process = cls.ProtocolServerProcess(cls.host, cls.get_application)
55+
cls._server_process.start()
56+
cls._server_process.ready.wait()
57+
cls._port = cls._server_process.port.value
58+
59+
@classmethod
60+
def tearDownClass(cls):
61+
from reactpy_django import config
62+
63+
# Close the Playwright browser
64+
cls.playwright.stop()
65+
66+
# Close the other server processes
67+
cls.tearDownServer()
68+
69+
# Repurposed from ChannelsLiveServerTestCase._post_teardown
70+
cls._live_server_modified_settings.disable()
71+
for db_name in ["default", config.REACTPY_DATABASE]:
72+
call_command(
73+
"flush",
74+
verbosity=0,
75+
interactive=False,
76+
database=db_name,
77+
reset_sequences=False,
78+
)
79+
80+
@classmethod
81+
def tearDownServer(cls):
82+
cls._server_process.terminate()
83+
cls._server_process.join()
84+
85+
def _pre_setup(self):
86+
"""Handled manually in `setUpClass` to speed things up."""
87+
88+
def _post_teardown(self):
89+
"""Handled manually in `tearDownClass` to prevent TransactionTestCase from doing
90+
database flushing. This is needed to prevent a `SynchronousOnlyOperation` from
91+
occuring due to a bug within `ChannelsLiveServerTestCase`."""
92+
93+
def setUp(self): ...

0 commit comments

Comments
 (0)
Please sign in to comment.