- make setup
- make test
- cd hello; chalice local
- curl http://localhost:8000/hello/world
I have left the static path in the app.py so it can be uncommented and the tests re-run to show it working / failing.
I have left the static path in the app.py so it can be uncommented and the tests re-run to show it working / failing.
from chalice import Chalice | |
app = Chalice(app_name='hello') | |
@app.route('/{hello}') | |
def index(hello): | |
return {'hello': hello} | |
# @app.route('/world') | |
# def index(): | |
# return {'hello': 'world'} |
.PHONY: test setup clean clean-pyc clean-test run | |
setup: | |
python -m pip install -e . | |
clean: clean-pyc clean-test | |
clean-pyc: | |
find . -name '*.pyc' -exec rm -f {} + | |
find . -name '*.pyo' -exec rm -f {} + | |
find . -name '*~' -exec rm -f {} + | |
find . -name '__pycache__' -exec rm -fr {} + | |
test: | |
pytest -v tests.py | |
test-lint: | |
flake8 --count |
import sys | |
from setuptools import setup | |
if sys.version_info < (3, 6): | |
sys.exit('Python < 3.6 is not supported') | |
setup( | |
name='hello', | |
packages=['hello'], | |
version='0.0.1', | |
include_package_data=True, | |
install_requires=[ | |
'chalice', | |
'pytest' | |
] | |
) |
from pytest import fixture | |
from hello.app import app | |
import json | |
# Module Methods | |
@fixture | |
def sample_app(): | |
"""Create a clean application instance.""" | |
return app | |
@fixture | |
def create_event(): | |
def create_event_inner( | |
uri, method, path={}, body="" | |
): | |
print("uri: {} method: {}".format(uri, method)) | |
return { | |
'requestContext': { | |
'httpMethod': method, | |
'resourcePath': uri, | |
}, | |
'headers': {}, | |
'pathParameters': path, | |
'queryStringParameters': {}, | |
'body': body, | |
'stageVariables': {}, | |
} | |
return create_event_inner | |
def assert_response_body_is(response, body): | |
assert json.loads(response['body']) == body | |
def test_index(sample_app, create_event): | |
event = create_event( | |
uri='/world', | |
method='GET' | |
) | |
response = sample_app(event, context=None) | |
assert_response_body_is( | |
response, | |
{"hello": "world"} | |
) |