Merge pull request #1366 from hramezani/lint_test_blueprints
Fix some lint errors and warnings in `tests/test_blueprints.py`
This commit is contained in:
commit
f4b4e3a58c
|
@ -16,11 +16,13 @@ from sanic.constants import HTTP_METHODS
|
|||
def get_file_path(static_file_directory, file_name):
|
||||
return os.path.join(static_file_directory, file_name)
|
||||
|
||||
|
||||
def get_file_content(static_file_directory, file_name):
|
||||
"""The content of the static file to check"""
|
||||
with open(get_file_path(static_file_directory, file_name), 'rb') as file:
|
||||
return file.read()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('method', HTTP_METHODS)
|
||||
def test_versioned_routes_get(app, method):
|
||||
bp = Blueprint('test_text')
|
||||
|
@ -57,22 +59,23 @@ def test_bp(app):
|
|||
|
||||
assert response.text == 'Hello'
|
||||
|
||||
|
||||
def test_bp_strict_slash(app):
|
||||
bp = Blueprint('test_text')
|
||||
|
||||
@bp.get('/get', strict_slashes=True)
|
||||
def handler(request):
|
||||
def get_handler(request):
|
||||
return text('OK')
|
||||
|
||||
@bp.post('/post/', strict_slashes=True)
|
||||
def handler(request):
|
||||
def post_handler(request):
|
||||
return text('OK')
|
||||
|
||||
app.blueprint(bp)
|
||||
|
||||
request, response = app.test_client.get('/get')
|
||||
assert response.text == 'OK'
|
||||
assert response.json == None
|
||||
assert response.json is None
|
||||
|
||||
request, response = app.test_client.get('/get/')
|
||||
assert response.status == 404
|
||||
|
@ -83,15 +86,16 @@ def test_bp_strict_slash(app):
|
|||
request, response = app.test_client.post('/post')
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_bp_strict_slash_default_value(app):
|
||||
bp = Blueprint('test_text', strict_slashes=True)
|
||||
|
||||
@bp.get('/get')
|
||||
def handler(request):
|
||||
def get_handler(request):
|
||||
return text('OK')
|
||||
|
||||
@bp.post('/post/')
|
||||
def handler(request):
|
||||
def post_handler(request):
|
||||
return text('OK')
|
||||
|
||||
app.blueprint(bp)
|
||||
|
@ -102,15 +106,16 @@ def test_bp_strict_slash_default_value(app):
|
|||
request, response = app.test_client.post('/post')
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_bp_strict_slash_without_passing_default_value(app):
|
||||
bp = Blueprint('test_text')
|
||||
|
||||
@bp.get('/get')
|
||||
def handler(request):
|
||||
def get_handler(request):
|
||||
return text('OK')
|
||||
|
||||
@bp.post('/post/')
|
||||
def handler(request):
|
||||
def post_handler(request):
|
||||
return text('OK')
|
||||
|
||||
app.blueprint(bp)
|
||||
|
@ -121,15 +126,16 @@ def test_bp_strict_slash_without_passing_default_value(app):
|
|||
request, response = app.test_client.post('/post')
|
||||
assert response.text == 'OK'
|
||||
|
||||
|
||||
def test_bp_strict_slash_default_value_can_be_overwritten(app):
|
||||
bp = Blueprint('test_text', strict_slashes=True)
|
||||
|
||||
@bp.get('/get', strict_slashes=False)
|
||||
def handler(request):
|
||||
def get_handler(request):
|
||||
return text('OK')
|
||||
|
||||
@bp.post('/post/', strict_slashes=False)
|
||||
def handler(request):
|
||||
def post_handler(request):
|
||||
return text('OK')
|
||||
|
||||
app.blueprint(bp)
|
||||
|
@ -140,6 +146,7 @@ def test_bp_strict_slash_default_value_can_be_overwritten(app):
|
|||
request, response = app.test_client.post('/post')
|
||||
assert response.text == 'OK'
|
||||
|
||||
|
||||
def test_bp_with_url_prefix(app):
|
||||
bp = Blueprint('test_text', url_prefix='/test1')
|
||||
|
||||
|
@ -173,15 +180,16 @@ def test_several_bp_with_url_prefix(app):
|
|||
request, response = app.test_client.get('/test2/')
|
||||
assert response.text == 'Hello2'
|
||||
|
||||
|
||||
def test_bp_with_host(app):
|
||||
bp = Blueprint('test_bp_host', url_prefix='/test1', host="example.com")
|
||||
|
||||
@bp.route('/')
|
||||
def handler(request):
|
||||
def handler1(request):
|
||||
return text('Hello')
|
||||
|
||||
@bp.route('/', host="sub.example.com")
|
||||
def handler(request):
|
||||
def handler2(request):
|
||||
return text('Hello subdomain!')
|
||||
|
||||
app.blueprint(bp)
|
||||
|
@ -212,14 +220,13 @@ def test_several_bp_with_host(app):
|
|||
return text('Hello')
|
||||
|
||||
@bp2.route('/')
|
||||
def handler2(request):
|
||||
def handler1(request):
|
||||
return text('Hello2')
|
||||
|
||||
@bp2.route('/other/')
|
||||
def handler2(request):
|
||||
return text('Hello3')
|
||||
|
||||
|
||||
app.blueprint(bp)
|
||||
app.blueprint(bp2)
|
||||
|
||||
|
@ -242,6 +249,7 @@ def test_several_bp_with_host(app):
|
|||
headers=headers)
|
||||
assert response.text == 'Hello3'
|
||||
|
||||
|
||||
def test_bp_middleware(app):
|
||||
blueprint = Blueprint('test_middleware')
|
||||
|
||||
|
@ -260,6 +268,7 @@ def test_bp_middleware(app):
|
|||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
|
||||
|
||||
def test_bp_exception_handler(app):
|
||||
blueprint = Blueprint('test_middleware')
|
||||
|
||||
|
@ -284,7 +293,6 @@ def test_bp_exception_handler(app):
|
|||
request, response = app.test_client.get('/1')
|
||||
assert response.status == 400
|
||||
|
||||
|
||||
request, response = app.test_client.get('/2')
|
||||
assert response.status == 200
|
||||
assert response.text == 'OK'
|
||||
|
@ -292,6 +300,7 @@ def test_bp_exception_handler(app):
|
|||
request, response = app.test_client.get('/3')
|
||||
assert response.status == 200
|
||||
|
||||
|
||||
def test_bp_listeners(app):
|
||||
blueprint = Blueprint('test_middleware')
|
||||
|
||||
|
@ -325,7 +334,8 @@ def test_bp_listeners(app):
|
|||
|
||||
request, response = app.test_client.get('/')
|
||||
|
||||
assert order == [1,2,3,4,5,6]
|
||||
assert order == [1, 2, 3, 4, 5, 6]
|
||||
|
||||
|
||||
def test_bp_static(app):
|
||||
current_file = inspect.getfile(inspect.currentframe())
|
||||
|
@ -342,6 +352,7 @@ def test_bp_static(app):
|
|||
assert response.status == 200
|
||||
assert response.body == current_file_contents
|
||||
|
||||
|
||||
@pytest.mark.parametrize('file_name', ['test.html'])
|
||||
def test_bp_static_content_type(app, file_name):
|
||||
# This is done here, since no other test loads a file here
|
||||
|
@ -363,6 +374,7 @@ def test_bp_static_content_type(app, file_name):
|
|||
assert response.body == get_file_content(static_directory, file_name)
|
||||
assert response.headers['Content-Type'] == 'text/html; charset=utf-8'
|
||||
|
||||
|
||||
def test_bp_shorthand(app):
|
||||
blueprint = Blueprint('test_shorhand_routes')
|
||||
ev = asyncio.Event()
|
||||
|
@ -373,37 +385,37 @@ def test_bp_shorthand(app):
|
|||
return text('OK')
|
||||
|
||||
@blueprint.put('/put')
|
||||
def handler(request):
|
||||
def put_handler(request):
|
||||
assert request.stream is None
|
||||
return text('OK')
|
||||
|
||||
@blueprint.post('/post')
|
||||
def handler(request):
|
||||
def post_handler(request):
|
||||
assert request.stream is None
|
||||
return text('OK')
|
||||
|
||||
@blueprint.head('/head')
|
||||
def handler(request):
|
||||
def head_handler(request):
|
||||
assert request.stream is None
|
||||
return text('OK')
|
||||
|
||||
@blueprint.options('/options')
|
||||
def handler(request):
|
||||
def options_handler(request):
|
||||
assert request.stream is None
|
||||
return text('OK')
|
||||
|
||||
@blueprint.patch('/patch')
|
||||
def handler(request):
|
||||
def patch_handler(request):
|
||||
assert request.stream is None
|
||||
return text('OK')
|
||||
|
||||
@blueprint.delete('/delete')
|
||||
def handler(request):
|
||||
def delete_handler(request):
|
||||
assert request.stream is None
|
||||
return text('OK')
|
||||
|
||||
@blueprint.websocket('/ws')
|
||||
async def handler(request, ws):
|
||||
async def websocket_handler(request, ws):
|
||||
assert request.stream is None
|
||||
ev.set()
|
||||
|
||||
|
@ -461,23 +473,24 @@ def test_bp_shorthand(app):
|
|||
assert response.status == 101
|
||||
assert ev.is_set()
|
||||
|
||||
|
||||
def test_bp_group(app):
|
||||
deep_0 = Blueprint('deep_0', url_prefix='/deep')
|
||||
deep_1 = Blueprint('deep_1', url_prefix = '/deep1')
|
||||
deep_1 = Blueprint('deep_1', url_prefix='/deep1')
|
||||
|
||||
@deep_0.route('/')
|
||||
def handler(request):
|
||||
return text('D0_OK')
|
||||
|
||||
@deep_1.route('/bottom')
|
||||
def handler(request):
|
||||
def bottom_handler(request):
|
||||
return text('D1B_OK')
|
||||
|
||||
mid_0 = Blueprint.group(deep_0, deep_1, url_prefix='/mid')
|
||||
mid_1 = Blueprint('mid_tier', url_prefix='/mid1')
|
||||
|
||||
@mid_1.route('/')
|
||||
def handler(request):
|
||||
def handler1(request):
|
||||
return text('M1_OK')
|
||||
|
||||
top = Blueprint.group(mid_0, mid_1)
|
||||
|
@ -485,7 +498,7 @@ def test_bp_group(app):
|
|||
app.blueprint(top)
|
||||
|
||||
@app.route('/')
|
||||
def handler(request):
|
||||
def handler2(request):
|
||||
return text('TOP_OK')
|
||||
|
||||
request, response = app.test_client.get('/')
|
||||
|
@ -505,24 +518,29 @@ def test_bp_group_with_default_url_prefix(app):
|
|||
|
||||
from sanic.response import json
|
||||
bp_resources = Blueprint('bp_resources')
|
||||
|
||||
@bp_resources.get('/')
|
||||
def list_resources_handler(request):
|
||||
resource = {}
|
||||
return json([resource])
|
||||
|
||||
bp_resource = Blueprint('bp_resource', url_prefix='/<resource_id>')
|
||||
|
||||
@bp_resource.get('/')
|
||||
def get_resource_hander(request, resource_id):
|
||||
resource = {'resource_id': resource_id}
|
||||
return json(resource)
|
||||
|
||||
bp_resources_group = Blueprint.group(bp_resources, bp_resource, url_prefix='/resources')
|
||||
bp_resources_group = Blueprint.group(bp_resources, bp_resource,
|
||||
url_prefix='/resources')
|
||||
bp_api_v1 = Blueprint('bp_api_v1')
|
||||
|
||||
@bp_api_v1.get('/info')
|
||||
def api_v1_info(request):
|
||||
return text('api_version: v1')
|
||||
|
||||
bp_api_v1_group = Blueprint.group(bp_api_v1, bp_resources_group, url_prefix='/v1')
|
||||
bp_api_v1_group = Blueprint.group(bp_api_v1, bp_resources_group,
|
||||
url_prefix='/v1')
|
||||
bp_api_group = Blueprint.group(bp_api_v1_group, url_prefix='/api')
|
||||
app.blueprint(bp_api_group)
|
||||
|
||||
|
@ -534,5 +552,6 @@ def test_bp_group_with_default_url_prefix(app):
|
|||
|
||||
from uuid import uuid4
|
||||
resource_id = str(uuid4())
|
||||
request, response = app.test_client.get('/api/v1/resources/{0}'.format(resource_id))
|
||||
request, response = app.test_client.get(
|
||||
'/api/v1/resources/{0}'.format(resource_id))
|
||||
assert response.json == {'resource_id': resource_id}
|
||||
|
|
Loading…
Reference in New Issue
Block a user