Support for mutable and immutable configurations

This commit is contained in:
2026-03-12 19:08:37 +01:00
parent 71fa689945
commit 66cbb21941
7 changed files with 176 additions and 18 deletions
+24 -1
View File
@@ -14,6 +14,7 @@
from src.config_parser.ini import INIConfiguration, INIConfigurationSection
from src.config_parser.exceptions import INISyntaxError
from src.config_parser import set_mutability
def test_generating():
config = INIConfiguration()
@@ -71,4 +72,26 @@ 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'}}
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
+23
View File
@@ -14,6 +14,7 @@
from src.config_parser.json import JSONConfiguration
from src.config_parser.exceptions import JSONObjectSyntaxError, JSONValueSyntaxError, JSONStringSyntaxError, JSONArraySyntaxError, EscapeSequenceSyntaxError
from src.config_parser import set_mutability
def test_json_configuration():
# Test valid JSON configuration parsing
@@ -220,4 +221,26 @@ def test_json_configuration():
JSONConfiguration.from_string('')
assert False, 'Excepted JSONValueSyntaxError'
except JSONValueSyntaxError:
pass
def test_mutability():
config = JSONConfiguration(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
+35 -2
View File
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.config_parser import Configuration
from src.config_parser import Configuration, set_mutability, is_mutable
def test_crud_configuration_attrs():
config = Configuration()
@@ -76,4 +76,37 @@ def test_crud_configuration_config_items():
config.key1
assert False, "AttributeError was not raised"
except AttributeError:
pass
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