Exemplo n.º 1
0
async def test_start_other_dir(tmpdir, loop, test_client, caplog):
    StartProject(path=str(tmpdir.join('the-path')), name='foobar', database=DatabaseChoice.NONE)
    assert {p.basename for p in tmpdir.listdir()} == {'the-path'}
    assert {p.basename for p in tmpdir.join('the-path').listdir()} == {
        'app',
        'Makefile',
        'requirements.txt',
        'README.md',
        'activate.settings.sh',
        'setup.cfg',
        'static',
        'tests',
    }
    assert """\
adev.main INFO: Starting new aiohttp project "foobar" at "/<tmpdir>/the-path"
adev.main INFO: config:
    template_engine: jinja
    session: secure
    database: none
    example: message-board
adev.main INFO: project created, 16 files generated\n""" == caplog.log.replace(str(tmpdir), '/<tmpdir>')
    config = Config(app_path='the-path/app/', root_path=str(tmpdir))
    app = config.app_factory(loop=loop)
    modify_main_app(app, config)
    assert isinstance(app, aiohttp.web.Application)

    cli = await test_client(app)
    r = await cli.get('/')
    assert r.status == 200
    text = await r.text()
    assert "Success! you've setup a basic aiohttp app." in text
Exemplo n.º 2
0
async def test_all_options(tmpdir, test_client, loop, template_engine, session,
                           database, example):
    StartProject(
        path=str(tmpdir),
        name='foobar',
        template_engine=template_engine,
        session=session,
        database=database,
        example=example,
    )
    assert 'app' in {p.basename for p in tmpdir.listdir()}
    style_guide = flake8.get_style_guide()
    report = style_guide.check_files([str(tmpdir)])
    assert report.total_errors == 0
    if database != 'none':
        return
    config = Config(app_path='app/main.py',
                    root_path=str(tmpdir),
                    static_path='.')

    app_factory = config.import_app_factory()
    app = app_factory()
    modify_main_app(app, config)
    cli = await test_client(app)
    r = await cli.get('/')
    assert r.status == 200
    text = await r.text()
    assert '<title>foobar</title>' in text
Exemplo n.º 3
0
async def test_start_run(tmpdir, loop, aiohttp_client, smart_caplog):
    StartProject(path=str(tmpdir.join('the-path')), name='foobar')
    assert {p.basename for p in tmpdir.listdir()} == {'the-path'}
    assert {p.basename
            for p in tmpdir.join('the-path').listdir()} == {
                'app',
                'requirements.txt',
                'README.md',
                'static',
            }
    assert """\
adev.main INFO: Starting new aiohttp project "foobar" at "/<tmpdir>/the-path"
adev.main INFO: project created, 13 files generated\n""" == smart_caplog.log.replace(
        str(tmpdir), '/<tmpdir>')
    config = Config(app_path='the-path/app/',
                    root_path=str(tmpdir),
                    static_path='.')
    app_factory = config.import_app_factory()
    app = await app_factory()
    modify_main_app(app, config)
    assert isinstance(app, aiohttp.web.Application)

    cli = await aiohttp_client(app)
    r = await cli.get('/')
    assert r.status == 200
    text = await r.text()
    assert "Success! you&#39;ve setup a basic aiohttp app." in text
Exemplo n.º 4
0
async def test_db_creation(tmpdir, test_client, loop):
    StartProject(
        path=str(tmpdir),
        name='foobar postgres test',
        template_engine=TemplateChoice.JINJA,
        session=SessionChoices.NONE,
        database=DatabaseChoice.PG_SA,
        example=ExampleChoice.MESSAGE_BOARD,
    )
    assert 'app' in {p.basename for p in tmpdir.listdir()}
    style_guide = flake8.get_style_guide()
    report = style_guide.check_files([str(tmpdir)])
    assert report.total_errors == 0
    db_password = os.getenv('APP_DB_PASSWORD', '')
    env = {
        'APP_DB_PASSWORD': db_password,
        'PATH': os.getenv('PATH', ''),
    }
    p = subprocess.run(['make', 'reset-database'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                       cwd=str(tmpdir), env=env, universal_newlines=True)
    assert p.returncode == 0, p.stdout
    assert 'creating database "foobar"...'
    assert 'creating tables from model definition...'

    os.environ['APP_DB_PASSWORD'] = db_password
    config = Config(app_path='app/main.py', root_path=str(tmpdir))

    app = config.app_factory(loop=loop)
    modify_main_app(app, config)
    cli = await test_client(app)
    r = await cli.get('/')
    assert r.status == 200
    text = await r.text()
    assert '<title>foobar postgres test</title>' in text
def test_modify_main_app_all_on(tmpworkdir):
    mktree(tmpworkdir, SIMPLE_APP)
    config = Config(app_path='app.py', debug_toolbar=True, static_path='.')
    app = DummyApplication()
    modify_main_app(app, config)
    assert len(app.on_response_prepare) == 1
    assert len(app.middlewares) == 2
    assert app['static_root_url'] == 'http://localhost:8001/static'
    assert app._debug is True
Exemplo n.º 6
0
def test_modify_main_app_all_off(tmpworkdir):
    mktree(tmpworkdir, SIMPLE_APP)
    config = Config(app_path='app.py', livereload=False, host='foobar.com', static_path='.')
    app = DummyApplication()
    modify_main_app(app, config)
    assert len(app.on_response_prepare) == 0
    assert len(app.middlewares) == 0
    assert app['static_root_url'] == 'http://foobar.com:8001/static'
    assert app._debug is True
Exemplo n.º 7
0
async def test_run_app_test_client(loop, tmpworkdir, test_client):
    mktree(tmpworkdir, SIMPLE_APP)
    config = Config(app_path='app.py')
    app = config.app_factory(loop=loop)
    modify_main_app(app, config)
    assert isinstance(app, aiohttp.web.Application)
    cli = await test_client(app)
    r = await cli.get('/')
    assert r.status == 200
    text = await r.text()
    assert text == 'hello world'
Exemplo n.º 8
0
def test_modify_main_app_all_on(tmpworkdir):
    mktree(tmpworkdir, SIMPLE_APP)
    config = Config(app_path='app.py', static_path='.')
    app = DummyApplication()
    subapp = DummyApplication()
    app.add_subapp("/sub/", subapp)
    modify_main_app(app, config)  # type: ignore[arg-type]
    assert len(app.on_response_prepare) == 1
    assert len(app.middlewares) == 1
    assert app['static_root_url'] == 'http://localhost:8001/static'
    assert subapp['static_root_url'] == "http://localhost:8001/static"
    assert app._debug is True
Exemplo n.º 9
0
async def test_modify_main_app_on_prepare(tmpworkdir):
    mktree(tmpworkdir, SIMPLE_APP)
    config = Config(app_path='app.py', host='foobar.com')
    app = DummyApplication()
    modify_main_app(app, config)  # type: ignore[arg-type]
    on_prepare = app.on_response_prepare[0]
    request = MagicMock(spec=Request)
    request.path = '/'
    response = MagicMock(spec=Response)
    response.body = b'<h1>body</h1>'
    response.content_type = 'text/html'
    await on_prepare(request, response)
    assert response.body == b'<h1>body</h1>\n<script src="http://foobar.com:8001/livereload.js"></script>\n'