Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 11 additions & 40 deletions packages/django-cf/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
import pytest
import requests
from testlib.host import (
COMPAT_CONFIGS,
GENERATED_FILE_PATTERN,
CompatConfig,
compat_config,
dev_server,
fail_with_log,
pywrangler_sync,
run_dev_server,
)
from testlib.host import (
register_in_worker_suites as register_testlib_suites,
worker_project_dir,
)

__all__ = ["dev_server"]
# Re-export fixtures so pytest discovers them
__all__ = ["compat_config", "dev_server", "worker_project_dir"]

TEST_DIR: Path = Path(__file__).parent
PACKAGE_DIR: Path = TEST_DIR.parent
Expand All @@ -41,27 +40,23 @@ class DevServer:
base_url: str


def _fail(log_path: Path, message: str) -> None:
pytest.fail(
f"{message}\n\n--- pywrangler dev log ---\n{log_path.read_text(errors='replace')}"
)


def _seed(base_url: str, log_path: Path) -> None:
for endpoint in ("__run_migrations__", "__create_admin__"):
try:
response = requests.get(f"{base_url}/{endpoint}/", timeout=SEED_TIMEOUT)
except requests.RequestException as error:
_fail(log_path, f"GET /{endpoint}/ failed: {error}")
fail_with_log(log_path, f"GET /{endpoint}/ failed: {error}")
else:
if response.status_code != 200:
_fail(
fail_with_log(
log_path,
f"GET /{endpoint}/ returned {response.status_code}: {response.text[:2000]}",
)
payload = response.json()
if payload.get("status") == "error":
_fail(log_path, f"GET /{endpoint}/ reported: {payload.get('message')}")
fail_with_log(
log_path, f"GET /{endpoint}/ reported: {payload.get('message')}"
)


def _serve(project_dir: Path, tmp_path: Path) -> Generator[DevServer]:
Expand Down Expand Up @@ -108,30 +103,6 @@ def r2_web_server(tmp_path_factory: pytest.TempPathFactory) -> Generator[DevServ
yield from _serve(R2_PROJECT, tmp_path_factory.mktemp("r2"))


@pytest.fixture(
scope="module",
params=COMPAT_CONFIGS,
ids=[c.python_version for c in COMPAT_CONFIGS],
)
def compat_config(request: pytest.FixtureRequest) -> CompatConfig:
return request.param


@pytest.fixture(scope="module")
def worker_project_dir() -> Path:
"""Worker project the `dev_server` fixture should serve.

Test modules using `dev_server` must override this fixture.
"""
raise NotImplementedError(
"override the `worker_project_dir` fixture in your test module"
)


@pytest.fixture(scope="module")
def dev_startup_timeout():
def dev_startup_timeout() -> int:
return DEV_STARTUP_TIMEOUT


def register_in_worker_suites(namespace: dict, src_dir: Path) -> None:
register_testlib_suites(namespace, src_dir, source_roots=[PACKAGE_DIR])
20 changes: 9 additions & 11 deletions packages/django-cf/tests/in_worker/test_in_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,28 @@

import pytest
import requests
from testlib.host import (
COMPAT_CONFIGS,
compat_config_fixture,
register_in_worker_suites,
)

from tests.conftest import COMPAT_CONFIGS, CompatConfig, register_in_worker_suites
from tests.conftest import PACKAGE_DIR

IN_WORKER_PROJECT: Path = Path(__file__).parent / "worker"
IN_WORKER_SRC_DIR: Path = IN_WORKER_PROJECT / "src"

MATRIX: list[CompatConfig] = [c for c in COMPAT_CONFIGS if c.python_version != "3.12"]


@pytest.fixture(
scope="module",
params=MATRIX,
ids=[c.python_version for c in MATRIX],
compat_config = compat_config_fixture(
[c for c in COMPAT_CONFIGS if c.python_version != "3.12"]
)
def compat_config(request: pytest.FixtureRequest) -> CompatConfig:
return request.param


@pytest.fixture(scope="module")
def worker_project_dir() -> Path:
return IN_WORKER_PROJECT


register_in_worker_suites(globals(), IN_WORKER_SRC_DIR)
register_in_worker_suites(globals(), IN_WORKER_SRC_DIR, source_roots=[PACKAGE_DIR])


def test_django_wsgi_header_transformation(dev_server: str) -> None:
Expand Down
6 changes: 0 additions & 6 deletions packages/django-cf/tests/in_worker/worker/src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

import pytest
from django.core.cache import cache
from workers import env as _env


@pytest.fixture(autouse=True)
def clear_cache():
cache.clear()


@pytest.fixture
def env():
return _env
10 changes: 0 additions & 10 deletions packages/django-cf/tests/in_worker/worker/src/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import django
import django.conf
import pytest
from _django_app import R2_LOCATION, django_wsgi_app
from django.http import HttpResponse, JsonResponse, StreamingHttpResponse
from django.urls import path
Expand Down Expand Up @@ -136,15 +135,6 @@ def _django_headers_view(request):
]


class EnvPlugin:
def __init__(self, env):
self._env = env

@pytest.fixture
def env(self):
return self._env


class Default(DjangoCF, TestRunnerEntrypoint):
def get_app(self):
return django_wsgi_app()
Expand Down
9 changes: 0 additions & 9 deletions packages/runtime-sdk/tests/bindings-test/src/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
# pyright: reportMissingImports=false
import uuid

import pytest

from workers import env as _env


@pytest.fixture
def env():
return _env


def unique_table_name() -> str:
"""Unique per call"""
Expand Down
50 changes: 5 additions & 45 deletions packages/runtime-sdk/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
"""Shared fixtures and helpers for the host-side test suite."""

from pathlib import Path
from typing import Any

import pytest
from testlib.host import (
COMPAT_CONFIGS,
CompatConfig,
compat_config,
dev_server,
dev_startup_timeout,
worker_project_dir,
)
from testlib.host import (
register_in_worker_suites as register_testlib_suites,
)

__all__ = ["dev_server"]


TEST_DIR: Path = Path(__file__).parent
# Re-export fixtures so pytest discovers them
__all__ = ["compat_config", "dev_server", "dev_startup_timeout", "worker_project_dir"]

OPT_IN_MARKERS: tuple[str, ...] = ("hyperdrive",)

Expand All @@ -33,36 +26,3 @@ def pytest_collection_modifyitems(
for item in items:
if marker in item.keywords:
item.add_marker(skip)


@pytest.fixture(
scope="module",
params=COMPAT_CONFIGS,
ids=[c.python_version for c in COMPAT_CONFIGS],
)
def compat_config(request: pytest.FixtureRequest) -> CompatConfig:
return request.param


@pytest.fixture(scope="module")
def worker_project_dir() -> Path:
"""Worker project the `dev_server` fixture should serve.

Test modules using `dev_server` must override this fixture.
"""
raise NotImplementedError(
"override the `worker_project_dir` fixture in your test module"
)


@pytest.fixture(scope="module")
def dev_startup_timeout():
return 120


def register_in_worker_suites(
namespace: dict[str, Any],
src_dir: Path,
marks: dict[str, pytest.MarkDecorator] | None = None,
) -> None:
register_testlib_suites(namespace, src_dir, marks=marks)
2 changes: 1 addition & 1 deletion packages/runtime-sdk/tests/test_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pathlib import Path

import pytest
from conftest import register_in_worker_suites
from testlib.host import register_in_worker_suites

BINDINGS_TEST_DIR: Path = Path(__file__).parent / "bindings-test"
BINDINGS_SRC_DIR: Path = BINDINGS_TEST_DIR / "src"
Expand Down
17 changes: 7 additions & 10 deletions packages/runtime-sdk/tests/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from pathlib import Path

import pytest
from conftest import COMPAT_CONFIGS, CompatConfig, register_in_worker_suites
from testlib.host import (
COMPAT_CONFIGS,
compat_config_fixture,
register_in_worker_suites,
)

WEB_FRAMEWORKS_DIR: Path = (
Path(__file__).parent / "web-frameworks-test" / "django-async"
Expand All @@ -26,16 +30,9 @@ def worker_project_dir() -> Path:


# Exclude Python 3.12 (Pyodide 0.26.0a2) for async tests
ASYNC_COMPAT_CONFIGS = [c for c in COMPAT_CONFIGS if c.python_version != "3.12"]


@pytest.fixture(
scope="module",
params=ASYNC_COMPAT_CONFIGS,
ids=[c.python_version for c in ASYNC_COMPAT_CONFIGS],
compat_config = compat_config_fixture(
[c for c in COMPAT_CONFIGS if c.python_version != "3.12"]
)
def compat_config(request: pytest.FixtureRequest) -> CompatConfig:
return request.param


register_in_worker_suites(globals(), WEB_FRAMEWORKS_SRC_DIR)
19 changes: 7 additions & 12 deletions packages/runtime-sdk/tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from pathlib import Path

import pytest
from conftest import COMPAT_CONFIGS, CompatConfig, register_in_worker_suites
from testlib.host import (
COMPAT_CONFIGS,
compat_config_fixture,
register_in_worker_suites,
)

WEB_FRAMEWORKS_DIR: Path = (
Path(__file__).parent / "web-frameworks-test" / "fastapi-tests"
Expand All @@ -23,18 +27,9 @@ def worker_project_dir() -> Path:


# Exclude Python 3.12 (Pyodide 0.26.0a2) for async tests.
FASTAPI_COMPAT_CONFIGS = [
c for c in COMPAT_CONFIGS if c.python_version not in ("3.12",)
]


@pytest.fixture(
scope="module",
params=FASTAPI_COMPAT_CONFIGS,
ids=[c.python_version for c in FASTAPI_COMPAT_CONFIGS],
compat_config = compat_config_fixture(
[c for c in COMPAT_CONFIGS if c.python_version != "3.12"]
)
def compat_config(request: pytest.FixtureRequest) -> CompatConfig:
return request.param


register_in_worker_suites(globals(), WEB_FRAMEWORKS_SRC_DIR)
11 changes: 1 addition & 10 deletions packages/runtime-sdk/tests/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path

import pytest
from conftest import COMPAT_CONFIGS, CompatConfig, register_in_worker_suites
from testlib.host import register_in_worker_suites

WEB_FRAMEWORKS_DIR = Path(__file__).parent / "web-frameworks-test" / "flask-tests"
WEB_FRAMEWORKS_SRC_DIR = WEB_FRAMEWORKS_DIR / "src"
Expand All @@ -14,13 +14,4 @@ def worker_project_dir() -> Path:
return WEB_FRAMEWORKS_DIR


@pytest.fixture(
scope="module",
params=COMPAT_CONFIGS,
ids=[config.python_version for config in COMPAT_CONFIGS],
)
def compat_config(request: pytest.FixtureRequest) -> CompatConfig:
return request.param


register_in_worker_suites(globals(), WEB_FRAMEWORKS_SRC_DIR)
4 changes: 1 addition & 3 deletions packages/runtime-sdk/tests/test_in_workerd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from pathlib import Path

import pytest
from conftest import (
from testlib.host import (
COMPAT_CONFIGS,
CompatConfig,
)
from testlib.host import (
configure_compatibility,
link_packages,
pywrangler_sync,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,6 @@ async def native_file():
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")


class EnvPlugin:
def __init__(self, env):
self._env = env

@pytest.fixture
def env(self):
return self._env


class FastAPIAppPlugin:
@pytest.fixture
def fastapi_app(self):
Expand Down
Loading
Loading