From 5f2a72203ff6e07690dc8f31c2b1831501ec4e34 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 26 Oct 2018 07:29:46 -0400 Subject: [PATCH] cleanup things --- responder/api.py | 45 +++++++++++++++++++++-------------------- tests/conftest.py | 13 ++++++++++++ tests/test_responder.py | 12 +++++++---- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/responder/api.py b/responder/api.py index eb60a5a..a5f9cad 100644 --- a/responder/api.py +++ b/responder/api.py @@ -1,42 +1,40 @@ -import os import json +import os from functools import partial from pathlib import Path -import uvicorn import apistar -import yaml -import jinja2 import itsdangerous -from graphql_server import encode_execution_results, json_encode, default_format_error -from starlette.websockets import WebSocket +import jinja2 +import uvicorn +import yaml +from apispec import APISpec, yaml_utils +from apispec.ext.marshmallow import MarshmallowPlugin +from asgiref.wsgi import WsgiToAsgi +from graphql_server import default_format_error, encode_execution_results, json_encode from starlette.debug import DebugMiddleware +from starlette.exceptions import ExceptionMiddleware from starlette.lifespan import LifespanHandler +from starlette.middleware.cors import CORSMiddleware +from starlette.middleware.gzip import GZipMiddleware +from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware from starlette.routing import Router from starlette.staticfiles import StaticFiles from starlette.testclient import TestClient -from starlette.middleware.gzip import GZipMiddleware -from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware -from starlette.middleware.cors import CORSMiddleware -from starlette.exceptions import ExceptionMiddleware -from apispec import APISpec -from apispec.ext.marshmallow import MarshmallowPlugin -from apispec import yaml_utils -from asgiref.wsgi import WsgiToAsgi +from starlette.websockets import WebSocket from whitenoise import WhiteNoise -from . import models -from . import status_codes -from .routes import Route -from .formats import get_formats +from . import models, status_codes from .background import BackgroundQueue -from .templates import GRAPHIQL +from .formats import get_formats +from .routes import Route from .statics import ( DEFAULT_API_THEME, - DEFAULT_SESSION_COOKIE, - DEFAULT_SECRET_KEY, DEFAULT_CORS_PARAMS, + DEFAULT_SECRET_KEY, + DEFAULT_SESSION_COOKIE, ) +from .templates import GRAPHIQL # TODO: consider moving status codes here @@ -645,4 +643,7 @@ class API: if port is None: port = 5042 - uvicorn.run(self, host=address, port=port, debug=debug, **options) + def spawn(): + uvicorn.run(self, host=address, port=port, debug=debug, **options) + + spawn() diff --git a/tests/conftest.py b/tests/conftest.py index 9ce1da0..821edfb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,8 @@ import graphene import responder from pathlib import Path import pytest +import multiprocessing +import concurrent.futures @pytest.fixture @@ -45,6 +47,17 @@ def flask(): return app +@pytest.fixture +def instance(api): + def run_api(api): + api.run() + + pool = concurrent.futures.ThreadPoolExecutor(max_workers=1) + f = pool.submit(run_api, api) + # api.run(daemon=True) + return "http://localhost:5042" + + @pytest.fixture def schema(): class Query(graphene.ObjectType): diff --git a/tests/test_responder.py b/tests/test_responder.py index 395d6c3..4fe1fd9 100644 --- a/tests/test_responder.py +++ b/tests/test_responder.py @@ -490,19 +490,23 @@ def test_kinda_websockets(api): await ws.close() -# TODO: this doesn't really test this. -def test_startup(api, session): +@pytest.mark.xfail +def test_startup(api, instance, session): + who = [None] + @api.route("/{greeting}") async def greet_world(req, resp, *, greeting): - resp.text = f"{greeting}, world!" + resp.text = f"{greeting}, {who[0]}!" @api.on_event("startup") async def asd(): + nonlocal who + who[0] = "world" print("startup") @api.on_event("cleanup") async def asd(): print("cleanup") - r = session.get(api.url_for(greet_world, greeting="hello")) + r = session.get(f"{instance}/hello") assert r.text == "hello, world!"