config/scheme.py: fix detection of extra profiles as 'unknown keys' and add unit test using pickle

This commit is contained in:
InsanePrawn 2022-08-18 05:37:54 +02:00
parent 30d9be0950
commit 91b44299ae
2 changed files with 27 additions and 2 deletions

View file

@ -52,7 +52,7 @@ class DataClass(Munch):
raise Exception(f'Unknown key "{key}"') raise Exception(f'Unknown key "{key}"')
else: else:
if isinstance(value, dict) and not isinstance(value, Munch): if isinstance(value, dict) and not isinstance(value, Munch):
value = DataClass.fromDict(value) value = Munch.fromDict(value)
results[key] = value results[key] = value
if values: if values:
if validate: if validate:
@ -129,6 +129,19 @@ class ProfilesSection(DataClass):
current: str current: str
default: Profile default: Profile
@classmethod
def transform(cls, values: Mapping[str, Any], validate: bool = True):
results = {}
for k, v in values.items():
if k == 'current':
results[k] = v
continue
results[k] = Profile.fromDict(v, validate=True)
return results
def update(self, d, validate: bool = True):
Munch.update(self, self.transform(d, validate=validate))
@munchclass() @munchclass()
class Config(DataClass): class Config(DataClass):
@ -146,7 +159,7 @@ class Config(DataClass):
for name, _class in cls._type_hints.items(): for name, _class in cls._type_hints.items():
if name not in values: if name not in values:
if not allow_incomplete: if not allow_incomplete:
raise Exception(f'Config key {name} not found') raise Exception(f'Config key "{name}" not in input dictionary')
continue continue
value = values.pop(name) value = values.pop(name)
if not isinstance(value, _class): if not isinstance(value, _class):

View file

@ -1,6 +1,7 @@
import pytest import pytest
import os import os
import pickle
import toml import toml
from tempfile import mktemp, gettempdir as get_system_tempdir from tempfile import mktemp, gettempdir as get_system_tempdir
@ -168,6 +169,17 @@ def test_config_scheme_modified():
assert c.wrapper.type == 'none' assert c.wrapper.type == 'none'
def test_configstate_profile_pickle():
c = ConfigStateHolder()
assert c.file.wrapper
assert c.file.profiles
# add new profile to check it doesn't error out due to unknown keys
c.file.profiles['graphical'] = {'username': 'kupfer123', 'hostname': 'test123'}
p = pickle.dumps(c)
unpickled = pickle.loads(p)
assert c.file == unpickled.file
def test_profile(): def test_profile():
p = None p = None
p = Profile.fromDict(PROFILE_DEFAULTS) p = Profile.fromDict(PROFILE_DEFAULTS)