[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,8 +16,7 @@ Cli = cli.cli
PROJNAME = os.environ.get('PROJ','tmp_api')
class TestCli():
def test_options(self, runner):
def test_options(runner):
# Wrong command
with runner.isolated_filesystem():
r = runner.invoke(Cli, ['foobar'])
@ -37,7 +36,7 @@ class TestCli():
assert r.exit_code == 0
def test_init_project_fail(self, runner):
def test_init_project_fail(runner):
# Missing argument (project)
testproject = 'testproject'
r = runner.invoke(Cli, ['init'])
@ -60,13 +59,13 @@ class TestCli():
r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1
def test_init_project(self, runner, halfapi_conf_dir):
def test_init_project(runner):
"""
"""
cp = ConfigParser()
with runner.isolated_filesystem():
env = {
'HALFAPI_CONF_DIR': halfapi_conf_dir
'HALFAPI_CONF_DIR': '.halfapi'
}
res = runner.invoke(Cli, ['init', PROJNAME], env=env)

View File

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

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')
"""