def main() -> None: # pragma: no cover setup_pythonpath() app_path = get_app_path() if os.path.exists(app_path): app = get_current_app() else: app = App() try: app.click() except ConfigurationError as exc: click.echo(str(exc)) sys.exit(1)
def test_render_template(): with tempfile.TemporaryDirectory() as tempdir: path = os.path.join(tempdir, 'index.html') with open(path, 'w') as index: index.write('<html><body>Hello, {{ username }}</body><html>') settings = Settings(TEMPLATES={'DIRS': [tempdir]}) app = App(routes=routes, settings=settings) client = TestClient(app) response = client.get('/render_template/?username=tom') assert response.status_code == 200 assert response.text == '<html><body>Hello, tom</body><html>'
def test_custom_command_with_int_arguments(): def add(a: int, b: int): click.echo(str(a + b)) app = App(commands=[add]) runner = CommandLineRunner(app) result = runner.invoke([]) assert 'add' in result.output result = runner.invoke(['add', '1', '2']) assert result.output == '3\n' assert result.exit_code == 0
def test_custom_command(): def custom(var): click.echo(var) app = App(commands=[custom]) runner = CommandLineRunner(app) result = runner.invoke([]) assert 'custom' in result.output result = runner.invoke(['custom', '123']) assert result.output == '123\n' assert result.exit_code == 0
def test_multiple_dirs(): with tempfile.TemporaryDirectory() as tempdir1: with tempfile.TemporaryDirectory() as tempdir2: path = os.path.join(tempdir2, 'index.txt') with open(path, 'w') as index: index.write('Hello, {{ username }}.') settings = Settings(TEMPLATES={'DIRS': [tempdir1, tempdir2]}) app = App(routes=routes, settings=settings) client = TestClient(app) response = client.get('/render_template/?username=tom') assert response.status_code == 200 assert response.text == 'Hello, tom.'
def test_static_files() -> None: with tempfile.TemporaryDirectory() as tempdir: path = os.path.join(tempdir, 'example.csv') with open(path, 'w') as example_file: example_file.write('1,2,3\n4,5,6\n') routes = [Route('/static/{path}', 'GET', serve_static)] settings = {'STATICS': {'DIR': tempdir}} app = App(routes=routes, settings=settings) client = TestClient(app) response = client.get('/static/example.csv') assert response.status_code == 200 assert response.text == '1,2,3\n4,5,6\n' response = client.head('/static/example.csv') assert response.status_code == 200 assert response.text == '' response = client.get('/static/404') assert response.status_code == 404 response = client.head('/static/404') assert response.status_code == 404
def test_template_not_found(): settings = Settings(TEMPLATES={'DIRS': []}) app = App(routes=routes, settings=settings) client = TestClient(app) with pytest.raises(ConfigurationError): client.get('/render_template/?username=tom')
import os import click from apistar import __version__, exceptions from apistar.app import App from apistar.main import setup_pythonpath from apistar.test import CommandLineRunner app = App() runner = CommandLineRunner(app) def test_help_flag(): result = runner.invoke([]) assert '--help' in result.output assert '--version' in result.output assert 'new' in result.output assert 'run' in result.output assert 'test' in result.output assert result.exit_code == 0 def test_version_flag(): result = runner.invoke(['--version']) assert __version__ in result.output assert result.exit_code == 0 def test_custom_command(): def custom(var):
def set_complete(ident: int, complete: bool): # pragma: nocover pass routes = [ Route('/todo/', 'GET', list_todo), Route('/todo/', 'POST', add_todo), Route('/todo/{ident}/', 'GET', show_todo), Route('/todo/{ident}/', 'PUT', set_complete), Route('/schema/', 'GET', serve_schema), Route('/docs/', 'GET', serve_docs), Route('/schema.js', 'GET', serve_schema_js) ] app = App(routes=routes) client = TestClient(app) expected = APISchema(url='/schema/', content={ 'list_todo': Link(url='/todo/', action='GET', fields=[ Field(name='search', location='query', required=False, schema=coreschema.String()) ]), 'add_todo':