Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Apr 12, 2024
1 parent 6407262 commit 898a4e2
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 21 deletions.
9 changes: 2 additions & 7 deletions rest_tools/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import asyncio
import logging
import time
from typing import Any, Optional, Dict

import requests

from .client import RestClient

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -34,11 +31,9 @@ async def request_and_validate(
Useful for testing and debugging.
"""
url, kwargs = rc._prepare(method, path, args=args)
await asyncio.sleep(0)
logging.critical(f"{time.time()} - {method} {path} {args} {url} {kwargs}")

# run request as async in case of other dependent, concurrent actions (ex: test suite runs server in same process)
response = await asyncio.wrap_future(rc.session.request(method, url, **kwargs)) # type: ignore[var-annotated,arg-type]
await asyncio.sleep(0)
logging.critical(f"{time.time()} - response: {response}")

# duck typing magic
class _DuckResponse(openapi_core.protocols.Response):
Expand Down
24 changes: 10 additions & 14 deletions tests/integrate_openapi/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Integration test fixtures."""

import logging
import socket
import time
from typing import AsyncIterator, Callable

import pytest
import pytest_asyncio
import tornado

from rest_tools.client import RestClient
from rest_tools.server import RestServer, RestHandler
Expand All @@ -24,22 +23,19 @@ def port() -> int:
return ephemeral_port


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

async def post(self) -> None:
logging.critical(f"{time.time()} - HERE IS THE POST")
self.write({})
# if self.get_argument("raise", None):
# raise tornado.web.HTTPError(400, self.get_argument("raise"))
# self.write(self.get_argument("echo", {}))


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

rs = RestServer(debug=True)

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

async 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", {}))

rs.add_route(TestHandler.ROUTE, TestHandler)
rs.startup(address="localhost", port=port)

Expand Down
175 changes: 175 additions & 0 deletions tests/integrate_openapi/validate_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
"""Test route handlers for the OpenAPI spec validation."""

import openapi_core
from jsonschema_path import SchemaPath

OPENAPI_SPEC = openapi_core.OpenAPI(
SchemaPath.from_dict(
{
"openapi": "3.1.0",
"info": {
"title": "Test Schema",
"summary": "A schema for testing",
"description": "This is a test description",
"contact": {
"name": "WIPAC Developers",
"url": "icecube.wisc.edu",
"email": "[email protected]",
},
"license": {"name": "MIT License"},
"version": "0.0.0",
},
"components": {
"parameters": {
"TaskforceUUIDParam": {
"name": "taskforce_uuid",
"in": "path",
"required": True,
"description": "the taskforce object's uuid",
"schema": {"type": "string"},
},
},
"schemas": {
"TaskDirectiveObject": {
"type": "object",
"properties": {
"task_id": {"type": "string"},
"cluster_locations": {
"type": "array",
"items": {"type": "string"},
"minItems": 1,
},
"task_image": {"type": "string"},
"task_args": {"type": "string"},
"timestamp": {"type": "integer"},
"aborted": {"type": "boolean"},
},
"required": [],
"additionalProperties": False,
},
},
},
"paths": {
"/echo/this": {
"parameters": [],
"get": {
"responses": {
"200": {
"description": "the openapi schema",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {},
"additionalProperties": True,
}
}
},
},
"400": {
"description": "invalid request arguments",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"code": {
"description": "http error code",
"type": "integer",
},
"error": {
"description": "http error reason",
"type": "string",
},
},
"required": ["code", "error"],
"additionalProperties": False,
}
}
},
},
}
},
},
"/task/directive": {
"parameters": [],
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"cluster_locations": {
"$ref": "#/components/schemas/TaskDirectiveObject/properties/cluster_locations"
},
"task_image": {
"$ref": "#/components/schemas/TaskDirectiveObject/properties/task_image"
},
"task_args": {
"$ref": "#/components/schemas/TaskDirectiveObject/properties/task_args"
},
"n_workers": {
"$ref": "#/components/schemas/TaskforceObject/properties/n_workers"
},
"worker_config": {
"$ref": "#/components/schemas/TaskforceObject/properties/worker_config"
},
"environment": {
"$ref": "#/components/schemas/TaskforceObject/properties/container_config/properties/environment"
},
"input_files": {
"$ref": "#/components/schemas/TaskforceObject/properties/container_config/properties/input_files"
},
},
"required": [
"cluster_locations",
"task_image",
"task_args",
"worker_config",
],
"additionalProperties": False,
}
}
}
},
"responses": {
"200": {
"description": "the matching task directive",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TaskDirectiveObject"
}
}
},
},
"400": {
"description": "invalid request arguments",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"code": {
"description": "http error code",
"type": "integer",
},
"error": {
"description": "http error reason",
"type": "string",
},
},
"required": ["code", "error"],
"additionalProperties": False,
}
}
},
},
},
},
},
},
}
)
)

0 comments on commit 898a4e2

Please sign in to comment.