From ffb339a1cf008bc6b59d1a6067cec5300537b204 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Tue, 21 May 2024 21:41:44 +0200 Subject: [PATCH 1/3] set config.deploy_url during AppHarness tests --- reflex/testing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reflex/testing.py b/reflex/testing.py index aa8cbb8935..ad08396324 100644 --- a/reflex/testing.py +++ b/reflex/testing.py @@ -325,6 +325,8 @@ def _wait_frontend(self): m = re.search(reflex.constants.Next.FRONTEND_LISTENING_REGEX, line) if m is not None: self.frontend_url = m.group(1) + config = reflex.config.get_config() + config.deploy_url = self.frontend_url break if self.frontend_url is None: raise RuntimeError("Frontend did not start") From d3ce9a9466b14b8b955471f7b00b17c051968368 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Tue, 21 May 2024 22:43:00 +0200 Subject: [PATCH 2/3] add slightly modified version of @abulvenz test --- integration/test_deploy_url.py | 74 ++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 integration/test_deploy_url.py diff --git a/integration/test_deploy_url.py b/integration/test_deploy_url.py new file mode 100644 index 0000000000..5112e36b8a --- /dev/null +++ b/integration/test_deploy_url.py @@ -0,0 +1,74 @@ +"""Integration tests for deploy_url.""" + +from __future__ import annotations + +from typing import Generator + +import pytest +from selenium.webdriver.common.by import By +from selenium.webdriver.remote.webdriver import WebDriver +from selenium.webdriver.support.ui import WebDriverWait + +from reflex.testing import AppHarness + + +def DeployUrlSample(): + """Sample app for testing config deploy_url is correct (in tests).""" + import reflex as rx + + class State(rx.State): + def goto_self(self): + return rx.redirect(rx.config.get_config().deploy_url) # type: ignore + + def index(): + return rx.fragment( + rx.button("GOTO SELF", on_click=State.goto_self, id="goto_self") + ) + + app = rx.App(state=rx.State) + app.add_page(index) + + +@pytest.fixture(scope="module") +def deploy_url_sample( + tmp_path_factory: pytest.TempPathFactory, +) -> Generator[AppHarness, None, None]: + """AppHarness fixture for testing deploy_url.""" + with AppHarness.create( + root=tmp_path_factory.mktemp("deploy_url_sample"), + app_source=DeployUrlSample, # type: ignore + ) as harness: + yield harness + + +@pytest.fixture() +def driver(deploy_url_sample: AppHarness) -> Generator[WebDriver, None, None]: + """WebDriver fixture for testing deploy_url.""" + assert deploy_url_sample.app_instance is not None, "app is not running" + driver = deploy_url_sample.frontend() + try: + yield driver + finally: + driver.quit() + + +def test_deploy_url(deploy_url_sample: AppHarness, driver: WebDriver): + """Test deploy_url is correct.""" + import reflex as rx + + deploy_url = rx.config.get_config().deploy_url + assert deploy_url is not None + assert deploy_url != "http://localhost:3000" + assert deploy_url == deploy_url_sample.frontend_url + driver.get(deploy_url) + assert driver.current_url == deploy_url + "/" + + +def test_deploy_url_in_app(deploy_url_sample: AppHarness, driver: WebDriver): + """Test deploy_url is correct in app.""" + driver.implicitly_wait(10) + driver.find_element(By.ID, "goto_self").click() + + WebDriverWait(driver, 10).until( + lambda driver: driver.current_url == f"{deploy_url_sample.frontend_url}/" + ) From 28a51c9fb06cb47ea19dbff220de3cb11e9875d2 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Tue, 21 May 2024 23:01:25 +0200 Subject: [PATCH 3/3] fix darglint, add return types --- integration/test_deploy_url.py | 38 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/integration/test_deploy_url.py b/integration/test_deploy_url.py index 5112e36b8a..b0421cfb72 100644 --- a/integration/test_deploy_url.py +++ b/integration/test_deploy_url.py @@ -12,7 +12,7 @@ from reflex.testing import AppHarness -def DeployUrlSample(): +def DeployUrlSample() -> None: """Sample app for testing config deploy_url is correct (in tests).""" import reflex as rx @@ -33,7 +33,14 @@ def index(): def deploy_url_sample( tmp_path_factory: pytest.TempPathFactory, ) -> Generator[AppHarness, None, None]: - """AppHarness fixture for testing deploy_url.""" + """AppHarness fixture for testing deploy_url. + + Args: + tmp_path_factory: pytest fixture for creating temporary directories. + + Yields: + AppHarness: An AppHarness instance. + """ with AppHarness.create( root=tmp_path_factory.mktemp("deploy_url_sample"), app_source=DeployUrlSample, # type: ignore @@ -43,7 +50,14 @@ def deploy_url_sample( @pytest.fixture() def driver(deploy_url_sample: AppHarness) -> Generator[WebDriver, None, None]: - """WebDriver fixture for testing deploy_url.""" + """WebDriver fixture for testing deploy_url. + + Args: + deploy_url_sample: AppHarness fixture for testing deploy_url. + + Yields: + WebDriver: A WebDriver instance. + """ assert deploy_url_sample.app_instance is not None, "app is not running" driver = deploy_url_sample.frontend() try: @@ -52,8 +66,13 @@ def driver(deploy_url_sample: AppHarness) -> Generator[WebDriver, None, None]: driver.quit() -def test_deploy_url(deploy_url_sample: AppHarness, driver: WebDriver): - """Test deploy_url is correct.""" +def test_deploy_url(deploy_url_sample: AppHarness, driver: WebDriver) -> None: + """Test deploy_url is correct. + + Args: + deploy_url_sample: AppHarness fixture for testing deploy_url. + driver: WebDriver fixture for testing deploy_url. + """ import reflex as rx deploy_url = rx.config.get_config().deploy_url @@ -64,8 +83,13 @@ def test_deploy_url(deploy_url_sample: AppHarness, driver: WebDriver): assert driver.current_url == deploy_url + "/" -def test_deploy_url_in_app(deploy_url_sample: AppHarness, driver: WebDriver): - """Test deploy_url is correct in app.""" +def test_deploy_url_in_app(deploy_url_sample: AppHarness, driver: WebDriver) -> None: + """Test deploy_url is correct in app. + + Args: + deploy_url_sample: AppHarness fixture for testing deploy_url. + driver: WebDriver fixture for testing deploy_url. + """ driver.implicitly_wait(10) driver.find_element(By.ID, "goto_self").click()