Skip to content

Commit

Permalink
Introduce ruff execution on the tests folder sanic-org#2871
Browse files Browse the repository at this point in the history
  • Loading branch information
iAndriy committed Dec 8, 2023
1 parent 160ec7a commit 8c9194b
Show file tree
Hide file tree
Showing 35 changed files with 195 additions and 150 deletions.
5 changes: 3 additions & 2 deletions Makefile
@@ -1,3 +1,4 @@
RUFF_FORMATTED_FOLDERS = sanic examples scripts tests
.DEFAULT: help

.PHONY: help
Expand Down Expand Up @@ -53,11 +54,11 @@ docker-test: clean

.PHONY: fix
fix:
ruff check sanic examples scripts --fix
ruff check ${RUFF_FORMATTED_FOLDERS} --fix

.PHONY: format
format:
ruff format sanic examples scripts
ruff format ${RUFF_FORMATTED_FOLDERS}

.PHONY: pretty
pretty: fix format
Expand Down
12 changes: 6 additions & 6 deletions tests/conftest.py
Expand Up @@ -214,12 +214,12 @@ def ext_instance():


@pytest.fixture(autouse=True) # type: ignore
def sanic_ext(ext_instance): # noqa
sanic_ext = MagicMock(__version__="1.2.3")
sanic_ext.Extend = MagicMock()
sanic_ext.Extend.return_value = ext_instance
sys.modules["sanic_ext"] = sanic_ext
yield sanic_ext
def mock_sanic_ext(ext_instance): # noqa
mock_sanic_ext = MagicMock(__version__="1.2.3")
mock_sanic_ext.Extend = MagicMock()
mock_sanic_ext.Extend.return_value = ext_instance
sys.modules["sanic_ext"] = mock_sanic_ext
yield mock_sanic_ext
with suppress(KeyError):
del sys.modules["sanic_ext"]

Expand Down
2 changes: 0 additions & 2 deletions tests/performance/sanic/http_response.py
Expand Up @@ -3,8 +3,6 @@
import sys
import timeit

import asyncpg

from sanic.response import json


Expand Down
19 changes: 14 additions & 5 deletions tests/performance/sanic/varied_server.py
Expand Up @@ -22,7 +22,7 @@ async def test(request):


@app.route("/sync", methods=["GET", "POST"])
def test(request):
def test_json_response(request):
return json({"test": True})


Expand All @@ -37,7 +37,7 @@ def exception(request):


@app.route("/exception/async")
async def test(request):
async def test_server_error(request):
raise ServerError("asunk")


Expand Down Expand Up @@ -67,9 +67,16 @@ def query_string(request):
# sanic.conn = []
# sanic.redis = []
# for x in range(10):
# sanic.conn.append(await asyncpg.connect(user='postgres', password='zomgdev', database='postgres', host='192.168.99.100'))
# sanic.conn.append(await asyncpg.connect(
# user='postgres',
# password='zomgdev',
# database='postgres',
# host='192.168.99.100'
# ))
# for n in range(30):
# connection = await asyncio_redis.Connection.create(host='192.168.99.100', port=6379)
# connection = await asyncio_redis.Connection.create(
# host='192.168.99.100', port=6379
# )
# sanic.redis.append(connection)


Expand All @@ -90,7 +97,9 @@ def query_string(request):
# try:
# values = await app.redis[r].get('my_key')
# except asyncio_redis.exceptions.ConnectionLostError:
# app.redis[r] = await asyncio_redis.Connection.create(host='127.0.0.1', port=6379)
# app.redis[r] = await asyncio_redis.Connection.create(
# host='127.0.0.1', port=6379
# )
# values = await app.redis[r].get('my_key')

# r += 1
Expand Down
3 changes: 1 addition & 2 deletions tests/test_asgi.py
@@ -1,4 +1,3 @@
import asyncio
import logging

from collections import deque, namedtuple
Expand All @@ -12,7 +11,7 @@

from sanic import Sanic
from sanic.application.state import Mode
from sanic.asgi import ASGIApp, Lifespan, MockTransport
from sanic.asgi import Lifespan, MockTransport
from sanic.exceptions import BadRequest, Forbidden, ServiceUnavailable
from sanic.request import Request
from sanic.response import json, text
Expand Down
12 changes: 6 additions & 6 deletions tests/test_blueprint_copy.py
Expand Up @@ -84,7 +84,7 @@ def test_bp_copy_without_route_overwriting(app: Sanic):
bpv1 = Blueprint("bp_v1", version=1, url_prefix="my_api")

@bpv1.route("/")
async def handler(request: Request):
async def handler_v1(request: Request):
return text("v1")

app.blueprint(bpv1)
Expand All @@ -100,15 +100,15 @@ async def handler(request: Request):
with pytest.raises(RouteExists, match="Route already registered*"):

@bpv2.route("/")
async def handler(request: Request):
async def handler_v2(request: Request):
return text("v2")

app.blueprint(bpv2)

with pytest.raises(RouteExists, match="Route already registered*"):

@bpv3.route("/")
async def handler(request: Request):
async def handler_v3(request: Request):
return text("v3")

app.blueprint(bpv3)
Expand All @@ -118,7 +118,7 @@ def test_bp_copy_with_route_overwriting(app: Sanic):
bpv1 = Blueprint("bp_v1", version=1, url_prefix="my_api")

@bpv1.route("/")
async def handler(request: Request):
async def handler_v1(request: Request):
return text("v1")

app.blueprint(bpv1)
Expand All @@ -129,13 +129,13 @@ async def handler(request: Request):
)

@bpv2.route("/")
async def handler(request: Request):
async def handler_v2(request: Request):
return text("v2")

app.blueprint(bpv2)

@bpv3.route("/")
async def handler(request: Request):
async def handler_v3(request: Request):
return text("v3")

app.blueprint(bpv3)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blueprint_group.py
Expand Up @@ -22,7 +22,7 @@ def test_bp_group_indexing(app: Sanic):
group = Blueprint.group(blueprint_1, blueprint_2)
assert group[0] == blueprint_1

with raises(expected_exception=IndexError) as e:
with raises(expected_exception=IndexError):
_ = group[3]


Expand Down
4 changes: 0 additions & 4 deletions tests/test_cancellederror.py
@@ -1,9 +1,5 @@
import asyncio

from asyncio import CancelledError

import pytest

from sanic import Request, Sanic, json


Expand Down
2 changes: 1 addition & 1 deletion tests/test_cookies.py
Expand Up @@ -25,7 +25,7 @@ def test_request_cookies():
assert c.getlist("abc") == ["xyz"]
assert c.getlist("") == ["bare", "bare2"]
assert (
c.getlist("bare") == None
c.getlist("bare") is None
) # [] might be sensible but we got None for now


Expand Down
3 changes: 2 additions & 1 deletion tests/test_errorpages.py
Expand Up @@ -471,7 +471,8 @@ def test_config_fallback_bad_value(app):
"",
"html",
"text/*,*/plain",
"The client accepts text/*, using 'html' from FALLBACK_ERROR_FORMAT",
"The client accepts text/*, using 'html' from"
" FALLBACK_ERROR_FORMAT",
),
(
"",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_exceptions_handler.py
Expand Up @@ -7,7 +7,7 @@
import pytest

from bs4 import BeautifulSoup
from pytest import LogCaptureFixture, MonkeyPatch, WarningsRecorder
from pytest import LogCaptureFixture, MonkeyPatch

from sanic import Sanic, handlers
from sanic.exceptions import BadRequest, Forbidden, NotFound, ServerError
Expand Down Expand Up @@ -169,7 +169,7 @@ def import_error_handler():
pass

try:
ModuleNotFoundError
ModuleNotFoundError # noqa: F823
except Exception:

class ModuleNotFoundError(ImportError):
Expand Down
28 changes: 14 additions & 14 deletions tests/test_ext_integration.py
@@ -1,14 +1,12 @@
import sys

from unittest.mock import MagicMock

import pytest

from sanic import Sanic


try:
import sanic_ext
import sanic_ext # noqa: F401

SANIC_EXT_IN_ENV = True
except ImportError:
Expand All @@ -24,37 +22,37 @@ async def stop(*_):
return app


def test_ext_is_loaded(stoppable_app: Sanic, sanic_ext):
def test_ext_is_loaded(stoppable_app: Sanic, mock_sanic_ext):
stoppable_app.run(single_process=True)
sanic_ext.Extend.assert_called_once_with(stoppable_app)
mock_sanic_ext.Extend.assert_called_once_with(stoppable_app)


def test_ext_is_not_loaded(stoppable_app: Sanic, sanic_ext):
def test_ext_is_not_loaded(stoppable_app: Sanic, mock_sanic_ext):
stoppable_app.config.AUTO_EXTEND = False
stoppable_app.run(single_process=True)
sanic_ext.Extend.assert_not_called()
mock_sanic_ext.Extend.assert_not_called()


def test_extend_with_args(stoppable_app: Sanic, sanic_ext):
def test_extend_with_args(stoppable_app: Sanic, mock_sanic_ext):
stoppable_app.extend(built_in_extensions=False)
stoppable_app.run(single_process=True)
sanic_ext.Extend.assert_called_once_with(
mock_sanic_ext.Extend.assert_called_once_with(
stoppable_app, built_in_extensions=False, config=None, extensions=None
)


def test_access_object_sets_up_extension(app: Sanic, sanic_ext):
def test_access_object_sets_up_extension(app: Sanic, mock_sanic_ext):
app.ext
sanic_ext.Extend.assert_called_once_with(app)
mock_sanic_ext.Extend.assert_called_once_with(app)


def test_extend_cannot_be_called_multiple_times(app: Sanic, sanic_ext):
def test_extend_cannot_be_called_multiple_times(app: Sanic, mock_sanic_ext):
app.extend()

message = "Cannot extend Sanic after Sanic Extensions has been setup."
with pytest.raises(RuntimeError, match=message):
app.extend()
sanic_ext.Extend.assert_called_once_with(
mock_sanic_ext.Extend.assert_called_once_with(
app, extensions=None, built_in_extensions=True, config=None
)

Expand All @@ -71,7 +69,9 @@ def test_fail_if_not_loaded(app: Sanic):
app.extend(built_in_extensions=False)


def test_can_access_app_ext_while_running(app: Sanic, sanic_ext, ext_instance):
def test_can_access_app_ext_while_running(
app: Sanic, mock_sanic_ext, ext_instance
):
class IceCream:
flavor: str

Expand Down
4 changes: 3 additions & 1 deletion tests/test_headers.py
Expand Up @@ -59,7 +59,9 @@ def raised_ceiling():
'form-data; name="foo%22;bar\\"; filename="😀"',
("form-data", {"name": 'foo";bar\\', "filename": "😀"}),
# cgi: ('form-data', {'name': 'foo%22;bar"; filename="😀'})
# werkzeug (pre 2.3.0): ('form-data', {'name': 'foo%22;bar"; filename='})
# werkzeug (pre 2.3.0): (
# 'form-data', {'name': 'foo%22;bar"; filename='}
# )
),
],
)
Expand Down
1 change: 0 additions & 1 deletion tests/test_multiprocessing.py
Expand Up @@ -14,7 +14,6 @@
from sanic import Blueprint, text
from sanic.compat import use_context
from sanic.log import logger
from sanic.server.socket import configure_socket


@pytest.mark.skipif(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_reloader.py
Expand Up @@ -12,7 +12,8 @@
import pytest


# We need to interrupt the autoreloader without killing it, so that the server gets terminated
# We need to interrupt the autoreloader without killing it,
# so that the server gets terminated
# https://stefan.sofa-rockers.org/2013/08/15/handling-sub-process-hierarchies-python-linux-os-x/

try:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_request_data.py
Expand Up @@ -59,7 +59,8 @@ def modify(request, response):
"has_missing": False,
"invalid": "'types.SimpleNamespace' object has no attribute 'missing'",
"response_mw_valid": "sanic",
"response_mw_invalid": "'types.SimpleNamespace' object has no attribute 'missing'",
"response_mw_invalid": "'types.SimpleNamespace' object has no"
" attribute 'missing'",
}


Expand Down
10 changes: 5 additions & 5 deletions tests/test_request_stream.py
Expand Up @@ -572,24 +572,24 @@ async def client_task(app, loop):
app.stop()

async def client(app, reader, writer):
# Unfortunately httpx does not support 2-way streaming, so do it by hand.
host = f"host: localhost:8000\r\n".encode()
# httpx doesn't support 2-way streaming,so do it by hand.
host = "host: localhost:8000\r\n".encode()
writer.write(
b"POST /echo HTTP/1.1\r\n" + host + b"content-length: 2\r\n"
b"content-type: text/plain; charset=utf-8\r\n"
b"\r\n"
)
# Read response
res = b""
while not b"\r\n\r\n" in res:
while b"\r\n\r\n" not in res:
res += await reader.read(4096)
assert res.startswith(b"HTTP/1.1 200 OK\r\n")
assert res.endswith(b"\r\n\r\n")
buffer = b""

async def read_chunk():
nonlocal buffer
while not b"\r\n" in buffer:
while b"\r\n" not in buffer:
data = await reader.read(4096)
assert data
buffer += data
Expand Down Expand Up @@ -618,6 +618,6 @@ async def read_chunk():
assert res == b"-"

res = await read_chunk()
assert res == None
assert res is None

app.run(access_log=False, single_process=True)

0 comments on commit 8c9194b

Please sign in to comment.