2020-08-06 23:39:04 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import importlib
|
|
|
|
import tempfile
|
|
|
|
from unittest.mock import patch
|
2022-07-18 23:23:09 +02:00
|
|
|
import json
|
2023-08-02 08:16:30 +02:00
|
|
|
import toml
|
2020-08-06 23:39:04 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from click.testing import CliRunner
|
|
|
|
from configparser import ConfigParser
|
2023-08-20 23:32:50 +02:00
|
|
|
from halfapi.conf import DEFAULT_CONF, PROJECT_LEVEL_KEYS, DOMAIN_LEVEL_KEYS
|
2020-08-06 23:39:04 +02:00
|
|
|
|
|
|
|
PROJNAME = os.environ.get('PROJ','tmp_api')
|
|
|
|
|
|
|
|
|
|
|
|
class TestCliProj():
|
2020-09-25 01:06:21 +02:00
|
|
|
def test_cmds(self, project_runner):
|
|
|
|
assert project_runner('--help').exit_code == 0
|
|
|
|
#assert project_runner('run', '--help').exit_code == 0
|
|
|
|
#assert project_runner('domain', '--help').exit_code == 0
|
|
|
|
|
|
|
|
|
2020-10-07 09:52:23 +02:00
|
|
|
def test_domain_commands(self, project_runner):
|
2021-12-01 13:31:46 +01:00
|
|
|
""" TODO: Test create command
|
|
|
|
"""
|
2023-08-20 23:32:50 +02:00
|
|
|
test_conf = {
|
|
|
|
'project': {
|
|
|
|
'port': '3010',
|
|
|
|
'loglevel': 'warning'
|
|
|
|
},
|
|
|
|
'domain': {
|
|
|
|
'dummy_domain': {
|
|
|
|
'port': 4242,
|
|
|
|
'name': 'dummy_domain',
|
|
|
|
'enabled': True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 09:52:23 +02:00
|
|
|
r = project_runner('domain')
|
2021-12-01 13:31:46 +01:00
|
|
|
print(r.stdout)
|
|
|
|
assert r.exit_code == 1
|
2022-07-18 23:23:09 +02:00
|
|
|
_, tmp_conf = tempfile.mkstemp()
|
|
|
|
with open(tmp_conf, 'w') as fh:
|
|
|
|
fh.write(
|
2023-08-20 23:32:50 +02:00
|
|
|
toml.dumps(test_conf)
|
2022-07-18 23:23:09 +02:00
|
|
|
)
|
|
|
|
|
2023-08-20 23:32:50 +02:00
|
|
|
r = project_runner(f'domain dummy_domain --conftest {tmp_conf}')
|
|
|
|
assert r.exit_code == 0
|
|
|
|
r_conf = toml.loads(r.stdout)
|
|
|
|
for key, value in r_conf.items():
|
|
|
|
if key == 'domain':
|
|
|
|
continue
|
|
|
|
assert key in PROJECT_LEVEL_KEYS
|
|
|
|
if key == 'port':
|
|
|
|
assert value == test_conf['domain']['dummy_domain']['port']
|
|
|
|
elif key == 'loglevel':
|
|
|
|
assert value == test_conf['project']['loglevel']
|
|
|
|
else:
|
|
|
|
assert value == DEFAULT_CONF[key.upper()]
|
|
|
|
|
|
|
|
|
|
|
|
assert json.dumps(test_conf['domain']) == json.dumps(r_conf['domain'])
|
|
|
|
|
|
|
|
for key in test_conf['domain']['dummy_domain'].keys():
|
|
|
|
assert key in DOMAIN_LEVEL_KEYS
|
|
|
|
|
|
|
|
# Default command "run"
|
2023-02-03 00:02:56 +01:00
|
|
|
r = project_runner(f'domain dummy_domain --dry-run {tmp_conf}')
|
2021-12-01 13:31:46 +01:00
|
|
|
print(r.stdout)
|
2020-10-07 09:52:23 +02:00
|
|
|
assert r.exit_code == 0
|
2020-09-25 01:06:21 +02:00
|
|
|
|
2021-12-01 13:31:46 +01:00
|
|
|
|
2020-10-07 09:52:23 +02:00
|
|
|
def test_config_commands(self, project_runner):
|
|
|
|
try:
|
|
|
|
r = project_runner('config')
|
|
|
|
assert r.exit_code == 0
|
|
|
|
except AssertionError as exc:
|
|
|
|
subprocess.call(['tree', '-a'])
|
|
|
|
raise exc
|
2020-08-06 23:39:04 +02:00
|
|
|
|