diff --git a/fastly_compute/testing/__init__.py b/fastly_compute/testing/__init__.py new file mode 100644 index 0000000..07fc269 --- /dev/null +++ b/fastly_compute/testing/__init__.py @@ -0,0 +1,14 @@ +"""Utilities for customers to use in their own tests""" + +# This module exists only to re-export public things. +# ruff: noqa F401 + +from .mock_http_server import MockHttpServer, make_test_request_handler +from .viceroy import ( + AutoViceroyTestBase, + ViceroyServer, + ViceroyReturn, + ViceroyException, + ViceroyTestBase, + on_viceroy, +) diff --git a/fastly_compute/test_server.py b/fastly_compute/testing/mock_http_server.py similarity index 99% rename from fastly_compute/test_server.py rename to fastly_compute/testing/mock_http_server.py index 89defb5..45793fb 100644 --- a/fastly_compute/test_server.py +++ b/fastly_compute/testing/mock_http_server.py @@ -126,7 +126,7 @@ def make_test_request_handler( ) -class LocalTestServer: +class MockHttpServer: """Local HTTP server for backend testing. This server can be used to mock external backends during testing. diff --git a/fastly_compute/testing.py b/fastly_compute/testing/viceroy.py similarity index 98% rename from fastly_compute/testing.py rename to fastly_compute/testing/viceroy.py index b6bbfad..faae714 100644 --- a/fastly_compute/testing.py +++ b/fastly_compute/testing/viceroy.py @@ -341,7 +341,7 @@ def ephemeral_wasm(cls): import bottle from bottle import Bottle, post -from fastly_compute.testing import AutoViceroyTestBase, _ViceroyException, _ViceroyReturn +from fastly_compute.testing import AutoViceroyTestBase, ViceroyException, ViceroyReturn AutoViceroyTestBase._is_on_viceroy = True import {cls.__module__} @@ -355,7 +355,7 @@ def ephemeral_wasm(cls): @app.post("/") def run_viceroy_chunk(func_path: str) -> bytes: """Run an `@on_viceroy`-decorated method from a test class in Viceroy, and - return its result, wrapped in a pickled _ViceroyReturn or _ViceroyException, + return its result, wrapped in a pickled ViceroyReturn or ViceroyException, over HTTP. :arg func_path: Fully qualified name of the function to run, typically like @@ -374,9 +374,9 @@ def run_viceroy_chunk(func_path: str) -> bytes: try: return_value = method(class_, *shipped_args, **shipped_kwargs) except Exception as exc: - result = _ViceroyException(exc) + result = ViceroyException(exc) else: - result = _ViceroyReturn(return_value) + result = ViceroyReturn(return_value) return pickle.dumps(result) @@ -419,7 +419,7 @@ def _as_class_method(method) -> classmethod: return classmethod(method) if isinstance(method, MethodType) else method -class _ViceroyException: +class ViceroyException: """An exception passed back from Viceroy-dwelling code""" def __init__(self, exception: Exception): @@ -430,7 +430,7 @@ def raise_or_return_value(self): raise self.exception -class _ViceroyReturn: +class ViceroyReturn: """A function return value passed back from Viceroy-dwelling code""" def __init__(self, return_value: Any): diff --git a/fastly_compute/tests/test_backend_requests.py b/fastly_compute/tests/test_backend_requests.py index fc8ec95..e186dc5 100644 --- a/fastly_compute/tests/test_backend_requests.py +++ b/fastly_compute/tests/test_backend_requests.py @@ -6,8 +6,7 @@ import pytest import requests -from fastly_compute.test_server import LocalTestServer -from fastly_compute.testing import ViceroyTestBase +from fastly_compute.testing import MockHttpServer, ViceroyTestBase class BackendRequestsTestBase(ViceroyTestBase): @@ -105,7 +104,7 @@ def setup_class(cls): } # Set up mock server - cls.test_server = LocalTestServer( + cls.test_server = MockHttpServer( host="127.0.0.1", port=0, responses=mock_responses ) cls.test_server_url = cls.test_server.start() @@ -225,7 +224,7 @@ def setup_class(cls): }, } - cls.test_server = LocalTestServer(host="127.0.0.1", port=0, responses=responses) + cls.test_server = MockHttpServer(host="127.0.0.1", port=0, responses=responses) cls.test_server_url = cls.test_server.start() # Set up backends for viceroy @@ -395,7 +394,7 @@ class TestRequestErrorHandling(BackendRequestsTestBase): def setup_class(cls): """Set up test backend.""" # Create a local test server for backend testing - cls.test_server = LocalTestServer(host="127.0.0.1", port=0) + cls.test_server = MockHttpServer(host="127.0.0.1", port=0) base_url = cls.test_server.start() # Set up backends for viceroy (keep the full URL with scheme) @@ -539,7 +538,7 @@ class TestBackendResolution(BackendRequestsTestBase): @classmethod def setup_class(cls): """Set up multiple backends for testing.""" - cls.test_server1 = LocalTestServer(host="127.0.0.1", port=0) + cls.test_server1 = MockHttpServer(host="127.0.0.1", port=0) base_url1 = cls.test_server1.start() cls.set_up_backends( @@ -597,7 +596,7 @@ class TestHTTPMethodHandling(BackendRequestsTestBase): @classmethod def setup_class(cls): """Set up test backend.""" - cls.test_server = LocalTestServer(host="127.0.0.1", port=0) + cls.test_server = MockHttpServer(host="127.0.0.1", port=0) base_url = cls.test_server.start() cls.set_up_backends({"test-be": base_url})