The structure of test directories is changed, template_engine module is added.

packages
Иванов Денис 4 years ago
parent e8b21f22ca
commit 7db61908ae

@ -0,0 +1,298 @@
from jinja2.ext import Extension
from jinja2 import Environment, FileSystemLoader, TemplateSyntaxError, nodes
from jinja2.utils import missing
from jinja2.runtime import Context, Undefined
from collections.abc import MutableMapping
class Variables(MutableMapping):
'''Класс заглушка вместо модуля переменных.'''
def __init__(self, *args, **kwargs):
self.__attrs = dict(*args, **kwargs)
def __next__(self):
iterator = iter(self.__attrs)
return next(iterator)
def __getattribute__(self, name):
if name == '_Variables__attrs':
return super().__getattribute__(name)
try:
return self.__attrs[name]
except KeyError:
raise AttributeError(name)
def __getitem__(self, name):
return self.__attrs[name]
def __setitem__(self, name, value):
self.__attrs[name] = value
def __delitem__(self, name):
del self.__attrs[name]
def __iter__(self):
return iter(self.__attrs)
def __len__(self):
return len(self.__attrs)
def __repr__(self):
return '<Variables {}>'.format(self.__attrs)
def resolve_or_missing(context, key, missing=missing, env={}):
datavars = context.parent['__datavars__']
if key in context.vars:
return context.vars[key]
if key in context.parent:
return context.parent[key]
if key in datavars:
return datavars[key]
for name in env:
if name in datavars and key in datavars[name]:
return datavars[name][key]
return missing
class CalculateContext(Context):
_env_set = set()
def resolve(self, key):
if self._legacy_resolve_mode:
rv = resolve_or_missing(self, key,
env=self._env_set)
else:
rv = self.resolve_or_missing(key)
if rv is missing:
return self.environment.undefined(name=key)
return rv
def resolve_or_missing(self, key):
if self._legacy_resolve_mode:
rv = self.resolve(key)
if isinstance(rv, Undefined):
rv = missing
return rv
return resolve_or_missing(self, key,
env=self._env_set)
class Parameters(MutableMapping):
'''Класс для хранения параметров и условий, взятых из шаблона, и передачи
их шаблонизатору.'''
def __init__(self, parameters_dictionary={}, condition=True):
self.__parameters = parameters_dictionary
self.__condition = condition
def set_parameters(self, *args, **kwargs):
parameters = dict(*args, **kwargs)
self.__parameters.update(parameters)
def set_condition(self, condition):
self.__condition = self.__condition and condition
def __getitem__(self, name):
return self.__parameters[name]
def __setitem__(self, name, value):
self.__parameters[name] = value
def __delitem__(self, name):
del self.__parameters[name]
def __iter__(self):
return iter(self.__parameters)
def __len__(self):
return len(self.__parameters)
def __repr__(self):
return '<Parameters: {0}, condition={1}>'.format(self.__parameters,
self.__condition)
@property
def condition(self):
return self.__condition
@property
def parameters(self):
return self.__parameters
class CalculateExtension(Extension):
_parameters_set = set()
def __init__(self, environment):
self.tags = {'calculate', 'save', 'set_var'}
self.CONDITION_TOKENS_TYPES = {'eq', 'ne', 'lt', 'gt', 'lteq', 'gteq'}
self.LITERAL_TOKENS_TYPES = {'string', 'integer', 'float'}
self.TARGET_FILES_SET = {'grp', 'system', 'etc', 'local', 'remote'}
self.parse_methods = {'calculate': self.parse_calculate,
'save': self.parse_save}
def parse(self, parser):
self.parser = parser
self.stream = parser.stream
tag_token = self.stream.current.value
return [self.parse_methods[tag_token]()]
def parse_save(self):
lineno = next(self.stream).lineno
target_file = nodes.Const('', lineno=lineno)
if self.stream.skip_if('dot'):
target_file_name = self.stream.expect('name').value
if target_file_name in self.TARGET_FILES_SET:
target_file = nodes.Const(target_file_name)
else:
TemplateSyntaxError("Unknown target file '{}'".
format(target_file_name),
lineno=lineno)
# получаем список из имени переменной.
module_name = self.stream.expect('name').value
variable_name = [nodes.Const(module_name, lineno=lineno)]
while self.stream.skip_if('dot'):
name = self.stream.expect('name').value
variable_name.append(nodes.Const(name, lineno=lineno))
variable_name = nodes.List(variable_name, lineno=lineno)
if self.stream.skip_if('assign'):
right_value = self.parser.parse_expression(with_condexpr=True)
save_variable_node = self.call_method('save_variable',
[variable_name,
right_value,
target_file,
nodes.ContextReference()],
lineno=lineno)
return nodes.Output([save_variable_node], lineno=lineno)
else:
TemplateSyntaxError("'=' is expected in 'save' tag.",
lineno=lineno)
def parse_calculate(self):
pairs_list = []
expect_comma_flag = False
conditions = nodes.Const(True)
lineno = next(self.stream).lineno
while self.stream.current.type != 'block_end':
if expect_comma_flag:
self.stream.expect('comma')
if self.stream.current.type == 'name':
if (self.stream.current.value in self._parameters_set and
self.stream.look().type != 'dot'):
pairs_list.append(self.get_parameter_node())
else:
conditions = nodes.And(
self.parser.parse_expression(
with_condexpr=True
),
conditions
)
else:
raise TemplateSyntaxError('Name is expected in calculate tag.',
lineno=self.stream.current.lineno)
expect_comma_flag = True
dictionary_node = nodes.Dict(pairs_list)
save_node = self.call_method('save_parameters',
[dictionary_node,
conditions,
nodes.ContextReference()],
lineno=lineno)
return nodes.Output([save_node], lineno=lineno)
def save_variable(self, variable_name, right_value, target_file, context):
'''Временный метод для сохранения значений переменных.'''
datavars = context.parent['__datavars__']
module_name = variable_name[0]
namespaces = variable_name[1:-1]
variable_name = variable_name[-1]
if module_name in datavars:
variables_module = datavars[module_name]
for namespace in namespaces:
if namespace not in variables_module:
variables_module[namespace] = Variables()
variables_module = variables_module[namespace]
variables_module[variable_name] = right_value
else:
AttributeError("Unknown variables module '{}'".
format(module_name))
return ''
def get_parameter_node(self):
lineno = self.stream.current.lineno
parameter_name = self.stream.expect('name').value
parameter_name_node = nodes.Const(parameter_name, lineno=lineno)
if self.stream.skip_if('assign'):
parameter_value = self.stream.current.value
parameter_rvalue = self.parser.parse_expression(with_condexpr=True)
if parameter_name == 'env':
env_names = parameter_value.split(',')
for name in env_names:
CalculateContext._env_set.add(name.strip())
else:
parameter_rvalue = nodes.Const(True, lineno=lineno)
return nodes.Pair(parameter_name_node, parameter_rvalue)
def save_parameters(cls, parameters_dictionary, compare_result, context):
context.parent['__parameters__'].set_parameters(parameters_dictionary)
context.parent['__parameters__'].set_condition(compare_result)
return ''
class TemplateEngine():
def __init__(self, directory_path='/',
parameters_set=set(),
env_set=set(),
datavars_module=Variables()):
CalculateExtension._parameters_set = parameters_set
self._datavars_module = datavars_module
self._parameters_object = Parameters()
self._template_text = ''
self.environment = Environment(loader=FileSystemLoader(directory_path),
extensions=[CalculateExtension])
self.environment.context_class = CalculateContext
def change_directory(self, directory_path):
self.environment.loader = FileSystemLoader(directory_path)
def process_template(self, template_path, env=set()):
CalculateContext._env_set = env
template = self.environment.get_template(template_path)
self._parameters_object = Parameters(parameters_dictionary={},
condition=True)
self._template_text = template.render(
__datavars__=self._datavars_module,
__parameters__=self._parameters_object
)
def process_template_from_string(self, string, env=set()):
CalculateContext._env_set = env
template = self.environment.from_string(string)
self._parameters_object = Parameters(parameters_dictionary={},
condition=True)
self._template_text = template.render(
__datavars__=self._datavars_module,
__parameters__=self._parameters_object
)
@property
def condition(self):
return self._parameters_object.condition
@property
def parameters(self):
return self._parameters_object.parameters
@property
def template_text(self):
text, self._template_text = self._template_text, ''
return text

@ -21,3 +21,4 @@ markers =
xml_xfce: marker for running tests for xml xfce format.
xml_gconf: marker for running tests for xml gconf format.
files: marker for running tests for calculate.utils.files module.
template_engine: marker for running tests for template_engine.

@ -56,7 +56,7 @@ class TestJoinMethod:
@pytest.mark.base
class TestLogicLinesMethod:
def test_if_input_is_text_document_the_method_returns_list_of_its_lines(self):
with open('./tests/format/testfiles/logic_lines_test.txt', 'r') as InputFile:
with open('./tests/templates/format/testfiles/logic_lines_test.txt', 'r') as InputFile:
InputText = InputFile.read()
processingMethods = []
@ -71,10 +71,10 @@ class TestLogicLinesMethod:
def test_if_lines_in_document_divided_using_backslash_as_continuation_symbol__method_returns_list_of_full_lines(self):
with open('./tests/format/testfiles/logic_lines_test_input.txt', 'r') as InputFile:
with open('./tests/templates/format/testfiles/logic_lines_test_input.txt', 'r') as InputFile:
InputText = InputFile.read()
with open('./tests/format/testfiles/logic_lines_test_output.txt', 'r') as OutputFile:
with open('./tests/templates/format/testfiles/logic_lines_test_output.txt', 'r') as OutputFile:
OutputText = OutputFile.read()
BaseObject = BaseFormat([])

@ -280,19 +280,19 @@ options {
assert bind_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/bind_original.conf', 'r') as original_file:
with open('./tests/templates/format/testfiles/bind_original.conf', 'r') as original_file:
original_text = original_file.read()
print(original_text)
bind_original_object = BINDFormat(original_text)
with open('./tests/format/testfiles/bind_template.conf', 'r') as template_file:
with open('./tests/templates/format/testfiles/bind_template.conf', 'r') as template_file:
template_text = template_file.read()
bind_template_object = BINDFormat(template_text,
ignore_comments=True)
bind_original_object.join_template(bind_template_object)
with open('./tests/format/testfiles/bind_result.conf', 'r') as result_file:
with open('./tests/templates/format/testfiles/bind_result.conf', 'r') as result_file:
result_text = result_file.read()
assert bind_original_object.get_document_text() == result_text

@ -201,18 +201,18 @@ class TestParsingMethods:
assert compiz_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/compiz_original', 'r') as original_file:
with open('./tests/templates/format/testfiles/compiz_original', 'r') as original_file:
original_text = original_file.read()
compiz_original_object = CompizFormat(original_text)
with open('./tests/format/testfiles/compiz_template', 'r') as template_file:
with open('./tests/templates/format/testfiles/compiz_template', 'r') as template_file:
template_text = template_file.read()
compiz_template_object = CompizFormat(template_text,
ignore_comments=True)
compiz_original_object.join_template(compiz_template_object)
with open('./tests/format/testfiles/compiz_result', 'r') as result_file:
with open('./tests/templates/format/testfiles/compiz_result', 'r') as result_file:
result_text = result_file.read()
assert compiz_original_object.get_document_text() == result_text

@ -9,7 +9,7 @@ import os
class TestExecuteMethods:
def test_if_diff_patch_used_for_patching_of_several_files__it_changes_patched_file_correctly(self):
test_result = True
root_path = path.join(os.getcwd(), 'tests/format/testfiles/')
root_path = path.join(os.getcwd(), 'tests/templates/format/testfiles/')
with open(path.join(root_path, 'diff_1.patch')) as patch_file:
patch_text = patch_file.read()
@ -46,9 +46,9 @@ class TestExecuteMethods:
def test_if_diff_patch_used_for_patching_of_directories__it_changes_files_in_directories_and_adds_ones(self):
test_result = True
root_path = path.join(os.getcwd(),
'tests/format/testfiles/a1')
'tests/templates/format/testfiles/a1')
patch_path = path.join(os.getcwd(),
'tests/format/testfiles/diff_2.patch')
'tests/templates/format/testfiles/diff_2.patch')
with open(path.join(patch_path)) as patch_file:
patch_text = patch_file.read()

@ -238,18 +238,18 @@ class TestParsingMethods:
assert dovecot_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/dovecot_original.conf', 'r') as original_file:
with open('./tests/templates/format/testfiles/dovecot_original.conf', 'r') as original_file:
original_text = original_file.read()
dovecot_original_object = DovecotFormat(original_text)
with open('./tests/format/testfiles/dovecot_template.conf', 'r') as template_file:
with open('./tests/templates/format/testfiles/dovecot_template.conf', 'r') as template_file:
template_text = template_file.read()
dovecot_template_object = DovecotFormat(template_text,
ignore_comments=True)
dovecot_original_object.join_template(dovecot_template_object)
with open('./tests/format/testfiles/dovecot_result.conf', 'r') as result_file:
with open('./tests/templates/format/testfiles/dovecot_result.conf', 'r') as result_file:
result_text = result_file.read()
assert dovecot_original_object.get_document_text() == result_text

@ -28,17 +28,17 @@ class TestParsingMethods:
assert jsonObject._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/json_original.json', 'r') as originalFile:
with open('./tests/templates/format/testfiles/json_original.json', 'r') as originalFile:
originalText = originalFile.read()
jsonOriginalObject = JSONFormat(originalText)
with open('./tests/format/testfiles/json_template.json', 'r') as templateFile:
with open('./tests/templates/format/testfiles/json_template.json', 'r') as templateFile:
templateText = templateFile.read()
jsonTemplateObject = JSONFormat(templateText)
jsonOriginalObject.join_template(jsonTemplateObject)
with open('./tests/format/testfiles/json_result.json', 'r') as resultFile:
with open('./tests/templates/format/testfiles/json_result.json', 'r') as resultFile:
resultText = resultFile.read()
assert jsonOriginalObject.get_document_text() == resultText

@ -205,18 +205,18 @@ class TestParsingMethods:
assert kde_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/kde_original', 'r') as original_file:
with open('./tests/templates/format/testfiles/kde_original', 'r') as original_file:
original_text = original_file.read()
kde_original_object = KDEFormat(original_text)
with open('./tests/format/testfiles/kde_template', 'r') as template_file:
with open('./tests/templates/format/testfiles/kde_template', 'r') as template_file:
template_text = template_file.read()
kde_template_object = KDEFormat(template_text,
ignore_comments=True)
kde_original_object.join_template(kde_template_object)
with open('./tests/format/testfiles/kde_result', 'r') as result_file:
with open('./tests/templates/format/testfiles/kde_result', 'r') as result_file:
result_text = result_file.read()
assert kde_original_object.get_document_text() == result_text

@ -95,18 +95,18 @@ class TestParsingMethods:
assert kernel_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/kernel_original', 'r') as original_file:
with open('./tests/templates/format/testfiles/kernel_original', 'r') as original_file:
original_text = original_file.read()
kernel_original_object = KernelFormat(original_text)
with open('./tests/format/testfiles/kernel_template', 'r') as template_file:
with open('./tests/templates/format/testfiles/kernel_template', 'r') as template_file:
template_text = template_file.read()
kernel_template_object = KernelFormat(template_text,
ignore_comments=True)
kernel_original_object.join_template(kernel_template_object)
with open('./tests/format/testfiles/kernel_result', 'r') as result_file:
with open('./tests/templates/format/testfiles/kernel_result', 'r') as result_file:
result_text = result_file.read()
assert kernel_original_object.get_document_text() == result_text

@ -6,7 +6,7 @@ from calculate.templates.format.ldap_format import LDAPFormat
@pytest.mark.ldap
class TestParsingMethods:
def test_if_logiclines_method_takes_text_with_lines_that_starts_whit_space_symbols__it_returns_joined_lines(self):
with open('./tests/format/testfiles/ldap_logic_lines_test.txt', 'r') as input_file:
with open('./tests/templates/format/testfiles/ldap_logic_lines_test.txt', 'r') as input_file:
input_text = input_file.read()
output_lines = ['First string of test file.',
@ -347,18 +347,18 @@ database bdb
assert ldap_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/ldap_original.conf', 'r') as original_file:
with open('./tests/templates/format/testfiles/ldap_original.conf', 'r') as original_file:
original_text = original_file.read()
ldap_original_object = LDAPFormat(original_text)
with open('./tests/format/testfiles/ldap_template.conf', 'r') as template_file:
with open('./tests/templates/format/testfiles/ldap_template.conf', 'r') as template_file:
template_text = template_file.read()
ldap_template_object = LDAPFormat(template_text,
ignore_comments=True)
ldap_original_object.join_template(ldap_template_object)
with open('./tests/format/testfiles/ldap_result.conf', 'r') as result_file:
with open('./tests/templates/format/testfiles/ldap_result.conf', 'r') as result_file:
result_text = result_file.read()
assert ldap_original_object.get_document_text() == result_text

@ -129,18 +129,18 @@ class TestParsingMethods:
assert openrc_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/openrc_original', 'r') as original_file:
with open('./tests/templates/format/testfiles/openrc_original', 'r') as original_file:
original_text = original_file.read()
openrc_original_object = OpenRCFormat(original_text)
with open('./tests/format/testfiles/openrc_template', 'r') as template_file:
with open('./tests/templates/format/testfiles/openrc_template', 'r') as template_file:
template_text = template_file.read()
openrc_template_object = OpenRCFormat(template_text,
ignore_comments=True)
openrc_original_object.join_template(openrc_template_object)
with open('./tests/format/testfiles/openrc_result', 'r') as result_file:
with open('./tests/templates/format/testfiles/openrc_result', 'r') as result_file:
result_text = result_file.read()
assert openrc_original_object.get_document_text() == result_text

@ -107,18 +107,18 @@ class TestParsingMethods:
assert postfix_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/postfix_original', 'r') as original_file:
with open('./tests/templates/format/testfiles/postfix_original', 'r') as original_file:
original_text = original_file.read()
postfix_original_object = PostfixFormat(original_text)
with open('./tests/format/testfiles/postfix_template', 'r') as template_file:
with open('./tests/templates/format/testfiles/postfix_template', 'r') as template_file:
template_text = template_file.read()
postfix_template_object = PostfixFormat(template_text,
ignore_comments=True)
postfix_original_object.join_template(postfix_template_object)
with open('./tests/format/testfiles/postfix_result', 'r') as result_file:
with open('./tests/templates/format/testfiles/postfix_result', 'r') as result_file:
result_text = result_file.read()
assert postfix_original_object.get_document_text() == result_text

@ -113,18 +113,18 @@ class TestParsingMethods:
assert procmail_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/procmail_original', 'r') as original_file:
with open('./tests/templates/format/testfiles/procmail_original', 'r') as original_file:
original_text = original_file.read()
procmail_original_object = ProcmailFormat(original_text)
with open('./tests/format/testfiles/procmail_template', 'r') as template_file:
with open('./tests/templates/format/testfiles/procmail_template', 'r') as template_file:
template_text = template_file.read()
procmail_template_object = ProcmailFormat(template_text,
ignore_comments=True)
procmail_original_object.join_template(procmail_template_object)
with open('./tests/format/testfiles/procmail_result', 'r') as result_file:
with open('./tests/templates/format/testfiles/procmail_result', 'r') as result_file:
result_text = result_file.read()
assert procmail_original_object.get_document_text() == result_text

@ -220,18 +220,18 @@ class TestParsingMethods:
assert proftpd_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/proftpd_original.conf', 'r') as original_file:
with open('./tests/templates/format/testfiles/proftpd_original.conf', 'r') as original_file:
original_text = original_file.read()
proftpd_original_object = ProFTPDFormat(original_text)
with open('./tests/format/testfiles/proftpd_template.conf', 'r') as template_file:
with open('./tests/templates/format/testfiles/proftpd_template.conf', 'r') as template_file:
template_text = template_file.read()
proftpd_template_object = ProFTPDFormat(template_text,
ignore_comments=True)
proftpd_original_object.join_template(proftpd_template_object)
with open('./tests/format/testfiles/proftpd_result.conf', 'r') as result_file:
with open('./tests/templates/format/testfiles/proftpd_result.conf', 'r') as result_file:
result_text = result_file.read()
assert proftpd_original_object.get_document_text() == result_text

@ -212,18 +212,18 @@ class TestParsingMethods:
assert samba_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/samba_original', 'r') as original_file:
with open('./tests/templates/format/testfiles/samba_original', 'r') as original_file:
original_text = original_file.read()
samba_original_object = SambaFormat(original_text)
with open('./tests/format/testfiles/samba_template', 'r') as template_file:
with open('./tests/templates/format/testfiles/samba_template', 'r') as template_file:
template_text = template_file.read()
samba_template_object = SambaFormat(template_text,
ignore_comments=True)
samba_original_object.join_template(samba_template_object)
with open('./tests/format/testfiles/samba_result', 'r') as result_file:
with open('./tests/templates/format/testfiles/samba_result', 'r') as result_file:
result_text = result_file.read()
assert samba_original_object.get_document_text() == result_text

@ -73,17 +73,17 @@ class TestParsingMethods:
assert xml_gconf_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/xml_gconf_original.xml', 'r') as original_file:
with open('./tests/templates/format/testfiles/xml_gconf_original.xml', 'r') as original_file:
original_text = original_file.read()
xml_gconf_original_object = XMLGConfFormat(original_text)
with open('./tests/format/testfiles/xml_gconf_template.xml', 'r') as template_file:
with open('./tests/templates/format/testfiles/xml_gconf_template.xml', 'r') as template_file:
template_text = template_file.read()
xml_gconf_template_object = XMLGConfFormat(template_text)
xml_gconf_original_object.join_template(xml_gconf_template_object)
with open('./tests/format/testfiles/xml_gconf_result.xml', 'r') as result_file:
with open('./tests/templates/format/testfiles/xml_gconf_result.xml', 'r') as result_file:
result_text = result_file.read()
assert xml_gconf_original_object.get_document_text() == result_text

@ -36,17 +36,17 @@ class TestParsingMethods:
assert xml_xfce_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/xml_xfce_original.xml', 'r') as original_file:
with open('./tests/templates/format/testfiles/xml_xfce_original.xml', 'r') as original_file:
original_text = original_file.read()
xml_xfce_original_object = XMLXfceFormat(original_text)
with open('./tests/format/testfiles/xml_xfce_template.xml', 'r') as template_file:
with open('./tests/templates/format/testfiles/xml_xfce_template.xml', 'r') as template_file:
template_text = template_file.read()
xml_xfce_template_object = XMLXfceFormat(template_text)
xml_xfce_original_object.join_template(xml_xfce_template_object)
with open('./tests/format/testfiles/xml_xfce_result.xml', 'r') as result_file:
with open('./tests/templates/format/testfiles/xml_xfce_result.xml', 'r') as result_file:
result_text = result_file.read()
assert xml_xfce_original_object.get_document_text() == result_text

@ -0,0 +1,120 @@
import pytest
from calculate.templates.template_engine import TemplateEngine, Variables
PARAMETERS_SET = {'name', 'path', 'append', 'chmod', 'chown',
'autoupdate', 'env', 'link', 'force', 'symbolic',
'format', 'comment', 'protected', 'mirror', 'run',
'exec', 'env', 'stretch', 'convert', 'dotall',
'multiline', 'dconf', 'package', 'merge',
'postmerge', 'action'}
@pytest.mark.template_engine
class TestUtils():
def test_if_an_input_template_contains_calculate_tag_with_some_parameters__the_template_engine_object_will_collect_its_parameters(self):
input_template = '''{% calculate name = 'filename', path = '/etc/path', force %}'''
parameters = {'name': 'filename', 'path': '/etc/path', 'force': True}
template_engine = TemplateEngine(parameters_set=PARAMETERS_SET)
template_engine.process_template_from_string(input_template)
output_parameters = template_engine.parameters
assert output_parameters == parameters
def test_if_an_input_template_binded_with_datavars_module__variables_available_in_a_template(self):
input_template = '''{% calculate name = vars.var_1, path = vars.var_2, autoupdate %}'''
parameters = {'name': 'filename', 'path': '/etc/path',
'autoupdate': True}
datavars_module = Variables({'vars':
Variables({'var_1': 'filename',
'var_2': '/etc/path'})})
template_engine = TemplateEngine(parameters_set=PARAMETERS_SET,
datavars_module=datavars_module)
template_engine.process_template_from_string(input_template)
output_parameters = template_engine.parameters
assert output_parameters == parameters
def test_if_an_input_template_contains_env_parameter_in_which_module_name_is_assigned__the_variables_from_this_module_can_be_used_in_template_without_determining_of_their_module(self):
input_template = '''{% calculate name = vars.var_1, path = var_3, env = 'other_vars' %}'''
parameters = {'name': 'filename', 'path': '/etc/other_path',
'env': 'other_vars'}
datavars_module = Variables({'vars':
Variables({'var_1': 'filename',
'var_2': '/etc/path'}),
'other_vars':
Variables({'var_3': '/etc/other_path'})})
template_engine = TemplateEngine(parameters_set=PARAMETERS_SET,
datavars_module=datavars_module)
template_engine.process_template_from_string(input_template)
output_parameters = template_engine.parameters
assert output_parameters == parameters
def test_if_an_input_template_contains_condition__the_template_engine_object_will_contain_the_value_of_its_condition(self):
input_template = '''{% calculate vars.var_1 < vars.var_2 or (not (var_3 == 'required status') and vars.var_4), env = 'vars' %}'''
datavars_module = Variables({'vars':
Variables({'var_1': 12,
'var_2': 1.2,
'var_3': 'unrequired status',
'var_4': True})})
template_engine = TemplateEngine(parameters_set=PARAMETERS_SET,
datavars_module=datavars_module)
template_engine.process_template_from_string(input_template)
condition = template_engine.condition
assert condition is True
def test_if_an_input_template_contains_several_calculate_tags__the_template_engine_will_parse_them_all_and_will_contain_all_parameters_and_result_of_all_conditions(self):
input_template = '''{% calculate name = vars.var_1, var_4 < var_5 %}
{% calculate path = var_3, var_6 == 'value' %}
{% calculate env = 'other_vars'%}'''
parameters = {'name': 'filename', 'path': '/etc/other_path',
'env': 'other_vars'}
datavars_module = Variables({'vars':
Variables({'var_1': 'filename',
'var_2': '/etc/path'}),
'other_vars':
Variables({'var_3': '/etc/other_path',
'var_4': 12, 'var_5': 1.2,
'var_6': 'value'})})
template_engine = TemplateEngine(parameters_set=PARAMETERS_SET,
datavars_module=datavars_module)
template_engine.process_template_from_string(input_template)
condition = template_engine.condition
output_parameters = template_engine.parameters
assert condition is False and output_parameters == parameters
def test_if_an_input_template_contains_variables_in_its_text__the_rendered_text_will_contain_values_of_this_variables(self):
input_template = '''{% calculate name = 'filename', force -%}
parameter_1 = {{ vars_2.var_3 }}
{% calculate env = 'vars_1' -%}
parameter_2 = {{ vars_2.var_4 }}
parameter_3 = {{ var_1 }}'''
output_text = '''parameter_1 = value_1
parameter_2 = value_2
parameter_3 = 12'''
datavars_module = Variables({'vars_1':
Variables({'var_1': 12,
'var_2': 1.2}),
'vars_2':
Variables({'var_3': 'value_1',
'var_4': 'value_2'})})
template_engine = TemplateEngine(parameters_set=PARAMETERS_SET,
datavars_module=datavars_module)
template_engine.process_template_from_string(input_template)
text = template_engine.template_text
assert text == output_text
def test_if_value_of_variable_is_set_using_save_tag__the_new_value_of_variable_can_be_used_in_template(self):
input_template = '''{% calculate name = 'filename', force -%}
{% save vars_1.var_3 = 13 -%}
parameter = {{ vars_1.var_3 - 1 + vars_1.var_2 * 2 }}
{% save.session vars_1.var_4 = True -%}
bool parameter = {{ vars_1.var_4 }}'''
output_text = '''parameter = 14.4
bool parameter = True'''
datavars_module = Variables({'vars_1':
Variables({'var_1': 12,
'var_2': 1.2})})
template_engine = TemplateEngine(parameters_set=PARAMETERS_SET,
datavars_module=datavars_module)
template_engine.process_template_from_string(input_template)
text = template_engine.template_text
assert text == output_text
Loading…
Cancel
Save