Skip to content

Commit

Permalink
use a conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Apr 11, 2024
1 parent 10d69d9 commit 54515f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
50 changes: 50 additions & 0 deletions tests/integrate_openapi/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Integration test fixtures."""

import socket
from typing import AsyncIterator, Callable

import pytest
import pytest_asyncio
import tornado
from tornado.web import RequestHandler

from rest_tools.client import RestClient
from rest_tools.server import RestServer, RestHandlerSetup


@pytest.fixture
def port() -> int:
"""Get an ephemeral port number."""
# unix.stackexchange.com/a/132524
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
addr = s.getsockname()
ephemeral_port = addr[1]
s.close()
return ephemeral_port


@pytest_asyncio.fixture
async def server(port: int) -> AsyncIterator[Callable[[], RestClient]]:
"""Start up REST server and attach handlers."""

class TestHandler(RequestHandler):
ROUTE = "/echo/this"

def post(self) -> None:
if self.get_argument("raise", None):
raise tornado.web.HTTPError(400, self.get_argument("raise"))
self.write(self.get_argument("echo"))

args = RestHandlerSetup({"debug": True})
rs = RestServer(debug=True)
rs.add_route(TestHandler.ROUTE, TestHandler, args)
rs.startup(address="localhost", port=port)

def client() -> RestClient:
return RestClient(f"http://localhost:{port}", retries=0)

try:
yield client
finally:
await rs.stop() # type: ignore[no-untyped-call]
46 changes: 1 addition & 45 deletions tests/integrate_openapi/request_and_validate_test.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,15 @@
"""Test client.utils.request_and_validate module."""

import socket
from typing import AsyncIterator, Callable
from typing import Callable

import openapi_core
import pytest
import pytest_asyncio
import requests
import tornado
from jsonschema_path import SchemaPath
from openapi_core.validation.response import exceptions
from tornado.web import RequestHandler

from rest_tools.client import RestClient
from rest_tools.client.utils import request_and_validate
from rest_tools.server import RestServer, RestHandlerSetup


@pytest.fixture
def port() -> int:
"""Get an ephemeral port number."""
# unix.stackexchange.com/a/132524
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
addr = s.getsockname()
ephemeral_port = addr[1]
s.close()
return ephemeral_port


@pytest_asyncio.fixture
async def server(port: int) -> AsyncIterator[Callable[[], RestClient]]:
"""Start up REST server and attach handlers."""

class TestHandler(RequestHandler):
ROUTE = "/echo/this"

def post(self) -> None:
if self.get_argument("raise", None):
raise tornado.web.HTTPError(400, self.get_argument("raise"))
self.write(self.get_argument("echo"))

args = RestHandlerSetup({"debug": True})
rs = RestServer(debug=True)
rs.add_route(TestHandler.ROUTE, TestHandler, args)
rs.startup(address="localhost", port=port)

def client() -> RestClient:
return RestClient(f"http://localhost:{port}", retries=0)

try:
yield client
finally:
await rs.stop() # type: ignore[no-untyped-call]


OPENAPI_SPEC = openapi_core.OpenAPI(
SchemaPath.from_dict(
Expand Down

0 comments on commit 54515f4

Please sign in to comment.