119 lines
3.2 KiB
Python
119 lines
3.2 KiB
Python
# Copyright 2026 jCloud Services GbR
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from src.jcloud_config_parser.ini import INIConfiguration, INIConfigurationSection
|
|
from src.jcloud_config_parser.exceptions import INISyntaxError
|
|
from src.jcloud_config_parser import set_mutability
|
|
|
|
def test_generating():
|
|
config = INIConfiguration()
|
|
config.test_group = INIConfigurationSection('test_group')
|
|
config.test_group.key1 = 'value1'
|
|
config.test_group.key2 = 'value2'
|
|
assert config.to_string() == '''[test_group]
|
|
key1=value1
|
|
key2=value2'''
|
|
|
|
def test_parsing():
|
|
# Test deserializer
|
|
config_string = '''
|
|
property1 = 42
|
|
property2=123
|
|
|
|
[configuration_group1]
|
|
hello=world
|
|
key1=value1
|
|
|
|
[group2]
|
|
world=hel#lo
|
|
key2 = "val#ue2"
|
|
[]
|
|
hello2=world2
|
|
invalid_line1
|
|
|
|
invalid_line2
|
|
|
|
# comment'''
|
|
|
|
assert dict(INIConfiguration.from_string(config_string, ignore_errors=True)) == {'configuration_group1': {'hello': 'world', 'key1': 'value1'}, 'group2': {'world': 'hel', 'key2': 'val#ue2', 'hello2': 'world2'}}
|
|
|
|
try:
|
|
INIConfiguration.from_string('key="value')
|
|
assert False, 'Expected INISyntaxError for unterminated literal'
|
|
except INISyntaxError:
|
|
pass
|
|
|
|
try:
|
|
INIConfiguration.from_string('key=value"')
|
|
assert False, 'Expected INISyntaxError for unterminated literal'
|
|
except INISyntaxError:
|
|
pass
|
|
|
|
def test_default():
|
|
default_configuration = INIConfiguration.from_string('''[section1]
|
|
k1=v1
|
|
k2=v2
|
|
k3=v3
|
|
|
|
[section2]
|
|
l1=w1
|
|
l2=w2''')
|
|
assert dict(INIConfiguration.from_string('''[section1]
|
|
k1=v1
|
|
k2=v2
|
|
k4=v4''', default = default_configuration)) == {'section1': {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}, 'section2': {'l1': 'w1', 'l2': 'w2'}}
|
|
|
|
def test_mutability():
|
|
config = INIConfiguration(mutable=False)
|
|
try:
|
|
config.key = 'value'
|
|
assert False, 'Expected TypeError'
|
|
except TypeError:
|
|
pass
|
|
|
|
set_mutability(config, True)
|
|
assert config._mutable == True
|
|
|
|
config.key = 'value'
|
|
|
|
set_mutability(config, False)
|
|
assert config._mutable == False
|
|
|
|
try:
|
|
del config.key
|
|
assert False, 'Expected TypeError'
|
|
except TypeError:
|
|
pass
|
|
|
|
configuration = '[section]\nkey=value'
|
|
config = INIConfiguration.from_string(configuration)
|
|
config.key1 = 'value1'
|
|
|
|
set_mutability(config, False)
|
|
try:
|
|
config.key2 = 'value2'
|
|
assert False, 'Expected TypeError'
|
|
except TypeError:
|
|
pass
|
|
|
|
config = INIConfiguration.from_string(configuration, mutable = False)
|
|
|
|
try:
|
|
config.key1 = 'value1'
|
|
assert False, 'Expected TypeError'
|
|
except TypeError:
|
|
pass
|
|
|
|
set_mutability(config, True)
|
|
config.key2 = 'value2' |