diff --git a/lms/djangoapps/discussion/rest_api/tests/test_utils.py b/lms/djangoapps/discussion/rest_api/tests/test_utils.py index d56385352b..fd2e986c81 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_utils.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_utils.py @@ -5,7 +5,7 @@ Tests for Discussion REST API utils. from datetime import datetime, timedelta from pytz import UTC - +import unittest from common.djangoapps.student.roles import CourseStaffRole, CourseInstructorRole from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory @@ -17,7 +17,7 @@ from lms.djangoapps.discussion.rest_api.utils import ( get_course_ta_users_list, get_course_staff_users_list, get_moderator_users_list, - get_archived_topics + get_archived_topics, remove_empty_sequentials ) @@ -94,3 +94,54 @@ class DiscussionAPIUtilsTestCase(ModuleStoreTestCase): # Assert that the output matches the expected output assert output == expected_output + + +class TestRemoveEmptySequentials(unittest.TestCase): + """ + Test for the remove_empty_sequentials function + """ + def test_empty_data(self): + # Test that the function can handle an empty list + data = [] + result = remove_empty_sequentials(data) + self.assertEqual(result, []) + + def test_no_empty_sequentials(self): + # Test that the function does not remove any sequentials if they all have children + data = [ + {"type": "sequential", "children": [{"type": "vertical"}]}, + {"type": "chapter", "children": [ + {"type": "sequential", "children": [{"type": "vertical"}]} + ]} + ] + result = remove_empty_sequentials(data) + self.assertEqual(result, data) + + def test_remove_empty_sequentials(self): + # Test that the function removes empty sequentials + data = [ + {"type": "sequential", "children": []}, + {"type": "chapter", "children": [ + {"type": "sequential", "children": [{"type": "vertical3"}]}, + {"type": "sequential", "children": []}, + {"type": "sequential", "children": []}, + {"type": "sequential", "children": [{"type": "vertical4"}]} + ]}, + {"type": "chapter", "children": [ + {"type": "sequential", "children": [{"type": "vertical1"}]}, + {"type": "sequential", "children": []}, + {"children": [{"type": "vertical2"}]} + ]} + ] + expected_output = [ + {"type": "chapter", "children": [ + {"type": "sequential", "children": [{"type": "vertical3"}]}, + {"type": "sequential", "children": [{"type": "vertical4"}]} + ]}, + {"type": "chapter", "children": [ + {"type": "sequential", "children": [{"type": "vertical1"}]}, + {"children": [{"type": "vertical2"}]} + ]} + ] + result = remove_empty_sequentials(data) + self.assertEqual(result, expected_output) diff --git a/lms/djangoapps/discussion/rest_api/utils.py b/lms/djangoapps/discussion/rest_api/utils.py index 9ea0691228..0ae0c5f07e 100644 --- a/lms/djangoapps/discussion/rest_api/utils.py +++ b/lms/djangoapps/discussion/rest_api/utils.py @@ -269,7 +269,28 @@ def create_topics_v3_structure(blocks, topics): if archived_topics['children']: structured_topics.append(archived_topics) - return structured_topics + return remove_empty_sequentials(structured_topics) + + +def remove_empty_sequentials(data): + """ + Removes all objects of type "sequential" from a nested list of objects if they have no children. + + Parameters: + data (list): A list of nested objects to check and remove empty sequentials from. + + Returns: + list: The modified list with empty sequentials removed. + """ + + new_data = [] + for obj in data: + block_type = obj.get('type') + if block_type != 'sequential' or (block_type == 'sequential' and obj.get('children')): + new_data.append(obj) + if obj.get('children'): + obj['children'] = remove_empty_sequentials(obj['children']) + return new_data def get_topic_ids_from_topics(topics: List[Dict[str, str]]) -> List[str]: