diff --git a/cms/djangoapps/contentstore/tests/test_utils.py b/cms/djangoapps/contentstore/tests/test_utils.py index eb7bfb6db9..0e34e3622d 100644 --- a/cms/djangoapps/contentstore/tests/test_utils.py +++ b/cms/djangoapps/contentstore/tests/test_utils.py @@ -1,6 +1,7 @@ """ Tests for utils. """ from contentstore import utils import mock +import collections from django.test import TestCase from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @@ -70,3 +71,80 @@ class UrlReverseTestCase(ModuleStoreTestCase): 'https://edge.edx.org/courses/edX/edX101/How_to_Create_an_edX_Course/about', utils.get_url_reverse('https://edge.edx.org/courses/edX/edX101/How_to_Create_an_edX_Course/about', course) ) + + +class ExtraPanelTabTestCase(TestCase): + """ Tests adding and removing extra course tabs. """ + + def get_tab_type_dicts(self, tab_types): + """ Returns an array of tab dictionaries. """ + if tab_types: + return [{'tab_type': tab_type} for tab_type in tab_types.split(',')] + else: + return [] + + def get_course_with_tabs(self, tabs=[]): + """ Returns a mock course object with a tabs attribute. """ + course = collections.namedtuple('MockCourse', ['tabs']) + if isinstance(tabs, basestring): + course.tabs = self.get_tab_type_dicts(tabs) + else: + course.tabs = tabs + return course + + def add_extra_panel_tab(self): + """ Tests if a tab can be added to a course tab list. """ + for tab_type in utils.EXTRA_TAB_PANELS.keys(): + tab = utils.EXTRA_TAB_PANELS.get(tab_type) + + # test adding with changed = True + for tab_setup in ['', 'x', 'x,y,z']: + course = self.get_course_with_tabs(tab_setup) + expected_tabs = course.tabs + tab + changed, actual_tabs = utils.add_extra_panel_tab(tab_type, course) + self.assertTrue(changed) + self.assertEqual(actual_tabs, expected_tabs) + + # test adding with changed = False + tab_test_setup = [ + [tab], + [tab, self.get_tab_type_dicts('x,y,z')], + [self.get_tab_type_dicts('x,y'), tab, self.get_tab_type_dicts('z')], + [self.get_tab_type_dicts('x,y,z'), tab]] + + for tab_setup in tab_test_setup: + course = self.get_course_with_tabs(tab_setup) + expected_tabs = course.tabs + changed, actual_tabs = utils.add_extra_panel_tab(tab_type, course) + self.assertFalse(changed) + self.assertEqual(actual_tabs, expected_tabs) + + def remove_tab_test(self): + """ Tests if a tab can be removed from a course tab list. """ + for tab_type in utils.EXTRA_TAB_PANELS.keys(): + tab = utils.EXTRA_TAB_PANELS.get(tab_type) + + # test removing with changed = True + tab_test_setup = [ + [tab], + [tab, self.get_tab_type_dicts('x,y,z')], + [self.get_tab_type_dicts('x,y'), tab, self.get_tab_type_dicts('z')], + [self.get_tab_type_dicts('x,y,z'), tab]] + + for tab_setup in tab_test_setup: + course = self.get_course_with_tabs(tab_setup) + expected_tabs = [t for t in course.tabs if t != utils.EXTRA_TAB_PANELS.get(tab_type)] + changed, actual_tabs = utils.remove_extra_panel_tab(tab_type, course) + self.assertTrue(changed) + self.assertEqual(actual_tabs, expected_tabs) + + # test removing with changed = False + for tab_setup in ['', 'x', 'x,y,z']: + course = self.get_course_with_tabs(tab_setup) + expected_tabs = course.tabs + print "expected_tabs = " + print expected_tabs + changed, actual_tabs = utils.remove_extra_panel_tab(tab_type, course) + self.assertFalse(changed) + self.assertEqual(actual_tabs, expected_tabs) +