UPD: try to make useful test stuff.

This commit is contained in:
2019-03-15 17:35:16 +01:00
parent 2dce52cb25
commit 1c3e5bf761
6 changed files with 130 additions and 2 deletions

View File

@@ -98,8 +98,11 @@ class ModuleConfig(object):
self._modules = dict()
with open(path, 'r') as f:
data = json.load(f)
if os.path.exists(path):
with open(path, 'r') as f:
data = json.load(f)
else:
data = dict()
if 'modules' in data:
for meta_dict in data['modules']:

View File

@@ -3,6 +3,7 @@ import os
import pkg_resources
import posix
import sys
from django.core.management import execute_from_command_line
from dav_base.config.modules import DJANGO_MAIN_MODULE, ModuleConfig
@@ -27,6 +28,25 @@ class AdminCommand(object):
subparser.add_argument('path', metavar='PATH',
help='A directory, where the project files will be installed.')
subparser = subparsers.add_parser('enable_module',
help='Enable a modular app within your django-dav installation.')
subparser.add_argument('path', metavar='PATH',
help='The directory, where your django project files are installed.')
subparser.add_argument('module', metavar='MODULE',
help='The name of the module.')
subparser = subparsers.add_parser('disable_module',
help='Disable a modular app within your django-dav installation.')
subparser.add_argument('path', metavar='PATH',
help='The directory, where your django project files are installed.')
subparser.add_argument('module', metavar='MODULE',
help='The name of the module.')
subparser = subparsers.add_parser('list_modules',
help='List enabled modular apps within your django-dav installation.')
subparser.add_argument('path', metavar='PATH',
help='The directory, where your django project files are installed.')
return parser
def _parse_args(self, argv=None):
@@ -91,6 +111,32 @@ class AdminCommand(object):
return posix.EX_OK
def _subcmd_enable_module(self, cmd_args):
django_main_module = DJANGO_MAIN_MODULE
django_base_dir = cmd_args.path
module_name = cmd_args.module
sys.path.append(django_base_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(django_main_module)
execute_from_command_line(['manage.py', 'enable_module', module_name])
return posix.EX_OK
def _subcmd_disable_module(self, cmd_args):
django_main_module = DJANGO_MAIN_MODULE
django_base_dir = cmd_args.path
module_name = cmd_args.module
sys.path.append(django_base_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(django_main_module)
execute_from_command_line(['manage.py', 'disable_module', module_name])
return posix.EX_OK
def _subcmd_list_modules(self, cmd_args):
django_main_module = DJANGO_MAIN_MODULE
django_base_dir = cmd_args.path
sys.path.append(django_base_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(django_main_module)
execute_from_command_line(['manage.py', 'list_modules'])
return posix.EX_OK
def __init__(self):
self._argparser = self._setup_argparser()

View File

@@ -11,11 +11,13 @@ class ViewsTestCase(SimpleTestCase):
self.assertIn('dav_base/root.html', template_names)
context = view.get_context_data()
self.assertIn('root_urls', context)
self.assertIsInstance(context['root_urls'], list)
def test_integrated_root(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'dav_base/root.html')
self.assertIn('root_urls', response.context, '\'root_urls\' not in context of root view')
self.assertIsInstance(response.context['root_urls'], list)
# TODO
# Maybe we should set a defined module config, so we could

View File

@@ -94,6 +94,7 @@ setup(
},
packages=find_packages(),
include_package_data=True,
test_suite='tests.test_suite',
entry_points={
'console_scripts': [
'django-dav-admin = dav_base.console_scripts.admin:main',

2
tests/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
from test_suite import TestSuite
test_suite = TestSuite()

74
tests/test_suite.py Normal file
View File

@@ -0,0 +1,74 @@
import datetime
import django
import os
import shutil
import sys
from django.conf import settings
from django.test.utils import get_runner
from tempfile import mkdtemp
class DjangoEnvironment(object):
def __init__(self, path=None, base_path=None, remove_after=True,
enable_modules=None):
self.path = path
if base_path is not None:
self._base_path = base_path
else:
self._base_path = os.getcwd()
self._remove_after = remove_after
self._original_sys_path = None
self._modified_sys_path = None
if enable_modules is not None:
self._enable_modules = enable_modules
else:
self._enable_modules = []
def admin_cmd(self, *args):
cmd = 'django-dav-admin'
for arg in args:
cmd += ' "{}"'.format(arg)
os.system(cmd)
def __enter__(self):
if self.path is None:
prefix = 'testrun-{datetime}-'.format(
datetime=datetime.datetime.now().strftime('%Y%m%d-%H%M')
)
self.path = mkdtemp(prefix=prefix, dir=self._base_path)
self.admin_cmd('setup', self.path)
for mod in self._enable_modules:
self.admin_cmd('enable_module', self.path, mod)
self._original_sys_path = sys.path
sys.path.append(self.path)
self._modified_sys_path = sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
django.setup()
return self
def __exit__(self, *args):
if self._modified_sys_path is not None and self._original_sys_path is not None:
if sys.path == self._modified_sys_path:
sys.path = self._original_sys_path
if self._remove_after:
shutil.rmtree(self.path)
class TestSuite(object):
def __call__(self):
modules = ['dav_auth']
tests = ['dav_base'] + modules
test_tags = None
exclude_test_tags = ['browser']
failures = 0
with DjangoEnvironment(enable_modules=modules):
test_runner_class = get_runner(settings)
test_runner = test_runner_class(tags=test_tags, exclude_tags=exclude_test_tags)
failures = test_runner.run_tests(tests)
sys.exit(bool(failures))