|
| 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