Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrated signals support #22

Merged
merged 13 commits into from
Aug 1, 2021
25 changes: 12 additions & 13 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ on:

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
2 changes: 1 addition & 1 deletion requirements.testing.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sanic>=21.3
git+https://github.com/sanic-org/sanic.git@signals
pytest
pytest-asyncio
2 changes: 1 addition & 1 deletion sanic_testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sanic_testing.manager import TestManager

__version__ = "0.7.0b1"
__version__ = "0.7.0b2"
__all__ = ("TestManager",)
11 changes: 7 additions & 4 deletions sanic_testing/reusable.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from sanic import Sanic
from sanic.log import logger
from sanic.request import Request
from sanic.server import trigger_events
from websockets.legacy.client import connect

from .testing import HOST, PORT, TestingResponse
Expand Down Expand Up @@ -56,12 +55,15 @@ def run(self):
self._loop._stopping = False
self.app.router.reset()
self.app.signal_router.reset()
self._run(Sanic.finalize(self.app, None))
trigger_events(self.app.listeners["before_server_start"], self._loop)
self._run(self.app._startup())
self._run(self.app._server_event("init", "before", loop=self._loop))
self._server = self._run(self._server_co)
trigger_events(self.app.listeners["after_server_start"], self._loop)
self._run(self.app._server_event("init", "after", loop=self._loop))

def stop(self):
self._run(
self.app._server_event("shutdown", "before", loop=self._loop)
)
if self._server:
self._server.close()
self._run(self._server.wait_closed())
Expand All @@ -70,6 +72,7 @@ def stop(self):
if self._session:
self._run(self._session.aclose())
self._session = None
self._run(self.app._server_event("shutdown", "after", loop=self._loop))

def _sanic_endpoint_test(
self,
Expand Down
62 changes: 25 additions & 37 deletions sanic_testing/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def json(self):
return self._json


def _blank(self):
...


class SanicTestClient:
def __init__(
self, app: Sanic, port: typing.Optional[int] = PORT, host: str = HOST
Expand All @@ -56,8 +60,12 @@ def __init__(
self.app = app
self.port = port
self.host = host
app.listener("after_server_start")(self._start_test_mode)
app.listener("before_server_stop")(self._end_test_mode)
self.app.test_mode = True
self._do_request = _blank
app.listener("after_server_start")(self._run_request)

def _run_request(self, *args, **kwargs):
return self._do_request(*args, **kwargs)

@classmethod
def _start_test_mode(cls, sanic, *args, **kwargs):
Expand Down Expand Up @@ -163,13 +171,8 @@ def _sanic_endpoint_test(
server_kwargs = server_kwargs or {"auto_reload": False}
_collect_request = partial(self._collect_request, results)

# This is required for the new Sanic router.
# Once that is merged we can remove this here.
try:
self.app.router.reset()
self.app.router.finalize()
except AttributeError:
...
self.app.router.reset()
self.app.signal_router.reset()

if gather_request:
self.app.request_middleware.appendleft( # type: ignore
Expand Down Expand Up @@ -216,19 +219,16 @@ def _sanic_endpoint_test(
# known until this function is called, so fix that here
url = url.replace(":None/", f":{port}/")

self.app.listener("after_server_start")(
partial(
self._collect_response,
method,
url,
exceptions,
results,
**request_kwargs,
)
self._do_request = partial(
self._collect_response,
method,
url,
exceptions,
results,
**request_kwargs,
)

self.app.run(debug=debug, **server_kwargs)
self.app.listeners["after_server_start"].pop()

if exceptions:
raise ValueError(f"Exception during request: {exceptions}")
Expand Down Expand Up @@ -330,9 +330,6 @@ def __init__(
self.gather_request = True
self.last_request = None

app.listener("after_server_start")(self._start_test_mode)
app.listener("before_server_stop")(self._end_test_mode)

def _collect_request(self, request):
if self.gather_request:
self.last_request = request
Expand All @@ -352,14 +349,9 @@ async def request( # type: ignore
) -> typing.Tuple[
typing.Optional[Request], typing.Optional[TestingResponse]
]:

# This is required for the new Sanic router.
# Once that is merged we can remove this here.
try:
self.sanic_app.router.reset()
self.sanic_app.router.finalize()
except AttributeError:
...
self.sanic_app.router.reset()
self.sanic_app.signal_router.reset()
await self.sanic_app._startup() # type: ignore

if not url.startswith(
("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:")
Expand Down Expand Up @@ -416,13 +408,9 @@ async def websocket(self, uri, subprotocols=None, *args, **kwargs):
"subprotocols": subprotocols,
}

# This is required for the new Sanic router.
# Once that is merged we can remove this here.
try:
self.sanic_app.router.reset()
self.sanic_app.router.finalize()
except AttributeError:
...
self.sanic_app.router.reset()
self.sanic_app.signal_router.reset()
await self.sanic_app._startup()

await self.sanic_app(scope, self._ws_receive, self._ws_send)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def test_pickle_app(protocol):
manager = TestManager(app)
assert app._test_manager == manager
my_dict = {"app": app}
app.router.reset()
app.signal_router.reset()
my_pickled = pickle.dumps(my_dict, protocol=protocol)
del my_dict
del app
Expand Down