From 79d432a4d8961185e0eafd76fdbfe3dbbf46bf50 Mon Sep 17 00:00:00 2001 From: Drikus Roor Date: Mon, 19 Feb 2024 14:02:35 +0100 Subject: [PATCH] fix: Fix code & tests after removing style from consent and nested styles --- backend/experiment/actions/consent.py | 2 +- backend/experiment/actions/frontend_style.py | 22 +++++++++++++- .../experiment/tests/test_frontend_style.py | 30 ++----------------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/backend/experiment/actions/consent.py b/backend/experiment/actions/consent.py index 662c948f6..53fd91d90 100644 --- a/backend/experiment/actions/consent.py +++ b/backend/experiment/actions/consent.py @@ -24,7 +24,7 @@ class Consent(BaseAction): # pylint: disable=too-few-public-methods ea sea expetenda suscipiantur contentiones." def __init__(self, text=default_text, title='Informed consent', confirm='I agree', deny='Stop'): - super().__init__(style) + super().__init__() self.text = text self.title = title self.confirm = confirm diff --git a/backend/experiment/actions/frontend_style.py b/backend/experiment/actions/frontend_style.py index 5b77a4059..7bf942641 100644 --- a/backend/experiment/actions/frontend_style.py +++ b/backend/experiment/actions/frontend_style.py @@ -32,7 +32,27 @@ def __init__(self, root_style: EFrontendStyle = EFrontendStyle.EMPTY): if not EFrontendStyle.is_valid(root_style): raise ValueError(f"Invalid root style: {root_style}") - self.styles = { 'root': root_style } + self.styles = {'root': root_style} + + def get_style(self, element: str) -> str: + """ + Get the style for a specific element. + :param element: The element identifier for which to get the style. + :return: The style name for the given element. + """ + return self.styles.get(element, None) + + def apply_style(self, element: str, style: str) -> None: + """ + Apply a specific style to an element after validating the style. + :param element: The element identifier to apply the style to. + :param style: The style name to apply. + """ + if EFrontendStyle.is_valid(style): + self.styles[element] = style + else: + valid_styles = ', '.join([str(s) for s in self.VALID_STYLES]) + raise ValueError(f"Invalid style: {style}. Valid styles are {valid_styles}.") def to_dict(self) -> dict: serialized_styles = { 'root': self.styles['root'].value } diff --git a/backend/experiment/tests/test_frontend_style.py b/backend/experiment/tests/test_frontend_style.py index 824d3ba90..b87dd58ca 100644 --- a/backend/experiment/tests/test_frontend_style.py +++ b/backend/experiment/tests/test_frontend_style.py @@ -13,15 +13,6 @@ def test_init_with_invalid_root_style(self): with self.assertRaises(ValueError): FrontendStyle("invalid-style") - def test_init_with_valid_nested_styles(self): - nested_style = FrontendStyle(EFrontendStyle.SUCCESS) - style = FrontendStyle(EFrontendStyle.PRIMARY, nested=nested_style) - self.assertIsInstance(style.get_style('nested'), FrontendStyle) - - def test_init_with_invalid_nested_styles(self): - with self.assertRaises(ValueError): - FrontendStyle(EFrontendStyle.PRIMARY, nested="invalid-style") - def test_get_style(self): style = FrontendStyle(EFrontendStyle.SECONDARY) self.assertEqual(style.get_style('root'), EFrontendStyle.SECONDARY) @@ -41,26 +32,9 @@ def test_apply_invalid_style(self): style.apply_style('root', "invalid-style") def test_to_dict(self): - nested_style = FrontendStyle(EFrontendStyle.SUCCESS) - style = FrontendStyle(EFrontendStyle.NEUTRAL, nested=nested_style) - expected_dict = {'root': EFrontendStyle.NEUTRAL.value, 'nested': {'root': EFrontendStyle.SUCCESS.value}} + style = FrontendStyle(EFrontendStyle.NEUTRAL) + expected_dict = {'root': EFrontendStyle.NEUTRAL.value } self.assertEqual(style.to_dict(), expected_dict) - def test_to_dict_with_deep_nesting(self): - nested_style_level_2 = FrontendStyle(EFrontendStyle.WARNING) - nested_style_level_1 = FrontendStyle(EFrontendStyle.INFO, nested_lvl2=nested_style_level_2) - style = FrontendStyle(EFrontendStyle.NEUTRAL, nested_lvl1=nested_style_level_1) - expected_dict = { - 'root': EFrontendStyle.NEUTRAL.value, - 'nested_lvl1': { - 'root': EFrontendStyle.INFO.value, - 'nested_lvl2': { - 'root': EFrontendStyle.WARNING.value - } - } - } - self.assertEqual(style.to_dict(), expected_dict) - - if __name__ == '__main__': unittest.main()