summaryrefslogtreecommitdiff
path: root/src/tests/test_validate.py
diff options
context:
space:
mode:
authorRob Dennis <rdennis@gmail.com>2023-01-17 16:58:24 -0500
committerGitHub <noreply@github.com>2023-01-17 16:58:24 -0500
commit62c2c88688e5ff110792bb0d0547c5206bd036c7 (patch)
treeb91f297fce6a249f770272e6d6cf356687f2b379 /src/tests/test_validate.py
parent148d8f8045fb72a00898057d156db40540918f78 (diff)
parent500cdaa38b55571a47f9ecbdf237cbc303476b27 (diff)
downloadconfigobj-git-62c2c88688e5ff110792bb0d0547c5206bd036c7.tar.gz
Merge pull request #228 from DiffSK/5.0.xv5.0.7
Teeing up version 5.0.7
Diffstat (limited to 'src/tests/test_validate.py')
-rw-r--r--src/tests/test_validate.py163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/tests/test_validate.py b/src/tests/test_validate.py
new file mode 100644
index 0000000..07c1de7
--- /dev/null
+++ b/src/tests/test_validate.py
@@ -0,0 +1,163 @@
+# coding=utf-8
+
+from configobj import ConfigObj
+import pytest
+from configobj.validate import Validator, VdtValueTooSmallError
+
+
+class TestBasic(object):
+ def test_values_too_small(self, val):
+ config = '''
+ test1=40
+ test2=hello
+ test3=3
+ test4=5.0
+ [section]
+ test1=40
+ test2=hello
+ test3=3
+ test4=5.0
+ [[sub section]]
+ test1=40
+ test2=hello
+ test3=3
+ test4=5.0
+ '''.splitlines()
+ configspec = '''
+ test1= integer(30,50)
+ test2= string
+ test3=integer
+ test4=float(6.0)
+ [section ]
+ test1=integer(30,50)
+ test2=string
+ test3=integer
+ test4=float(6.0)
+ [[sub section]]
+ test1=integer(30,50)
+ test2=string
+ test3=integer
+ test4=float(6.0)
+ '''.splitlines()
+ c1 = ConfigObj(config, configspec=configspec)
+ test = c1.validate(val)
+ assert test == {
+ 'test1': True,
+ 'test2': True,
+ 'test3': True,
+ 'test4': False,
+ 'section': {
+ 'test1': True,
+ 'test2': True,
+ 'test3': True,
+ 'test4': False,
+ 'sub section': {
+ 'test1': True,
+ 'test2': True,
+ 'test3': True,
+ 'test4': False,
+ },
+ },
+ }
+
+ with pytest.raises(VdtValueTooSmallError) as excinfo:
+ val.check(c1.configspec['test4'], c1['test4'])
+ assert str(excinfo.value) == 'the value "5.0" is too small.'
+
+ def test_values(self, val):
+ val_test_config = '''
+ key = 0
+ key2 = 1.1
+ [section]
+ key = some text
+ key2 = 1.1, 3.0, 17, 6.8
+ [[sub-section]]
+ key = option1
+ key2 = True'''.splitlines()
+ val_test_configspec = '''
+ key = integer
+ key2 = float
+ [section]
+ key = string
+ key2 = float_list(4)
+ [[sub-section]]
+ key = option(option1, option2)
+ key2 = boolean'''.splitlines()
+ val_test = ConfigObj(val_test_config, configspec=val_test_configspec)
+ assert val_test.validate(val)
+ val_test['key'] = 'text not a digit'
+ val_res = val_test.validate(val)
+ assert val_res == {'key2': True, 'section': True, 'key': False}
+
+ def test_defaults(self, val):
+ configspec = '''
+ test1=integer(30,50, default=40)
+ test2=string(default="hello")
+ test3=integer(default=3)
+ test4=float(6.0, default=6.0)
+ [section ]
+ test1=integer(30,50, default=40)
+ test2=string(default="hello")
+ test3=integer(default=3)
+ test4=float(6.0, default=6.0)
+ [[sub section]]
+ test1=integer(30,50, default=40)
+ test2=string(default="hello")
+ test3=integer(default=3)
+ test4=float(6.0, default=6.0)
+ '''.splitlines()
+ default_test = ConfigObj(['test1=30'], configspec=configspec)
+ assert repr(default_test) == "ConfigObj({'test1': '30'})"
+ assert default_test.defaults == []
+ assert default_test.default_values == {}
+ assert default_test.validate(val)
+ assert default_test == {
+ 'test1': 30,
+ 'test2': 'hello',
+ 'test3': 3,
+ 'test4': 6.0,
+ 'section': {
+ 'test1': 40,
+ 'test2': 'hello',
+ 'test3': 3,
+ 'test4': 6.0,
+ 'sub section': {
+ 'test1': 40,
+ 'test3': 3,
+ 'test2': 'hello',
+ 'test4': 6.0,
+ },
+ },
+ }
+
+ assert default_test.defaults == ['test2', 'test3', 'test4']
+ assert default_test.default_values == {
+ 'test1': 40, 'test2': 'hello',
+ 'test3': 3, 'test4': 6.0
+ }
+ assert default_test.restore_default('test1') == 40
+ assert default_test['test1'] == 40
+ assert 'test1' in default_test.defaults
+
+ def change(section, key):
+ section[key] = 3
+ default_test.walk(change)
+ assert default_test['section']['sub section']['test4'] == 3
+
+ default_test.restore_defaults()
+ assert default_test == {
+ 'test1': 40,
+ 'test2': "hello",
+ 'test3': 3,
+ 'test4': 6.0,
+ 'section': {
+ 'test1': 40,
+ 'test2': "hello",
+ 'test3': 3,
+ 'test4': 6.0,
+ 'sub section': {
+ 'test1': 40,
+ 'test2': "hello",
+ 'test3': 3,
+ 'test4': 6.0
+ }}}