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.
calculate-utils-4-lib/tests/variables/test_variables.py

1038 lines
45 KiB

import os
import shutil
import pytest
from calculate.variables.datavars import NamespaceNode, VariableNode,\
Namespace, Variable, Dependence,\
CyclicVariableError, HashType,\
StringType, IntegerType,\
VariableType, VariableError,\
TableType, BooleanType,\
VariableTypeError, FloatType,\
ListType
from calculate.variables.loader import NamespaceIniFiller, Datavars
from calculate.utils.files import stderr_devnull
TESTFILES_PATH = os.path.join(os.getcwd(), 'tests/variables/testfiles')
@pytest.mark.vars
class TestNamespace:
# Сначала тестируем классы и методы необходимые для построения дерева
# переменных и пространств имен.
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):
namespace_1 = NamespaceNode('namespace_1')
assert namespace_1.namespaces == dict()
assert namespace_1.variables == dict()
assert namespace_1.get_fullname() == 'namespace_1'
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):
namespace_1 = NamespaceNode('namespace_1')
namespace_2 = NamespaceNode('namespace_2')
namespace_1.add_namespace(namespace_2)
assert namespace_1.namespaces == {'namespace_2': namespace_2}
assert namespace_1.variables == dict()
assert namespace_1.get_fullname() == 'namespace_1'
assert namespace_1.namespace_2 == namespace_2
assert namespace_1['namespace_2'] == namespace_2
assert namespace_2.namespaces == dict()
assert namespace_2.variables == dict()
assert namespace_2.parent == namespace_1
assert namespace_2.get_fullname() == 'namespace_1.namespace_2'
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):
namespace_1 = NamespaceNode('namespace_1')
variable_1 = VariableNode('var_1', namespace_1, source='value_1')
variable_2 = VariableNode('var_2', namespace_1, source='value_2')
assert namespace_1.namespaces == dict()
assert namespace_1.variables == {'var_1': variable_1,
'var_2': variable_2}
assert namespace_1.get_fullname() == 'namespace_1'
assert namespace_1.var_1 == 'value_1'
assert namespace_1['var_1'] == variable_1
assert namespace_1.var_2 == 'value_2'
assert namespace_1['var_2'] == variable_2
assert namespace_1['var_1'].get_fullname() == 'namespace_1.var_1'
assert namespace_1['var_2'].get_fullname() == 'namespace_1.var_2'
def test_if_two_dependencies_is_created_with_the_same_variables_and_their_depend_functions_are_equivalent_lambdas__the_dependencies_are_equal(self):
namespace_1 = NamespaceNode('namespace_1')
variable_1 = VariableNode('var_1', namespace_1, source=2,
variable_type=IntegerType)
variable_2 = VariableNode('var_2', namespace_1, source=4,
variable_type=IntegerType)
dependence_1 = Dependence(
variable_1, variable_2, depend=lambda arg_1, arg_2:
'greater' if arg_1.value > arg_2.value else 'less')
dependence_2 = Dependence(
variable_1, variable_2, depend=lambda var_1, var_2:
'greater' if var_1.value > var_2.value else 'less')
assert dependence_1 == dependence_2
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):
namespace_1 = NamespaceNode('namespace_1')
variable_1 = VariableNode('var_1', namespace_1, source=2,
variable_type=IntegerType)
variable_2 = VariableNode('var_2', namespace_1, source=4,
variable_type=IntegerType)
dependence_1 = Dependence(
variable_1, variable_2, depend=lambda arg_1, arg_2:
'greater' if arg_1.value > arg_2.value else 'less')
def comparator(var_1, var_2):
if var_1.value > var_2.value:
return 'greater'
else:
return 'less'
dependence_2 = Dependence(variable_1, variable_2, depend=comparator)
assert dependence_1 == dependence_2
def test_if_two_dependencies_is_created_with_the_same_variables_and_their_depend_functions_are_not_equivalent_lambdas__the_dependencies_are_equal(self):
namespace_1 = NamespaceNode('namespace_1')
variable_1 = VariableNode('var_1', namespace_1, source=2,
variable_type=IntegerType)
variable_2 = VariableNode('var_2', namespace_1, source=4,
variable_type=IntegerType)
dependence_1 = Dependence(
variable_1, variable_2, depend=lambda arg_1, arg_2:
'less' if arg_1.value > arg_2.value else 'greater')
dependence_2 = Dependence(
variable_1, variable_2, depend=lambda var_1, var_2:
'greater' if var_1.value > var_2.value else 'less')
assert dependence_1 != dependence_2
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):
namespace_1 = NamespaceNode('namespace_1')
variable_1 = VariableNode('var_1', namespace_1, source=2,
variable_type=IntegerType)
variable_2 = VariableNode('var_2', namespace_1, source=4,
variable_type=IntegerType)
dependence_1 = Dependence(
variable_1, variable_2, depend=lambda arg_1, arg_2:
'greater' if arg_1.value > arg_2.value else 'less')
def comparator(var_1, var_2):
if var_1 > var_2:
return 'less'
else:
return 'greater'
dependence_2 = Dependence(variable_1, variable_2, depend=comparator)
assert dependence_1 != dependence_2
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):
namespace_1 = NamespaceNode('namespace_1')
variable_1 = VariableNode('var_1', namespace_1, source=2,
variable_type=IntegerType)
variable_2 = VariableNode('var_2', namespace_1, source=4,
variable_type=IntegerType)
namespace_2 = NamespaceNode('namespace_2')
variable = VariableNode('var_1', namespace_2)
variable.source = Dependence(
variable_1, variable_2, depend=lambda var_1, var_2:
'greater' if var_1.value > var_2.value else 'less')
assert namespace_2.var_1 == 'less'
namespace_1['var_1'].source = 5
assert namespace_2.var_1 == 'greater'
# Теперь тестируем интерфейс создания переменных.
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='val_1')
Variable('var_2', source=2, type=IntegerType)
Variable('var_3', source=4, type=IntegerType)
with Namespace('namespace_1_1'):
Variable('var_1', source='val_1')
assert datavars.namespace_1.var_1 == 'val_1'
assert datavars.namespace_1.var_2 == 2
assert datavars.namespace_1.var_3 == 4
assert datavars.namespace_1.namespace_1_1.var_1 == 'val_1'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='val_1')
Variable('var_2', source=2, type=IntegerType)
Variable('var_3', source=4, type=IntegerType)
with pytest.raises(VariableError):
datavars.namespace_1.var_4
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='val_1', type=StringType)
with Namespace('namespace_2'):
assert Dependence._find_variable('namespace_1.var_1') ==\
datavars.namespace_1['var_1']
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):
Namespace.reset()
with Namespace('namespace_1'):
Variable('var_1', source='val_1', type=StringType)
with Namespace('namespace_2'):
with pytest.raises(VariableError):
Dependence._find_variable('namespace_1.var_2')
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='val_1', type=StringType)
Variable('var_2', source=2, type=IntegerType)
Variable('var_3', source=4, type=IntegerType)
with Namespace('namespace_1_1'):
Variable('var_1', source='val_1', type=StringType)
assert Dependence._find_variable('.var_1') ==\
datavars.namespace_1.namespace_1_1['var_1']
assert Dependence._find_variable('..var_3') ==\
datavars.namespace_1['var_3']
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):
Namespace.reset()
with Namespace('namespace_1'):
Variable('var_1', source='val_1', type=StringType)
Variable('var_2', source=2, type=IntegerType)
with Namespace('namespace_1_1'):
with pytest.raises(VariableError):
Dependence._find_variable('..var_3')
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
def source_function():
return 'value_from_function'
Variable('var', source=Dependence(depend=source_function),
type=VariableType)
assert datavars.namespace_1.var == 'value_from_function'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source=5, type=IntegerType)
Variable('var_2', source='less')
Variable('var_3', source='greater')
def solver(arg_1, arg_2, arg_3):
if arg_1.value < 6:
return arg_2.value
else:
return arg_3.value
Variable('var_4', type=StringType,
source=Dependence('.var_1', '.var_2', '.var_3',
depend=solver))
assert datavars.namespace_1.var_4 == datavars.namespace_1.var_2
assert datavars.namespace_1['var_1'] in\
datavars.namespace_1['var_4']._subscriptions
assert datavars.namespace_1['var_2'] in\
datavars.namespace_1['var_4']._subscriptions
assert datavars.namespace_1['var_3'] not in\
datavars.namespace_1['var_4']._subscriptions
assert datavars.namespace_1['var_3'].value is None
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
var_1 = Variable('var_1', source=2, type=IntegerType)
Variable('var_2', source=4, type=IntegerType)
with Namespace('namespace_2'):
Variable('var_1', source=Dependence(
var_1, '..namespace_1.var_2', depend=lambda arg_1, arg_2:
'greater' if arg_1.value > arg_2.value else 'less'))
assert datavars.namespace_1.var_1 == 2
assert datavars.namespace_1.var_2 == 4
assert datavars.namespace_2.var_1 == 'less'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
var_1 = Variable('var_1', source=2, type=IntegerType)
Variable('var_2', source=4, type=IntegerType)
with Namespace('namespace_2'):
Variable('var_1', source=Dependence(
var_1, '..namespace_1.var_2', depend=lambda arg_1, arg_2:
'greater' if arg_1.value > arg_2.value else 'less'))
datavars = Namespace.datavars
assert datavars.namespace_2.var_1 == 'less'
datavars.namespace_1['var_1'].source = 5
assert datavars.namespace_2['var_1'].value is None
assert datavars.namespace_2.var_1 == 'greater'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
var_1 = Variable('var_1', source='value_1')
Variable('var_2', source=Dependence(
var_1, depend=lambda arg_1:
'from var_1: {}'.format(arg_1.value)))
assert datavars.namespace_1.var_1 == 'value_1'
assert datavars.namespace_1.var_2 == 'from var_1: value_1'
with Namespace('namespace_1'):
Variable('var_1', source='value_2')
assert datavars.namespace_1.var_1 == 'value_2'
assert datavars.namespace_1.var_2 == 'from var_1: value_2'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='value_1')
Variable('var_2', source=Dependence(
'.var_1', depend=lambda arg_1:
'from var_1: {}'.format(arg_1.value)))
Variable('var_3', source='value_3')
assert datavars.namespace_1.var_1 == 'value_1'
assert datavars.namespace_1.var_2 == 'from var_1: value_1'
with Namespace('namespace_1'):
Variable('var_2', source=Dependence(
'.var_3', depend=lambda arg_1:
'from var_3: {}'.format(arg_1.value)))
assert datavars.namespace_1.var_2 == 'from var_3: value_3'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='value_1')
Variable('var_2', source=Dependence(
'namespace_1.var_1', depend=lambda arg_1:
'from var_1: {}'.format(arg_1.value)))
def comparator(arg_1):
return 'from var_2: {}'.format(arg_1.value)
Variable('var_3', source=Dependence('.var_2', depend=comparator))
Variable('var_1',
source=Dependence('.var_3', depend=lambda arg_1:
'from var_3: {}'.format(arg_1.value)))
with pytest.raises(CyclicVariableError):
datavars.namespace_1.var_3
def test_if_a_variable_is_created_as_readonly_variable_and_there_is_a_try_to_change_its_source__the_variable_raises_the_VariableError_exception(self):
Namespace.reset()
with Namespace('namespace_1'):
Variable('var_1', source='value_1', type=StringType.readonly)
with Namespace('namespace_1'):
with pytest.raises(VariableError):
Variable('var_1', source='value_2', type=StringType)
def test_if_variables_with_string_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_strings(self):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='value', type=StringType)
Variable('var_2', source=2, type=StringType)
Variable('var_3', source=False, type=StringType)
assert datavars.namespace_1.var_1 == 'value'
assert datavars.namespace_1.var_2 == '2'
assert datavars.namespace_1.var_3 == 'False'
def test_if_variables_with_integer_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_integers(self):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source=1, type=IntegerType)
Variable('var_2', source='2', type=IntegerType)
Variable('var_3', source=3.1, type=IntegerType)
Variable('var_4', source='-4', type=IntegerType)
assert datavars.namespace_1.var_1 == 1
assert datavars.namespace_1.var_2 == 2
assert datavars.namespace_1.var_3 == 3
assert datavars.namespace_1.var_4 == -4
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='a', type=IntegerType)
with pytest.raises(VariableTypeError):
datavars.namespace_1.var_1
def test_if_variables_with_float_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_floats(self):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source=1.1, type=FloatType)
Variable('var_2', source='2.2', type=FloatType)
Variable('var_3', source=3, type=FloatType)
Variable('var_4', source='-4.4', type=FloatType)
assert datavars.namespace_1.var_1 == 1.1
assert datavars.namespace_1.var_2 == 2.2
assert datavars.namespace_1.var_3 == 3.0
assert datavars.namespace_1.var_4 == -4.4
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='a', type=FloatType)
with pytest.raises(VariableTypeError):
datavars.namespace_1.var_1
def test_if_variables_with_bool_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_bool(self):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source=True, type=BooleanType)
Variable('var_2', source='false', type=BooleanType)
Variable('var_3', source='True', type=BooleanType)
Variable('var_4', source='0', type=BooleanType)
assert datavars.namespace_1.var_1 is True
assert datavars.namespace_1.var_2 is False
assert datavars.namespace_1.var_3 is True
assert datavars.namespace_1.var_4 is True
def test_if_variables_with_list_type_is_created_with_correct_source__the_variable_api_creates_this_variables_and_the_variables_contains_lists(self):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source=['a', 'b', 'c'], type=ListType)
Variable('var_2', source='a, b, c', type=ListType)
Variable('var_3', source={'a', 'b', 'c'}, type=ListType)
assert datavars.namespace_1.var_1 == ['a', 'b', 'c']
assert datavars.namespace_1.var_2 == ['a', 'b', 'c']
assert datavars.namespace_1.var_3 == list({'a', 'b', 'c'})
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source=1, type=ListType)
with pytest.raises(VariableTypeError):
datavars.namespace_1.var_1
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', type=HashType, source={'key_1': 'value_1',
'key_2': 'value_2'})
assert datavars.namespace_1.var_1.key_1 == 'value_1'
assert datavars.namespace_1.var_1.key_2 == 'value_2'
with Namespace('namespace_1'):
Variable('var_1', source={'key_1': 'another_value',
'key_2': 'other_value'})
assert datavars.namespace_1.var_1.key_1 == 'another_value'
assert datavars.namespace_1.var_1.key_2 == 'other_value'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='value_1', type=StringType)
Variable('var_2', type=HashType, source={'key_1': 'value_1',
'key_2': 'value_2'})
Variable('var_3', source='value_3', type=StringType)
def depend_hash(arg_1, arg_2):
return {'key_1': 'value',
'key_2': arg_1.value,
'key_3': arg_2.value}
Variable('var_4', type=HashType,
source=Dependence('.var_1', '.var_2.key_1',
depend=depend_hash))
assert datavars.namespace_1.var_4.key_1 == 'value'
assert datavars.namespace_1.var_4.key_2 == 'value_1'
assert datavars.namespace_1.var_4.key_3 == 'value_1'
with Namespace('namespace_1'):
Variable('var_1', source='other_value', type=StringType)
Variable('var_2', source={'key_1': 'another_value',
'key_2': 'value_2'})
assert datavars.namespace_1.var_2.key_1 == 'another_value'
assert datavars.namespace_1.var_4.key_2 == 'other_value'
assert datavars.namespace_1.var_4.key_3 == 'another_value'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', type=HashType, source={'key_1': 'value_1',
'key_2': 'value_2'})
assert datavars.namespace_1.var_1.key_1 == 'value_1'
assert datavars.namespace_1.var_1.key_2 == 'value_2'
with Namespace('namespace_1'):
Variable('var_1', source={'key_1': 'another_value',
'key_2': 'other_value',
'key_3': 'new_value'})
assert datavars.namespace_1.var_1.key_1 == 'another_value'
assert datavars.namespace_1.var_1.key_2 == 'other_value'
assert datavars.namespace_1.var_1.key_3 == 'new_value'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', type=HashType.fixed, source={'key_1': 'value_1',
'key_2': 'value_2'})
assert datavars.namespace_1.var_1.key_1 == 'value_1'
assert datavars.namespace_1.var_1.key_2 == 'value_2'
with Namespace('namespace_1'):
Variable('var_1', source={'key_1': 'another_value',
'key_2': 'other_value',
'key_3': 'new_value'})
assert datavars.namespace_1['var_1'].fixed
with pytest.raises(VariableError):
datavars.namespace_1.var_1.key_1
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', source='value_1', type=StringType)
Variable('var_2', type=HashType, source={'key_1': 'value_1',
'key_2': 'value_2'})
Variable('var_3', source='value_3', type=StringType)
def depend_hash(arg_1):
return arg_1.value['key_1']
Variable('var_4', type=StringType,
source=Dependence('.var_2', depend=depend_hash))
assert datavars.namespace_1.var_4 == 'value_1'
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):
Namespace.reset()
datavars = Namespace.datavars
with Namespace('namespace_1'):
Variable('var_1', type=TableType, source=[{'name': 'name_1',
'value': 'value_1'},
{'name': 'name_2',
'value': 'value_2'},
{'name': 'name_3',
'value': 'value_3'}])
def depend_function(table):
for row in table.value:
if row['name'] == 'name_2':
return row['value']
Variable('var_2', type=StringType,
source=Dependence('namespace_1.var_1',
depend=depend_function))
assert datavars.namespace_1.var_2 == 'value_2'
def test_if_variable_of_TableType_is_created_using_dependence_that_creates_table__the_created_variable_has_generated_table_as_its_value(self):
from portage.package.ebuild.config import config
value = [{'name': name, 'path': path}
for path, name in
config().repositories.location_map.items()]
Namespace.reset()
datavars = Namespace.datavars
with Namespace('main'):
Variable('chroot', source='/', type=StringType.readonly)
with Namespace('os'):
def source_function(chroot_path):
if chroot_path.value == '/':
with stderr_devnull():
return config()
Variable('config', source=Dependence('main.chroot',
depend=source_function),
type=VariableType)
def config_source(config):
return [{'name': name, 'path': path}
for path, name in
config.value.repositories.location_map.items()]
Variable('repositories', type=TableType,
source=Dependence('.config', depend=config_source))
assert datavars.os.repositories == value
# Тестируем теперь заполнитель переменных из calculate.ini
def test_if_calculate_ini_file_is_used_just_to_create_some_variables__the_namespace_filler_will_create_them(self):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller(restrict_creation=False)
calculate_ini_text = """
[os]
test = 123
[os][linux]
shortname = CLD
fullname = Calculate Linux Desktop
"""
namespace_filler.fill(datavars, calculate_ini_text)
assert datavars.os.test == '123'
assert datavars.os.linux.shortname == 'CLD'
assert datavars.os.linux.fullname == 'Calculate Linux Desktop'
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller(restrict_creation=False)
first_ini_text = """
[os]
test = 123
[os][linux]
shortname = CLD
fullname = Calculate Linux Desktop
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.os.test == '123'
assert datavars.os.linux.shortname == 'CLD'
assert datavars.os.linux.fullname == 'Calculate Linux Desktop'
second_ini_text = """
[os][linux]
shortname = CLDX
"""
namespace_filler.fill(datavars, second_ini_text)
assert datavars.os.linux.shortname == 'CLDX'
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller(restrict_creation=False)
first_ini_text = """
[os]
test = 123
[os]
test += 345
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.os.test == "123,345"
second_ini_text = """
[os]
test += asdf,qwer,zxcv
"""
namespace_filler.fill(datavars, second_ini_text)
assert datavars.os.test == "123,345,asdf,qwer,zxcv"
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller(restrict_creation=False)
first_ini_text = """
[os]
test = 123,345,asdf,qwer,zxcv
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.os.test == "123,345,asdf,qwer,zxcv"
second_ini_text = """
[os]
test -= 345
"""
namespace_filler.fill(datavars, second_ini_text)
assert datavars.os.test == "123,asdf,qwer,zxcv"
third_ini_text = """
[os]
test -= asdf,zxcv
"""
namespace_filler.fill(datavars, third_ini_text)
assert datavars.os.test == "123,qwer"
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller()
with Namespace('os'):
Variable('var_1', type=StringType, source='value_1')
Variable('var_2', type=StringType, source='value_2')
first_ini_text = """
[os]
var_1 = other_value
var_3 = value_3
[custom][test_1]
var_1 = value_1
[custom][test_2]
var_1 = value_1
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.os.var_1 == "other_value"
assert 'var_3' not in datavars.os
assert datavars.custom.test_1.var_1 == "value_1"
assert datavars.custom.test_2.var_1 == "value_1"
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller()
with Namespace('os'):
Variable('var_1', type=StringType, source='value_1')
Variable('var_2', type=StringType, source='value_2')
first_ini_text = """
[custom][test]
var_1 = value_1
var_2 = value_2
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.custom.test.var_1 == 'value_1'
assert datavars.custom.test.var_2 == 'value_2'
second_ini_text = """
[custom][test][]
[os][]
"""
namespace_filler.fill(datavars, second_ini_text)
assert datavars.os.var_1 == 'value_1'
assert datavars.os.var_2 == 'value_2'
assert 'var_1' not in datavars.custom.test.variables
assert 'var_2' not in datavars.custom.test.variables
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller()
with Namespace('os'):
Variable('var_1', type=StringType, source='value_1')
Variable('var_2', type=StringType, source='value_2')
first_ini_text = """
[custom][test][0]
name = name_0
value = value_0
[custom][test][1]
name = name_1
value = value_1
[custom][test][2]
name = name_2
value = value_2
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.custom.test ==\
[{'name': 'name_0', 'value': 'value_0'},
{'name': 'name_1', 'value': 'value_1'},
{'name': 'name_2', 'value': 'value_2'}]
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller()
with Namespace('os'):
Variable('var_1', type=StringType, source='value_1')
Variable('var_2', type=StringType, source='value_2')
first_ini_text = """
[os][test][0]
name = name_0
value = value_0
[os][test][1]
name = name_1
value = value_1
[os][test][2]
name = name_2
value = value_2
"""
namespace_filler.fill(datavars, first_ini_text)
assert 'test' not in datavars.os
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):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller()
with Namespace('os'):
Variable('test', type=TableType, source=[{'name': 'name_1',
'value': 'value_1'},
{'name': 'name_2',
'value': 'value_2'},
{'name': 'name_3',
'value': 'value_3'}])
first_ini_text = """
# Создаем таблицу.
[custom][test][0]
name = name_0
value = value_0
[custom][test][1]
name = name_1
value = value_1
# Очищаем таблицу.
[custom][test][]
# Добавляем в таблицу строку.
[custom][test][0]
name = name_0
value = value_0
# Очищаем таблицу созданную интерфейсом.
[os][test][]
# Добавляем в эту таблицу строку.
[os][test][0]
name = strange_name
value = weird_value
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.custom.test ==\
[{'name': 'name_0', 'value': 'value_0'}]
assert datavars.os.test ==\
[{'name': 'strange_name', 'value': 'weird_value'}]
def test_if_calculate_ini_file_is_used_for_modifying_of_the_table_from_calculate_ini_file__the_NamespaceIniFiller_object_modifies_the_table(self):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller()
first_ini_text = """
# Создаем таблицу.
[custom][test][0]
name = name_0
value = value_0
[custom][test][1]
name = name_1
value = value_1
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.custom.test ==\
[{'name': 'name_0', 'value': 'value_0'},
{'name': 'name_1', 'value': 'value_1'}]
second_ini_text = """
[custom][test][1]
name = other_name
value = another_value
"""
namespace_filler.fill(datavars, second_ini_text)
assert datavars.custom.test ==\
[{'name': 'name_0', 'value': 'value_0'},
{'name': 'other_name', 'value': 'another_value'}]
def test_if_calculate_ini_file_is_used_for_modifying_of_the_table_created_using_variables_api__the_NamespaceIniFiller_object_modifies_the_table(self):
Namespace.reset()
datavars = Namespace.datavars
namespace_filler = NamespaceIniFiller()
with Namespace('namespace_1'):
Variable('test', type=TableType, source=[{'name': 'name_1',
'value': 'value_1'},
{'name': 'name_2',
'value': 'value_2'},
{'name': 'name_3',
'value': 'value_3'}])
first_ini_text = """
[namespace_1][test][0]
name = new_name
value = other_value
[namespace_1][test][1]
name = common_name
value = another_value
"""
namespace_filler.fill(datavars, first_ini_text)
assert datavars.namespace_1.test ==\
[{'name': 'new_name', 'value': 'other_value'},
{'name': 'common_name', 'value': 'another_value'},
{'name': 'name_3', 'value': 'value_3'}]
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):
datavars = Datavars(
variables_path='tests/variables/testfiles/variables_0',
repository_map={})
assert datavars.os.ns.var_1 == 'value_1'
assert datavars.os.ns.var_2 == 2
assert 'main' not in datavars.root
assert datavars.main.strange_variable == 'weird_value'
assert datavars.main.plain_variable is True
assert 'main' in datavars.root
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):
datavars = Datavars(
variables_path='tests/variables/testfiles/variables_1',
repository_map={})
assert datavars.main.chroot == '/'
assert 'os' not in datavars.root
assert datavars.os.calculate == 'test1 test2'
assert 'os' in datavars.root
assert datavars.level.level_2.vargetter == '/ test'
assert 'main' in datavars.root
assert datavars.level.simple == "simple value"
assert datavars.level.use_local_simple == "Using simple value"
assert datavars.level.use_full_simple == "Using simple value"
assert datavars.level.device_child == "hdd"
assert datavars.level.device == [{"dev": "/dev/sda",
"type": "hdd",
"name": "Samsung SSD"},
{"dev": "/dev/sdb",
"type": "flash",
"name": "Transcend 64GB"}]
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):
datavars = Datavars(
variables_path='tests/variables/testfiles/variables_4',
repository_map={})
assert datavars.main.chroot == '/'
assert 'os' not in datavars.root
assert datavars.level.level_2.vargetter == '/ test'
assert 'main' in datavars.root
assert datavars.level.simple == "simple value"
datavars._loader.fill_from_custom_ini(os.path.join(TESTFILES_PATH,
'calculate_0.ini'))
assert 'os' in datavars.root
assert datavars.level.simple == 'weird value'
assert datavars.main.chroot == '/any/other/path'
assert datavars.level.level_2.vargetter == '/any/other/path test'
def test_to_make_testfiles(self):
shutil.copytree(os.path.join(TESTFILES_PATH, 'gentoo.backup'),
os.path.join(TESTFILES_PATH, 'gentoo'),
symlinks=True)
def test_ini_loader(self):
repository_map = {'distros': os.path.join(TESTFILES_PATH,
"gentoo/repos/distros"),
'calculate': os.path.join(TESTFILES_PATH,
"gentoo/repos/calculate"),
'gentoo': os.path.join(TESTFILES_PATH,
"gentoo/portage")}
datavars = Datavars(
variables_path=os.path.join(TESTFILES_PATH, 'variables_2'),
repository_map=repository_map)
def test_get_repository_map(self):
datavars = Datavars(
variables_path=os.path.join(TESTFILES_PATH, 'variables_3'))
print('LOADING IS DONE')
print('hashvar.value1 = {}'.format(datavars.os.hashvar.value1))
print('hashvar.value2 = {}'.format(datavars.os.hashvar.value2))
assert False
def test_for_removing_testfile(self):
shutil.rmtree(os.path.join(TESTFILES_PATH, 'gentoo'))