From 115a3cca116feb26fa98d2c3f41651cc59dcf1a5 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 27 Oct 2019 14:52:19 -0400 Subject: [PATCH] Add tests to the platform app Signed-off-by: Roberto Rosario --- HISTORY.rst | 1 + mayan/apps/platform/apps.py | 2 +- mayan/apps/platform/classes.py | 21 ++++--- .../management/commands/platformtemplate.py | 6 +- mayan/apps/platform/tests/__init__.py | 0 .../tests/test_management_commands.py | 61 +++++++++++++++++++ 6 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 mayan/apps/platform/tests/__init__.py create mode 100644 mayan/apps/platform/tests/test_management_commands.py diff --git a/HISTORY.rst b/HISTORY.rst index 0423226112..c9d1a6d2f4 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,7 @@ ================== - Move IMAPMockServer to its own module. - Display feedback message when testing a mailing profile. +- Add tests to the platform app. 3.2.8 (2019-10-01) ================== diff --git a/mayan/apps/platform/apps.py b/mayan/apps/platform/apps.py index 561d881224..2e03707aaf 100644 --- a/mayan/apps/platform/apps.py +++ b/mayan/apps/platform/apps.py @@ -9,7 +9,7 @@ class PlatformApp(MayanAppConfig): app_namespace = 'platform' app_url = 'platform' has_rest_api = False - has_tests = False + has_tests = True name = 'mayan.apps.platform' verbose_name = _('Platform') diff --git a/mayan/apps/platform/classes.py b/mayan/apps/platform/classes.py index 1d0152442f..2e9422df2c 100644 --- a/mayan/apps/platform/classes.py +++ b/mayan/apps/platform/classes.py @@ -9,6 +9,8 @@ except ImportError: from yaml import SafeLoader from django.template import loader +from django.template.base import Template +from django.template.context import Context from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ @@ -34,8 +36,10 @@ class PlatformTemplate(object): context = {} context_defaults = {} label = None + name = None settings = None template_name = None + template_string = None variables = None @classmethod @@ -99,10 +103,15 @@ class PlatformTemplate(object): stream=context_string, Loader=SafeLoader ) ) - return loader.render_to_string( - template_name=self.get_template_name(), - context=context - ) + + if self.template_string: + template = Template(template_string=self.template_string) + return template.render(context=Context(dict_=context)) + else: + return loader.render_to_string( + template_name=self.get_template_name(), + context=context + ) class PlatformTemplateSupervisord(PlatformTemplate): @@ -159,9 +168,7 @@ class PlatformTemplateSupervisord(PlatformTemplate): ) def get_context(self): - return { - 'workers': Worker.all() - } + return {'workers': Worker.all()} class PlatformTemplateSupervisordDocker(PlatformTemplate): diff --git a/mayan/apps/platform/management/commands/platformtemplate.py b/mayan/apps/platform/management/commands/platformtemplate.py index 3517d5c14a..4c881b0542 100644 --- a/mayan/apps/platform/management/commands/platformtemplate.py +++ b/mayan/apps/platform/management/commands/platformtemplate.py @@ -40,6 +40,8 @@ class Command(management.BaseCommand): ) exit(1) else: - self.stdout.write(template().render( - context_string=options.get('context')) + self.stdout.write( + template().render( + context_string=options.get('context') + ) ) diff --git a/mayan/apps/platform/tests/__init__.py b/mayan/apps/platform/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/mayan/apps/platform/tests/test_management_commands.py b/mayan/apps/platform/tests/test_management_commands.py new file mode 100644 index 0000000000..aaae5a364f --- /dev/null +++ b/mayan/apps/platform/tests/test_management_commands.py @@ -0,0 +1,61 @@ +from __future__ import unicode_literals + +from io import BytesIO + +from django.core import management +from django.test import TestCase + +from ..classes import PlatformTemplate + +TEST_TEMPLATE_LABEL = 'test template label' +TEST_TEMPLATE_NAME = 'test_template_name' +TEST_TEMPLATE_STRING = ''' +test template string +test template variable: {{ test_template_variable }} +''' +TEST_TEMPLATE_VARIABLE_VALUE = 'test_variable_value' +TEST_TEMPLATE_VARIABLE_VALUE_ALT = 'test_variable_value_alt' +TEST_TEMPLATE_STRING_RENDER = ''' +test template string +test template variable: {} +'''.format(TEST_TEMPLATE_VARIABLE_VALUE) +TEST_TEMPLATE_STRING_RENDER_ALT = ''' +test template string +test template variable: {} +'''.format(TEST_TEMPLATE_VARIABLE_VALUE_ALT) + + +class TestPlatformTemplate(PlatformTemplate): + context = {'test_template_variable': TEST_TEMPLATE_VARIABLE_VALUE} + label = TEST_TEMPLATE_LABEL + name = TEST_TEMPLATE_NAME + template_string = TEST_TEMPLATE_STRING + + +PlatformTemplate.register(klass=TestPlatformTemplate) + + +class PlatformTemplateManagementCommandTestCase(TestCase): + + def test_platform_template_simple(self): + output = BytesIO() + args = (TEST_TEMPLATE_NAME,) + options = { + 'stdout': output + } + management.call_command('platformtemplate', *args, **options) + self.assertEqual(output.getvalue(), TEST_TEMPLATE_STRING_RENDER) + + def test_platform_template_context(self): + output = BytesIO() + args = ( + TEST_TEMPLATE_NAME, '--context', + 'test_template_variable: {}'.format( + TEST_TEMPLATE_VARIABLE_VALUE_ALT + ) + ) + options = { + 'stdout': output + } + management.call_command('platformtemplate', *args, **options) + self.assertEqual(output.getvalue(), TEST_TEMPLATE_STRING_RENDER_ALT)