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/utils/test_calculateini.py

131 lines
4.9 KiB

import pytest
from mock import call
from calculate.utils.calculateini import CalculateIniParser
Define = CalculateIniParser.Define
@pytest.mark.calculateini
class TestCalculateIni:
def test_empty_calculate_ini(self):
pass
#ns = Namespace(varPath=None)
#assert ns.varPath is None
def test_section_values(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
cip.parse("[section]\n"
"varval1 = value1\n")
spy_error.assert_not_called()
spy_section.assert_has_calls([call(['section'])])
spy_def_key.assert_has_calls([
call(['section'],'varval1', 'value1', Define.Assign),])
def test_simple_calculate_ini_with_comments(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
cip.parse("[section]\n"
"varval1 = value1\n"
"# some comment\n"
"varval2 += value2\n"
"varval3 -= value3\n")
spy_error.assert_not_called()
spy_section.assert_has_calls([call(['section'])])
spy_def_key.assert_has_calls([
call(['section'],'varval1', 'value1', Define.Assign),
call(['section'],'varval2', 'value2', Define.Append),
call(['section'],'varval3', 'value3', Define.Remove),
])
def test_some_complex_section_calculate_ini(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
cip.parse("[section][sub]\n"
"varval1 = value1\n"
"varval2 = value2\n"
"[section][sub2]\n"
"varval1 = value1\n"
"\n"
"[section2]\n"
"varval1 = value1\n"
)
spy_error.assert_not_called()
spy_section.assert_has_calls([call(['section','sub'])])
spy_section.assert_has_calls([call(['section','sub2'])])
spy_section.assert_has_calls([call(['section2'])])
spy_def_key.assert_has_calls([
call(['section', 'sub'],'varval1','value1', Define.Assign),
call(['section', 'sub'],'varval2','value2', Define.Assign),
call(['section', 'sub2'],'varval1','value1', Define.Assign),
call(['section2'],'varval1','value1', Define.Assign),
])
def test_error(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
cip.parse("[section\n"
"varval1 = value1\n"
"varval2 = value2\n"
"[section][sub2]\n"
"varval1 = value1\n"
"eee\n"
"\n"
"[section2\n"
"varval4 = value4\n"
"[section][]\n"
"[section][][sub]\n"
"[section][][sub][]\n"
)
# проверяем, что расозналась только одна секция
spy_section.assert_has_calls([call(['section','sub2'])])
# проверяем, что значение из криво определённой section2 попало
# в section.sub2
spy_def_key.assert_has_calls([
call(['section', 'sub2'],'varval1','value1', Define.Assign),
call(['section', 'sub2'],'varval4','value4', Define.Assign),
])
# проверяем, все нераспознанные строки попали в ошибки
# криво объявленная первая секция
# её переменные
# кривая переменная eee
# криво объявленная section2
spy_error.assert_has_calls([
call('[section', 1, 1),
call('varval1 = value1', 2, 1),
call('varval2 = value2', 3, 1),
call('eee', 6, 1),
call('[section2', 8, 1),
call('[section][][sub]', 11, 1),
call('[section][][sub][]', 12, 1)
])
def test_clear_section(self, mocker):
cip = CalculateIniParser()
spy_section = mocker.spy(cip, 'startSection')
spy_def_key = mocker.spy(cip, 'defineKey')
spy_error = mocker.spy(cip, 'parseError')
spy_clear_sec = mocker.spy(cip, 'clearSection')
cip.parse("[section][test][]\n")
spy_error.assert_not_called()
spy_def_key.assert_not_called()
spy_section.assert_not_called()
spy_clear_sec.assert_has_calls([call(['section','test'])])