Skip to content

Commit

Permalink
test: Add unit tests for FrontendStyle class
Browse files Browse the repository at this point in the history
  • Loading branch information
drikusroor committed Jan 4, 2024
1 parent c26e8e1 commit 0ed2ed8
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions backend/experiment/tests/test_frontend_style.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import unittest

from experiment.actions.frontend_style import FrontendStyle

class TestFrontendStyle(unittest.TestCase):

def test_init_with_valid_root_style(self):
style = FrontendStyle(FrontendStyle.PRIMARY)
self.assertEqual(style.get_style('root'), FrontendStyle.PRIMARY)

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(FrontendStyle.SUCCESS)
style = FrontendStyle(FrontendStyle.PRIMARY, nested=nested_style)
self.assertIsInstance(style.get_style('nested'), FrontendStyle)

def test_init_with_invalid_nested_styles(self):
with self.assertRaises(ValueError):
FrontendStyle(FrontendStyle.PRIMARY, nested="invalid-style")

def test_get_style(self):
style = FrontendStyle(FrontendStyle.SECONDARY)
self.assertEqual(style.get_style('root'), FrontendStyle.SECONDARY)

def test_get_style_non_existing_element(self):
style = FrontendStyle(FrontendStyle.SECONDARY)
self.assertIsNone(style.get_style('non-existing'))

def test_apply_valid_style(self):
style = FrontendStyle(FrontendStyle.EMPTY)
style.apply_style('root', FrontendStyle.INFO)
self.assertEqual(style.get_style('root'), FrontendStyle.INFO)

def test_apply_invalid_style(self):
style = FrontendStyle(FrontendStyle.EMPTY)
with self.assertRaises(ValueError):
style.apply_style('root', "invalid-style")

def test_to_dict(self):
nested_style = FrontendStyle(FrontendStyle.SUCCESS)
style = FrontendStyle(FrontendStyle.NEUTRAL, nested=nested_style)
expected_dict = {'root': FrontendStyle.NEUTRAL, 'nested': {'root': FrontendStyle.SUCCESS}}
self.assertEqual(style.to_dict(), expected_dict)

def test_to_dict_with_deep_nesting(self):
nested_style_level_2 = FrontendStyle(FrontendStyle.WARNING)
nested_style_level_1 = FrontendStyle(FrontendStyle.INFO, nested_lvl2=nested_style_level_2)
style = FrontendStyle(FrontendStyle.NEUTRAL, nested_lvl1=nested_style_level_1)
expected_dict = {
'root': FrontendStyle.NEUTRAL,
'nested_lvl1': {
'root': FrontendStyle.INFO,
'nested_lvl2': {
'root': FrontendStyle.WARNING
}
}
}
self.assertEqual(style.to_dict(), expected_dict)

if __name__ == '__main__':
unittest.main()

0 comments on commit 0ed2ed8

Please sign in to comment.