Skip to content

Commit

Permalink
Merge pull request #1366 from hramezani/lint_test_blueprints
Browse files Browse the repository at this point in the history
Fix some lint errors and warnings in `tests/test_blueprints.py`
  • Loading branch information
yunstanford committed Oct 15, 2018
2 parents def2e03 + dfec182 commit f4b4e3a
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
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')
Expand Down Expand Up @@ -57,22 +59,23 @@ def handler(request):

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
Expand All @@ -83,15 +86,16 @@ def handler(request):
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)
Expand All @@ -102,15 +106,16 @@ def handler(request):
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)
Expand All @@ -121,15 +126,16 @@ def handler(request):
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)
Expand All @@ -140,6 +146,7 @@ def handler(request):
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')

Expand Down Expand Up @@ -173,15 +180,16 @@ def handler2(request):
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)
Expand Down Expand Up @@ -212,14 +220,13 @@ def handler(request):
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)

Expand All @@ -242,6 +249,7 @@ def handler2(request):
headers=headers)
assert response.text == 'Hello3'


def test_bp_middleware(app):
blueprint = Blueprint('test_middleware')

Expand All @@ -260,6 +268,7 @@ async def handler(request):
assert response.status == 200
assert response.text == 'OK'


def test_bp_exception_handler(app):
blueprint = Blueprint('test_middleware')

Expand All @@ -284,14 +293,14 @@ def handler_exception(request, exception):
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'

request, response = app.test_client.get('/3')
assert response.status == 200


def test_bp_listeners(app):
blueprint = Blueprint('test_middleware')

Expand Down Expand Up @@ -325,7 +334,8 @@ def handler_6(sanic, loop):

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())
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -373,37 +385,37 @@ def handler(request):
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()

Expand Down Expand Up @@ -461,31 +473,32 @@ async def handler(request, ws):
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)

app.blueprint(top)

@app.route('/')
def handler(request):
def handler2(request):
return text('TOP_OK')

request, response = app.test_client.get('/')
Expand All @@ -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)

Expand All @@ -534,5 +552,6 @@ def api_v1_info(request):

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}

0 comments on commit f4b4e3a

Please sign in to comment.