|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import unittest |
| 6 | +from unittest.mock import patch, Mock |
| 7 | +from lxml import etree |
6 | 8 |
|
7 | 9 | from xblock.core import XBlock |
8 | 10 | from xblock.test.toy_runtime import ToyRuntime |
9 | | -from xblock.utils.helpers import child_isinstance |
| 11 | +from xblock.utils.helpers import ( |
| 12 | + child_isinstance, |
| 13 | + name_to_pathname, |
| 14 | + is_pointer_tag, |
| 15 | + load_definition_xml, |
| 16 | + format_filepath, |
| 17 | + file_to_xml, |
| 18 | + XML_PARSER, |
| 19 | +) |
10 | 20 |
|
11 | 21 |
|
12 | 22 | # pylint: disable=unnecessary-pass |
@@ -78,3 +88,59 @@ def test_child_isinstance_descendants(self): |
78 | 88 | self.assertTrue(child_isinstance(root, block.children[1], DogXBlock)) |
79 | 89 | self.assertTrue(child_isinstance(root, block.children[1], GoldenRetrieverXBlock)) |
80 | 90 | self.assertFalse(child_isinstance(root, block.children[1], CatXBlock)) |
| 91 | + |
| 92 | + |
| 93 | +class TestPointerTagParsing(unittest.TestCase): |
| 94 | + """ |
| 95 | + Tests for core functions in XBlock. |
| 96 | + """ |
| 97 | + def test_name_to_pathname(self): |
| 98 | + self.assertEqual(name_to_pathname("course:subcourse"), "course/subcourse") |
| 99 | + self.assertEqual(name_to_pathname("module:lesson:part"), "module/lesson/part") |
| 100 | + self.assertEqual(name_to_pathname("no_colon"), "no_colon") |
| 101 | + |
| 102 | + def test_is_pointer_tag(self): |
| 103 | + # Case 1: Valid pointer tag |
| 104 | + xml_obj = etree.Element("some_tag", url_name="test_url") |
| 105 | + self.assertTrue(is_pointer_tag(xml_obj)) |
| 106 | + |
| 107 | + # Case 2: Valid course pointer tag |
| 108 | + xml_obj = etree.Element("course", url_name="test_url", course="test_course", org="test_org") |
| 109 | + self.assertTrue(is_pointer_tag(xml_obj)) |
| 110 | + |
| 111 | + # Case 3: Invalid case - extra attribute |
| 112 | + xml_obj = etree.Element("some_tag", url_name="test_url", extra_attr="invalid") |
| 113 | + self.assertFalse(is_pointer_tag(xml_obj)) |
| 114 | + |
| 115 | + # Case 4: Invalid case - has text |
| 116 | + xml_obj = etree.Element("some_tag", url_name="test_url") |
| 117 | + xml_obj.text = "invalid_text" |
| 118 | + self.assertFalse(is_pointer_tag(xml_obj)) |
| 119 | + |
| 120 | + # Case 5: Invalid case - has children |
| 121 | + xml_obj = etree.Element("some_tag", url_name="test_url") |
| 122 | + _ = etree.SubElement(xml_obj, "child") |
| 123 | + self.assertFalse(is_pointer_tag(xml_obj)) |
| 124 | + |
| 125 | + @patch("xblock.utils.helpers.load_file") |
| 126 | + def test_load_definition_xml(self, mock_load_file): |
| 127 | + mock_load_file.return_value = "<mock_xml />" |
| 128 | + node = etree.Element("course", url_name="test_url") |
| 129 | + runtime = Mock() |
| 130 | + def_id = "mock_id" |
| 131 | + |
| 132 | + definition_xml, filepath = load_definition_xml(node, runtime, def_id) |
| 133 | + self.assertEqual(filepath, "course/test_url.xml") |
| 134 | + self.assertEqual(definition_xml, "<mock_xml />") |
| 135 | + mock_load_file.assert_called_once() |
| 136 | + |
| 137 | + def test_format_filepath(self): |
| 138 | + self.assertEqual(format_filepath("course", "test_url"), "course/test_url.xml") |
| 139 | + |
| 140 | + @patch("xblock.utils.helpers.etree.parse") |
| 141 | + def test_file_to_xml(self, mock_etree_parse): |
| 142 | + mock_etree_parse.return_value.getroot.return_value = "mock_xml_root" |
| 143 | + file_object = Mock() |
| 144 | + result = file_to_xml(file_object) |
| 145 | + self.assertEqual(result, "mock_xml_root") |
| 146 | + mock_etree_parse.assert_called_once_with(file_object, parser=XML_PARSER) |
0 commit comments