[tests] rework some tests, avoid calling project_runner multiple times (should be tested better, but for now is just buggy imports...)
This commit is contained in:
parent
7e7bbb3a62
commit
53ecbb58fc
@ -16,76 +16,75 @@ Cli = cli.cli
|
|||||||
PROJNAME = os.environ.get('PROJ','tmp_api')
|
PROJNAME = os.environ.get('PROJ','tmp_api')
|
||||||
|
|
||||||
|
|
||||||
class TestCli():
|
def test_options(runner):
|
||||||
def test_options(self, runner):
|
# Wrong command
|
||||||
# Wrong command
|
with runner.isolated_filesystem():
|
||||||
with runner.isolated_filesystem():
|
r = runner.invoke(Cli, ['foobar'])
|
||||||
r = runner.invoke(Cli, ['foobar'])
|
|
||||||
assert r.exit_code == 2
|
|
||||||
|
|
||||||
# Test existing commands
|
|
||||||
with runner.isolated_filesystem():
|
|
||||||
r = runner.invoke(Cli, ['--help'])
|
|
||||||
assert r.exit_code == 0
|
|
||||||
|
|
||||||
with runner.isolated_filesystem():
|
|
||||||
r = runner.invoke(Cli, ['--version'])
|
|
||||||
assert r.exit_code == 0
|
|
||||||
|
|
||||||
with runner.isolated_filesystem():
|
|
||||||
r = runner.invoke(Cli, ['init', '--help'])
|
|
||||||
assert r.exit_code == 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_project_fail(self, runner):
|
|
||||||
# Missing argument (project)
|
|
||||||
testproject = 'testproject'
|
|
||||||
r = runner.invoke(Cli, ['init'])
|
|
||||||
assert r.exit_code == 2
|
assert r.exit_code == 2
|
||||||
|
|
||||||
with runner.isolated_filesystem():
|
# Test existing commands
|
||||||
# Fail : Wrong project name
|
with runner.isolated_filesystem():
|
||||||
r = runner.invoke(Cli, ['init', 'test*-project'])
|
r = runner.invoke(Cli, ['--help'])
|
||||||
assert r.exit_code == 1
|
assert r.exit_code == 0
|
||||||
|
|
||||||
with runner.isolated_filesystem():
|
with runner.isolated_filesystem():
|
||||||
# Fail : Already existing folder
|
r = runner.invoke(Cli, ['--version'])
|
||||||
os.mkdir(testproject)
|
assert r.exit_code == 0
|
||||||
r = runner.invoke(Cli, ['init', testproject])
|
|
||||||
assert r.exit_code == 1
|
|
||||||
|
|
||||||
with runner.isolated_filesystem():
|
with runner.isolated_filesystem():
|
||||||
# Fail : Already existing nod
|
r = runner.invoke(Cli, ['init', '--help'])
|
||||||
os.mknod(testproject)
|
assert r.exit_code == 0
|
||||||
r = runner.invoke(Cli, ['init', testproject])
|
|
||||||
assert r.exit_code == 1
|
|
||||||
|
|
||||||
def test_init_project(self, runner, halfapi_conf_dir):
|
|
||||||
"""
|
|
||||||
"""
|
|
||||||
cp = ConfigParser()
|
|
||||||
with runner.isolated_filesystem():
|
|
||||||
env = {
|
|
||||||
'HALFAPI_CONF_DIR': halfapi_conf_dir
|
|
||||||
}
|
|
||||||
|
|
||||||
res = runner.invoke(Cli, ['init', PROJNAME], env=env)
|
|
||||||
try:
|
|
||||||
assert os.path.isdir(PROJNAME)
|
|
||||||
assert os.path.isdir(os.path.join(PROJNAME, '.halfapi'))
|
|
||||||
|
|
||||||
|
|
||||||
# .halfapi/config check
|
def test_init_project_fail(runner):
|
||||||
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'config'))
|
# Missing argument (project)
|
||||||
cp.read(os.path.join(PROJNAME, '.halfapi', 'config'))
|
testproject = 'testproject'
|
||||||
assert cp.has_section('project')
|
r = runner.invoke(Cli, ['init'])
|
||||||
assert cp.has_option('project', 'name')
|
assert r.exit_code == 2
|
||||||
assert cp.get('project', 'name') == PROJNAME
|
|
||||||
assert cp.get('project', 'halfapi_version') == __version__
|
|
||||||
assert cp.has_section('domain')
|
|
||||||
except AssertionError as exc:
|
|
||||||
subprocess.run(['tree', '-a', os.getcwd()])
|
|
||||||
raise exc
|
|
||||||
|
|
||||||
assert res.exit_code == 0
|
with runner.isolated_filesystem():
|
||||||
assert res.exception is None
|
# Fail : Wrong project name
|
||||||
|
r = runner.invoke(Cli, ['init', 'test*-project'])
|
||||||
|
assert r.exit_code == 1
|
||||||
|
|
||||||
|
with runner.isolated_filesystem():
|
||||||
|
# Fail : Already existing folder
|
||||||
|
os.mkdir(testproject)
|
||||||
|
r = runner.invoke(Cli, ['init', testproject])
|
||||||
|
assert r.exit_code == 1
|
||||||
|
|
||||||
|
with runner.isolated_filesystem():
|
||||||
|
# Fail : Already existing nod
|
||||||
|
os.mknod(testproject)
|
||||||
|
r = runner.invoke(Cli, ['init', testproject])
|
||||||
|
assert r.exit_code == 1
|
||||||
|
|
||||||
|
def test_init_project(runner):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
cp = ConfigParser()
|
||||||
|
with runner.isolated_filesystem():
|
||||||
|
env = {
|
||||||
|
'HALFAPI_CONF_DIR': '.halfapi'
|
||||||
|
}
|
||||||
|
|
||||||
|
res = runner.invoke(Cli, ['init', PROJNAME], env=env)
|
||||||
|
try:
|
||||||
|
assert os.path.isdir(PROJNAME)
|
||||||
|
assert os.path.isdir(os.path.join(PROJNAME, '.halfapi'))
|
||||||
|
|
||||||
|
|
||||||
|
# .halfapi/config check
|
||||||
|
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'config'))
|
||||||
|
cp.read(os.path.join(PROJNAME, '.halfapi', 'config'))
|
||||||
|
assert cp.has_section('project')
|
||||||
|
assert cp.has_option('project', 'name')
|
||||||
|
assert cp.get('project', 'name') == PROJNAME
|
||||||
|
assert cp.get('project', 'halfapi_version') == __version__
|
||||||
|
assert cp.has_section('domain')
|
||||||
|
except AssertionError as exc:
|
||||||
|
subprocess.run(['tree', '-a', os.getcwd()])
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
assert res.exit_code == 0
|
||||||
|
assert res.exception is None
|
||||||
|
@ -1,44 +1,57 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
import sys
|
||||||
|
import pytest
|
||||||
from halfapi.halfapi import HalfAPI
|
from halfapi.halfapi import HalfAPI
|
||||||
|
|
||||||
halfapi_arg = { 'domain': { 'name': 'dummy_domain', 'router': 'routers' } }
|
class TestConf(TestCase):
|
||||||
def test_conf_production_default():
|
def setUp(self):
|
||||||
halfapi = HalfAPI({
|
self.args = {
|
||||||
**halfapi_arg
|
'domain': {
|
||||||
})
|
'name': 'dummy_domain',
|
||||||
assert halfapi.PRODUCTION is True
|
'router': 'dummy_domain.routers'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def test_conf_production_true():
|
def test_conf_production_default(self):
|
||||||
halfapi = HalfAPI({
|
halfapi = HalfAPI({
|
||||||
**halfapi_arg,
|
**self.args
|
||||||
'production': True,
|
})
|
||||||
})
|
assert halfapi.PRODUCTION is True
|
||||||
assert halfapi.PRODUCTION is True
|
|
||||||
|
|
||||||
def test_conf_production_false():
|
def test_conf_production_true(self):
|
||||||
halfapi = HalfAPI({
|
halfapi = HalfAPI({
|
||||||
**halfapi_arg,
|
**self.args,
|
||||||
'production': False,
|
'production': True,
|
||||||
})
|
})
|
||||||
assert halfapi.PRODUCTION is False
|
assert halfapi.PRODUCTION is True
|
||||||
|
|
||||||
def test_conf_variables():
|
def test_conf_production_false(self):
|
||||||
from halfapi.conf import (
|
halfapi = HalfAPI({
|
||||||
CONFIG,
|
**self.args,
|
||||||
SCHEMA,
|
'production': False,
|
||||||
SECRET,
|
})
|
||||||
DOMAINSDICT,
|
assert halfapi.PRODUCTION is False
|
||||||
PROJECT_NAME,
|
|
||||||
HOST,
|
|
||||||
PORT,
|
|
||||||
CONF_DIR
|
|
||||||
)
|
|
||||||
|
|
||||||
assert isinstance(CONFIG, dict)
|
def test_conf_variables(self):
|
||||||
assert isinstance(SCHEMA, dict)
|
from halfapi.conf import (
|
||||||
assert isinstance(SECRET, str)
|
CONFIG,
|
||||||
assert isinstance(DOMAINSDICT(), dict)
|
SCHEMA,
|
||||||
assert isinstance(PROJECT_NAME, str)
|
SECRET,
|
||||||
assert isinstance(HOST, str)
|
DOMAINSDICT,
|
||||||
assert isinstance(PORT, str)
|
PROJECT_NAME,
|
||||||
assert str(int(PORT)) == PORT
|
HOST,
|
||||||
assert isinstance(CONF_DIR, str)
|
PORT,
|
||||||
|
CONF_DIR
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(CONFIG, dict)
|
||||||
|
assert isinstance(SCHEMA, dict)
|
||||||
|
assert isinstance(SECRET, str)
|
||||||
|
assert isinstance(DOMAINSDICT(), dict)
|
||||||
|
assert isinstance(PROJECT_NAME, str)
|
||||||
|
assert isinstance(HOST, str)
|
||||||
|
assert isinstance(PORT, str)
|
||||||
|
assert str(int(PORT)) == PORT
|
||||||
|
assert isinstance(CONF_DIR, str)
|
||||||
|
@ -2,34 +2,38 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from starlette.authentication import UnauthenticatedUser
|
from starlette.authentication import UnauthenticatedUser
|
||||||
from starlette.testclient import TestClient
|
from starlette.testclient import TestClient
|
||||||
|
import subprocess
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from halfapi.lib.constants import API_SCHEMA
|
||||||
|
|
||||||
|
|
||||||
def test_whoami(project_runner, application_debug):
|
def test_routes(application_debug):
|
||||||
# @TODO : test with fake login
|
# @TODO : If we use isolated filesystem multiple times that creates a bug.
|
||||||
|
# So we use a single function with fixture "application debug"
|
||||||
|
|
||||||
c = TestClient(application_debug)
|
c = TestClient(application_debug)
|
||||||
r = c.get('/halfapi/whoami')
|
r = c.get('/halfapi/whoami')
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
|
|
||||||
def test_log(application_debug):
|
|
||||||
c = TestClient(application_debug)
|
|
||||||
r = c.get('/halfapi/log')
|
r = c.get('/halfapi/log')
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
|
|
||||||
def test_error(application_debug):
|
|
||||||
c = TestClient(application_debug)
|
|
||||||
r = c.get('/halfapi/error/400')
|
r = c.get('/halfapi/error/400')
|
||||||
assert r.status_code == 400
|
assert r.status_code == 400
|
||||||
r = c.get('/halfapi/error/404')
|
r = c.get('/halfapi/error/404')
|
||||||
assert r.status_code == 404
|
assert r.status_code == 404
|
||||||
r = c.get('/halfapi/error/500')
|
r = c.get('/halfapi/error/500')
|
||||||
assert r.status_code == 500
|
assert r.status_code == 500
|
||||||
|
r = c.get('/')
|
||||||
|
d_r = r.json()
|
||||||
|
assert isinstance(d_r, dict)
|
||||||
|
assert API_SCHEMA.validate(d_r)
|
||||||
|
|
||||||
@pytest.mark.skip
|
"""
|
||||||
def test_exception(application_debug):
|
TODO: Find a way to test exception raising
|
||||||
c = TestClient(application_debug)
|
|
||||||
try:
|
try:
|
||||||
r = c.get('/halfapi/exception')
|
r = c.get('/halfapi/exception')
|
||||||
assert r.status_code == 500
|
assert r.status_code == 500
|
||||||
except Exception:
|
except Exception:
|
||||||
print('exception')
|
print('exception')
|
||||||
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user