-
Notifications
You must be signed in to change notification settings - Fork 21
Add support for Sanic 21+ #71
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| Sanic<21 | ||
| Sanic>=20,<21 | ||
| aioredis<2 | ||
| sanic_redis<0.3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Sanic>=21,<22 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Sanic>=22,<23 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # these are constrained by the files in tests/constraints/*.txt | ||
| # to support a triple stack Django/Flask/Sanic | ||
| aiohttp | ||
| aioredis | ||
| Sanic | ||
| sanic_redis | ||
| uvloop>=0.14.0rc1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| # these are constrained by the files in tests/constraints/*.txt | ||
| # to support a triple stack Django/Flask/Sanic | ||
| aiohttp | ||
| aioredis<2.0.0 | ||
| aioredis | ||
| Sanic | ||
| sanic_redis<0.3.0 | ||
| sanic_redis | ||
| sanic-testing | ||
| uvloop>=0.14.0rc1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,21 +3,26 @@ | |
| # file, you can obtain one at http://mozilla.org/MPL/2.0/. | ||
| import functools | ||
| import logging | ||
| import socket | ||
| import uuid | ||
|
|
||
| import aioredis | ||
| import pytest | ||
| import sanic.testing | ||
| import sanic | ||
| import sanic_redis.core | ||
| from sanic import Sanic, response | ||
| from sanic_redis import SanicRedis | ||
|
|
||
| from dockerflow import health | ||
| from dockerflow.sanic import Dockerflow, checks | ||
|
|
||
| if sanic.__version__.startswith("20."): | ||
| from sanic.testing import SanicTestClient | ||
| else: | ||
| from sanic_testing.testing import SanicTestClient | ||
|
|
||
|
|
||
| class FakeRedis: | ||
| def __init__(self, error=None, **kw): | ||
| def __init__(self, *args, error=None, **kw): | ||
| self.error = error | ||
|
|
||
| def __await__(self): | ||
|
|
@@ -30,15 +35,19 @@ def __enter__(self): | |
| def __exit__(self, *exc_info): | ||
| pass | ||
|
|
||
| def close(self): | ||
| async def close(self): | ||
| pass | ||
|
|
||
| async def wait_closed(self): | ||
| pass | ||
|
|
||
| async def ping(self): | ||
| if self.error == "connection": | ||
| raise aioredis.ConnectionClosedError("fake") | ||
| if aioredis.__version__.startswith("1."): | ||
| RedisConnectionError = aioredis.ConnectionClosedError | ||
| else: | ||
| RedisConnectionError = aioredis.ConnectionError | ||
| raise RedisConnectionError("fake") | ||
| elif self.error == "redis": | ||
| raise aioredis.RedisError("fake") | ||
| elif self.error == "malformed": | ||
|
|
@@ -47,13 +56,20 @@ async def ping(self): | |
| return b"PONG" | ||
|
|
||
|
|
||
| async def fake_redis(**kw): | ||
| return FakeRedis(**kw) | ||
| class FakeRedis1(FakeRedis): | ||
| def close(self): | ||
| pass | ||
|
|
||
|
|
||
| async def fake_redis(*args, **kw): | ||
| if aioredis.__version__.startswith("1."): | ||
| return FakeRedis1(*args, **kw) | ||
| return FakeRedis(*args, **kw) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def app(): | ||
| app = Sanic("dockerflow") | ||
| app = Sanic(f"dockerflow-{uuid.uuid4().hex}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice way to generate unique app IDs for tests! |
||
|
|
||
| @app.route("/") | ||
| async def root(request): | ||
|
|
@@ -77,30 +93,27 @@ def dockerflow_redis(app): | |
|
|
||
| @pytest.fixture | ||
| def test_client(app): | ||
| # Create SanicTestClient manually and provide a socket object instead of host | ||
| # and port when calling Sanic.run in order to avoid parallel test failures | ||
| # caused by Sanic.test_client bindngs to a static port | ||
| s = socket.socket() | ||
| s.bind((sanic.testing.HOST, 0)) | ||
| try: | ||
| # initialize test_client with socket's port | ||
| test_client = sanic.testing.SanicTestClient(app, s.getsockname()[1]) | ||
| # override app.run to drop host and port in favor of socket | ||
| run = app.run | ||
| app.run = lambda host, port, **kw: run(sock=s, **kw) | ||
| # yield test_client | ||
| yield test_client | ||
| finally: | ||
| s.close() | ||
| return SanicTestClient(app) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love to see code removed! 😍 |
||
|
|
||
|
|
||
| def test_instantiating(app): | ||
| @pytest.mark.skipif(not sanic.__version__.startswith("20."), reason="requires sanic 20") | ||
| def test_instantiating_sanic_20(app): | ||
| dockerflow = Dockerflow() | ||
| assert "dockerflow.heartbeat" not in app.router.routes_names | ||
| dockerflow.init_app(app) | ||
| assert "dockerflow.heartbeat" in app.router.routes_names | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| sanic.__version__.startswith("20."), reason="requires sanic 21 or later" | ||
| ) | ||
| def test_instantiating(app): | ||
| Dockerflow() | ||
| assert ("__heartbeat__",) not in app.router.routes_all | ||
| Dockerflow(app) | ||
| assert ("__heartbeat__",) in app.router.routes_all | ||
|
|
||
|
|
||
| def test_version_exists(dockerflow, mocker, test_client, version_content): | ||
| mocker.patch.object(dockerflow, "_version_callback", return_value=version_content) | ||
| _, response = test_client.get("/__version__") | ||
|
|
@@ -178,7 +191,10 @@ async def warning_check2(): | |
|
|
||
| def test_redis_check(dockerflow_redis, mocker, test_client): | ||
| assert "check_redis_connected" in dockerflow_redis.checks | ||
| mocker.patch.object(sanic_redis.core, "create_redis_pool", fake_redis) | ||
| if aioredis.__version__.startswith("1."): | ||
| mocker.patch.object(sanic_redis.core, "create_redis_pool", fake_redis) | ||
| else: | ||
| mocker.patch.object(sanic_redis.core, "from_url", fake_redis) | ||
| _, response = test_client.get("/__heartbeat__") | ||
| assert response.status == 200 | ||
| assert response.json["status"] == "ok" | ||
|
|
@@ -198,7 +214,10 @@ def test_redis_check(dockerflow_redis, mocker, test_client): | |
| def test_redis_check_error(dockerflow_redis, mocker, test_client, error, messages): | ||
| assert "check_redis_connected" in dockerflow_redis.checks | ||
| fake_redis_error = functools.partial(fake_redis, error=error) | ||
| mocker.patch.object(sanic_redis.core, "create_redis_pool", fake_redis_error) | ||
| if aioredis.__version__.startswith("1."): | ||
| mocker.patch.object(sanic_redis.core, "create_redis_pool", fake_redis_error) | ||
| else: | ||
| mocker.patch.object(sanic_redis.core, "from_url", fake_redis_error) | ||
| _, response = test_client.get("/__heartbeat__") | ||
| assert response.status == 500 | ||
| assert response.json["status"] == "error" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sanic 20.* needs a separate requirements file to remove the
sanic-testingrequirement.