Commit 80f0bb5accdabffbf8666cd9908871d1a060f0f5
Committed by
Gust
1 parent
e2aa919e
Exists in
master
and in
4 other branches
Adding unregister widget method and tests
Showing
2 changed files
with
49 additions
and
4 deletions
Show diff stats
colab/widgets/tests/test_widget_manager.py
| ... | ... | @@ -4,9 +4,48 @@ from colab.widgets.widget_manager import WidgetManager, Widget |
| 4 | 4 | |
| 5 | 5 | class WidgetManagerTest(TestCase): |
| 6 | 6 | |
| 7 | + html_content = "<head><meta charset='UTF-8'></head><body><p>test</p></body>" | |
| 8 | + widget_area = 'profile' | |
| 9 | + widget_id = 'widget_id' | |
| 10 | + | |
| 11 | + def custom_widget_instance(self, content): | |
| 12 | + class CustomWidget(Widget): | |
| 13 | + identifier = 'widget_id' | |
| 14 | + def generate_content(self, request=None): | |
| 15 | + self.content = content | |
| 16 | + return CustomWidget() | |
| 17 | + | |
| 18 | + def setUp(self): | |
| 19 | + WidgetManager.register_widget(self.widget_area, \ | |
| 20 | + self.custom_widget_instance(self.html_content)) | |
| 21 | + | |
| 22 | + def tearDown(self): | |
| 23 | + WidgetManager.unregister_widget(self.widget_area, self.widget_id) | |
| 24 | + | |
| 7 | 25 | def test_add_widgets_to_key_area(self): |
| 8 | - area = 'profile' | |
| 9 | - WidgetManager.register_widget(area, Widget()) | |
| 26 | + self.assertEqual(len(WidgetManager.get_widgets(self.widget_area)), 1) | |
| 27 | + | |
| 28 | + def test_remove_widgets_in_key_area(self): | |
| 29 | + area = 'admin' | |
| 30 | + widget_instance = self.custom_widget_instance(self.html_content) | |
| 31 | + | |
| 32 | + WidgetManager.register_widget(area, widget_instance) | |
| 33 | + WidgetManager.unregister_widget(area, self.widget_id) | |
| 34 | + | |
| 35 | + self.assertEqual(len(WidgetManager.get_widgets(area)), 0) | |
| 36 | + | |
| 37 | + def test_get_body(self): | |
| 38 | + customWidget = self.custom_widget_instance(self.html_content) | |
| 39 | + | |
| 40 | + customWidget.generate_content() | |
| 41 | + self.assertEqual(customWidget.get_body(), "<p>test</p>") | |
| 42 | + | |
| 43 | + def test_get_header(self): | |
| 44 | + customWidget = self.custom_widget_instance(self.html_content) | |
| 10 | 45 | |
| 11 | - self.assertEqual(len(WidgetManager.get_widgets(area)), 1) | |
| 46 | + customWidget.generate_content() | |
| 47 | + self.assertEqual(customWidget.get_header(), "<meta charset='UTF-8'>") | |
| 12 | 48 | |
| 49 | + def test_generate_content(self): | |
| 50 | + widgets = WidgetManager.get_widgets(self.widget_area) | |
| 51 | + self.assertEqual(widgets[0].content, self.html_content) | ... | ... |
colab/widgets/widget_manager.py
| ... | ... | @@ -3,7 +3,6 @@ from django.utils.safestring import mark_safe |
| 3 | 3 | class Widget(object): |
| 4 | 4 | identifier = None |
| 5 | 5 | name = None |
| 6 | - default_url = None | |
| 7 | 6 | content = '' |
| 8 | 7 | |
| 9 | 8 | def get_body(self): |
| ... | ... | @@ -43,6 +42,13 @@ class WidgetManager(object): |
| 43 | 42 | WidgetManager.widget_categories[category].append(widget) |
| 44 | 43 | |
| 45 | 44 | @staticmethod |
| 45 | + def unregister_widget(category, widget_identifier): | |
| 46 | + if WidgetManager.widget_categories.has_key(category): | |
| 47 | + for widget in WidgetManager.widget_categories[category]: | |
| 48 | + if widget.identifier == widget_identifier: | |
| 49 | + WidgetManager.widget_categories[category].remove(widget) | |
| 50 | + | |
| 51 | + @staticmethod | |
| 46 | 52 | def get_widgets(category, request=None): |
| 47 | 53 | if not WidgetManager.widget_categories.has_key(category): |
| 48 | 54 | return [] | ... | ... |