|
| 1 | +""" |
| 2 | +Test the objecttag_export_helpers module |
| 3 | +""" |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase |
| 7 | +from xmodule.modulestore.tests.factories import BlockFactory, CourseFactory |
| 8 | + |
| 9 | +from .... import api |
| 10 | +from ....tests.test_api import TestGetAllObjectTagsMixin |
| 11 | +from ..objecttag_export_helpers import TaggedContent, build_object_tree_with_objecttags, iterate_with_level |
| 12 | + |
| 13 | + |
| 14 | +class TaggedCourseMixin(TestGetAllObjectTagsMixin, ModuleStoreTestCase): # type: ignore[misc] |
| 15 | + """ |
| 16 | + Mixin with a course structure and taxonomies |
| 17 | + """ |
| 18 | + MODULESTORE = TEST_DATA_SPLIT_MODULESTORE |
| 19 | + CREATE_USER = False |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + super().setUp() |
| 23 | + |
| 24 | + # Patch modulestore |
| 25 | + self.patcher = patch("openedx.core.djangoapps.content_tagging.tasks.modulestore", return_value=self.store) |
| 26 | + self.addCleanup(self.patcher.stop) |
| 27 | + self.patcher.start() |
| 28 | + |
| 29 | + # Create course |
| 30 | + self.course = CourseFactory.create( |
| 31 | + org=self.orgA.short_name, |
| 32 | + number="test_course", |
| 33 | + run="test_run", |
| 34 | + display_name="Test Course", |
| 35 | + ) |
| 36 | + self.expected_tagged_xblock = TaggedContent( |
| 37 | + display_name="Test Course", |
| 38 | + block_id="course-v1:orgA+test_course+test_run", |
| 39 | + category="course", |
| 40 | + children=[], |
| 41 | + object_tags={ |
| 42 | + self.taxonomy_1.id: list(self.course_tags), |
| 43 | + }, |
| 44 | + ) |
| 45 | + |
| 46 | + # Create XBlocks |
| 47 | + self.sequential = BlockFactory.create( |
| 48 | + parent=self.course, |
| 49 | + category="sequential", |
| 50 | + display_name="test sequential", |
| 51 | + ) |
| 52 | + # Tag blocks |
| 53 | + tagged_sequential = TaggedContent( |
| 54 | + display_name="test sequential", |
| 55 | + block_id="block-v1:orgA+test_course+test_run+type@sequential+block@test_sequential", |
| 56 | + category="sequential", |
| 57 | + children=[], |
| 58 | + object_tags={ |
| 59 | + self.taxonomy_1.id: list(self.sequential_tags1), |
| 60 | + self.taxonomy_2.id: list(self.sequential_tags2), |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + assert self.expected_tagged_xblock.children is not None # type guard |
| 65 | + self.expected_tagged_xblock.children.append(tagged_sequential) |
| 66 | + |
| 67 | + # Untagged blocks |
| 68 | + sequential2 = BlockFactory.create( |
| 69 | + parent=self.course, |
| 70 | + category="sequential", |
| 71 | + display_name="untagged sequential", |
| 72 | + ) |
| 73 | + untagged_sequential = TaggedContent( |
| 74 | + display_name="untagged sequential", |
| 75 | + block_id="block-v1:orgA+test_course+test_run+type@sequential+block@untagged_sequential", |
| 76 | + category="sequential", |
| 77 | + children=[], |
| 78 | + object_tags={}, |
| 79 | + ) |
| 80 | + assert self.expected_tagged_xblock.children is not None # type guard |
| 81 | + self.expected_tagged_xblock.children.append(untagged_sequential) |
| 82 | + BlockFactory.create( |
| 83 | + parent=sequential2, |
| 84 | + category="vertical", |
| 85 | + display_name="untagged vertical", |
| 86 | + ) |
| 87 | + untagged_vertical = TaggedContent( |
| 88 | + display_name="untagged vertical", |
| 89 | + block_id="block-v1:orgA+test_course+test_run+type@vertical+block@untagged_vertical", |
| 90 | + category="vertical", |
| 91 | + children=[], |
| 92 | + object_tags={}, |
| 93 | + ) |
| 94 | + assert untagged_sequential.children is not None # type guard |
| 95 | + untagged_sequential.children.append(untagged_vertical) |
| 96 | + # /Untagged blocks |
| 97 | + |
| 98 | + vertical = BlockFactory.create( |
| 99 | + parent=self.sequential, |
| 100 | + category="vertical", |
| 101 | + display_name="test vertical1", |
| 102 | + ) |
| 103 | + tagged_vertical = TaggedContent( |
| 104 | + display_name="test vertical1", |
| 105 | + block_id="block-v1:orgA+test_course+test_run+type@vertical+block@test_vertical1", |
| 106 | + category="vertical", |
| 107 | + children=[], |
| 108 | + object_tags={ |
| 109 | + self.taxonomy_2.id: list(self.vertical1_tags), |
| 110 | + }, |
| 111 | + ) |
| 112 | + assert tagged_sequential.children is not None # type guard |
| 113 | + tagged_sequential.children.append(tagged_vertical) |
| 114 | + |
| 115 | + vertical2 = BlockFactory.create( |
| 116 | + parent=self.sequential, |
| 117 | + category="vertical", |
| 118 | + display_name="test vertical2", |
| 119 | + ) |
| 120 | + untagged_vertical2 = TaggedContent( |
| 121 | + display_name="test vertical2", |
| 122 | + block_id="block-v1:orgA+test_course+test_run+type@vertical+block@test_vertical2", |
| 123 | + category="vertical", |
| 124 | + children=[], |
| 125 | + object_tags={}, |
| 126 | + ) |
| 127 | + assert tagged_sequential.children is not None # type guard |
| 128 | + tagged_sequential.children.append(untagged_vertical2) |
| 129 | + |
| 130 | + html = BlockFactory.create( |
| 131 | + parent=vertical2, |
| 132 | + category="html", |
| 133 | + display_name="test html", |
| 134 | + ) |
| 135 | + tagged_text = TaggedContent( |
| 136 | + display_name="test html", |
| 137 | + block_id="block-v1:orgA+test_course+test_run+type@html+block@test_html", |
| 138 | + category="html", |
| 139 | + children=[], |
| 140 | + object_tags={ |
| 141 | + self.taxonomy_2.id: list(self.html_tags), |
| 142 | + }, |
| 143 | + ) |
| 144 | + assert untagged_vertical2.children is not None # type guard |
| 145 | + untagged_vertical2.children.append(tagged_text) |
| 146 | + |
| 147 | + self.all_object_tags, _ = api.get_all_object_tags(self.course.id) |
| 148 | + self.expected_tagged_content_list = [ |
| 149 | + (self.expected_tagged_xblock, 0), |
| 150 | + (tagged_sequential, 1), |
| 151 | + (tagged_vertical, 2), |
| 152 | + (untagged_vertical2, 2), |
| 153 | + (tagged_text, 3), |
| 154 | + (untagged_sequential, 1), |
| 155 | + (untagged_vertical, 2), |
| 156 | + ] |
| 157 | + |
| 158 | + |
| 159 | +class TestContentTagChildrenExport(TaggedCourseMixin): # type: ignore[misc] |
| 160 | + """ |
| 161 | + Test helper functions for exporting tagged content |
| 162 | + """ |
| 163 | + def test_build_object_tree(self) -> None: |
| 164 | + """ |
| 165 | + Test if we can export a course |
| 166 | + """ |
| 167 | + with self.assertNumQueries(3): |
| 168 | + tagged_xblock = build_object_tree_with_objecttags(self.course.id, self.all_object_tags) |
| 169 | + |
| 170 | + assert tagged_xblock == self.expected_tagged_xblock |
| 171 | + |
| 172 | + def test_iterate_with_level(self) -> None: |
| 173 | + """ |
| 174 | + Test if we can iterate over the tagged content in the correct order |
| 175 | + """ |
| 176 | + tagged_content_list = list(iterate_with_level(self.expected_tagged_xblock)) |
| 177 | + assert tagged_content_list == self.expected_tagged_content_list |
0 commit comments