Skip to content

Commit 33b592e

Browse files
author
Andrei Neagu
committed
tests are finally working as expected
1 parent 104e2e9 commit 33b592e

File tree

6 files changed

+76
-28
lines changed

6 files changed

+76
-28
lines changed

services/dynamic-scheduler/tests/unit/api_frontend/routes_internal_scheduler/test__index.py renamed to services/dynamic-scheduler/tests/unit/api_frontend/_routes_internal_scheduler/test__index_.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# pylint:disable=redefined-outer-name
22
# pylint:disable=unused-argument
33

4-
5-
from fastapi import FastAPI
4+
import pytest
65
from helpers import assert_contains_text
76
from playwright.async_api import Page
87
from simcore_service_dynamic_scheduler.api.frontend._utils import get_settings
@@ -18,11 +17,9 @@
1817
]
1918

2019

20+
@pytest.mark.parametrize("use_internal_scheduler", [True])
2121
async def test_placeholder_index(
22-
app_runner: None,
23-
async_page: Page,
24-
server_host_port: str,
25-
not_initialized_app: FastAPI,
22+
app_runner: None, async_page: Page, server_host_port: str
2623
):
2724
await async_page.goto(
2825
f"{server_host_port}{get_settings().DYNAMIC_SCHEDULER_UI_MOUNT_PATH}"

services/dynamic-scheduler/tests/unit/api_frontend/conftest.py

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from collections.abc import AsyncIterable
77
from contextlib import suppress
88

9-
import nicegui
109
import pytest
1110
import sqlalchemy as sa
1211
from fastapi import FastAPI, status
@@ -37,21 +36,19 @@ def disable_status_monitor_background_task(mocker: MockerFixture) -> None:
3736
def app_environment(
3837
monkeypatch: pytest.MonkeyPatch,
3938
app_environment: EnvVarsDict,
40-
use_internal_scheduler: bool, # defined in conftest inside subdirectiories of this module
39+
use_internal_scheduler: bool, # has to be added to every test
4140
postgres_db: sa.engine.Engine,
4241
postgres_host_config: PostgresTestConfig,
4342
disable_status_monitor_background_task: None,
4443
rabbit_service: RabbitSettings,
4544
redis_service: RedisSettings,
4645
remove_redis_data: None,
4746
) -> EnvVarsDict:
48-
setenvs_from_dict(
49-
monkeypatch,
50-
{
51-
"DYNAMIC_SCHEDULER_USE_INTERNAL_SCHEDULER": f"{use_internal_scheduler}",
52-
},
53-
)
54-
return app_environment
47+
to_set = {
48+
"DYNAMIC_SCHEDULER_USE_INTERNAL_SCHEDULER": f"{use_internal_scheduler}",
49+
}
50+
setenvs_from_dict(monkeypatch, to_set)
51+
return {**app_environment, **to_set}
5552

5653

5754
@pytest.fixture
@@ -62,8 +59,59 @@ def server_host_port() -> str:
6259
@pytest.fixture
6360
def not_initialized_app(app_environment: EnvVarsDict) -> FastAPI:
6461
# forces rebuild of middleware stack on next test
65-
nicegui.app.user_middleware.clear()
62+
import importlib
63+
64+
import nicegui
65+
from nicegui import Client, binding, core, run
66+
from nicegui.page import page
67+
from starlette.routing import Route
68+
69+
for route in list(nicegui.app.routes):
70+
if isinstance(route, Route) and route.path.startswith("/_nicegui/auto/static/"):
71+
nicegui.app.remove_route(route.path)
72+
73+
all_page_routes = set(Client.page_routes.values())
74+
all_page_routes.add("/")
75+
for path in all_page_routes:
76+
nicegui.app.remove_route(path)
77+
78+
for route in list(nicegui.app.routes):
79+
if (
80+
isinstance(route, Route)
81+
and "{" in route.path
82+
and "}" in route.path
83+
and not route.path.startswith("/_nicegui/")
84+
):
85+
nicegui.app.remove_route(route.path)
86+
87+
nicegui.app.openapi_schema = None
6688
nicegui.app.middleware_stack = None
89+
nicegui.app.user_middleware.clear()
90+
nicegui.app.urls.clear()
91+
core.air = None
92+
# # NOTE favicon routes must be removed separately because they are not "pages"
93+
# for route in list(nicegui.app.routes):
94+
# if isinstance(route, Route) and route.path.endswith('/favicon.ico'):
95+
# nicegui.app.routes.remove(route)
96+
97+
importlib.reload(core)
98+
importlib.reload(run)
99+
100+
Client.instances.clear()
101+
Client.page_routes.clear()
102+
nicegui.app.reset()
103+
104+
# Client.auto_index_client = Client(
105+
# page("/"), request=None
106+
# ).__enter__() # pylint: disable=unnecessary-dunder-call
107+
# Client.auto_index_client.layout.parent_slot = (
108+
# None # NOTE: otherwise the layout is nested in the previous client
109+
# )
110+
# # NOTE we need to re-add the auto index route because we removed all routes above
111+
# nicegui.app.get("/")(Client.auto_index_client.build_response)
112+
113+
binding.reset()
114+
67115
return create_app()
68116

69117

@@ -118,6 +166,15 @@ def download_playwright_browser() -> None:
118166
async def async_page(download_playwright_browser: None) -> AsyncIterable[Page]:
119167
async with async_playwright() as p:
120168
browser = await p.chromium.launch()
121-
page = await browser.new_page()
169+
# Create a new incognito context (no shared cache/cookies)
170+
context = await browser.new_context()
171+
page = await context.new_page()
172+
# Optional: Intercept requests to forcibly disable cache
173+
await page.route(
174+
"**/*",
175+
lambda route, request: route.continue_(
176+
headers={**request.headers, "Cache-Control": "no-store"}
177+
),
178+
)
122179
yield page
123180
await browser.close()

services/dynamic-scheduler/tests/unit/api_frontend/routes_external_scheduler/conftest.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
)
1212

1313

14-
@pytest.fixture
15-
def use_internal_scheduler() -> bool:
16-
return False
17-
18-
1914
@pytest.fixture
2015
def mock_stop_dynamic_service(mocker: MockerFixture) -> AsyncMock:
2116
async_mock = AsyncMock()

services/dynamic-scheduler/tests/unit/api_frontend/routes_external_scheduler/test__index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
]
4343

4444

45+
@pytest.mark.parametrize("use_internal_scheduler", [False])
4546
async def test_index_with_elements(
4647
app_runner: None,
4748
async_page: Page,
@@ -69,6 +70,7 @@ async def test_index_with_elements(
6970
await assert_contains_text(async_page, "Details", instances=2)
7071

7172

73+
@pytest.mark.parametrize("use_internal_scheduler", [False])
7274
@pytest.mark.parametrize(
7375
"service_status",
7476
[

services/dynamic-scheduler/tests/unit/api_frontend/routes_external_scheduler/test__service.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
]
3939

4040

41+
@pytest.mark.parametrize("use_internal_scheduler", [False])
4142
async def test_service_details_no_status_present(
4243
app_runner: None,
4344
async_page: Page,
@@ -66,6 +67,7 @@ async def test_service_details_no_status_present(
6667
await assert_contains_text(async_page, "Remove from tracking", instances=1)
6768

6869

70+
@pytest.mark.parametrize("use_internal_scheduler", [False])
6971
async def test_service_details_renders_friendly_404(
7072
app_runner: None, async_page: Page, server_host_port: str, node_id: NodeID
7173
):
@@ -75,6 +77,7 @@ async def test_service_details_renders_friendly_404(
7577
await assert_contains_text(async_page, "Sorry could not find any details for")
7678

7779

80+
@pytest.mark.parametrize("use_internal_scheduler", [False])
7881
@pytest.mark.parametrize(
7982
"service_status",
8083
[

services/dynamic-scheduler/tests/unit/api_frontend/routes_internal_scheduler/conftest.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)