fix: removed subsections with no units from v3 topics api (#31553)
* fix: removed subsections with no units from v3 topics API * fix: fixed unit test
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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]:
|
||||
|
||||
Reference in New Issue
Block a user