test_crud.py 12.2 KB
Newer Older
1
import unittest
2

3 4 5
from xmodule import templates
from xmodule.modulestore.tests import persistent_factories
from xmodule.course_module import CourseDescriptor
6
from xmodule.modulestore.django import modulestore, loc_mapper, clear_existing_modulestores
7 8
from xmodule.seq_module import SequenceDescriptor
from xmodule.capa_module import CapaDescriptor
9
from xmodule.modulestore.locator import CourseLocator, BlockUsageLocator, LocalId
10
from xmodule.modulestore.exceptions import ItemNotFoundError, DuplicateCourseError
11
from xmodule.html_module import HtmlDescriptor
12
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
13 14 15 16 17 18 19


class TemplateTests(unittest.TestCase):
    """
    Test finding and using the templates (boilerplates) for xblocks.
    """

20
    def setUp(self):
21 22 23
        clear_existing_modulestores()  # redundant w/ cleanup but someone was getting errors
        self.addCleanup(ModuleStoreTestCase.drop_mongo_collections, 'split')
        self.addCleanup(clear_existing_modulestores)
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    def test_get_templates(self):
        found = templates.all_templates()
        self.assertIsNotNone(found.get('course'))
        self.assertIsNotNone(found.get('about'))
        self.assertIsNotNone(found.get('html'))
        self.assertIsNotNone(found.get('problem'))
        self.assertEqual(len(found.get('course')), 0)
        self.assertEqual(len(found.get('about')), 1)
        self.assertGreaterEqual(len(found.get('html')), 2)
        self.assertGreaterEqual(len(found.get('problem')), 10)
        dropdown = None
        for template in found['problem']:
            self.assertIn('metadata', template)
            self.assertIn('display_name', template['metadata'])
            if template['metadata']['display_name'] == 'Dropdown':
                dropdown = template
                break
        self.assertIsNotNone(dropdown)
        self.assertIn('markdown', dropdown['metadata'])
        self.assertIn('data', dropdown)
        self.assertRegexpMatches(dropdown['metadata']['markdown'], r'^Dropdown.*')
        self.assertRegexpMatches(dropdown['data'], r'<problem>\s*<p>Dropdown.*')

    def test_get_some_templates(self):
        self.assertEqual(len(SequenceDescriptor.templates()), 0)
        self.assertGreater(len(HtmlDescriptor.templates()), 0)
        self.assertIsNone(SequenceDescriptor.get_template('doesntexist.yaml'))
        self.assertIsNone(HtmlDescriptor.get_template('doesntexist.yaml'))
        self.assertIsNotNone(HtmlDescriptor.get_template('announcement.yaml'))

    def test_factories(self):
56
        test_course = persistent_factories.PersistentCourseFactory.create(
57
            course_id='testx.tempcourse', org='testx',
58 59
            display_name='fun test course', user_id='testbot'
        )
60 61 62 63
        self.assertIsInstance(test_course, CourseDescriptor)
        self.assertEqual(test_course.display_name, 'fun test course')
        index_info = modulestore('split').get_course_index_info(test_course.location)
        self.assertEqual(index_info['org'], 'testx')
64
        self.assertEqual(index_info['_id'], 'testx.tempcourse')
65 66 67 68 69 70

        test_chapter = persistent_factories.ItemFactory.create(display_name='chapter 1',
            parent_location=test_course.location)
        self.assertIsInstance(test_chapter, SequenceDescriptor)
        # refetch parent which should now point to child
        test_course = modulestore('split').get_course(test_chapter.location)
71
        self.assertIn(test_chapter.location.block_id, test_course.children)
72

73 74
        with self.assertRaises(DuplicateCourseError):
            persistent_factories.PersistentCourseFactory.create(
75
                course_id='testx.tempcourse', org='testx', 
76 77 78
                display_name='fun test course', user_id='testbot'
            )

79 80
    def test_temporary_xblocks(self):
        """
81
        Test create_xblock to create non persisted xblocks
82
        """
83
        test_course = persistent_factories.PersistentCourseFactory.create(
84
            course_id='testx.tempcourse', org='testx',
85 86
            display_name='fun test course', user_id='testbot'
        )
87

88 89 90
        test_chapter = modulestore('split').create_xblock(
            test_course.system, 'chapter', {'display_name': 'chapter n'}, parent_xblock=test_course
        )
91 92 93 94 95 96
        self.assertIsInstance(test_chapter, SequenceDescriptor)
        self.assertEqual(test_chapter.display_name, 'chapter n')
        self.assertIn(test_chapter, test_course.get_children())

        # test w/ a definition (e.g., a problem)
        test_def_content = '<problem>boo</problem>'
97 98 99
        test_problem = modulestore('split').create_xblock(
            test_course.system, 'problem', {'data': test_def_content}, parent_xblock=test_chapter
        )
100 101 102 103 104 105 106 107 108 109
        self.assertIsInstance(test_problem, CapaDescriptor)
        self.assertEqual(test_problem.data, test_def_content)
        self.assertIn(test_problem, test_chapter.get_children())
        test_problem.display_name = 'test problem'
        self.assertEqual(test_problem.display_name, 'test problem')

    def test_persist_dag(self):
        """
        try saving temporary xblocks
        """
Don Mitchell committed
110
        test_course = persistent_factories.PersistentCourseFactory.create(
111 112 113
            course_id='testx.tempcourse', org='testx', 
            display_name='fun test course', user_id='testbot'
        )
114 115 116 117
        test_chapter = modulestore('split').create_xblock(
            test_course.system, 'chapter', {'display_name': 'chapter n'}, parent_xblock=test_course
        )
        self.assertEqual(test_chapter.display_name, 'chapter n')
118
        test_def_content = '<problem>boo</problem>'
119
        # create child
120 121 122 123
        new_block = modulestore('split').create_xblock(
            test_course.system,
            'problem',
            fields={
Don Mitchell committed
124 125
                'data': test_def_content,
                'display_name': 'problem'
126
            },
127 128 129 130
            parent_xblock=test_chapter
        )
        self.assertIsNotNone(new_block.definition_locator)
        self.assertTrue(isinstance(new_block.definition_locator.definition_id, LocalId))
131 132 133 134 135 136 137 138 139 140 141 142
        # better to pass in persisted parent over the subdag so
        # subdag gets the parent pointer (otherwise 2 ops, persist dag, update parent children,
        # persist parent
        persisted_course = modulestore('split').persist_xblock_dag(test_course, 'testbot')
        self.assertEqual(len(persisted_course.children), 1)
        persisted_chapter = persisted_course.get_children()[0]
        self.assertEqual(persisted_chapter.category, 'chapter')
        self.assertEqual(persisted_chapter.display_name, 'chapter n')
        self.assertEqual(len(persisted_chapter.children), 1)
        persisted_problem = persisted_chapter.get_children()[0]
        self.assertEqual(persisted_problem.category, 'problem')
        self.assertEqual(persisted_problem.data, test_def_content)
Don Mitchell committed
143 144 145 146
        # update it
        persisted_problem.display_name = 'altered problem'
        persisted_problem = modulestore('split').persist_xblock_dag(persisted_problem, 'testbot')
        self.assertEqual(persisted_problem.display_name, 'altered problem')
147 148 149

    def test_delete_course(self):
        test_course = persistent_factories.PersistentCourseFactory.create(
150
            course_id='edu.harvard.history.doomed', org='testx',
151 152 153 154 155
            display_name='doomed test course',
            user_id='testbot')
        persistent_factories.ItemFactory.create(display_name='chapter 1',
            parent_location=test_course.location)

156
        id_locator = CourseLocator(package_id=test_course.location.package_id, branch='draft')
157 158 159 160 161
        guid_locator = CourseLocator(version_guid=test_course.location.version_guid)
        # verify it can be retireved by id
        self.assertIsInstance(modulestore('split').get_course(id_locator), CourseDescriptor)
        # and by guid
        self.assertIsInstance(modulestore('split').get_course(guid_locator), CourseDescriptor)
162
        modulestore('split').delete_course(id_locator.package_id)
163 164 165 166 167 168 169 170 171 172
        # test can no longer retrieve by id
        self.assertRaises(ItemNotFoundError, modulestore('split').get_course, id_locator)
        # but can by guid
        self.assertIsInstance(modulestore('split').get_course(guid_locator), CourseDescriptor)

    def test_block_generations(self):
        """
        Test get_block_generations
        """
        test_course = persistent_factories.PersistentCourseFactory.create(
173
            course_id='edu.harvard.history.hist101', org='testx',
174
            display_name='history test course',
175 176
            user_id='testbot'
        )
177 178 179 180
        chapter = persistent_factories.ItemFactory.create(display_name='chapter 1',
            parent_location=test_course.location, user_id='testbot')
        sub = persistent_factories.ItemFactory.create(display_name='subsection 1',
            parent_location=chapter.location, user_id='testbot', category='vertical')
181 182
        first_problem = persistent_factories.ItemFactory.create(
            display_name='problem 1', parent_location=sub.location, user_id='testbot', category='problem',
183
            data="<problem></problem>"
184
        )
185
        first_problem.max_attempts = 3
186
        first_problem.save()  # decache the above into the kvs
187
        updated_problem = modulestore('split').update_item(first_problem, '**replace_user**')
188 189 190 191
        self.assertIsNotNone(updated_problem.previous_version)
        self.assertEqual(updated_problem.previous_version, first_problem.update_version)
        self.assertNotEqual(updated_problem.update_version, first_problem.update_version)
        updated_loc = modulestore('split').delete_item(updated_problem.location, 'testbot', delete_children=True)
192

193 194
        second_problem = persistent_factories.ItemFactory.create(
            display_name='problem 2',
195
            parent_location=BlockUsageLocator(updated_loc, block_id=sub.location.block_id),
196
            user_id='testbot', category='problem',
197
            data="<problem></problem>"
198
        )
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

        # course root only updated 2x
        version_history = modulestore('split').get_block_generations(test_course.location)
        self.assertEqual(version_history.locator.version_guid, test_course.location.version_guid)
        self.assertEqual(len(version_history.children), 1)
        self.assertEqual(version_history.children[0].children, [])
        self.assertEqual(version_history.children[0].locator.version_guid, chapter.location.version_guid)

        # sub changed on add, add problem, delete problem, add problem in strict linear seq
        version_history = modulestore('split').get_block_generations(sub.location)
        self.assertEqual(len(version_history.children), 1)
        self.assertEqual(len(version_history.children[0].children), 1)
        self.assertEqual(len(version_history.children[0].children[0].children), 1)
        self.assertEqual(len(version_history.children[0].children[0].children[0].children), 0)

        # first and second problem may show as same usage_id; so, need to ensure their histories are right
        version_history = modulestore('split').get_block_generations(updated_problem.location)
        self.assertEqual(version_history.locator.version_guid, first_problem.location.version_guid)
        self.assertEqual(len(version_history.children), 1)  # updated max_attempts
        self.assertEqual(len(version_history.children[0].children), 0)

        version_history = modulestore('split').get_block_generations(second_problem.location)
        self.assertNotEqual(version_history.locator.version_guid, first_problem.location.version_guid)
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    def test_split_inject_loc_mapper(self):
        """
        Test that creating a loc_mapper causes it to automatically attach to the split mongo store
        """
        # instantiate location mapper before split
        mapper = loc_mapper()
        # split must inject the location mapper itself since the mapper existed before it did
        self.assertEqual(modulestore('split').loc_mapper, mapper)

    def test_loc_inject_into_split(self):
        """
        Test that creating a loc_mapper causes it to automatically attach to the split mongo store
        """
        # force instantiation of split modulestore before there's a location mapper and verify
        # it has no pointer to loc mapper
        self.assertIsNone(modulestore('split').loc_mapper)
        # force instantiation of location mapper which must inject itself into the split
        mapper = loc_mapper()
        self.assertEqual(modulestore('split').loc_mapper, mapper)