Skip to content

Commit

Permalink
fix: Fix code & tests after removing style from consent and nested st…
Browse files Browse the repository at this point in the history
…yles
  • Loading branch information
drikusroor committed Feb 19, 2024
1 parent 46d4a37 commit 79d432a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
2 changes: 1 addition & 1 deletion backend/experiment/actions/consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion backend/experiment/actions/frontend_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
30 changes: 2 additions & 28 deletions backend/experiment/tests/test_frontend_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

0 comments on commit 79d432a

Please sign in to comment.