112 lines
2.9 KiB
Python
112 lines
2.9 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 import Configuration, set_mutability, is_mutable
|
|
|
|
def test_crud_configuration_attrs():
|
|
config = Configuration()
|
|
|
|
# Test setting an attribute
|
|
config.abc = 42
|
|
|
|
# Test getting an attribute
|
|
assert config.abc == 42
|
|
|
|
# Test getting a non-existing attribute
|
|
try:
|
|
config.non_existing
|
|
assert False, "AttributeError was not raised"
|
|
except AttributeError:
|
|
pass
|
|
|
|
# Test updating an attribute
|
|
config.abc = 100
|
|
|
|
# Test getting the updated attribute
|
|
assert config.abc == 100
|
|
|
|
# Test getting dictionary
|
|
assert dict(config) == {'abc': 100}
|
|
|
|
# Test deleting an attribute
|
|
del config.abc
|
|
try:
|
|
config.abc
|
|
assert False, "AttributeError was not raised"
|
|
except AttributeError:
|
|
pass
|
|
|
|
def test_crud_configuration_config_items():
|
|
config = Configuration()
|
|
|
|
# Test setting an item
|
|
config['key1'] = 'value1'
|
|
|
|
# Test getting an item
|
|
assert config['key1'] == 'value1'
|
|
assert config.key1 == 'value1'
|
|
|
|
# Test updating an item
|
|
config['key1'] = 'value2'
|
|
|
|
# Test getting the updated item
|
|
assert config['key1'] == 'value2'
|
|
assert config.key1 == 'value2'
|
|
|
|
# Test deleting an item and exceptions
|
|
del config['key1']
|
|
try:
|
|
config['key1']
|
|
assert False, "KeyError was not raised"
|
|
except KeyError:
|
|
pass
|
|
|
|
try:
|
|
config.key1
|
|
assert False, "AttributeError was not raised"
|
|
except AttributeError:
|
|
pass
|
|
|
|
def test_mutability():
|
|
config = Configuration(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
|
|
assert is_mutable(config) == False
|
|
|
|
try:
|
|
del config.key
|
|
assert False, 'Expected TypeError'
|
|
except TypeError:
|
|
pass
|
|
|
|
|
|
# Test operate_on_original_object parameter of set_mutability
|
|
config = Configuration(mutable = True)
|
|
set_mutability(config, False, operate_on_original_object = True)
|
|
assert is_mutable(config) == False
|
|
|
|
new_config = set_mutability(config, True, operate_on_original_object = False)
|
|
assert is_mutable(new_config) == True
|
|
assert is_mutable(config) == False |