[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:
Maxime Alves LIRMM@home 2021-12-01 12:20:01 +01:00
parent 7e7bbb3a62
commit 53ecbb58fc
3 changed files with 131 additions and 115 deletions

View File

@ -16,76 +16,75 @@ Cli = cli.cli
PROJNAME = os.environ.get('PROJ','tmp_api')
class TestCli():
def test_options(self, runner):
# Wrong command
with runner.isolated_filesystem():
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'])
def test_options(runner):
# Wrong command
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['foobar'])
assert r.exit_code == 2
with runner.isolated_filesystem():
# Fail : Wrong project name
r = runner.invoke(Cli, ['init', 'test*-project'])
assert r.exit_code == 1
# Test existing commands
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['--help'])
assert r.exit_code == 0
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():
r = runner.invoke(Cli, ['--version'])
assert r.exit_code == 0
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(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'))
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['init', '--help'])
assert r.exit_code == 0
# .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
def test_init_project_fail(runner):
# Missing argument (project)
testproject = 'testproject'
r = runner.invoke(Cli, ['init'])
assert r.exit_code == 2
assert res.exit_code == 0
assert res.exception is None
with runner.isolated_filesystem():
# 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

View File

@ -1,44 +1,57 @@
from unittest import TestCase
import sys
import pytest
from halfapi.halfapi import HalfAPI
halfapi_arg = { 'domain': { 'name': 'dummy_domain', 'router': 'routers' } }
def test_conf_production_default():
halfapi = HalfAPI({
**halfapi_arg
})
assert halfapi.PRODUCTION is True
class TestConf(TestCase):
def setUp(self):
self.args = {
'domain': {
'name': 'dummy_domain',
'router': 'dummy_domain.routers'
}
}
def tearDown(self):
pass
def test_conf_production_true():
halfapi = HalfAPI({
**halfapi_arg,
'production': True,
})
assert halfapi.PRODUCTION is True
def test_conf_production_default(self):
halfapi = HalfAPI({
**self.args
})
assert halfapi.PRODUCTION is True
def test_conf_production_false():
halfapi = HalfAPI({
**halfapi_arg,
'production': False,
})
assert halfapi.PRODUCTION is False
def test_conf_production_true(self):
halfapi = HalfAPI({
**self.args,
'production': True,
})
assert halfapi.PRODUCTION is True
def test_conf_variables():
from halfapi.conf import (
CONFIG,
SCHEMA,
SECRET,
DOMAINSDICT,
PROJECT_NAME,
HOST,
PORT,
CONF_DIR
)
def test_conf_production_false(self):
halfapi = HalfAPI({
**self.args,
'production': False,
})
assert halfapi.PRODUCTION is False
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)
def test_conf_variables(self):
from halfapi.conf import (
CONFIG,
SCHEMA,
SECRET,
DOMAINSDICT,
PROJECT_NAME,
HOST,
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)

View File

@ -2,34 +2,38 @@
import pytest
from starlette.authentication import UnauthenticatedUser
from starlette.testclient import TestClient
import subprocess
import json
import os
import sys
from halfapi.lib.constants import API_SCHEMA
def test_whoami(project_runner, application_debug):
# @TODO : test with fake login
def test_routes(application_debug):
# @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)
r = c.get('/halfapi/whoami')
assert r.status_code == 200
def test_log(application_debug):
c = TestClient(application_debug)
r = c.get('/halfapi/log')
assert r.status_code == 200
def test_error(application_debug):
c = TestClient(application_debug)
r = c.get('/halfapi/error/400')
assert r.status_code == 400
r = c.get('/halfapi/error/404')
assert r.status_code == 404
r = c.get('/halfapi/error/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):
c = TestClient(application_debug)
"""
TODO: Find a way to test exception raising
try:
r = c.get('/halfapi/exception')
assert r.status_code == 500
except Exception:
print('exception')
"""