Fix some lint errors and warnings in tests/test_blueprints.py.

This commit is contained in:
Hasan Ramezani 2018-10-14 15:31:13 +02:00
parent 619bb79a2f
commit dfec18278b

View File

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