config/scheme.py: fix detection of extra profiles as 'unknown keys' and add unit test using pickle
This commit is contained in:
parent
30d9be0950
commit
91b44299ae
2 changed files with 27 additions and 2 deletions
|
@ -52,7 +52,7 @@ class DataClass(Munch):
|
|||
raise Exception(f'Unknown key "{key}"')
|
||||
else:
|
||||
if isinstance(value, dict) and not isinstance(value, Munch):
|
||||
value = DataClass.fromDict(value)
|
||||
value = Munch.fromDict(value)
|
||||
results[key] = value
|
||||
if values:
|
||||
if validate:
|
||||
|
@ -129,6 +129,19 @@ class ProfilesSection(DataClass):
|
|||
current: str
|
||||
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()
|
||||
class Config(DataClass):
|
||||
|
@ -146,7 +159,7 @@ class Config(DataClass):
|
|||
for name, _class in cls._type_hints.items():
|
||||
if name not in values:
|
||||
if not allow_incomplete:
|
||||
raise Exception(f'Config key {name} not found')
|
||||
raise Exception(f'Config key "{name}" not in input dictionary')
|
||||
continue
|
||||
value = values.pop(name)
|
||||
if not isinstance(value, _class):
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import pytest
|
||||
|
||||
import os
|
||||
import pickle
|
||||
import toml
|
||||
|
||||
from tempfile import mktemp, gettempdir as get_system_tempdir
|
||||
|
@ -168,6 +169,17 @@ def test_config_scheme_modified():
|
|||
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():
|
||||
p = None
|
||||
p = Profile.fromDict(PROFILE_DEFAULTS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue