|
|
|
@ -3,8 +3,9 @@ from calculate.vars.alt_datavars import NamespaceNode, VariableNode,\
|
|
|
|
|
Namespace, Variable, Dependence,\
|
|
|
|
|
CyclicVariableError, HashType,\
|
|
|
|
|
StringType, IntegerType, VariableType,\
|
|
|
|
|
VariableError, TableType
|
|
|
|
|
from calculate.vars.alt_vars_loader import NamespaceIniFiller
|
|
|
|
|
VariableError, TableType, BooleanType,\
|
|
|
|
|
VariableTypeError, FloatType, ListType
|
|
|
|
|
from calculate.vars.alt_vars_loader import NamespaceIniFiller, VariableLoader
|
|
|
|
|
from calculate.utils.files import stderr_devnull
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -55,7 +56,7 @@ class TestNamespace:
|
|
|
|
|
assert namespace_1['var_1'].get_fullname() == 'namespace_1.var_1'
|
|
|
|
|
assert namespace_1['var_2'].get_fullname() == 'namespace_1.var_2'
|
|
|
|
|
|
|
|
|
|
def test_compare_two_dependencies_equal(self):
|
|
|
|
|
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)
|
|
|
|
@ -71,7 +72,7 @@ class TestNamespace:
|
|
|
|
|
'greater' if var_1.value > var_2.value else 'less')
|
|
|
|
|
assert dependence_1 == dependence_2
|
|
|
|
|
|
|
|
|
|
def test_compare_two_dependencies_equal_but_one_is_function_and_other_is_lambda(self):
|
|
|
|
|
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)
|
|
|
|
@ -90,7 +91,7 @@ class TestNamespace:
|
|
|
|
|
dependence_2 = Dependence(variable_1, variable_2, depend=comparator)
|
|
|
|
|
assert dependence_1 == dependence_2
|
|
|
|
|
|
|
|
|
|
def test_compare_two_dependencies_not_equal(self):
|
|
|
|
|
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)
|
|
|
|
@ -106,7 +107,7 @@ class TestNamespace:
|
|
|
|
|
'greater' if var_1.value > var_2.value else 'less')
|
|
|
|
|
assert dependence_1 != dependence_2
|
|
|
|
|
|
|
|
|
|
def test_compare_two_dependencies_not_equal_but_one_is_function_and_other_is_lambda(self):
|
|
|
|
|
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)
|
|
|
|
@ -163,6 +164,18 @@ class TestNamespace:
|
|
|
|
|
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
|
|
|
|
@ -357,6 +370,107 @@ class TestNamespace:
|
|
|
|
|
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
|
|
|
|
@ -567,7 +681,7 @@ test -= asdf,zxcv
|
|
|
|
|
namespace_filler.fill(datavars, third_ini_text)
|
|
|
|
|
assert datavars.os.test == "123,qwer"
|
|
|
|
|
|
|
|
|
|
def test_custom_and_not_custom_creation(self):
|
|
|
|
|
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()
|
|
|
|
@ -594,7 +708,38 @@ var_1 = value_1
|
|
|
|
|
assert datavars.custom.test_1.var_1 == "value_1"
|
|
|
|
|
assert datavars.custom.test_2.var_1 == "value_1"
|
|
|
|
|
|
|
|
|
|
def test_table_from_ini(self):
|
|
|
|
|
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()
|
|
|
|
@ -623,14 +768,145 @@ value = value_2
|
|
|
|
|
{'name': 'name_1', 'value': 'value_1'},
|
|
|
|
|
{'name': 'name_2', 'value': 'value_2'}]
|
|
|
|
|
|
|
|
|
|
second_ini_text = """
|
|
|
|
|
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, second_ini_text)
|
|
|
|
|
namespace_filler.fill(datavars, first_ini_text)
|
|
|
|
|
assert datavars.custom.test.get_table() ==\
|
|
|
|
|
[{'name': 'name_0', 'value': 'value_0'}]
|
|
|
|
|
assert datavars.os.test.get_table() ==\
|
|
|
|
|
[{'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.get_table() ==\
|
|
|
|
|
[{'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.get_table() ==\
|
|
|
|
|
[{'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.get_table() ==\
|
|
|
|
|
[{'name': 'new_name', 'value': 'other_value'},
|
|
|
|
|
{'name': 'common_name', 'value': 'another_value'},
|
|
|
|
|
{'name': 'name_3', 'value': 'value_3'}]
|
|
|
|
|
|
|
|
|
|
def test_loader(self):
|
|
|
|
|
Namespace.reset()
|
|
|
|
|
loader = VariableLoader()
|
|
|
|
|
datavars = loader.load_variables('tests/vars/testfiles/variables_0')
|
|
|
|
|
assert datavars.os.ns.var_1 == 'value_1'
|
|
|
|
|
assert datavars.os.ns.var_2 == 2
|
|
|
|
|
|
|
|
|
|
assert datavars.main.strange_variable == 'weird_value'
|
|
|
|
|
assert datavars.main.plain_variable is True
|
|
|
|
|