You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1690 lines
76 KiB

  1. import os
  2. import shutil
  3. import pytest
  4. import logging
  5. from calculate.variables.datavars import NamespaceNode, VariableNode,\
  6. Namespace, Variable, Dependence,\
  7. CyclicVariableError, HashType,\
  8. StringType, IntegerType,\
  9. VariableType, VariableError,\
  10. TableType, BooleanType,\
  11. VariableTypeError, FloatType,\
  12. ListType, VariableNotFoundError,\
  13. Copy, Format, Calculate, Static
  14. from calculate.templates.template_engine import TemplateEngine, FILE
  15. from calculate.templates.template_processor import TemplateExecutor
  16. from calculate.variables.loader import NamespaceIniFiller, Datavars
  17. from calculate.utils.files import stderr_devnull, read_file
  18. TESTFILES_PATH = os.path.join(os.getcwd(), 'tests/variables/testfiles')
  19. APPENDS_SET = TemplateExecutor(cl_config_path=os.path.join(
  20. TESTFILES_PATH,
  21. 'var/lib/calculate/config')
  22. ).available_appends
  23. class LoggingHandler(logging.Handler):
  24. '''Тестовый хэндлер для логгирования в список, который затем можно
  25. проверять в тестах.'''
  26. def __init__(self, msg_list: list):
  27. logging.Handler.__init__(self)
  28. # Список для хранения сообщений логгера.
  29. self.msg_list = msg_list
  30. def emit(self, record):
  31. log_record = (record.levelname, record.getMessage())
  32. self.msg_list.append(log_record)
  33. @pytest.mark.vars
  34. class TestDatavars:
  35. # Сначала тестируем классы и методы необходимые для построения дерева
  36. # переменных и пространств имен.
  37. def test_if_NamespaceNode_just_initialized_with_its_name__the_NamespaceNode_object_contains_empty_namespaces_and_variables_dictionaries_and_fullname_method_returns_only_the_namespace_s_name(self):
  38. namespace_1 = NamespaceNode('namespace_1')
  39. assert namespace_1._namespaces == dict()
  40. assert namespace_1._variables == dict()
  41. assert namespace_1.get_fullname() == 'namespace_1'
  42. def test_if_namespace_node_added_in_an_other_namespace_node__a_parent_namespace_node_contains_child_namespace_in_its_namespaces_dictionary_and_child_namespace_object_has_parent_namespace_in_its_parent_attribute_and_its_fullname_method_returns_names_of_both_namespaces(self):
  43. namespace_1 = NamespaceNode('namespace_1')
  44. namespace_2 = NamespaceNode('namespace_2')
  45. namespace_1.add_namespace(namespace_2)
  46. assert namespace_1._namespaces == {'namespace_2': namespace_2}
  47. assert namespace_1._variables == dict()
  48. assert namespace_1.get_fullname() == 'namespace_1'
  49. assert namespace_1.namespace_2 == namespace_2
  50. assert namespace_1['namespace_2'] == namespace_2
  51. assert namespace_2._namespaces == dict()
  52. assert namespace_2._variables == dict()
  53. assert namespace_2._parent == namespace_1
  54. assert namespace_2.get_fullname() == 'namespace_1.namespace_2'
  55. def test_if_two_VariableNode_objects_are_initialized_and_added_to_a_namespace__the_NamespaceNode_object_contains_this_variables_and_can_be_used_to_get_variables_values_and_variables_have_namespace_name_in_their_fullnames(self):
  56. namespace_1 = NamespaceNode('namespace_1')
  57. variable_1 = VariableNode('var_1', namespace_1, source='value_1')
  58. variable_2 = VariableNode('var_2', namespace_1, source='value_2')
  59. variable_3 = VariableNode('name', namespace_1, source='name_value')
  60. assert namespace_1._namespaces == dict()
  61. assert namespace_1._variables == {'var_1': variable_1,
  62. 'var_2': variable_2,
  63. 'name': variable_3}
  64. assert namespace_1.get_fullname() == 'namespace_1'
  65. assert namespace_1.var_1 == 'value_1'
  66. assert namespace_1['var_1'] == variable_1
  67. assert namespace_1.var_2 == 'value_2'
  68. assert namespace_1['var_2'] == variable_2
  69. assert namespace_1.name == 'name_value'
  70. assert namespace_1['name'] == variable_3
  71. assert namespace_1['var_1'].get_fullname() == 'namespace_1.var_1'
  72. assert namespace_1['var_2'].get_fullname() == 'namespace_1.var_2'
  73. assert namespace_1['name'].get_fullname() == 'namespace_1.name'
  74. def test_if_two_dependencies_is_created_with_the_same_variables_and_their_depend_functions_are_equivalent_lambdas__the_dependencies_are_equal(self):
  75. namespace_1 = NamespaceNode('namespace_1')
  76. variable_1 = VariableNode('var_1', namespace_1, source=2,
  77. variable_type=IntegerType)
  78. variable_2 = VariableNode('var_2', namespace_1, source=4,
  79. variable_type=IntegerType)
  80. dependence_1 = Dependence(
  81. variable_1, variable_2, depend=lambda arg_1, arg_2:
  82. 'greater' if arg_1.value > arg_2.value else 'less')
  83. dependence_2 = Dependence(
  84. variable_1, variable_2, depend=lambda var_1, var_2:
  85. 'greater' if var_1.value > var_2.value else 'less')
  86. assert dependence_1 == dependence_2
  87. def test_if_two_dependencies_is_created_with_the_same_variables_and_their_depend_functions_are_equivalent_lambda_and_function__the_dependencies_are_equal(self):
  88. namespace_1 = NamespaceNode('namespace_1')
  89. variable_1 = VariableNode('var_1', namespace_1, source=2,
  90. variable_type=IntegerType)
  91. variable_2 = VariableNode('var_2', namespace_1, source=4,
  92. variable_type=IntegerType)
  93. dependence_1 = Dependence(
  94. variable_1, variable_2, depend=lambda arg_1, arg_2:
  95. 'greater' if arg_1.value > arg_2.value else 'less')
  96. def comparator(var_1, var_2):
  97. if var_1.value > var_2.value:
  98. return 'greater'
  99. else:
  100. return 'less'
  101. dependence_2 = Dependence(variable_1, variable_2, depend=comparator)
  102. assert dependence_1 == dependence_2
  103. def test_if_two_dependencies_is_created_with_the_same_variables_and_their_depend_functions_are_not_equivalent_lambdas__the_dependencies_are_equal(self):
  104. namespace_1 = NamespaceNode('namespace_1')
  105. variable_1 = VariableNode('var_1', namespace_1, source=2,
  106. variable_type=IntegerType)
  107. variable_2 = VariableNode('var_2', namespace_1, source=4,
  108. variable_type=IntegerType)
  109. dependence_1 = Dependence(
  110. variable_1, variable_2, depend=lambda arg_1, arg_2:
  111. 'less' if arg_1.value > arg_2.value else 'greater')
  112. dependence_2 = Dependence(
  113. variable_1, variable_2, depend=lambda var_1, var_2:
  114. 'greater' if var_1.value > var_2.value else 'less')
  115. assert dependence_1 != dependence_2
  116. def test_if_two_dependencies_is_created_with_the_same_variables_and_their_depend_functions_are_not_equivalent_lambda_and_function__the_dependencies_are_equal(self):
  117. namespace_1 = NamespaceNode('namespace_1')
  118. variable_1 = VariableNode('var_1', namespace_1, source=2,
  119. variable_type=IntegerType)
  120. variable_2 = VariableNode('var_2', namespace_1, source=4,
  121. variable_type=IntegerType)
  122. dependence_1 = Dependence(
  123. variable_1, variable_2, depend=lambda arg_1, arg_2:
  124. 'greater' if arg_1.value > arg_2.value else 'less')
  125. def comparator(var_1, var_2):
  126. if var_1 > var_2:
  127. return 'less'
  128. else:
  129. return 'greater'
  130. dependence_2 = Dependence(variable_1, variable_2, depend=comparator)
  131. assert dependence_1 != dependence_2
  132. def test_if_a_variable_subscribed_to_two_other_variables_using_set_function__the_variable_updates_using_the_variables_and_the_set_function_to_calculate_its_value(self):
  133. namespace_1 = NamespaceNode('namespace_1')
  134. variable_1 = VariableNode('var_1', namespace_1, source=2,
  135. variable_type=IntegerType)
  136. variable_2 = VariableNode('var_2', namespace_1, source=4,
  137. variable_type=IntegerType)
  138. namespace_2 = NamespaceNode('namespace_2')
  139. variable = VariableNode('var_1', namespace_2)
  140. variable.source = Dependence(
  141. variable_1, variable_2, depend=lambda var_1, var_2:
  142. 'greater' if var_1.value > var_2.value else 'less')
  143. assert namespace_2.var_1 == 'less'
  144. namespace_1['var_1'].source = 5
  145. assert namespace_2.var_1 == 'greater'
  146. # Теперь тестируем интерфейс создания переменных.
  147. def test_if_variables_creation_api_is_used_for_creating_of_a_number_of_variables_without_any_dependencies_in_their_sources__the_api_fabrics_creates_the_variables(self):
  148. Namespace.reset()
  149. datavars = Namespace.datavars
  150. with Namespace('namespace_1'):
  151. Variable('var_1', source='val_1')
  152. Variable('var_2', source=2, type=IntegerType)
  153. Variable('var_3', source=4, type=IntegerType)
  154. with Namespace('namespace_1_1'):
  155. Variable('var_1', source='val_1')
  156. assert datavars.namespace_1.var_1 == 'val_1'
  157. assert datavars.namespace_1.var_2 == 2
  158. assert datavars.namespace_1.var_3 == 4
  159. assert datavars.namespace_1.namespace_1_1.var_1 == 'val_1'
  160. def test_if_variables_creation_api_is_used_for_creating_of_a_namespace_and_of_a_number_of_variables_and_there_is_attempt_to_get_value_of_an_unexistent_variable_from_a_created_namespace__the_namespace_raises_the_VariableError_exception(self):
  161. Namespace.reset()
  162. datavars = Namespace.datavars
  163. with Namespace('namespace_1'):
  164. Variable('var_1', source='val_1')
  165. Variable('var_2', source=2, type=IntegerType)
  166. Variable('var_3', source=4, type=IntegerType)
  167. with pytest.raises(VariableError):
  168. datavars.namespace_1.var_4
  169. def test_if_dependence_interface_object_is_used_for_finding_an_existing_variable_from_an_other_namespace_using_absolute_variable_name__the_find_variable_method_returns_the_desired_variable(self):
  170. Namespace.reset()
  171. datavars = Namespace.datavars
  172. with Namespace('namespace_1'):
  173. Variable('var_1', source='val_1', type=StringType)
  174. with Namespace('namespace_2'):
  175. assert Dependence._get_variable('namespace_1.var_1') ==\
  176. datavars.namespace_1['var_1']
  177. def test_if_dependence_interface_object_is_used_for_finding_an_unexisting_variable_from_an_other_namespace_using_absolute_variable_name__the_find_variable_method_raises_the_VariableError_exception(self):
  178. Namespace.reset()
  179. with Namespace('namespace_1'):
  180. Variable('var_1', source='val_1', type=StringType)
  181. with Namespace('namespace_2'):
  182. with pytest.raises(VariableError):
  183. Dependence._get_variable('namespace_1.var_2')
  184. def test_if_dependence_interface_object_is_used_for_finding_an_existing_variable_from_a_same_namespace_using_relative_variable_name__the_find_variable_method_returns_the_desired_variable(self):
  185. Namespace.reset()
  186. datavars = Namespace.datavars
  187. with Namespace('namespace_1'):
  188. Variable('var_1', source='val_1', type=StringType)
  189. Variable('var_2', source=2, type=IntegerType)
  190. Variable('var_3', source=4, type=IntegerType)
  191. with Namespace('namespace_1_1'):
  192. Variable('var_1', source='val_1', type=StringType)
  193. assert Dependence._get_variable('.var_1') ==\
  194. datavars.namespace_1.namespace_1_1['var_1']
  195. assert Dependence._get_variable('..var_3') ==\
  196. datavars.namespace_1['var_3']
  197. def test_if_dependence_interface_object_is_used_for_finding_an_unexisting_variable_from_a_same_namespace_using_relative_variable_name__the_find_variable_method_raises_the_VariableError_exception(self):
  198. Namespace.reset()
  199. with Namespace('namespace_1'):
  200. Variable('var_1', source='val_1', type=StringType)
  201. Variable('var_2', source=2, type=IntegerType)
  202. with Namespace('namespace_1_1'):
  203. with pytest.raises(VariableError):
  204. Dependence._get_variable('..var_3')
  205. def test_if_variable_is_created_with_dependence_in_its_source_and_the_dependence_has_only_set_function_without_any_arguments__the_variable_updates_its_value_using_only_set_function(self):
  206. Namespace.reset()
  207. datavars = Namespace.datavars
  208. with Namespace('namespace_1'):
  209. def source_function():
  210. return 'value_from_function'
  211. Variable('var', source=Dependence(depend=source_function),
  212. type=VariableType)
  213. assert datavars.namespace_1.var == 'value_from_function'
  214. def test_if_a_variable_is_created_using_a_Copy_of_a_variable_in_the_source__the_new_variable_uses_this_variable_s_value_to_create_its_own_value(self):
  215. Namespace.reset()
  216. datavars = Namespace.datavars
  217. with Namespace('namespace_1'):
  218. Variable('var_1', source='value_1', type=StringType)
  219. Variable('var_2', type=StringType, source=Copy('.var_1'))
  220. assert datavars.namespace_1.var_2 == 'value_1'
  221. def test_if_a_variable_is_created_using_a_Format_with_a_format_string_and_a_variable_in_the_source__the_new_variable_uses_the_variable_and_format_string_to_produce_a_new_string_value(self):
  222. Namespace.reset()
  223. datavars = Namespace.datavars
  224. with Namespace('namespace_1'):
  225. Variable('var_1', source='value_1', type=StringType)
  226. Variable('var_2', type=StringType,
  227. source=Format('var_1 = { .var_1 }'))
  228. assert datavars.namespace_1.var_2 == 'var_1 = value_1'
  229. def test_if_a_variable_is_created_using_a_Calculate_with_a_variable_and_some_function_in_the_source__the_new_variable_uses_the_variable_and_the_function_for_calculating_of_its_value(self):
  230. Namespace.reset()
  231. datavars = Namespace.datavars
  232. with Namespace('namespace_1'):
  233. Variable('var_1', source='value_1', type=StringType)
  234. def formatter(var_1):
  235. return 'var_1 = {}'.format(var_1.value)
  236. Variable('var_2', type=StringType,
  237. source=Calculate(formatter, '.var_1'))
  238. assert datavars.namespace_1.var_2 == 'var_1 = value_1'
  239. def test_if_a_variable_is_created_using_a_Dependence_with_a_static_value_a_variable_and_depend_function_in_the_source__the_new_variable_uses_the_variable_the_function_and_the_static_value_for_calculating_of_its_value(self):
  240. Namespace.reset()
  241. datavars = Namespace.datavars
  242. with Namespace('namespace_1'):
  243. def source_function(static_value):
  244. return 'static value = {}'.format(static_value.value)
  245. Variable('var', type=StringType,
  246. source=Dependence(Static('value'),
  247. depend=source_function))
  248. assert datavars.namespace_1.var == 'static value = value'
  249. def test_if_a_variable_is_created_using_a_Calculate_with_a_static_value_a_variable_and_some_function_in_the_source__the_new_variable_uses_the_variable_the_function_and_the_static_value_for_calculating_of_its_value(self):
  250. Namespace.reset()
  251. datavars = Namespace.datavars
  252. with Namespace('namespace_1'):
  253. Variable('var_1', source='var value', type=StringType)
  254. def suspicious_function(static_value, var_1):
  255. return '{} and {}'.format(static_value.value, var_1.value)
  256. Variable('var_2', type=StringType,
  257. source=Calculate(suspicious_function,
  258. Static('static value'), '.var_1'))
  259. assert datavars.namespace_1.var_2 == 'static value and var value'
  260. def test_if_a_variable_is_created_using_a_Calculate_with_a_non_string_value_a_variable_and_some_function_in_the_source__the_new_variable_uses_the_variable_the_function_and_the_static_value_for_calculating_of_its_value(self):
  261. Namespace.reset()
  262. datavars = Namespace.datavars
  263. with Namespace('namespace_1'):
  264. Variable('var_1', source=10, type=IntegerType)
  265. def suspicious_function(static_value, var_1):
  266. return static_value.value + var_1.value
  267. Variable('var_2', type=IntegerType,
  268. source=Calculate(suspicious_function,
  269. 12, '.var_1'))
  270. assert datavars.namespace_1.var_2 == 22
  271. def test_if_a_variable_is_created_using_dependence_with_three_variables_and_a_set_function_but_during_the_calculation_only_two_variables_was_used__the_unused_variable_is_not_in_variable_subscriptions_and_is_invalidated_before_first_usage(self):
  272. Namespace.reset()
  273. datavars = Namespace.datavars
  274. with Namespace('namespace_1'):
  275. Variable('var_1', source=5, type=IntegerType)
  276. Variable('var_2', source='less')
  277. Variable('var_3', source='greater')
  278. def solver(arg_1, arg_2, arg_3):
  279. if arg_1.value < 6:
  280. return arg_2.value
  281. else:
  282. return arg_3.value
  283. Variable('var_4', type=StringType,
  284. source=Dependence('.var_1', '.var_2', '.var_3',
  285. depend=solver))
  286. assert datavars.namespace_1.var_4 == datavars.namespace_1.var_2
  287. assert datavars.namespace_1['var_1'] in\
  288. datavars.namespace_1['var_4']._subscriptions
  289. assert datavars.namespace_1['var_2'] in\
  290. datavars.namespace_1['var_4']._subscriptions
  291. assert datavars.namespace_1['var_3'] not in\
  292. datavars.namespace_1['var_4']._subscriptions
  293. assert datavars.namespace_1['var_3'].value is None
  294. def test_if_variables_creation_api_is_used_for_creating_of_a_number_of_variables_with_existing_variables_dependencies_in_their_sources__the_api_fabrics_creates_the_variables(self):
  295. Namespace.reset()
  296. datavars = Namespace.datavars
  297. with Namespace('namespace_1'):
  298. var_1 = Variable('var_1', source=2, type=IntegerType)
  299. Variable('var_2', source=4, type=IntegerType)
  300. with Namespace('namespace_2'):
  301. Variable('var_1', source=Dependence(
  302. var_1, '..namespace_1.var_2', depend=lambda arg_1, arg_2:
  303. 'greater' if arg_1.value > arg_2.value else 'less'))
  304. assert datavars.namespace_1.var_1 == 2
  305. assert datavars.namespace_1.var_2 == 4
  306. assert datavars.namespace_2.var_1 == 'less'
  307. def test_if_variable_has_variable_dependence_in_its_source_and_the_subscription_variable_source_is_changed__the_subscriber_is_invalidated_and_then_updates_its_value(self):
  308. Namespace.reset()
  309. datavars = Namespace.datavars
  310. with Namespace('namespace_1'):
  311. var_1 = Variable('var_1', source=2, type=IntegerType)
  312. Variable('var_2', source=4, type=IntegerType)
  313. with Namespace('namespace_2'):
  314. Variable('var_1', source=Dependence(
  315. var_1, '..namespace_1.var_2', depend=lambda arg_1, arg_2:
  316. 'greater' if arg_1.value > arg_2.value else 'less'))
  317. datavars = Namespace.datavars
  318. assert datavars.namespace_2.var_1 == 'less'
  319. datavars.namespace_1['var_1'].source = 5
  320. assert datavars.namespace_2['var_1']._invalidated
  321. assert datavars.namespace_2.var_1 == 'greater'
  322. def test_if_variable_was_created_using_variables_api_and_then_was_created_again_with_simple_source_value__the_variable_just_changes_its_source(self):
  323. Namespace.reset()
  324. datavars = Namespace.datavars
  325. with Namespace('namespace_1'):
  326. var_1 = Variable('var_1', source='value_1')
  327. Variable('var_2', source=Dependence(
  328. var_1, depend=lambda arg_1:
  329. 'from var_1: {}'.format(arg_1.value)))
  330. assert datavars.namespace_1.var_1 == 'value_1'
  331. assert datavars.namespace_1.var_2 == 'from var_1: value_1'
  332. with Namespace('namespace_1'):
  333. Variable('var_1', source='value_2')
  334. assert datavars.namespace_1.var_1 == 'value_2'
  335. assert datavars.namespace_1.var_2 == 'from var_1: value_2'
  336. def test_if_variable_was_created_using_variables_api_and_then_was_created_again_with_a_new_dependency_as_its_source__the_variable_just_changes_its_source_to_the_new_dependency(self):
  337. Namespace.reset()
  338. datavars = Namespace.datavars
  339. with Namespace('namespace_1'):
  340. Variable('var_1', source='value_1')
  341. Variable('var_2', source=Dependence(
  342. '.var_1', depend=lambda arg_1:
  343. 'from var_1: {}'.format(arg_1.value)))
  344. Variable('var_3', source='value_3')
  345. assert datavars.namespace_1.var_1 == 'value_1'
  346. assert datavars.namespace_1.var_2 == 'from var_1: value_1'
  347. with Namespace('namespace_1'):
  348. Variable('var_2', source=Dependence(
  349. '.var_3', depend=lambda arg_1:
  350. 'from var_3: {}'.format(arg_1.value)))
  351. assert datavars.namespace_1.var_2 == 'from var_3: value_3'
  352. def test_if_some_variables_created_with_dependencies_in_its_sources_and_subscribed_to_each_other__variables_raise_the_CyclicVariableError_exception_while_its_calculating(self):
  353. Namespace.reset()
  354. datavars = Namespace.datavars
  355. with Namespace('namespace_1'):
  356. Variable('var_1', source='value_1')
  357. Variable('var_2', source=Dependence(
  358. 'namespace_1.var_1', depend=lambda arg_1:
  359. 'from var_1: {}'.format(arg_1.value)))
  360. def comparator(arg_1):
  361. return 'from var_2: {}'.format(arg_1.value)
  362. Variable('var_3', source=Dependence('.var_2', depend=comparator))
  363. Variable('var_1',
  364. source=Dependence('.var_3', depend=lambda arg_1:
  365. 'from var_3: {}'.format(arg_1.value)))
  366. with pytest.raises(CyclicVariableError):
  367. datavars.namespace_1.var_3
  368. def test_if_variables_with_string_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_strings(self):
  369. Namespace.reset()
  370. datavars = Namespace.datavars
  371. with Namespace('namespace_1'):
  372. Variable('var_1', source='value', type=StringType)
  373. Variable('var_2', source=2, type=StringType)
  374. Variable('var_3', source=False, type=StringType)
  375. assert datavars.namespace_1.var_1 == 'value'
  376. assert datavars.namespace_1.var_2 == '2'
  377. assert datavars.namespace_1.var_3 == 'False'
  378. def test_if_variables_with_integer_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_integers(self):
  379. Namespace.reset()
  380. datavars = Namespace.datavars
  381. with Namespace('namespace_1'):
  382. Variable('var_1', source=1, type=IntegerType)
  383. Variable('var_2', source='2', type=IntegerType)
  384. Variable('var_3', source=3.1, type=IntegerType)
  385. Variable('var_4', source='-4', type=IntegerType)
  386. assert datavars.namespace_1.var_1 == 1
  387. assert datavars.namespace_1.var_2 == 2
  388. assert datavars.namespace_1.var_3 == 3
  389. assert datavars.namespace_1.var_4 == -4
  390. def test_if_variable_with_integer_type_is_created_with_source_that_can_not_be_converted_to_an_integer_value__the_variable_api_raises_the_VariableTypeError_exception(self):
  391. Namespace.reset()
  392. datavars = Namespace.datavars
  393. with Namespace('namespace_1'):
  394. Variable('var_1', source='a', type=IntegerType)
  395. with pytest.raises(VariableTypeError):
  396. datavars.namespace_1.var_1
  397. def test_if_variables_with_float_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_floats(self):
  398. Namespace.reset()
  399. datavars = Namespace.datavars
  400. with Namespace('namespace_1'):
  401. Variable('var_1', source=1.1, type=FloatType)
  402. Variable('var_2', source='2.2', type=FloatType)
  403. Variable('var_3', source=3, type=FloatType)
  404. Variable('var_4', source='-4.4', type=FloatType)
  405. assert datavars.namespace_1.var_1 == 1.1
  406. assert datavars.namespace_1.var_2 == 2.2
  407. assert datavars.namespace_1.var_3 == 3.0
  408. assert datavars.namespace_1.var_4 == -4.4
  409. def test_if_variable_with_float_type_is_created_with_source_that_can_not_be_converted_to_an_float_value__the_variable_api_raises_the_VariableTypeError_exception(self):
  410. Namespace.reset()
  411. datavars = Namespace.datavars
  412. with Namespace('namespace_1'):
  413. Variable('var_1', source='a', type=FloatType)
  414. with pytest.raises(VariableTypeError):
  415. datavars.namespace_1.var_1
  416. def test_if_variables_with_bool_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_bool(self):
  417. Namespace.reset()
  418. datavars = Namespace.datavars
  419. with Namespace('namespace_1'):
  420. Variable('var_1', source=True, type=BooleanType)
  421. Variable('var_2', source='false', type=BooleanType)
  422. Variable('var_3', source='True', type=BooleanType)
  423. Variable('var_4', source='0', type=BooleanType)
  424. assert datavars.namespace_1.var_1 is True
  425. assert datavars.namespace_1.var_2 is False
  426. assert datavars.namespace_1.var_3 is True
  427. assert datavars.namespace_1.var_4 is True
  428. def test_if_variables_with_list_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_lists(self):
  429. Namespace.reset()
  430. datavars = Namespace.datavars
  431. with Namespace('namespace_1'):
  432. Variable('var_1', source=['a', 'b', 'c'], type=ListType)
  433. Variable('var_2', source='a, b, c', type=ListType)
  434. Variable('var_3', source={'a', 'b', 'c'}, type=ListType)
  435. assert datavars.namespace_1.var_1 == ['a', 'b', 'c']
  436. assert datavars.namespace_1.var_2 == ['a', 'b', 'c']
  437. assert datavars.namespace_1.var_3 == list({'a', 'b', 'c'})
  438. def test_if_variable_with_list_type_is_created_with_source_that_can_not_be_converted_to_an_list_value__the_variable_api_raises_the_VariableTypeError_exception(self):
  439. Namespace.reset()
  440. datavars = Namespace.datavars
  441. with Namespace('namespace_1'):
  442. Variable('var_1', source=1, type=ListType)
  443. with pytest.raises(VariableTypeError):
  444. datavars.namespace_1.var_1
  445. def test_if_hash_variable_is_created_with_Hash_type_and_with_a_dictionary_in_its_source__the_variable_is_created_with_Hash_value_in_it(self):
  446. Namespace.reset()
  447. datavars = Namespace.datavars
  448. with Namespace('namespace_1'):
  449. Variable('var_1', type=HashType, source={'key_1': 'value_1',
  450. 'key_2': 'value_2'})
  451. assert datavars.namespace_1.var_1.key_1 == 'value_1'
  452. assert datavars.namespace_1.var_1.key_2 == 'value_2'
  453. with Namespace('namespace_1'):
  454. Variable('var_1', source={'key_1': 'another_value',
  455. 'key_2': 'other_value'})
  456. assert datavars.namespace_1.var_1.key_1 == 'another_value'
  457. assert datavars.namespace_1.var_1.key_2 == 'other_value'
  458. def test_if_hash_variable_is_created_with_Hash_type_and_with_a_dependence_in_its_source__the_variable_is_created_with_Hash_value_in_it_using_dictionary_returned_by_depend_function(self):
  459. Namespace.reset()
  460. datavars = Namespace.datavars
  461. with Namespace('namespace_1'):
  462. Variable('var_1', source='value_1', type=StringType)
  463. Variable('var_2', type=HashType, source={'key_1': 'value_1',
  464. 'key_2': 'value_2'})
  465. Variable('var_3', source='value_3', type=StringType)
  466. def depend_hash(arg_1, arg_2):
  467. return {'key_1': 'value',
  468. 'key_2': arg_1.value,
  469. 'key_3': arg_2.value}
  470. Variable('var_4', type=HashType,
  471. source=Dependence('.var_1', '.var_2.key_1',
  472. depend=depend_hash))
  473. assert datavars.namespace_1.var_4.key_1 == 'value'
  474. assert datavars.namespace_1.var_4.key_2 == 'value_1'
  475. assert datavars.namespace_1.var_4.key_3 == 'value_1'
  476. with Namespace('namespace_1'):
  477. Variable('var_1', source='other_value', type=StringType)
  478. Variable('var_2', source={'key_1': 'another_value',
  479. 'key_2': 'value_2'})
  480. assert datavars.namespace_1.var_2.key_1 == 'another_value'
  481. assert datavars.namespace_1.var_4.key_2 == 'other_value'
  482. assert datavars.namespace_1.var_4.key_3 == 'another_value'
  483. def test_if_hash_variable_is_created_as_not_fixed_and_then_changed_using_dictionary_with_a_new_key__the_hash_adds_a_new_key_to_the_first_hash(self):
  484. Namespace.reset()
  485. datavars = Namespace.datavars
  486. with Namespace('namespace_1'):
  487. Variable('var_1', type=HashType, source={'key_1': 'value_1',
  488. 'key_2': 'value_2'})
  489. assert datavars.namespace_1.var_1.key_1 == 'value_1'
  490. assert datavars.namespace_1.var_1.key_2 == 'value_2'
  491. with Namespace('namespace_1'):
  492. Variable('var_1', source={'key_1': 'another_value',
  493. 'key_2': 'other_value',
  494. 'key_3': 'new_value'})
  495. assert datavars.namespace_1.var_1.key_1 == 'another_value'
  496. assert datavars.namespace_1.var_1.key_2 == 'other_value'
  497. assert datavars.namespace_1.var_1.key_3 == 'new_value'
  498. def test_if_hash_variable_is_created_as_fixed_and_then_changed_using_dictionary_with_a_new_key__the_hash_raises_the_VariableError_exception(self):
  499. Namespace.reset()
  500. datavars = Namespace.datavars
  501. with Namespace('namespace_1'):
  502. Variable('var_1', type=HashType.fixed, source={'key_1': 'value_1',
  503. 'key_2': 'value_2'})
  504. assert datavars.namespace_1.var_1.key_1 == 'value_1'
  505. assert datavars.namespace_1.var_1.key_2 == 'value_2'
  506. with Namespace('namespace_1'):
  507. Variable('var_1', source={'key_1': 'another_value',
  508. 'key_2': 'other_value',
  509. 'key_3': 'new_value'})
  510. assert datavars.namespace_1['var_1'].fixed
  511. with pytest.raises(VariableError):
  512. datavars.namespace_1.var_1.key_1
  513. def test_if_variable_is_created_with_dependence_and_one_of_arguments_of_a_depend_function_is_whole_hash__the_variable_will_set_this_argument_as_dictionary_of_the_whole_hash_in_a_depend_function(self):
  514. Namespace.reset()
  515. datavars = Namespace.datavars
  516. with Namespace('namespace_1'):
  517. Variable('var_1', source='value_1', type=StringType)
  518. Variable('var_2', type=HashType, source={'key_1': 'value_1',
  519. 'key_2': 'value_2'})
  520. Variable('var_3', source='value_3', type=StringType)
  521. def depend_hash(arg_1):
  522. return arg_1.value['key_1']
  523. Variable('var_4', type=StringType,
  524. source=Dependence('.var_2', depend=depend_hash))
  525. assert datavars.namespace_1.var_4 == 'value_1'
  526. def test_if_a_variable_has_TableType_and_created_using_list_of_dicts_in_its_source_and_other_variable_created_using_depend_function_with_the_first_variable_in_its_arg__the_fitst_variable_is_created_with_Table_value_and_depend_function_of_the_second_variable_can_use_whole_table_for_the_calculating_of_the_variable_s_value(self):
  527. Namespace.reset()
  528. datavars = Namespace.datavars
  529. with Namespace('namespace_1'):
  530. Variable('var_1', type=TableType, source=[{'name': 'name_1',
  531. 'value': 'value_1'},
  532. {'name': 'name_2',
  533. 'value': 'value_2'},
  534. {'name': 'name_3',
  535. 'value': 'value_3'}])
  536. def depend_function(table):
  537. for row in table.value:
  538. if row['name'] == 'name_2':
  539. return row['value']
  540. Variable('var_2', type=StringType,
  541. source=Dependence('namespace_1.var_1',
  542. depend=depend_function))
  543. datavars.namespace_1.var_1[0]
  544. print(datavars.namespace_1.var_1[0])
  545. assert datavars.namespace_1.var_2 == 'value_2'
  546. def test_if_variable_of_TableType_is_created_using_dependence_that_creates_table__the_created_variable_has_generated_table_as_its_value(self):
  547. try:
  548. from portage.package.ebuild.config import config
  549. value = [{'name': name, 'path': path}
  550. for path, name in
  551. config().repositories.location_map.items()]
  552. Namespace.reset()
  553. datavars = Namespace.datavars
  554. with Namespace('main'):
  555. Variable('chroot', source='/', type=StringType.readonly)
  556. with Namespace('os'):
  557. def source_function(chroot_path):
  558. if chroot_path.value == '/':
  559. with stderr_devnull():
  560. return config()
  561. Variable('config', source=Dependence('main.chroot',
  562. depend=source_function),
  563. type=VariableType)
  564. def config_source(config):
  565. return [{'name': name, 'path': path}
  566. for path, name in
  567. config.value.repositories.location_map.items()]
  568. Variable('repositories', type=TableType,
  569. source=Dependence('.config', depend=config_source))
  570. assert datavars.os.repositories.get_table() == value
  571. except ImportError:
  572. # Если тестируем на другой системе модуль portage будет не найден.
  573. assert True
  574. # Тестируем теперь заполнитель переменных из calculate.ini
  575. def test_if_calculate_ini_file_is_used_just_to_create_some_variables__the_namespace_filler_will_create_them(self):
  576. Namespace.reset()
  577. datavars = Namespace.datavars
  578. namespace_filler = NamespaceIniFiller(restrict_creation=False)
  579. calculate_ini_text = """
  580. [os]
  581. test = 123
  582. [os][linux]
  583. shortname = CLD
  584. fullname = Calculate Linux Desktop
  585. """
  586. namespace_filler.fill(datavars, calculate_ini_text)
  587. assert datavars.os.test == '123'
  588. assert datavars.os.linux.shortname == 'CLD'
  589. assert datavars.os.linux.fullname == 'Calculate Linux Desktop'
  590. def test_if_a_calculate_ini_file_is_used_to_create_some_variables_and_an_other_one_is_used_to_update_value_of_the_variable__the_namespace_filler_creates_them_using_a_first_calculate_ini_and_then_changes_value_using_a_second_calculate_ini(self):
  591. Namespace.reset()
  592. datavars = Namespace.datavars
  593. namespace_filler = NamespaceIniFiller(restrict_creation=False)
  594. first_ini_text = """
  595. [os]
  596. test = 123
  597. [os][linux]
  598. shortname = CLD
  599. fullname = Calculate Linux Desktop
  600. """
  601. namespace_filler.fill(datavars, first_ini_text)
  602. assert datavars.os.test == '123'
  603. assert datavars.os.linux.shortname == 'CLD'
  604. assert datavars.os.linux.fullname == 'Calculate Linux Desktop'
  605. second_ini_text = """
  606. [os][linux]
  607. shortname = CLDX
  608. """
  609. namespace_filler.fill(datavars, second_ini_text)
  610. assert datavars.os.linux.shortname == 'CLDX'
  611. def test_if_calculate_ini_file_creates_a_variable_and_then_uses_append_operation_to_append_any_value_to_the_created_variable__the_namespace_filler_appends_the_value_to_the_created_variable(self):
  612. Namespace.reset()
  613. datavars = Namespace.datavars
  614. namespace_filler = NamespaceIniFiller(restrict_creation=False)
  615. first_ini_text = """
  616. [os]
  617. test = 123
  618. [os]
  619. test += 345
  620. """
  621. namespace_filler.fill(datavars, first_ini_text)
  622. assert datavars.os.test == "123,345"
  623. second_ini_text = """
  624. [os]
  625. test += asdf,qwer,zxcv
  626. """
  627. namespace_filler.fill(datavars, second_ini_text)
  628. assert datavars.os.test == "123,345,asdf,qwer,zxcv"
  629. def test_if_calculate_ini_file_creates_a_variable_and_then_uses_remove_operation_to_remove_any_values_in_the_created_variable__the_namespace_filler_removes_the_values_from_the_created_variable(self):
  630. Namespace.reset()
  631. datavars = Namespace.datavars
  632. namespace_filler = NamespaceIniFiller(restrict_creation=False)
  633. first_ini_text = """
  634. [os]
  635. test = 123,345,asdf,qwer,zxcv
  636. """
  637. namespace_filler.fill(datavars, first_ini_text)
  638. assert datavars.os.test == "123,345,asdf,qwer,zxcv"
  639. second_ini_text = """
  640. [os]
  641. test -= 345
  642. """
  643. namespace_filler.fill(datavars, second_ini_text)
  644. assert datavars.os.test == "123,asdf,qwer,zxcv"
  645. third_ini_text = """
  646. [os]
  647. test -= asdf,zxcv
  648. """
  649. namespace_filler.fill(datavars, third_ini_text)
  650. assert datavars.os.test == "123,qwer"
  651. def test_if_calculate_ini_file_is_used_for_creation_variables_in_the_custom_and_some_other_namespaces_and_for_changing_of_a_variable_from_not_custom_namespaces__the_NamespaceIniFiller_object_just_creates_variables_in_the_custom_namespace_and_modifies_variables_from_other_namespaces(self):
  652. Namespace.reset()
  653. datavars = Namespace.datavars
  654. namespace_filler = NamespaceIniFiller()
  655. with Namespace('os'):
  656. Variable('var_1', type=StringType, source='value_1')
  657. Variable('var_2', type=StringType, source='value_2')
  658. first_ini_text = """
  659. [os]
  660. var_1 = other_value
  661. var_3 = value_3
  662. [custom][test_1]
  663. var_1 = value_1
  664. [custom][test_2]
  665. var_1 = value_1
  666. """
  667. namespace_filler.fill(datavars, first_ini_text)
  668. assert datavars.os.var_1 == "other_value"
  669. assert 'var_3' not in datavars.os
  670. assert datavars.custom.test_1.var_1 == "value_1"
  671. assert datavars.custom.test_2.var_1 == "value_1"
  672. def test_if_calculate_ini_file_is_used_for_clearing_namespaces_in_the_custom_and_some_other_namespaces__the_NamespaceIniFiller_object_clears_namespaces_only_from_the_custom_namespace(self):
  673. Namespace.reset()
  674. datavars = Namespace.datavars
  675. namespace_filler = NamespaceIniFiller()
  676. with Namespace('os'):
  677. Variable('var_1', type=StringType, source='value_1')
  678. Variable('var_2', type=StringType, source='value_2')
  679. first_ini_text = """
  680. [custom][test]
  681. var_1 = value_1
  682. var_2 = value_2
  683. """
  684. namespace_filler.fill(datavars, first_ini_text)
  685. assert datavars.custom.test.var_1 == 'value_1'
  686. assert datavars.custom.test.var_2 == 'value_2'
  687. second_ini_text = """
  688. [custom][test][]
  689. [os][]
  690. """
  691. namespace_filler.fill(datavars, second_ini_text)
  692. assert datavars.os.var_1 == 'value_1'
  693. assert datavars.os.var_2 == 'value_2'
  694. assert 'var_1' not in datavars.custom.test._variables
  695. assert 'var_2' not in datavars.custom.test._variables
  696. def test_if_calculate_ini_file_contains_some_errors__the_NamespaceIniFiller_object_collect_all_error_messages_to_the_error_attribute(self):
  697. Namespace.reset()
  698. datavars = Namespace.datavars
  699. namespace_filler = NamespaceIniFiller()
  700. input_text = '''[section
  701. varval1 = value1
  702. varval2 = value2
  703. [section][sub2]
  704. varval1 = value1
  705. eee
  706. [section2
  707. varval4 = value4
  708. [section][]
  709. [section][][sub]
  710. [section][][sub][]'''
  711. if isinstance(datavars, Datavars):
  712. errors = ['SyntaxError:1: [section',
  713. 'SyntaxError:2: varval1 = value1',
  714. 'SyntaxError:3: varval2 = value2',
  715. ("VariableError:4: variables package 'section' is not"
  716. " found."),
  717. 'SyntaxError:6: eee',
  718. 'SyntaxError:8: [section2',
  719. ("VariableError:10: can not clear namespace 'section'."
  720. " Variables package 'section' is not found."),
  721. 'SyntaxError:11: [section][][sub]',
  722. 'SyntaxError:12: [section][][sub][]']
  723. else:
  724. errors = ['SyntaxError:1: [section',
  725. 'SyntaxError:2: varval1 = value1',
  726. 'SyntaxError:3: varval2 = value2',
  727. ("VariableError:4: can not create namespace"
  728. " '<root>.section' in not 'custom' namespace."),
  729. 'SyntaxError:6: eee',
  730. 'SyntaxError:8: [section2',
  731. ("VariableError:10: can not clear namespace 'section'."
  732. " Namespace is not found."),
  733. 'SyntaxError:11: [section][][sub]',
  734. 'SyntaxError:12: [section][][sub][]']
  735. namespace_filler.fill(datavars, input_text)
  736. for parsed_error, error in zip(namespace_filler.errors, errors):
  737. assert parsed_error == error
  738. def test_if_a_variable_is_created_as_readonly_variable_and_there_is_a_try_to_new_value_using_ini_file__the_ini_parser_does_not_change_the_variable_and_sets_variable_error(self):
  739. Namespace.reset()
  740. datavars = Namespace.datavars
  741. namespace_filler = NamespaceIniFiller()
  742. with Namespace('os'):
  743. Variable('var_1', source='value_1', type=StringType.readonly)
  744. Variable('var_2', type=HashType.readonly,
  745. source={'key_1': 'value_1', 'key_2': 'value_2'})
  746. first_ini_text = """[os]
  747. var_1 = new_value
  748. [os][var_2]
  749. key_1 = new_value
  750. """
  751. namespace_filler.fill(datavars, first_ini_text)
  752. assert datavars.os.var_1 == 'value_1'
  753. assert datavars.os.var_2.get_hash() == {'key_1': 'value_1',
  754. 'key_2': 'value_2'}
  755. assert namespace_filler.errors == ["VariableError:2: can not change"
  756. " readonly variable 'os.var_1'",
  757. "VariableError:5: can not change"
  758. " readonly hash variable 'os.var_2'"
  759. ]
  760. def test_if_calculate_ini_file_is_used_for_changing_of_an_unfixed_hash_variable_created_using_the_variables_api_and_for_adding_of_the_new_key_in_it__the_NamespaceIniFiller_object_changes_hash_value_and_adds_a_new_key(self):
  761. Namespace.reset()
  762. datavars = Namespace.datavars
  763. namespace_filler = NamespaceIniFiller()
  764. with Namespace('os'):
  765. Variable('var_1', type=HashType, source={'key_1': 'value_1',
  766. 'key_2': 'value_2'})
  767. first_ini_text = """
  768. [os][var_1]
  769. key_1 = new_value
  770. key_3 = value_3
  771. """
  772. namespace_filler.fill(datavars, first_ini_text)
  773. assert datavars.os.var_1.get_hash() == {'key_1': 'new_value',
  774. 'key_2': 'value_2',
  775. 'key_3': 'value_3'}
  776. def test_if_calculate_ini_file_is_used_just_for_changing_of_an_fixed_hash_variable_created_using_the_variables_api__the_NamespaceIniFiller_object_changes_hash_value(self):
  777. Namespace.reset()
  778. datavars = Namespace.datavars
  779. namespace_filler = NamespaceIniFiller()
  780. with Namespace('os'):
  781. Variable('var_1', type=HashType.fixed, source={'key_1': 'value_1',
  782. 'key_2': 'value_2'})
  783. first_ini_text = """
  784. [os][var_1]
  785. key_1 = new_value
  786. key_2 = weird_value
  787. """
  788. namespace_filler.fill(datavars, first_ini_text)
  789. print('NEW HASH VALUE: {}'.format(datavars.os.var_1.get_hash()))
  790. assert datavars.os.var_1.get_hash() == {'key_1': 'new_value',
  791. 'key_2': 'weird_value'}
  792. def test_if_calculate_ini_file_is_used_just_for_adding_a_new_key_to_an_fixed_hash_variable_created_using_the_variables_api__the_NamespaceIniFiller_object_raises_the_VariableError_exception(self):
  793. Namespace.reset()
  794. datavars = Namespace.datavars
  795. namespace_filler = NamespaceIniFiller()
  796. with Namespace('os'):
  797. Variable('var_1', type=HashType.fixed, source={'key_1': 'value_1',
  798. 'key_2': 'value_2'})
  799. first_ini_text = """
  800. [os][var_1]
  801. key_3 = weird_value
  802. """
  803. namespace_filler.fill(datavars, first_ini_text)
  804. assert datavars.os.var_1.get_hash() == {'key_1': 'value_1',
  805. 'key_2': 'value_2'}
  806. def test_if_calculate_ini_file_is_used_for_creation_of_the_table_variable_in_the_custom_namespace__the_NamespaceIniFiller_object_creates_table_in_the_custom_namespace(self):
  807. Namespace.reset()
  808. datavars = Namespace.datavars
  809. namespace_filler = NamespaceIniFiller()
  810. with Namespace('os'):
  811. Variable('var_1', type=StringType, source='value_1')
  812. Variable('var_2', type=StringType, source='value_2')
  813. first_ini_text = """
  814. [custom][test][0]
  815. name = name_0
  816. value = value_0
  817. [custom][test][1]
  818. name = name_1
  819. value = value_1
  820. [custom][test][2]
  821. name = name_2
  822. value = value_2
  823. """
  824. namespace_filler.fill(datavars, first_ini_text)
  825. assert datavars.custom.test.get_table() ==\
  826. [{'name': 'name_0', 'value': 'value_0'},
  827. {'name': 'name_1', 'value': 'value_1'},
  828. {'name': 'name_2', 'value': 'value_2'}]
  829. def test_if_calculate_ini_file_is_used_for_creation_of_the_table_variable_in_not_custom_namespaces__the_NamespaceIniFiller_object_does_not_create_anything(self):
  830. Namespace.reset()
  831. datavars = Namespace.datavars
  832. namespace_filler = NamespaceIniFiller()
  833. with Namespace('os'):
  834. Variable('var_1', type=StringType, source='value_1')
  835. Variable('var_2', type=StringType, source='value_2')
  836. first_ini_text = """
  837. [os][test][0]
  838. name = name_0
  839. value = value_0
  840. [os][test][1]
  841. name = name_1
  842. value = value_1
  843. [os][test][2]
  844. name = name_2
  845. value = value_2
  846. """
  847. namespace_filler.fill(datavars, first_ini_text)
  848. assert 'test' not in datavars.os
  849. def test_if_two_tables_are_created_using_calculate_ini_file_and_variables_api_and_then_calculate_ini_file_is_used_for_clearing_and_add_rows_in_the_both_tables__the_NamespaceIniFiller_object_clears_tables_and_adds_rows(self):
  850. Namespace.reset()
  851. datavars = Namespace.datavars
  852. namespace_filler = NamespaceIniFiller()
  853. with Namespace('os'):
  854. Variable('test', type=TableType, source=[{'name': 'name_1',
  855. 'value': 'value_1'},
  856. {'name': 'name_2',
  857. 'value': 'value_2'},
  858. {'name': 'name_3',
  859. 'value': 'value_3'}])
  860. first_ini_text = """
  861. # Создаем таблицу.
  862. [custom][test][0]
  863. name = name_0
  864. value = value_0
  865. [custom][test][1]
  866. name = name_1
  867. value = value_1
  868. # Очищаем таблицу.
  869. [custom][test][]
  870. # Добавляем в таблицу строку.
  871. [custom][test][0]
  872. name = name_0
  873. value = value_0
  874. # Очищаем таблицу созданную интерфейсом.
  875. [os][test][]
  876. # Добавляем в эту таблицу строку.
  877. [os][test][0]
  878. name = strange_name
  879. value = weird_value
  880. """
  881. namespace_filler.fill(datavars, first_ini_text)
  882. assert datavars.custom.test.get_table() ==\
  883. [{'name': 'name_0', 'value': 'value_0'}]
  884. assert datavars.os.test.get_table() ==\
  885. [{'name': 'strange_name', 'value': 'weird_value'}]
  886. def test_if_calculate_ini_file_is_used_for_modifying_of_the_table_from_calculate_ini_file__the_NamespaceIniFiller_object_modifies_the_table(self):
  887. Namespace.reset()
  888. datavars = Namespace.datavars
  889. namespace_filler = NamespaceIniFiller()
  890. first_ini_text = """
  891. # Создаем таблицу.
  892. [custom][test][0]
  893. name = name_0
  894. value = value_0
  895. [custom][test][1]
  896. name = name_1
  897. value = value_1
  898. """
  899. namespace_filler.fill(datavars, first_ini_text)
  900. assert datavars.custom.test.get_table() ==\
  901. [{'name': 'name_0', 'value': 'value_0'},
  902. {'name': 'name_1', 'value': 'value_1'}]
  903. second_ini_text = """
  904. [custom][test][1]
  905. name = other_name
  906. value = another_value
  907. """
  908. namespace_filler.fill(datavars, second_ini_text)
  909. assert datavars.custom.test.get_table() ==\
  910. [{'name': 'name_0', 'value': 'value_0'},
  911. {'name': 'other_name', 'value': 'another_value'}]
  912. def test_if_calculate_ini_file_is_used_for_modifying_of_the_table_created_using_variables_api__the_NamespaceIniFiller_object_modifies_the_table(self):
  913. Namespace.reset()
  914. datavars = Namespace.datavars
  915. namespace_filler = NamespaceIniFiller()
  916. with Namespace('namespace_1'):
  917. Variable('test', type=TableType, source=[{'name': 'name_1',
  918. 'value': 'value_1'},
  919. {'name': 'name_2',
  920. 'value': 'value_2'},
  921. {'name': 'name_3',
  922. 'value': 'value_3'}])
  923. first_ini_text = """
  924. [namespace_1][test][0]
  925. name = new_name
  926. value = other_value
  927. [namespace_1][test][1]
  928. name = common_name
  929. value = another_value
  930. """
  931. namespace_filler.fill(datavars, first_ini_text)
  932. assert datavars.namespace_1.test.get_table() ==\
  933. [{'name': 'new_name', 'value': 'other_value'},
  934. {'name': 'common_name', 'value': 'another_value'},
  935. {'name': 'name_3', 'value': 'value_3'}]
  936. # Теперь тестируем применение объекта Datavars, через который
  937. # осуществляется доступ к переменным и их загрузка.
  938. def test_if_a_Datavars_object_is_created_with_path_to_the_variables_without_any_Dependencies_and_then_used_to_get_access_to_the_some_variables_from__the_datavars_object_dynamically_loads_variables_and_retruns_necessary_variables(self):
  939. datavars = Datavars(
  940. variables_path='tests/variables/testfiles/variables_0',
  941. repository_map={})
  942. assert datavars.os.ns.var_1 == 'value_1'
  943. assert datavars.os.ns.var_2 == 2
  944. assert 'other' not in datavars.root
  945. assert datavars.other.strange_variable == 'weird_value'
  946. assert datavars.other.plain_variable is True
  947. assert 'other' in datavars.root
  948. def test_if_a_Datavars_object_is_created_with_path_to_the_variables_with_some_Dependencies_and_then_used_to_get_access_to_the_some_variables_from__the_datavars_object_dynamically_loads_variables_and_retruns_necessary_variables(self):
  949. datavars = Datavars(
  950. variables_path='tests/variables/testfiles/variables_1',
  951. repository_map={})
  952. assert datavars.main.chroot == '/'
  953. assert 'os' not in datavars.root
  954. assert datavars.os.calculate == 'test1 test2'
  955. assert 'os' in datavars.root
  956. assert datavars.level.level_2.vargetter == '/ test'
  957. assert 'main' in datavars.root
  958. assert datavars.level.simple == "simple value"
  959. assert datavars.level.use_local_simple == "Using simple value"
  960. assert datavars.level.use_full_simple == "Using simple value"
  961. assert datavars.level.device_child == "hdd"
  962. assert datavars.level.device.get_table() == [
  963. {"dev": "/dev/sda",
  964. "type": "hdd",
  965. "name": "Samsung SSD"},
  966. {"dev": "/dev/sdb",
  967. "type": "flash",
  968. "name": "Transcend 64GB"}]
  969. def test_if_a_Datavars_object_is_created_with_path_to_the_variables_with_Dependencies_and_then_the_variables_are_changed_using_calculate_ini_files_and_the_datavars_object_used_to_get_access_to_the_some_variables_from__the_datavars_object_dynamically_loads_variables_and_retruns_necessary_variables(self):
  970. datavars = Datavars(
  971. variables_path='tests/variables/testfiles/variables_4',
  972. repository_map={})
  973. assert datavars.main.chroot == '/'
  974. assert 'os' not in datavars.root
  975. assert datavars.level.level_2.vargetter == '/ test'
  976. assert 'main' in datavars.root
  977. assert datavars.level.simple == "simple value"
  978. datavars._loader.fill_from_custom_ini(os.path.join(TESTFILES_PATH,
  979. 'calculate.ini'))
  980. assert 'os' in datavars.root
  981. assert datavars.level.simple == 'weird value'
  982. assert datavars.main.chroot == '/any/other/path'
  983. assert datavars.level.level_2.vargetter == '/any/other/path test'
  984. def test_to_make_testfiles(self):
  985. shutil.copytree(os.path.join(TESTFILES_PATH, 'gentoo.backup'),
  986. os.path.join(TESTFILES_PATH, 'gentoo'),
  987. symlinks=True)
  988. assert os.path.exists(os.path.join(TESTFILES_PATH, 'gentoo'))
  989. shutil.copytree(os.path.join(TESTFILES_PATH, 'ini_vars.backup'),
  990. os.path.join(TESTFILES_PATH, 'ini_vars'))
  991. assert os.path.exists(os.path.join(TESTFILES_PATH, 'ini_vars'))
  992. def test_test_if_Datavars_object_is_created_with_a_specific_variables_path_and_repository_map_is_set_and_profile_path_is_set_in_the_variables__the_Datavars_object_finds_all_variables_packages_and_ini_files_from_a_profile_using_the_profile_path_variable_and_the_repository_map(self):
  993. repository_map = {'distros': os.path.join(TESTFILES_PATH,
  994. "gentoo/repos/distros"),
  995. 'calculate': os.path.join(TESTFILES_PATH,
  996. "gentoo/repos/calculate"),
  997. 'gentoo': os.path.join(TESTFILES_PATH,
  998. "gentoo/portage")}
  999. datavars = Datavars(
  1000. variables_path=os.path.join(TESTFILES_PATH, 'variables_2'),
  1001. repository_map=repository_map)
  1002. assert datavars.os.linux.shortname == 'CLD'
  1003. assert datavars.os.linux.fullname == 'Calculate Linux Desktop'
  1004. assert datavars.os.linux.ver == '20'
  1005. assert datavars.os.linux.subname == 'KDE'
  1006. assert datavars.os.linux.arch == 'amd64'
  1007. assert datavars.os.hashvar.value1 == 'new1'
  1008. assert datavars.os.hashvar.value2 == 'new2'
  1009. assert datavars.os.tablevar.get_table() == [
  1010. {'dev': '/dev/sda1', 'mount': 'swap'},
  1011. {'dev': '/dev/sda2', 'mount': '/'},
  1012. {'dev': '/dev/sda5', 'mount': '/var/calculate'}]
  1013. def test_test_if_Datavars_object_is_created_with_a_specific_variables_path_and_the_repository_map_and_the_current_profile_path_is_set_in_the_variables__the_Datavars_object_finds_all_variables_packages_and_ini_files_from_a_profile_using_the_profile_path_variable_and_the_repositories_variable(self):
  1014. datavars = Datavars(
  1015. variables_path=os.path.join(TESTFILES_PATH, 'variables_3'))
  1016. assert datavars.os.linux.shortname == 'CLD'
  1017. assert datavars.os.linux.fullname == 'Calculate Linux Desktop'
  1018. assert datavars.os.linux.ver == '20'
  1019. assert datavars.os.linux.subname == 'KDE'
  1020. assert datavars.os.linux.arch == 'amd64'
  1021. assert datavars.os.hashvar.value1 == 'new1'
  1022. assert datavars.os.hashvar.value2 == 'new2'
  1023. assert datavars.os.tablevar.get_table() == [
  1024. {'dev': '/dev/sda1', 'mount': 'swap'},
  1025. {'dev': '/dev/sda2', 'mount': '/'},
  1026. {'dev': '/dev/sda5', 'mount': '/var/calculate'}]
  1027. def test_test_if_Datavars_object_is_created_with_a_specific_variables_path_and_the_repository_map_the_current_profile_path_and_some_ini_files_paths_is_set_in_the_variables__the_Datavars_object_finds_all_variables_packages_and_ini_files_from_a_profile_using_the_profile_path_variable_and_the_repositories_variable_and_loads_variables_from_ini_files_from_the_env_order_and_the_env_path_variables(self):
  1028. datavars = Datavars(
  1029. variables_path=os.path.join(TESTFILES_PATH, 'variables_5'))
  1030. assert datavars.main.cl.system.env_order == ['system', 'local']
  1031. assert datavars.main.cl.system.env_path.get_hash() == {
  1032. 'system':
  1033. os.path.join(
  1034. TESTFILES_PATH,
  1035. 'ini_vars/calculate_0.ini'),
  1036. 'local':
  1037. os.path.join(
  1038. TESTFILES_PATH,
  1039. 'ini_vars/calculate_1.ini')}
  1040. assert datavars.custom.ns.var_1 == '1'
  1041. assert datavars.custom.ns.var_2 == 'weird value'
  1042. assert datavars.custom.ns.var_3 ==\
  1043. '/the/true/path/to/eternity/in/the/abyss'
  1044. assert datavars.custom.ns.var_4 == '12'
  1045. assert datavars.os.linux.test == 'new value'
  1046. assert datavars.os.linux.test_var == 'test = new value'
  1047. assert datavars.os.hashvar.value1 == 'new1'
  1048. assert datavars.os.hashvar.value2 == 'new2'
  1049. assert datavars.os.calculate == 'new1 new2'
  1050. def test_if_Datavars_object_is_created_and_all_variables_are_loaded__the_set_method_can_be_used_for_changing_just_variable_s_values_and_save_their_sources_and_the_reset_method_can_return_values_from_source(self):
  1051. datavars = Datavars(
  1052. variables_path=os.path.join(TESTFILES_PATH, 'variables_6'))
  1053. datavars.os.linux['test'].set('other value')
  1054. datavars.os['hashvar'].set({'value1': 'other1',
  1055. 'value2': 'other2'})
  1056. assert datavars.os.linux['test'].set_by_user
  1057. assert datavars.os['hashvar'].set_by_user
  1058. assert datavars.os.linux.test == 'other value'
  1059. assert datavars.os.hashvar.value1 == 'other1'
  1060. assert datavars.os.hashvar.value2 == 'other2'
  1061. assert datavars.os.linux.test_var == 'test = other value'
  1062. assert datavars.os.calculate == 'other1 other2'
  1063. datavars.os['calculate'].set('special value')
  1064. assert datavars.os.calculate == 'special value'
  1065. datavars.os['hashvar'].set({'value1': 'weird1',
  1066. 'value2': 'weird2'})
  1067. assert datavars.os.hashvar.value1 == 'weird1'
  1068. assert datavars.os.hashvar.value2 == 'weird2'
  1069. # При изменении зависимости эта переменная не инвалидируется, потому
  1070. # что была задана пользователем. Поэтому ее значение установленное set
  1071. # сохраняется.
  1072. assert datavars.os.calculate == 'special value'
  1073. # Если сбросить значения будут пересчитаны, используя установленный
  1074. # источник значения.
  1075. datavars.os['calculate'].reset()
  1076. assert datavars.os.calculate == 'weird1 weird2'
  1077. datavars.os.linux['test'].reset()
  1078. assert datavars.os.linux.test == 'new value'
  1079. datavars.os['hashvar'].reset()
  1080. assert datavars.os.hashvar.value1 == 'new1'
  1081. assert datavars.os.hashvar.value2 == 'new2'
  1082. assert datavars.os.calculate == 'new1 new2'
  1083. def test_if_Datavars_object_is_created_and_all_variables_are_loaded__the_set_method_can_be_used_for_changing_hash_values_with_saving_of_their_sources(self):
  1084. datavars = Datavars(variables_path=os.path.join(TESTFILES_PATH,
  1085. 'variables_6'))
  1086. datavars.os.hashvar['value1'].set('old but gold')
  1087. assert datavars.os.hashvar.get_hash() == {'value1': 'old but gold',
  1088. 'value2': 'new2'}
  1089. datavars.os['hashvar'].reset()
  1090. assert datavars.os.hashvar.get_hash() == {'value1': 'new1',
  1091. 'value2': 'new2'}
  1092. datavars.os.hashvar['value2'].set('almost blue')
  1093. assert datavars.os.hashvar.get_hash() == {'value1': 'new1',
  1094. 'value2': 'almost blue'}
  1095. datavars.os.hashvar['value2'].reset()
  1096. assert datavars.os.hashvar.get_hash() == {'value1': 'new1',
  1097. 'value2': 'new2'}
  1098. # Теперь тестируем применение объекта Datavars в шаблонах.
  1099. def test_if_Datavars_object_is_set_as_the_datavars_for_the_TemplateEngine_object__variables_from_the_Datavars_object_can_be_inserted_in_the_processed_template(self):
  1100. datavars = Datavars(
  1101. variables_path=os.path.join(TESTFILES_PATH, 'variables_7'))
  1102. template_engine = TemplateEngine(appends_set=APPENDS_SET,
  1103. chroot_path=TESTFILES_PATH,
  1104. datavars_module=datavars)
  1105. input_template = '''{% calculate name = 'filename', force -%}
  1106. os.linux.test = {{ os.linux.test }}
  1107. os.hashvar.value1 = {{ os.hashvar.value1 }}
  1108. os.hashvar.value2 = {{ os.hashvar.value2 }}
  1109. os.calculate = {{ os.calculate }}'''
  1110. output_text = '''os.linux.test = new value
  1111. os.hashvar.value1 = new1
  1112. os.hashvar.value2 = new2
  1113. os.calculate = new1 new2'''
  1114. template_engine.process_template_from_string(input_template, FILE)
  1115. text = template_engine.template_text
  1116. assert text == output_text
  1117. def test_if_Datavars_object_is_set_as_the_datavars_for_the_TemplateEngine_object_and_save_tag_is_used_for_saving_some_variables_and_target_file_is_not_set_for_the_save_tag__the_Datavars_object_saves_and_modifies_variables_from_the_save_tag(self):
  1118. datavars = Datavars(
  1119. variables_path=os.path.join(TESTFILES_PATH, 'variables_7'))
  1120. template_engine = TemplateEngine(appends_set=APPENDS_SET,
  1121. chroot_path=TESTFILES_PATH,
  1122. datavars_module=datavars)
  1123. input_template_1 = '''{% calculate name = 'filename', force -%}
  1124. {% save custom.ns.value = 12 -%}
  1125. {% save os.linux.test = 'new value' %}
  1126. {% save os.hashvar = {'value1': 'weird1', 'value2': 'weird2'} %}'''
  1127. template_engine.process_template_from_string(input_template_1, FILE)
  1128. assert datavars.custom.ns.value == '12'
  1129. assert datavars.os.linux.test == 'new value'
  1130. assert datavars.os.linux.test_var == 'test = new value'
  1131. assert datavars.os.hashvar.value1 == 'weird1'
  1132. assert datavars.os.hashvar.value2 == 'weird2'
  1133. assert datavars.os.calculate == 'weird1 weird2'
  1134. input_template_2 = '''{% calculate name = 'filename', force -%}
  1135. {% save os.hashvar.value1 = 'new1' %}'''
  1136. template_engine.process_template_from_string(input_template_2, FILE)
  1137. assert datavars.os.hashvar.value1 == 'new1'
  1138. assert datavars.os.hashvar.value2 == 'weird2'
  1139. assert datavars.os.calculate == 'new1 weird2'
  1140. def test_if_Datavars_object_is_set_as_the_datavars_for_the_TemplateEngine_object_and_save_tag_is_used_for_appending_and_removing_some_variable_s_values_and_target_file_is_not_set_for_the_save_tag__the_Datavars_object_changes_variables_from_the_save_tag(self):
  1141. datavars = Datavars(
  1142. variables_path=os.path.join(TESTFILES_PATH, 'variables_8'))
  1143. template_engine = TemplateEngine(appends_set=APPENDS_SET,
  1144. chroot_path=TESTFILES_PATH,
  1145. datavars_module=datavars)
  1146. input_template_1 = '''{% calculate name = 'filename', force -%}
  1147. {% save custom.ns.var_1 += 12 -%}
  1148. {% save os.linux.test_1 += 10 -%}
  1149. {% save os.linux.test_2 += 12 -%}
  1150. {% save os.linux.test_3 += 'd' -%}
  1151. {% save os.linux.test_4 += [4, 5] -%}
  1152. {% save custom.ns.var_2 += 'val4,val5' -%}
  1153. '''
  1154. template_engine.process_template_from_string(input_template_1, FILE)
  1155. assert datavars.custom.ns.var_1 == '1,12'
  1156. assert datavars.os.linux.test_1 == 22
  1157. assert datavars.os.linux.test_2 == 13.2
  1158. assert datavars.os.linux.test_3 == ['a', 'b', 'c', 'd']
  1159. assert datavars.os.linux.test_4 == [1, 2, 3, 4, 5]
  1160. assert datavars.custom.ns.var_2 == 'val1,val2,val3,val4,val5'
  1161. input_template_2 = '''{% calculate name = 'filename', force -%}
  1162. {% save custom.ns.var_1 -= 1 -%}
  1163. {% save os.linux.test_1 -= 11 -%}
  1164. {% save os.linux.test_2 -= 2.1 -%}
  1165. {% save os.linux.test_3 -= 'a' -%}
  1166. {% save os.linux.test_4 -= [1, 3] -%}
  1167. {% save custom.ns.var_2 -= 'val1,val4' -%}
  1168. '''
  1169. template_engine.process_template_from_string(input_template_2, FILE)
  1170. assert datavars.custom.ns.var_1 == '12'
  1171. assert datavars.os.linux.test_1 == 11
  1172. assert datavars.os.linux.test_2 == 11.1
  1173. assert datavars.os.linux.test_3 == ['b', 'c', 'd']
  1174. assert datavars.os.linux.test_4 == [2, 4, 5]
  1175. assert datavars.custom.ns.var_2 == 'val2,val3,val5'
  1176. def test_if_Datavars_object_is_set_as_the_datavars_for_the_TemplateEngine_object_and_save_tag_is_used_for_changing_some_variable_s_values_and_target_file_is_set_for_the_save_tag__the_Datavars_object_creates_new_and_modifies_existing_variables_and_saves_them_to_the_target_files(self):
  1177. datavars = Datavars(
  1178. variables_path=os.path.join(TESTFILES_PATH, 'variables_9'))
  1179. template_engine = TemplateEngine(appends_set=APPENDS_SET,
  1180. chroot_path=TESTFILES_PATH,
  1181. datavars_module=datavars)
  1182. input_template_1 = '''{% calculate name = 'filename', force -%}
  1183. {% save.local custom.ns.var_3 = 'value' -%}
  1184. {% save.local custom.ns.var_2 += 'val4' -%}
  1185. {% save.system custom.ns.var_5 = '/highway/to/hell' -%}
  1186. '''
  1187. local_ini_result = '''[custom][ns]
  1188. var_1 = 1
  1189. var_2 = val1,val2,val3,val4
  1190. var_3 = value
  1191. [os][linux]
  1192. test_1 += 12
  1193. '''
  1194. system_ini_result = '''[custom][ns]
  1195. var_5 = /highway/to/hell
  1196. var_6 = 12
  1197. [os][linux]
  1198. test = new value
  1199. '''
  1200. template_engine.process_template_from_string(input_template_1, FILE)
  1201. datavars.save_variables()
  1202. assert local_ini_result == read_file(
  1203. datavars.main.cl.system.env_path['local'].value)
  1204. assert system_ini_result == read_file(
  1205. datavars.main.cl.system.env_path['system'].value)
  1206. def test_if_Datavars_object_is_set_as_the_datavars_for_the_TemplateEngine_object_and_save_tag_is_used_for_changing_some_hash_variable_s_values_and_target_file_is_set_for_the_save_tag__the_Datavars_object_modifies_hash_variables_and_saves_them_to_the_target_files_as_namespaces(self):
  1207. datavars = Datavars(
  1208. variables_path=os.path.join(TESTFILES_PATH, 'variables_10'))
  1209. template_engine = TemplateEngine(appends_set=APPENDS_SET,
  1210. chroot_path=TESTFILES_PATH,
  1211. datavars_module=datavars)
  1212. input_template_1 = '''{% calculate name = 'filename', force -%}
  1213. {% save.system os.hashvar_0 = {'value1': 'new1', 'value2': 'new2'} -%}
  1214. {% save.system os.hashvar_1.key1 = 'weird1' -%}
  1215. {% save.system os.hashvar_2.id_1 = 1575 -%}
  1216. {% save.system os.hashvar_2.id_2 = 1349 -%}
  1217. '''
  1218. system_ini_result = '''[os][hashvar_0]
  1219. value1 = new1
  1220. value2 = new2
  1221. [os][hashvar_1]
  1222. key1 = weird1
  1223. [os][hashvar_2]
  1224. id_1 = 1575
  1225. id_2 = 1349
  1226. '''
  1227. template_engine.process_template_from_string(input_template_1, FILE)
  1228. datavars.save_variables()
  1229. assert system_ini_result ==\
  1230. read_file(datavars.main.cl.system.env_path['system'].value)
  1231. # Теперь тестируем обработку ошибок.
  1232. def test_if_calculate_ini_file_has_some_syntax_errors_in_its_text__loader_skips_incorrect_lines_and_puts_some_error_messages_in_the_output(self):
  1233. test_messages = []
  1234. test_handler = LoggingHandler(test_messages)
  1235. test_logger = logging.getLogger("test")
  1236. test_logger.addHandler(test_handler)
  1237. Datavars(variables_path=os.path.join(TESTFILES_PATH, 'variables_12'),
  1238. logger=test_logger)
  1239. messages = [('ERROR', 'SyntaxError:1: [section'),
  1240. ('ERROR', 'SyntaxError:2: varval1 = value1'),
  1241. ('ERROR', 'SyntaxError:3: varval2 = value2'),
  1242. ('ERROR', ("VariableError:4: variables package 'section'"
  1243. " is not found.")),
  1244. ('ERROR', 'SyntaxError:6: eee'),
  1245. ('ERROR', 'SyntaxError:8: [section2'),
  1246. ('ERROR', ("VariableError:10: can not clear namespace"
  1247. " 'section'. Variables package 'section' is not"
  1248. " found.")),
  1249. ('ERROR', 'SyntaxError:11: [section][][sub]'),
  1250. ('ERROR', 'SyntaxError:12: [section][][sub][]'),
  1251. ('WARNING', 'Some variables was not loaded.'),
  1252. ('INFO', "Variables from 'system' are loaded"),
  1253. ('INFO', "Loading variables from file: 'local'"),
  1254. ('WARNING', ("File 'local' is not found. Variables"
  1255. " are not loaded"))]
  1256. print('logged messages:', test_messages)
  1257. for message, datavars_message in zip(
  1258. messages,
  1259. test_messages[-len(messages):]):
  1260. assert message == datavars_message
  1261. def test_if_any_variables_module_has_syntax_error_in_its_code__the_variables_loader_raises_the_VariableError_exception(self):
  1262. with pytest.raises(VariableError):
  1263. Datavars(variables_path=os.path.join(TESTFILES_PATH,
  1264. 'variables_13'))
  1265. def test_if_datavars_object_is_used_to_get_access_to_an_unexisting_variable_from_a_not_custom_namespace_and_from_a_custom_namespace__the_datavars_module_raises_the_VariableNotFound_exception_in_the_first_case_and_returns_None_value_in_the_second_case(self):
  1266. datavars = Datavars(variables_path=os.path.join(TESTFILES_PATH,
  1267. 'variables_14'))
  1268. with pytest.raises(VariableNotFoundError):
  1269. datavars.level.variable
  1270. assert datavars.custom.variable is None
  1271. def test_if_some_template_processed_by_the_template_engine_tries_to_get_value_of_a_variable_that_does_not_exist__the_template_engine_raises_the_VariableNotFound_error(self):
  1272. datavars = Datavars(
  1273. variables_path=os.path.join(TESTFILES_PATH,
  1274. 'variables_15'))
  1275. template_engine = TemplateEngine(appends_set=APPENDS_SET,
  1276. chroot_path=TESTFILES_PATH,
  1277. datavars_module=datavars)
  1278. input_template = '''{% calculate name = 'filename', force -%}
  1279. os.linux.test_3 = {{ os.linux.test_3 }}
  1280. '''
  1281. with pytest.raises(VariableNotFoundError):
  1282. template_engine.process_template_from_string(input_template, FILE)
  1283. def test_two_datavars_objects(self):
  1284. datavars_1 = Datavars(variables_path=os.path.join(TESTFILES_PATH,
  1285. 'variables_7'))
  1286. datavars_2 = Datavars(variables_path=os.path.join(TESTFILES_PATH,
  1287. 'variables_7'))
  1288. input_template_1 = '''{% calculate name = 'filename', force -%}
  1289. {%- save os.linux.test = 'new_first' -%}'''
  1290. input_template_2 = '''{% calculate name = 'filename', force -%}
  1291. {%- save os.linux.test = 'new_second' -%}'''
  1292. template_engine_1 = TemplateEngine(appends_set=APPENDS_SET,
  1293. chroot_path=TESTFILES_PATH,
  1294. datavars_module=datavars_1)
  1295. template_engine_2 = TemplateEngine(appends_set=APPENDS_SET,
  1296. chroot_path=TESTFILES_PATH,
  1297. datavars_module=datavars_2)
  1298. template_engine_1.process_template_from_string(input_template_1, FILE)
  1299. template_engine_2.process_template_from_string(input_template_2, FILE)
  1300. assert datavars_1.os.linux.test == 'new_first'
  1301. assert datavars_2.os.linux.test == 'new_second'
  1302. def test_adding_some_custom_variables(self):
  1303. datavars = Datavars(
  1304. variables_path=os.path.join(TESTFILES_PATH, 'variables_7'))
  1305. template_engine = TemplateEngine(appends_set=APPENDS_SET,
  1306. chroot_path=TESTFILES_PATH,
  1307. datavars_module=datavars)
  1308. input_template_1 = '''{% calculate run='/bin/bash' -%}
  1309. {% save custom.qwerty = 'www-firefox,www-chromium' -%}
  1310. {% save custom.zxcv2.qwerty = 'www-firefox,www-chromium2' -%}
  1311. {% save custom.zxcv2.qwerty2 = 'www-firefox,www-chromium' -%}
  1312. echo {{ custom.qwerty }}
  1313. echo {{ custom.zxcv2.qwerty }}
  1314. echo {{ custom.zxcv2.qwerty2 }}
  1315. exit 0
  1316. '''
  1317. expected_output = '''echo www-firefox,www-chromium
  1318. echo www-firefox,www-chromium2
  1319. echo www-firefox,www-chromium
  1320. exit 0'''
  1321. template_engine.process_template_from_string(input_template_1, FILE)
  1322. result = template_engine.template_text
  1323. assert result == expected_output
  1324. def test_vars_with_class_method_as_depend_function(self):
  1325. datavars = Datavars(variables_path=os.path.join(TESTFILES_PATH,
  1326. 'variables_7'))
  1327. assert datavars.main.test_0 == "test_0"
  1328. assert datavars.main.test_1 == "test_1"
  1329. def test_for_removing_testfiles(self):
  1330. shutil.rmtree(os.path.join(TESTFILES_PATH, 'gentoo'))
  1331. assert not os.path.exists(os.path.join(TESTFILES_PATH, 'gentoo'))
  1332. shutil.rmtree(os.path.join(TESTFILES_PATH, 'ini_vars'))
  1333. assert not os.path.exists(os.path.join(TESTFILES_PATH, 'ini_vars'))
  1334. def test_getting_value_of_the_unexisting_row(self):
  1335. Namespace.reset()
  1336. datavars = Namespace.datavars
  1337. with Namespace('namespace_1'):
  1338. Variable('var_1', type=TableType, source=[{'name': 'name_1',
  1339. 'value': 'value_1'},
  1340. {'name': 'name_2',
  1341. 'value': 'value_2'},
  1342. {'name': 'name_3',
  1343. 'value': 'value_3'}])
  1344. assert datavars.namespace_1.var_1[3]['value'] is None