[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') 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'])
@ -37,7 +36,7 @@ class TestCli():
assert r.exit_code == 0 assert r.exit_code == 0
def test_init_project_fail(self, runner): def test_init_project_fail(runner):
# Missing argument (project) # Missing argument (project)
testproject = 'testproject' testproject = 'testproject'
r = runner.invoke(Cli, ['init']) r = runner.invoke(Cli, ['init'])
@ -60,13 +59,13 @@ class TestCli():
r = runner.invoke(Cli, ['init', testproject]) r = runner.invoke(Cli, ['init', testproject])
assert r.exit_code == 1 assert r.exit_code == 1
def test_init_project(self, runner, halfapi_conf_dir): def test_init_project(runner):
""" """
""" """
cp = ConfigParser() cp = ConfigParser()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
env = { env = {
'HALFAPI_CONF_DIR': halfapi_conf_dir 'HALFAPI_CONF_DIR': '.halfapi'
} }
res = runner.invoke(Cli, ['init', PROJNAME], env=env) 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 from halfapi.halfapi import HalfAPI
halfapi_arg = { 'domain': { 'name': 'dummy_domain', 'router': 'routers' } } class TestConf(TestCase):
def test_conf_production_default(): 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 = HalfAPI({
**halfapi_arg **self.args
}) })
assert halfapi.PRODUCTION is True assert halfapi.PRODUCTION is True
def test_conf_production_true(): def test_conf_production_true(self):
halfapi = HalfAPI({ halfapi = HalfAPI({
**halfapi_arg, **self.args,
'production': True, 'production': True,
}) })
assert halfapi.PRODUCTION is True assert halfapi.PRODUCTION is True
def test_conf_production_false(): def test_conf_production_false(self):
halfapi = HalfAPI({ halfapi = HalfAPI({
**halfapi_arg, **self.args,
'production': False, 'production': False,
}) })
assert halfapi.PRODUCTION is False assert halfapi.PRODUCTION is False
def test_conf_variables(): def test_conf_variables(self):
from halfapi.conf import ( from halfapi.conf import (
CONFIG, CONFIG,
SCHEMA, SCHEMA,

View File

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